//----------------------------------------------------------------------- // File: prob.cc // Description: Middle Earth math // Author: Evan Golub (egolub@cs.umd.edu) //----------------------------------------------------------------------- #include #include #include #include #include #include // Uncomment the following lines if using APCS classes // #include "apvector.h" // #include "apmatrix.h" // #include "apstack.h" // #include "apqueue.h" #include "apstring.cpp" ////////////////// BEGIN your solution ////////////////// void buildMath(int base, int operand1, int operand2, char operation, int* steps, int& numSteps, int& result) { if (operation=='*') { numSteps=2; steps[0] = operand1; steps[1] = operand1; result = operand1 * operand2; } else { numSteps = 0; result = operand1 + operand2; } } ////////////////// END your solution ////////////////// //----------------------------------------------------------------------- // verifyInput - make sure input is legal for base //----------------------------------------------------------------------- void verifyInput(int base, int num) { if (num >= 10000) { cout << "ERROR: number too large " << num << endl; return; } int n = num; while (n > 0) { if ((n % 10) >= base) { cout << "ERROR: illegal number " << num << endl; return; } n = n/10; } } //----------------------------------------------------------------------- // writeOut - write output //----------------------------------------------------------------------- void writeOut(ostream& os, int operand1, int operand2, char operation, int steps[100], int n, int result) { int i, WIDTH; WIDTH = (int) ceil(log10(result+1)) + 2; if ((i = (int) ceil(log10(operand1+1)) + 2) > WIDTH) WIDTH = i; if ((i = (int) ceil(log10(operand2+1)) + 2) > WIDTH) WIDTH = i; os << setw(WIDTH) << operand1 << endl; os << operation << setw(WIDTH-1) << operand2 << endl; if (n>1) { for (i=0; i