int main() { DisplayOpeningScreen("Firstname:Lastname:A00123456:csc34201", "Submission 05: Building and Displaying Power Sets"); cout << "\nThis program prompts for an integer value n and builds " "the set\n{1, 2, 3, ..., n}. It then calls a (recursive) function " "powerSet()\nthat returns the power set of the set. The power set " "is the set of\nall subsets of the set and has 2^n elements. " "After displaying the\nsize of the power set, the program calls " "the function WriteAllSubsets()\nthat outputs each subset using " "the notation {e_1, e_2, ..., e_m},\nfour sets per line.\n\n"; Pause(); int n; ReadInt("Enter a non-negative integer " "(<= 6 if you don't want a lot of output): ", n); set<int> sInt; //Set of integers whose power set we construct for (int i=1; i<=n; i++) sInt.insert(i); //Build set {1,2,3, ..., n} set<set<int>> pSet = powerSet(sInt); //Build the power set //Output number of subsets, followed by all subsets, four per line cout << "\nNumber of subsets = " << pSet.size() << ", and here they are:\n"; WritePowerSet(pSet); Pause(); }
//option 3 void TransferTextToBinary ( fstream& ioFile, bool& successful ) { string fileName; ReadString("Enter name of input textfile containing club " "members: ",fileName); ifstream inputTextFile(fileName); ioFile.open("working.bin",ios::binary| ios::out| ios::in| ios::trunc); if(!inputTextFile) { cout << "\nError opening input textfile.\nReturning to menu." << endl; Pause(); successful = false; } else if(!ioFile) { cout << "\nError opening working binary file." "\nReturning to menu." << endl; Pause(); successful = false; } else { string temp; int count = 1; //get the name while(getline(inputTextFile,temp,'\n')) { if(count == 1) { //clear the char array memset(member.name,'\0',31); for(unsigned int i = 0 ; i < temp.length(); i++) member.name[i] = temp[i]; count++; } //get the age and balance getline(inputTextFile,temp,'\n'); if(count == 2) { string::size_type space = temp.find(' '); string tempAge = temp.substr(0,space); //find the age and store it in struc member.age = atoi(tempAge.c_str()); string::size_type dollar = temp.find('$'); string tempBalance = temp.substr(dollar+1); //find the balance and store it in struc member.balance = atof(tempBalance.c_str()); count--; } //write the struc into the binary file ioFile.write((char *)&member, sizeof(ClubMember)); } successful = true; inputTextFile.close(); } }
int main() { DisplayOpeningScreen("Lastname:Firstname:A00123456:CSC34201", "Submission ??: " "Implementing an ADT Binary Search Tree of Characters, Part ?"); BSTChar bst; Menu menu; BuildMenu(menu); int menuChoice; do { menu.display(); menuChoice = menu.getChoice(); switch(menuChoice) { case -1: case 1: cout << "Program now terminating.\n"; break; case 2: DescribeProgram(); break; case 3: BuildTree(bst); break; case 4: DisplayTree(bst); break; case 5: GetTreeSize(bst); break; case 6: GetTreeHeight(bst); break; case 7: DeleteAllValues(bst); break; case 8: FindAValue(bst); break; case 9: InsertAValue(bst); break; case 10: DeleteAValue(bst); break; case 11: ModifyAllValues(bst); break; case 12: BalanceTree(bst); break; } Pause(); } while (menuChoice != -1 && menuChoice != 1); }
void DisplayUsage() { cout <<"This program evaluates any valid prefix or postfix " "expression which\ncontains positive integer operands and the " "operators +, -, * and/or /.\nThe expression to be evaluated " "must be entered on the command line.\nA single positive integer " "also evaluates as itself." "\n\nTypical usage examples:" "\n> eval_pre_post + * 2 3 4" "\n+ * 2 3 4 = 10" "\n> eval_pre_post 2 3 4 + *" "\n2 3 4 + * = 14" "\n\nThe following errors are recognized and reported:" "\n\nFor prefix expressions:" "\nError: End of prefix expression reached unexpectedly." "\nError: Bad operator ? encountered evaluating prefix expression." "\nError: Extraneous characters found after valid prefix expression." "\n\nFor postfix expressions:" "\nError: End of postfix expression reached unexpectedly." "\nError: Bad operator ? encountered evaluating postfix expression." "\nError: Not enough operands for postfix operator ?." "\nError: Found too many operands when evaluating postfix expression." "\n"; Pause(); }
//option 8 void WriteTextArchiveAndDeleteBinary ( fstream& ioFile, bool& successful ) { ioFile.clear(); string outputFile; ReadThisLine("Enter name of output textfile for archiving " "club member data: ",outputFile); ofstream outFile(outputFile); if(!outFile) { cout << "\nError opening out textfile.\nReturning to menu." << endl; Pause(); successful = false; } else if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { //start at beginning of file ioFile.seekg(0); while(!ioFile.eof()) { //read the binary file ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //store the info from binary file back into a txt file outFile << member.name << endl; outFile << member.age << " $" << member.balance << endl; } } successful = true; //clear the txtfile as well as the binary file outFile.clear(); outFile.close(); ioFile.clear(); ioFile.close(); //delete the binary file remove("working.bin"); } }
//option 4 void DisplayAllMembers ( fstream& ioFile, bool& successful ) { ioFile.clear(); int lineNum = 0; if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { //start at the beginning of file ioFile.seekg(0); while(!ioFile.eof()) { //read the strucs ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //print the club members cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; } lineNum++; //if more than 8 members, pause if(lineNum == 8) { Pause(); lineNum = 0; } } successful = true; } }
//option 5 void DisplayOneOrMoreMembers ( fstream& ioFile, bool& successful ) { ioFile.clear(); if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { string searchedName; ReadThisLine("Enter full or partial name of member(s) whose data " "you wish to display:",searchedName); //start at the beginning of file ioFile.seekg(0); while(!ioFile.eof()) { ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //if found the searchedName(full or partial) if(((string)member.name).find(searchedName) != string::npos) { //print it cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; } } } successful = true; } }
//option 6 void AddNewMember ( fstream& ioFile, bool& successful ) { if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { ioFile.clear(); string newMemberName; int newMemberAge; double newMemberBalance; //ask for info for the new club member ReadThisLine("Enter name of new member: ",newMemberName); ReadInt("Enter age of new member: ", newMemberAge); ReadDouble("Enter opening balance of new member: ",newMemberBalance); //clear the array memset(member.name,'\0',31); for(unsigned int i = 0 ; i < newMemberName.length(); i++) member.name[i] = newMemberName[i]; //store the info into the struc member.age = newMemberAge; member.balance = newMemberBalance; ioFile.seekg(0, ios::end); //write the struc into the binary file ioFile.write((char *)&member, sizeof(ClubMember)); ioFile.seekg(0); successful = true; } }
int main() { Date d1; Date d2(2000); Date d3(2010, 4); Date d4(1900, 11, 7); Date d5(-213, 30, 85); // used in setter tests below cout << "Constructor & Print Tests:"; cout << "\n\tShould be 2014-01-01: "; d1.print(); cout << "\n\tShould be 2000-01-01: "; d2.print(); cout << "\n\tShould be 2010-04-01: "; d3.print(); cout << "\n\tShould be 1900-11-07: "; d4.print(); cout << "\n\n"; Pause(); cout << "Getter Tests:"; cout << "\n\tShould be 1900: " << d4.getYear(); cout << "\n\tShould be 11: " << d4.getMonth(); cout << "\n\tShould be 7: " << d4.getDay(); cout << "\n\tShould be 2010: " << d3.getYear(); cout << "\n\n"; Pause(); cout << "Leap Year Tests:"; cout << "\n\tShould be 1: " << Date::isLeapYear(1960); cout << "\n\tShould be 0: " << Date::isLeapYear(1962); cout << "\n\tShould be 0: " << Date::isLeapYear(1900); cout << "\n\tShould be 1: " << Date::isLeapYear(2000); cout << "\n\n"; Pause(); /* cout << "Month Tests:"; cout << "\n\tShould be 31: " << Date::daysInMonth(1); cout << "\n\tShould be 29: " << Date::daysInMonth(2, 1960); cout << "\n\tShould be 28: " << Date::daysInMonth(2, 1963); cout << "\n\tShould be 28: " << Date::daysInMonth(2); cout << "\n\tShould be 31: " << Date::daysInMonth(3); cout << "\n\tShould be 30: " << Date::daysInMonth(4); cout << "\n\tShould be 31: " << Date::daysInMonth(5); cout << "\n\tShould be 30: " << Date::daysInMonth(6); cout << "\n\tShould be 31: " << Date::daysInMonth(7); cout << "\n\tShould be 31: " << Date::daysInMonth(8); cout << "\n\tShould be 30: " << Date::daysInMonth(9); cout << "\n\tShould be 31: " << Date::daysInMonth(10); cout << "\n\tShould be 30: " << Date::daysInMonth(11); cout << "\n\tShould be 31: " << Date::daysInMonth(12); cout << "\n\tDon't care(!): " << Date::daysInMonth(13); cout << "\n\n"; Pause(); */ cout << "Setter Tests:"; d1.set(1904, 10, 10); cout << "\n\tShould be 1904-10-10: "; d1.print(); d1.set(1905, 11, 40); cout << "\n\tShould be 1904-10-10: "; d1.print(); d1.set(1906, 15, 11); cout << "\n\tShould be 1904-10-10: "; d1.print(); d1.set(1907, 3, 31); cout << "\n\tShould be 1907-03-31: "; d1.print(); d1.set(1908, 4, 31); cout << "\n\tShould be 1907-03-31: "; d1.print(); d1.set(1909, 2, 29); cout << "\n\tShould be 1907-03-31: "; d1.print(); d1.set(1912, 2, 29); cout << "\n\tShould be 1912-02-29: "; d1.print(); d1.set(d4.getYear(), d4.getMonth(), d4.getDay()); cout << "\n\tShould be 1900-11-07: "; d1.print(); cout << "\nSetter + Constructor Test:"; cout << "\n\tShould be 2014-01-01: "; d5.print(); cout << "\n\n"; Pause(); cout << "Equals Tests:"; testEquals(Date(1950, 10, 10), Date(1950, 10, 10), "true"); testEquals(Date(1950, 10, 20), Date(1950, 10, 15), "false"); testEquals(Date(1950, 9, 20), Date(1950, 10, 20), "false"); testEquals(Date(1950, 10, 20), Date(1951, 10, 20), "false"); cout << "\n\n"; Pause(); cout << "Before Tests:"; testBefore(Date(1950, 10, 10), Date(1950, 10, 10), "false"); testBefore(Date(1950, 10, 10), Date(1950, 10, 9), "false"); testBefore(Date(1950, 10, 10), Date(1950, 9, 11), "false"); testBefore(Date(1950, 10, 10), Date(1850, 11, 11), "false"); testBefore(Date(1950, 10, 15), Date(1950, 10, 20), "true"); testBefore(Date(1950, 10, 20), Date(1950, 11, 10), "true"); testBefore(Date(1950, 10, 20), Date(1951, 5, 1), "true"); cout << "\n\n"; Pause(); }
//option 7 void UpdateMemberData ( fstream& ioFile, bool& successful ) { //store the position of the stream(later to be used for seekg/p) streamoff position; ioFile.clear(); int count = 0; if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { string identity; ReadThisLine("Enter enough of the member's name to " "identify him/her: ", identity); //start at beginning ioFile.seekg(0); while(!ioFile.eof()) { //make a temporary position to hold the beginning of the struc streamoff tempPos = ioFile.tellg(); //read the binary file ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { if(((string)member.name).find(identity) != string::npos) { //put the beginning of the struc into the position position = tempPos; //print club member cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; count++; } } } if(count != 1) { cout << "\n=====>Either more than one member, or no members, " "matched your input.\n=====>You need to identify a " "unique club member to update.\n=====>Try again with " "more specific input.\n" << endl; Pause(); successful = false; } if(count == 1) { cout << "\nUpdating data for the above club member." << endl; string choice; do { cout << "Enter n/a/b to update name/age/balance, or " "q to quit updating this member: "; getline(cin,choice); if(choice == "q") { cout << "\nOK, no more updates for this club member " "at this time." << endl; Pause(); successful = true; } if(choice == "n") { string revisedName; ReadThisLine("Enter revised name: ", revisedName); //clear the array memset(member.name,'\0',31); for(unsigned int i = 0 ; i < revisedName.length(); i++) //store the new name into the struc member.name[i] = revisedName[i]; } if(choice == "a") { int newAge; ReadInt("Enter revised age: ", newAge); //store new age in struc member.age = newAge; } if(choice == "b") { double newBalance; ReadDouble("Enter revised balance: ", newBalance); //store new balance in struc member.balance = newBalance; } } while(choice != "q"); } ioFile.clear(); //go to the position ioFile.seekg(position); ioFile.seekp(position); //finally write the info back into the binary file ioFile.write((char *)&member, sizeof(ClubMember)); successful = true; } }
int main() { DisplayOpeningScreen(MY_ID_INFO, "Submission 05: Computing with Fractions, Part 2"); Menu menu; BuildMenu(menu); menu.display(); int choice; choice = menu.getChoice(); while(choice > 0) { //if user chooses 1, terminate program if(choice == 1) { cout << "Program is now terminating.\n"; Pause(); return 0; } //if user chooses 2, display program info if(choice == 2) { TEXT.displayItem("Program Description"); } //if user chooses 3 , evaluate the fractions if(choice == 3) { string answer; do { cout << "\nEnter fractional expression to be evaluated: "; //declare all variables in this scope Fraction fraction1, fraction2, finalFraction; char operators; cin >> fraction1; cin >> operators; cin >> fraction2; //if operator is + , do addition if(operators == '+') finalFraction = fraction1+fraction2; //if operator is - , do subtraction else if(operators == '-') finalFraction = fraction1-fraction2; //if operator is * , do multiplication else if(operators == '*') finalFraction = fraction1*fraction2; //if operator is / , do division else if(operators == '/') finalFraction = fraction1/fraction2; cout << "Answer: " << fraction1 << " " << operators << " " << fraction2 << " = " << finalFraction; cout << "\nDo another calculation?"; cin.ignore(); } while(userSaysYes(answer)); } //if user chooses 4, compare the fractions if(choice == 4) { string answer; do { cout << "\nEnter two fractions to be compared: "; //declare all variables in this scope Fraction fraction1, fraction2, differenceFraction1, differenceFraction2; cin >> fraction1; cin >> fraction2; //if the 2nd one is bigger than the first , calculate the //difference between the two differenceFraction1 = fraction2 - fraction1; //if the 1st one is bigger than the second , calculate the //difference between the two differenceFraction2 = fraction1 - fraction2; //if the 1st fraction is less than the second, print it as well //as the difference between the two if(fraction1 < fraction2) { cout << fraction2 << " is greater than " << fraction1 << " and the difference is " << differenceFraction1 << "."; } //if the 2nd fraction is less than the first, print it as well //as the difference between the two if(fraction2 < fraction1) { cout << fraction1 << " is greater than " << fraction2 << " and the difference is " << differenceFraction2 << "."; } //if the fractions are equal print it else if(fraction1==fraction2) { cout << "The two fractions are equal."; } cout << "\nDo another comparison?"; cin.ignore(); } while(userSaysYes(answer)); }
int main() { DisplayOpeningScreen("Iqbal:Arsum:A00351433:CSC34218", "Creating and Displaying Balanced Binary Search Trees"); vector<char> treeValues; RandomGenerator randomValues; TreeNode node; int count = 0; int height = 0; bool success = false; Menu m("Menu for Balanced Character BST Creation and Display"); AddMenuOptions(m); int menuChoice; do { m.display(); menuChoice = m.getChoice(); switch (menuChoice) { case 1: case -1: break; case 2: cout << "Enter characters into the vector (terminate with an end" "-of-file on a new line): "; //clears previous values in the vector treeValues.clear(); char values; while(cin >> values) //store characters into vector treeValues.push_back(values); //clear input stream cin.clear(); Pause(0, "All characters have been entered into the vector."); //set success to false because bst hasnt been created yet success = false; break; case 3: int requiredVal; cout << "Enter required number of values: "; cin >> requiredVal; char firstVal; cout << "Enter first value of character range: "; cin >> firstVal; char secondVal; cout << "Enter last value of character range: "; cin >> secondVal; //clear previous value of vector treeValues.clear(); while(count != requiredVal) { //get a random string from given character range string temp = randomValues.getNextStringFromCharRange (1,firstVal,secondVal); //convert to a character const char* generatedVal = temp.c_str(); vector<char>::iterator p1 = treeValues.begin(); //find the character in the vector p1 = find(treeValues.begin(),treeValues.end(),*generatedVal); //if it couldnt find the value if(p1 == treeValues.end()) { //insert the value into the vector treeValues.push_back(*generatedVal); count++; } } //reset counter count = 0; //clear input stream cin.ignore(); Pause(0, "All values have been generated."); //set success to false because bst hasnt been created yet success = false; break; case 4: cout << "\nHere are the current contents of the vector:\n" << endl; //Display values from the vector copy(treeValues.begin(),treeValues.end(), ostream_iterator<char>(cout, " ")); Pause(0, "\n"); break; case 5: //create the binary search tree createBst(treeValues,node); Pause(0, "The balanced binary search tree has been created."); //set success to true because bst has been created success = true; break; case 6: //if the bst wasnt created if(!success) { cout << "Tree is empty. Nothing to display.\n"; Pause(); break; } //find the height of the bst height = findHeight(&node); if (height <= 3) displayTree(&node); else cout << "\nHeight of tree is too great for display (>=4).\n"; Pause(); break; } } while (menuChoice != 1 && menuChoice != -1); Pause(0, "Program now terminating."); }