Assignment 4 Solutions
Solution polyCalc.cc code:
/******************************************************************* * Name: Brenda Student * Date: 3/16/14 * Course: CSCI 132 Section 01 * Assignment: Assignment 4 * Instructor: Royden * Program: polyCalc.cc * Purpose: A polynomial calculator. This program allows a user to enter * polynomials and perform arithmetic operations on them--addition and * subtraction and multiplication-- and evaluate the polynomial at a particular * real number. Results can be printed. ***************************************************************************/ #include <iostream> #include "polynomial.h" #include "queue.h" #include "stack.h" using namespace std; void introduction(); void instructions(); char get_command(); bool do_command(char, Stack &); int main(void) /* Post: The program has executed simple polynomial arithmetic commands entered by the user. Uses: The classes Stack and Polynomial and the functions introduction, instructions,do_command, and get_command. */ { Stack stored_polynomials; //Stack to hold entered polynomials introduction( ); instructions( ); while (do_command(get_command( ), stored_polynomials)); } void introduction() /* Pre: none. Post: Prints out an introduction to the polynomial calculator. */ { cout << "Welcome to the polynomial calculator! " << endl; cout << "This program allows you to perform arithmetic operations on polynomials." << endl; } void instructions() /* Pre: none. Post: Prints out instructions for the polynomial calculator. */ { cout << "First enter a command, ?, =, +, -, *, or q " << endl; cout << "Each command allows specific operations as follows: " << endl; cout << "?: Allows entry of a polynomial. Each term of the polynomial is specified by two integers." << endl; cout << " The first integer specifies the coefficient and the second specifies the degree of the term." << endl; cout << " Terms must be entered in order of decreasing degree." << endl; cout << " Terms with coefficient of zero should be left out." << endl; cout << " End of entry is indicated with a zero coefficient." << endl << endl; cout << "=: Print out polynomial at the top of the stack (the result of the last operation). " << endl; cout << "+: Add the two polynomials at the top of the stack." << endl; cout << "-: Subtract the two polynomials at the top of the stack. " << endl; cout << "*: Multiply the two polynomials at the top of the stack. " << endl; } char get_command() { /* Post: Prompts for and reads in a user command. Returns entered character. */ char ans; cout << "Enter a command: ?, =, +, -, *, or q" << endl; cin >> ans; return ans; } bool do_command(char command, Stack &stored_polynomials) /* Pre: The first parameter species a valid calculator command. Post: The command specied by the rst parameter has been applied to the Stack of Polynomial objects given by the second parameter. A result of true is returned unless command == q. Uses: The classes Stack and Polynomial. */ { Polynomial p, q, r; //Polynomials for calculations switch (command) { case '?': p.read( ); if (stored_polynomials.push(p) == overflow) cout << "Warning: Stack full, lost polynomial" << endl; break; case '=': if (stored_polynomials.empty( )) cout << "Stack empty" << endl; else { stored_polynomials.top(p); p.print( ); } //end if--else break; case '+': if (stored_polynomials.empty( )) cout << "Stack empty" << endl; else { stored_polynomials.top(p); stored_polynomials.pop( ); if (stored_polynomials.empty( )) { cout << "Stack has just one polynomial" << endl; stored_polynomials.push(p); } else { stored_polynomials.top(q); stored_polynomials.pop( ); r.equals_sum(q, p); if (stored_polynomials.push(r) == overflow) { cout << "Warning: Stack full, lost polynomial" << endl; } //end if } //end if-- else } //end if--else break; case '-': if (stored_polynomials.empty( )) cout << "Stack empty" << endl; else { stored_polynomials.top(p); stored_polynomials.pop( ); if (stored_polynomials.empty( )) { cout << "Stack has just one polynomial" << endl; stored_polynomials.push(p); } else { stored_polynomials.top(q); stored_polynomials.pop( ); r.equals_difference(q, p); if (stored_polynomials.push(r) == overflow) { cout << "Warning: Stack full, lost polynomial" << endl; } //end if } //end if-- else } //end if--else break; case '*': if (stored_polynomials.empty( )) cout << "Stack empty" << endl; else { stored_polynomials.top(p); stored_polynomials.pop( ); if (stored_polynomials.empty( )) { cout << "Stack has just one polynomial" << endl; stored_polynomials.push(p); } else { stored_polynomials.top(q); stored_polynomials.pop( ); r.equals_product(q, p); if (stored_polynomials.push(r) == overflow) { cout << "Warning: Stack full, lost polynomial" << endl; } //end if } //end if-- else } //end if--else break; case 'e': if (!stored_polynomials.empty( )) { cout << "Give an argument at which to evaluate" << " the polynomial: "; cin >> number; cout << number; stored_polynomials.top(p); value = p.evaluate(number); cout << "\nThe polynomial: "; p.print(); cout << " at " << number << " evaluates to " << value << endl; } //end evaluate and print break; case 'q': cout << "Calculation finished." << endl; return false; } //end switch return true; } //end main
Home | | Syllabus | | Assignments | | Lecture Notes