//----------------------------------------------------------------------- // 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" //MY SOLUTION PART BEGINS HERE int convertBaseToBase(int initBase, int newBase, int number) { int exp10; int expB; int positions; //convert from base initBase to base 10 int base10 = 0; positions = (int)ceil(log10(number+1)); for (int i=0; i= newBase) { ans+=exp10*(base10%newBase); exp10*=10; base10/=newBase; } ans+=base10*exp10; return ans; } int baseMult(int base, int op1, int op2, char operation) { int ans; int val1=convertBaseToBase(base, 10, op1); int val2=convertBaseToBase(base, 10, op2); switch (operation) { case '*': ans=convertBaseToBase(10, base, val1*val2); break; case '+': ans=convertBaseToBase(10, base, val1+val2); break; } return ans; } void buildMath(int base, int operand1, int operand2, char operation, int* steps, int& numSteps, int& result) { if (operation=='*') { numSteps=(int)ceil(log10(operand2+1)); if (numSteps>1) { int tempOperand=operand2; for (int i=0; i= 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; } } //----------------------------------------------------------------------- // readIn - read input //----------------------------------------------------------------------- void readIn(istream& is, char& race, int& operand1, int& operand2, char& operation) { is >> ws >> race >> ws >> operand1 >> ws >> operand2 >> ws >> operation; } //----------------------------------------------------------------------- // 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