讲解:C++STLData Structures vectorstring C/C++

Introduction本次课程主要是用c++实现数据结构,要求用标准的STL库,比如vector和string,从输入文件中读取文件,并且按照3种对其方式进行格式对齐,完成程序,具体运行结果如图:RequirementCSCI-1200 Data Structures — Spring 2018Homework 1 — Text JustificationBefore starting this homework, make sure you have read and understood the Academic Integrity Policy.This homework assignment is a programming exercise involving simple text manipulation to format inputtext into a framed column with a specified width. You will work with command line arguments, file inputand output, and the C++ Standard Template Library string and vector classes. Please read the entireassignment before starting to program. When you are ready to begin, create a subdirectory for homeworkswithin your main Data Structures course directory and within that directory create a subdirectory hw1 forthis assignment.Your program will read words from an input file and greedily pack the words one at a time onto each line,making sure not to exceed the specified width. Your program will support three different methods for dealingwith any extra, unused space on each line. The first, flush left, simply positions each word as far to the left aspossible, leaving a gap of spaces (if any) on the right. The second, flush right, does the opposite, pushing allthe words to the right, and puts the extra spaces on the left. In the final mode, full justify, the extra spacesare distributed evenly between the words. If the number of extra spaces on the line do not divide evenly intothose “between” slots, the slots on the left will be assigned more spaces that the slots on the right.Let’s look at a simple example. Here is a sample input file, named example.txt:Here is an example of text justification.And here’s the output produced for that input file for the three different justification modes, for the specifiedtext width of 16:flush left flush right full justify——————–| Here is an || example of text || justification. |——————–——————–| Here is an || example of text || justification. |——————–——————–| Here is an || example of text || justification. |——————–First note that the output includes a simple ascii art line box drawn around the text, but those charactersdo not count towards the specified line width of the text. In other words, each line of final output is actually4 characters longer, in this example, 20 total characters. Notice that the same words appear on each linein all three cases. The only difference is where the “extra” spaces appear. The top line has 6 extra spaces(16 - # of characters in “Here is an” = 6). In full justify mode, these spaces are evenly divided into the 2slots between the 3 words. The second line has 1 extra space (16 - # of characters in “example of text”= 1). This extra space is assigned to the leftmost of the two slots on the second line. Also note a typicalconvention for full justify formatting: The last line of a block of text formatted with full justify is not forcedto stretch all the way to the right edge. No extra space is inserted between words on the last line – it shouldbe flush left justified only.File I/OYou will read the words to format from an input text file. You should not make any assumptions about theformatting of this file, except that the words will be separated by at least one whitespace character. Rememberthat whitespace characters include spaces, tabs, and newlines. Any punctuation in the file (including periods,commas, apostrophes, etc.) should be treated as part of the word if it is not separated from the word byspaces. The basic iostream string input operator, >>, will work perfectly for this assignment. The output ofyour program will be written to a file, and should follow the specifC++代写STL库Data Structures 如vector和string 代做留学生C/C++程序ications in this handout and match ourexamples. Reading and writing files in C++ is very similar to std::cin and std::cout. See examples ofSTL file streams on the course webpage “Misc. C++ Programming Information”.Command Line ArgumentsYour program will expect 4 command line arguments. The first is the name of the input file. The second isthe name of the output file. The third argument is an integer that specifies the width of the text column. Thefourth argument will be a string (flush_left, flush_right, or full_justify) specifying which formattingmode should be used. Here are examples of valid command lines for your program:./justify.exe example.txt example_16_flush_left.txt 16 flush_left./justify.exe example.txt example_16_flush_right.txt 16 flush_right./justify.exe example.txt example_16_full_justify.txt 16 full_justifyYou should implement very simple error checking to ensure that 4 arguments are provided and that theinput and output file streams are successfully opened. You should also check that the values for the thirdand fourth arguments are valid. Your program should exit gracefully with a useful error message sent tostd::cerr if there is a problem with the arguments.You must follow the specifications for the command line, input file, and output file exactly to ensure youreceive full credit from the Submitty homework submission autograder. We have provided sample input output files on the course website. Examples of using command line arguments can be found on the coursewebpage: “Misc. C++ Programming Information”.Corner CasesSo now you understand the core requirements for this assignment, but you may already have thought of afew problem cases. For example, how should the program full justify a line that contains just a single word?There are no available slots between words to use to insert the extra spaces. In this case your programshould simply left justify the line. Another question you might ask is what to do if one of the words in thefile is (by itself!) wider than the width of the column? In your initial coding and testing we recommend youassume that this will never happen. This solution will be worth nearly full credit. To receive full credit onthe assignment your program should handle this case by splitting the word and inserting a hyphen. Notethat we do not expect you to properly split the words between syllables, as that would require a databaseof English words and syllables. If you think of other corner cases as you work on the assignment, proposereasonable ways to handle those situations. If those solutions are overly complex or tricky, you do not needto tackle the implementation, but you should write up your thoughts in your README.txt for the graderto read.Extra CreditOnce the fundamental requirements of the assignment are completed, you may implement some fun extensionsto earn a few points of extra credit. Rather than formatting the text inside a simple rectangular column,output the text inside of a triangular, trapezoidal, or even circular frame. Document your extensions in yourREADME.txt file and include a sample command line and sample output file with the results.Submission DetailsUse good coding style. when you design and implement your program. Organize your program into functions:don’t put all the code in main! Be sure to read the “Homework Grading Criteria” as you put the finishingtouches on your solution. Be sure to make up new test cases to fully debug your program and don’t forgetto comment your code! Use the provided template README.txt file for notes you want the grader to read.You must do this assignment on your own, as described in the “Collaboration Policy Academic Integrity”handout. If you did discuss the problem or error messages, etc. with anyone, please list their names in yourREADME.txt file. Prepare and submit your assignment as instructed on the course webpage. Please ask a TAif you need help preparing your assignment for submission or if you have difficulty writing portable code.2& 转自:http://ass.3daixie.com/2018052610496761.html

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • By clicking to agree to this Schedule 2, which is hereby ...
    qaz0622阅读 5,431评论 0 2
  • 我小的时候很调皮,虽然没怎么挨揍,但挨骂的次数是数不清的。 每次挨骂的时候我都会哭,因为大人的语气让我感到害怕和不...
    我是文青阅读 4,786评论 9 11
  • 一个男孩说:“如果我保护的女孩, 被你们搞抑郁了,110带走我120带走你 从此十宗罪多的一个章节。” ...
    晚宁宝贝阅读 11,252评论 0 11
  • 我是老师,学生全寄宿,晚上要轮班。 在我值班的一晚,我出了教室去一楼上厕所。发现一只全身黑色,毛色发亮...
    小芦荟哈哈阅读 3,251评论 2 2
  • 2017~3~22 在本草纲目里,李时珍谈到过紫草之根的护肤功效,所以中国的草本护肤已经有很多年的历史了,现在日本...
    楠楠妈阅读 3,192评论 0 1

友情链接更多精彩内容