C++ linklist supercard

profilekuucd
TestSupercard.cpp

//Catherine Stringfellow and ???? // // #include<iostream> #include<fstream> #include <string> #include "Supercard.h" using namespace std; //Purpose: Introduces the program. //Requires: Outfile. void printHeading(ofstream &outfile); //Purpose: Prints message (string) as label and the digits of supercard. //Requires: A Supercard and outfile. void printSuperCard(ofstream & outfile, string message, Supercard & S); //Purpose: Set S1 to the 10s complement of S2 //Requires: Two Supercards S1 and S2 void tensComplement(Supercard & S1, Supercard & S2); //Purpose: Sets result to sum of S1 and S2 //Requires: Three supercards S1, S2, and S3 void addSupercards(Supercard & S1, Supercard & S2, Supercard & result); //Purpose: Opens file to be written to. void openFiles(ofstream &outfile); void printEndMessage(ofstream & outfile); //Purpose: Thanks the user for running SuperCard Program. //Requires: outfile. int main() { Supercard S1, S2, S3; ofstream outfile; openFiles(outfile); printHeading(outfile); //test SetDigit and initialize S1 outfile << "Initialize S1\n"; for (int i = 1; i <= 9; i++) S1.setDigit(i, i); for (int i = 10; i <= 14; i++) S1.setDigit(i, 0); //test length outfile << "The length of S1 is " << S1.getLength() << endl; //output value of S1 printSuperCard(outfile, "S1 = ", S1); //swap S1 and S2 and print S2 outfile << "Swap S1 and s2" << endl; S1.swap(S2); printSuperCard(outfile, "S2 = ", S2); //clear S2 and reset it to 987654321 and ouput value of S2 outfile << "Reset S2 to 987654321\n"; S2.clear(); for (int i = 1; i <=9; i++) S2.setDigit(i, i); printSuperCard(outfile, "S2 = ", S2); //test get digit and compute 10's complement of S2 outfile << "Let S1 be the 10's complement of S2\n"; tensComplement(S1, S2); //output the new value of S1 printSuperCard(outfile, "S1 = ", S1); //add S1 and S2, store in S3 and print it addSupercards(S1, S2, S3); printSuperCard(outfile, "Sum of S1 and S2 is ", S3); //test destructor outfile<<"Getting ready to destruct S1 and S2.\n"; printEndMessage(outfile); return 0; } void openFiles(ofstream &outfile) { //opens file to be written to. outfile.open("SCOut.txt"); } void printHeading(ofstream & outfile) { outfile << "ZHANG YI" << endl << "Program 5" << endl << "20 April 2018" << endl << "Program will process big numbers." << endl << endl; } void printEndMessage(ofstream & outfile) { outfile << "Thank you for using the SuperCard Program. \n\n"; outfile << "If your program does not crash - destructor worked.\n\n"; outfile << "Goodbye." << endl; } void printSuperCard(ofstream & outfile, string message, Supercard & S) { outfile << message; for (int i = S.getLength(); i > 0; i--) { outfile << S.getDigit(i); } } void tensComplement(Supercard & S1, Supercard & S2) { S1.clear(); //clear S1 for (int i = 1; i < S2.getLength();i++) { S1.setDigit(i, 10 - (S2.getDigit(i))); } //set each digit of S1 to 10 - the corresponding digit in S2 } void addSupercards(Supercard & S1, Supercard & S2, Supercard & result) { result.clear(); //clear result int carry = 0, x, y; int i = 1; int sum = 0; //declare variables needed while (i <= S1.getLength() || i <= S2.getLength()) { if (S1.getDigit(i) != -1) { x = S1.getDigit(i); } else x = 0; //while more digits in either S1 or S2, keep adding digits if (S2.getDigit(i) != -1) { y = S2.getDigit(i); } else y = 0; sum = S1.getDigit(i) + S2.getDigit(i) + carry; carry = sum / 10; sum = sum % 10; result.setDigit(i, sum); i++; if (carry == 1 && !(i <= S1.getLength() || i <= S2.getLength())) result.setDigit(i, carry); }