Question

Using C++ write a program that keeps reading lines of text using getline(). After reading a...

Using C++ write a program that keeps reading lines of text using getline(). After reading a paragraph, it adjusts all the lines into an instructed width, so that it can show the words evenly spread out and fit in a straight edge at both margins (similar to "align full" option in a Microsoft word document).

As an example, consider a line containing 5 words and 30 characters altogether. If this line needs to be justified into a 40-character width, the remaining10 spaces needs to be spread out between the words. Assume that the first word of the next line has more than 9 characters, that is, the word in the next line cannot be placed in the tail of this line so as to consume the extra spaces.

In this case, 2 spaces are placed between the first 4 words, followed by 4 spaces prior to the last word (tail adjustment), or 3 spaces are placed between the first 4 words, followed by a space prior to the last word (even adjustment).

The requirements of this assignment are:

1) the program keeps reading lines of text until reading an empty line

2) the program then reads a width for the read paragraph

3) the program then justifies the paragraph based on tail adjustment

4) the program then shows the result in a bounding box

5) the program allows to adjust the paragraph by going back to 2

6) the program ends when it reads 0 as a new width

Extra points will be considered for even adjustment implementation.

Here is a hint to realize this mission - lines of words, say vector<string> words, is a straightforward conversion as implemented in the previous assignment and posted lecture example. Now, user types some width for justification. Let this width be W. The mission is to fill i-words in one line of this W. Note i > 0, i.e., every line must have at least one word. The logic will be:

let { w1, w2, ..., wi } be a collection of words. Then, w1.length() + w2.length() + ... + wi.length() is the total length of this collection

since we need at least one space between these i words, we need i - 1 spaces at least, and therefore, we need Wmin = w1.length() + w2.length() + ... + wi.length() + (i - 1) characters, which must be less than or equal to W

your first loop must identify this i and Wmin by going through vector<string> words you created

in the second loop, you simply create a line by adding w1 through wi-1 by placing a space between the two consecutive words

before placing the last word wi, you need to place W - Wmin spaces because this number is the excess spaces to fill in for justification (tail adjustment)!

There is no miracle or hidden trick to deal with these operations.The left-hand side of the next example shows tail adjustment while the right-hand side shows even adjustment:

Enter text, empty return will quit the input
> Every photo, every edit, every album now lives 
> in your iCloud Photo Library, easily viewable 
> and consistent on all your devices. 
> Automatically. The all-new Photos app makes 
> it simpler than ever to find and rediscover   
> your favorite photos. And you can make every 
> shot look even better immediately after you've 
> taken it with powerful new editing tools.
>
> Enter the width of text: 25
|-------------------------|
|very photo, every   edit,|
|every album now lives  in|
|your     iCloud     Photo|
|Library, easily  viewable|
|and  consistent  on   all|
|your             devices.|
|Automatically.        The|
|all-new Photos app  makes|
|it simpler than ever   to|
|find and rediscover  your|
|favorite photos. And  you|
|can make every shot  look|
|even  better  immediately|
|after  you've  taken   it|
|with powerful new editing|
|tools.                   |
|-------------------------|
Enter the width of text: 30
|------------------------------|
|very photo, every edit,  every|
|album now lives in your iCloud|
|Photo Library, easily viewable|
|and  consistent  on  all  your|
|devices.  Automatically.   The|
|all-new Photos app makes    it|
|simpler than ever to find  and|
|rediscover    your    favorite|
|photos. And you can make every|
|shot    look    even    better|
|immediately after you've taken|
|it with powerful new   editing|
|tools.                        |
|------------------------------|
Enter the width of text: 0
 Enter text, empty return will quit the input
>  Every photo, every edit, every album now lives
> in your iCloud Photo Library, easily viewable 
>  and consistent on all your devices. 
> Automatically. The all-new Photos app makes 
> it simpler than ever to find and rediscover   
>  your favorite photos. And you can make every 
> shot look even better immediately after you've 
> taken it with powerful new editing tools.
>
> Enter the width of text: 25
|-------------------------|
|Every  photo, every edit,|
|every  album now lives in|
|your     iCloud     Photo|
|Library,  easily viewable|
|and   consistent  on  all|
|your             devices.|
|Automatically.        The|
|all-new  Photos app makes|
|it  simpler  than ever to|
|find  and rediscover your|
|favorite  photos. And you|
|can  make every shot look|
|even  better  immediately|
|after   you've  taken  it|
|with powerful new editing|
|tools.                   |
|-------------------------|
Enter the width of text: 30
|------------------------------|
|Every photo, every edit, every|
|album now lives in your iCloud|
|Photo Library, easily viewable|
|and  consistent  on  all  your|
|devices.   Automatically.  The|
|all-new  Photos  app  makes it|
|simpler  than ever to find and|
|rediscover    your    favorite|
|photos. And you can make every|
|shot    look    even    better|
|immediately after you've taken|
|it  with  powerful new editing|
|tools.                        |
|------------------------------|
Enter the width of text: 0


























0 0
Add a comment Improve this question Transcribed image text
Answer #1

井include #include #include <iostream> <sstream> <list> typedef std: :list<std: : string> WordList; WordList splitTextIntoWordstd : : ut << line << std : : endl; void justifyText( const std: : string text ) std: : string line; for (const std: : stringint main() justifyText(This small code sample will format a paragraph which is passed to the justify text function to fil

Add a comment
Know the answer?
Add Answer to:
Using C++ write a program that keeps reading lines of text using getline(). After reading a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • How does this article relate to the factors of productions in economics? From Music to Maps,...

    How does this article relate to the factors of productions in economics? From Music to Maps, How Apple’s iPhone Changed Business Ten years ago, hailing a cab meant waiving one's arm at passing traffic, consumers routinely purchased cameras, and a phone was something people made calls on. The iPhone, released a decade ago this month, changed all of that and more, sparking a business transformation as sweeping as the one triggered by the personal computer in the 1980s. Apple Inc.'s...

  • Please use own words. Thank you. CASE QUESTIONS AND DISCUSSION > Analyze and discuss the questions...

    Please use own words. Thank you. CASE QUESTIONS AND DISCUSSION > Analyze and discuss the questions listed below in specific detail. A minimum of 4 pages is required; ensure that you answer all questions completely Case Questions Who are the main players (name and position)? What business (es) and industry or industries is the company in? What are the issues and problems facing the company? (Sort them by importance and urgency.) What are the characteristics of the environment in which...

  • Actions that damage a company and its employees should be stamped out, everyone would agree. But ...

    Actions that damage a company and its employees should be stamped out, everyone would agree. But should the people responsible be stamped out, too? HBR CASE STUDY The Reign of Zero Tolerance by Ben Gerson "Mr. Pemberton?" manager. The guards had radioed her that the "Yes, that's me," Simon replied distractedly, his back turned. target wasn't putting up much resistance. "Your personal belongings will be messen The two burly gentlemen who had suddenly gered to your home later today," Sallie...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT