// Name // CS201 HW5 // Problem 1 #include #include using namespace std; // triangle function prototype void triangle (int s1, int s2, int s3, double& area, double& perimeter); int main() { double area=0, perimeter=0; int a, b, c; cout << "Enter the lengths of each side for a triangle: \n"; cin >> a >> b >> c; if (a+b>c && a+c>b && b+c>a) { //check for a reasonable combination of a, b, c triangle(a, b, c, area, perimeter); cout << " \nThe perimeter of the triangle is " < #include #include #include using namespace std; //creat the initial advice, you may use any text editor to do this void initial_advice(); //function to print out the previous advice void my_advice(); //function to ask for your advice void your_advice(); int main() { initial_advice(); // generate advice.txt file include the very first advice //if you have advice.txt already, you don't need to do this char repeat = 'y'; do { my_advice(); your_advice(); cout << "Would you like to run this program again? (y/n) \n"; cin >> repeat; } while (repeat=='y' || repeat=='Y'); return 0; } void initial_advice() { ofstream out_stream; out_stream.open("advice.txt"); if (out_stream.fail()) { cout << "Output file opening failed.\n"; exit(1); } out_stream <<"Please put your name and problem # in your program\n"; out_stream.close(); } void my_advice() { ifstream in_stream; in_stream.open("advice.txt"); if (in_stream.fail()) { cout << "Input file opening failed.\n"; exit(1); } cout << "The following is my advice to you:\n"; char next_symbol; in_stream.get(next_symbol); while (!in_stream.eof()) { cout << next_symbol; in_stream.get(next_symbol); } in_stream.close(); cout << endl; } void your_advice() { ofstream out_stream; out_stream.open("advice.txt"); if (out_stream.fail()) { cout << "Output file opening failed.\n"; exit(1); } char next_symbol; cout << "Enter some new advice and press enter twice when done:\n"; int return_check = 0; // a flag for meeting two consecutive \n s do { cin.get(next_symbol); if (next_symbol == '\n') { out_stream << next_symbol; cin.get(next_symbol); if (next_symbol == '\n') // If two consecutive \n, it ends return_check = 1; else out_stream << next_symbol; } else out_stream << next_symbol; } while (return_check != 1); out_stream.close(); } /* The following is my advice to you: Please put your name and problem # in your program Enter some new advice and press enter twice when done: Please keep your program as simple as possible Would you like to run this program again? (y/n) y The following is my advice to you: Please keep your program as simple as possible Enter some new advice and press enter twice when done: Please do exactly what the problem ask you to do, and use as many functions as possible in your programs Would you like to run this program again? (y/n) n Press any key to continue */ // Name // CS201 HW5 // Problem 3 #include #include #include using namespace std; // function prototype void order_list(ifstream& file1, ifstream& file2, ofstream& output); void checkfile(char file[16]); int main() { char retry='y'; do{ // assume file namce has 16 or less charaters char file_one[16], file_two[16], file_three[16]; ifstream in_stream1, in_stream2; ofstream out_stream; // enter input file one cout << "Enter input file one:\n"; cin >> file_one; in_stream1.open(file_one, ios::in|ios::binary ); checkfile(file_one); // enter input file two cout << "Enter input file two:\n"; cin >> file_two; in_stream2.open(file_two, ios::in|ios::binary ); checkfile(file_two); // enter output file name cout << "Enter output file:\n"; cin >> file_three; out_stream.open(file_three); checkfile(file_three); //sort the numbers from two files and output them to the third file order_list (in_stream1, in_stream2, out_stream); // run program again? */ cout << endl << "Again? (y/n)"; cin >> retry; } while(retry=='y'||retry=='Y'); return 0; } // check file to see if it openned void checkfile(char file[16]) { ifstream stream; stream.open(file); if (stream.fail()) { cout << "Opening the file '" << file <<"' failed!\n"; exit(1); } stream.close(); } void order_list(ifstream& in_stream1, ifstream& in_stream2, ofstream& output) { int number_one; int number_two; // get two nubmers from the two files in_stream1 >> number_one; in_stream2 >> number_two; // if the two files both donot come to EOF, save the smaller (<=) number from one file // and get another number from that file for next comparsion. while (!in_stream1.eof() && !in_stream2.eof()) { if ( number_one <= number_two ) { output << number_one << "\n"; in_stream1 >> number_one; } else { output << number_two << "\n"; in_stream2 >> number_two; } } // if come to the end of one file, all the rest in the // other file must be larger and in order if ( !in_stream1.eof() ) // in_stream2 come to EOF while (!in_stream1.eof()) { output << number_one << "\n"; in_stream1 >> number_one; } else // in_stream1 come to EOF while (!in_stream2.eof()) { output << number_two << "\n"; in_stream2 >> number_two; } } /* Enter input file one: input1.dat Enter input file two: input2.dat Enter output file: output1.dat Again? (y/n)y Enter input file one: input1.dat Enter input file two: input3.dat Enter output file: output2.dat Again? (y/n)y Enter input file one: input2.dat Enter input file two: input3.dat Enter output file: output3.dat Again? (y/n)n Press any key to continue */ /****output1.dat******* 1 2 10 11 12 13 14 */ /*****output2.dat****** 1 5 5 11 13 35 35 */ /*****output3.dat****** 2 5 5 10 12 14 35 35 */