C++代写:C201 Search File

代写C++基础的I/O作业,读取文件,然后分析文本。

Part I

Write a program that will search a file of numbers of type int and write the largest and the smallest numbers to the screen. The file contains nothing but numbers of type int separated by blanks or line breaks. Your program should start with asking user to input the name of the file. Test your program against the data file provided by the instructor (hw3p1.txt, make sure you copy this file to your project folder: yourproject/yourproject/).

Part II

Write a program that merges the numbers in two files and writes all the numbers into a third file. Your program takes input from two different files and writes its output to a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to the largest. After the program is run, the output file will contain all the numbers in the two files in one longer list in sorted order from smallest to largest. Your program should define a function merge(), with a declaration shown as below, that is called with the two input-file streams and the output-file streams as three arguments. After the merging is complete, re-open each the three files and print out their contents to screen. Test your program against the data file provided by the instructor (hw3p2file1.txt, hw3p2file2.txt).

1
2
3
4
5
6
7
8
9
void merge(ifstream& input1, ifstream& input2, ofstream& output);
// Precondition:
// The input files are text files.
// The input files have been opened.
// The input files contain ASCII encoding of int objects separated by
// white space The int objects represented are in increasing order.
// Postcondition: The output file created is a file containing ASCII
// encoding of int objects separated by white space. The int objects
// are in increasing order.

Part III

One problem using cin to read directly into a variable such as an int is that if the user enters non- integer data then the program will continue with erroneous data and usually crash. A solution to this problem is to input data as a string, perform input validation, and then convert the string to an integer. Write a function getInteger() that prompts the user to enter an integer. It should use getline to read the user’s input into a string. Then use the stringstream class to extract an integer from the string. If an integer cannot be extracted then the user should be prompted to try again. The function should return the extracted integer. A test run of the program is shown below.

Hint: to check if an stringstream object, assuming it is named as ss, contains only one integer and nothing else, you can use the condition: (ss >> intVal) && (ss.eof()).

What to submit

  • Submit your code for all the three parts to Canvas (using the “Assignments” function).
  • Submit a hard copy of your code and test-run output (or screenshot).
  • Make sure that you follow the “Assignment_style-guideline_C201” or you will lose credits.