int runMenu(Polynomial& lhs, Polynomial& rhs, Polynomial& ans) { //prompt cout << "How would you like to add Polynomials?" << endl << " 1. Do the whole shebang!" << endl << " 2. Enter first Polynomial." << endl << " 3. Enter second Polynomial." << endl << " 4. View current Polynomials." << endl << " 5. Compute!" << endl << " 6. Quit." << endl << ": "; string input; bool error = false; cin >> input; switch (strtol(input.c_str(),NULL,10)) { //protects from cin >> int not getting an int and throwing a tantrum case 1: //enter two polynomials, compute, and view, without going back to the menu error = inputPoly(lhs); if (error) break; error = inputPoly(rhs); if (error) break; ans = lhs + rhs; lhs.reduce(); rhs.reduce(); cout << "\n\n" << lhs.toString() << endl << "+\n" << rhs.toString() << endl << "=\n" << ans.toString() << endl << endl; break; case 2: //input left operand error = inputPoly(lhs); lhs.reduce(); break; case 3: //input right operand error = inputPoly(rhs); rhs.reduce(); break; case 4: //view stored polynomials without adding cout << lhs.toString() << endl << endl << rhs.toString() << endl << endl << ans.toString() << endl << endl; break; case 5: //add two polynomials and display the resulting equation ans = lhs + rhs; cout << "\n\n" << lhs.toString() << endl << "+\n" << rhs.toString() << endl << "=\n" << ans.toString() << endl << endl; break; case 6: //end program cout << "See ya!" << endl; //only viewable if your terminal doesn't automatically close return 0; break; default: //unexpected input cout << "Invalid selection" << endl; } if (error) cout << "\nError parsing string!" << endl; return 1; }
Polynomial operator+(const Polynomial& p1, const Polynomial& p2) { Polynomial p; p._degree = std::max(p1._degree, p2._degree); p._terms = Polynomial::TermList(p1._terms.size() + p2._terms.size()); merge(p1._terms.begin(), p1._terms.end(), p2._terms.begin(), p2._terms.end(), p._terms.begin()); p.reduce(); return p; }