//CMPT 212-Assignment 1 //Raymond Kin Hei Lee //Feb 7, 2008 // //Every number, I broke into sections of 9 digits, and put them each in a long variable, then //I put them into an vector for data manipulation. // //Addition/Subtraction - I made two functions (Sum() and Difference()) for adding the //ABSOLUTE values of two values. I purposely broke the numbers up into chunks of 9 digits //(long holds 10), so I could store the result (long)a+(long)b back into a long variable. //After that, I took the carry and moved it onto the next vector component. Subtraction is //a similar idea. Afterwards, I made another function called Add and Subtract for performing //different Sum() and Difference() depending on the sign. ie. Subtract(-a,+b) = Difference(a,b)... //Add(-a,-b) = -Sum(a,b), etc. // //Multiplication - No special algorithms, just the same way as I would do on paper. However, I treated each //component within a vector (a long with 9 digits) as a single number when multiplying. // //Division - Since I couldn't come out with a good division algorithm, I decided to do it using a //Multiplication-Iteration method. ie. if a/b = c, then b*c = a. I used the midway iteration method, //which cuts down the possibility of getting the nx answers by 1/2 for every iteration. // //Other Functions - Most of them were just for my own convenience.. comparing vector sizes, //displaying the vector on dos prompt, etc. #include #include #include #include using namespace std; int CompareV(vector a, vector b); // Compares the ABSOLUTE value of the two //vectors, returns 1 if a>b, returns 2 if a readInput(string stringA, bool *sign, long *digit); // Takes the Cin input as a form of a //string, then turns it into a //vector the method I mentioned before. void DisplayValue(vector v_in, bool sign); //cout the values of the vector bool checkValid(string stringA); //Check for "Invalid Inputs" vector Sum(vector accum, vector input); //Adds the ABSOLUTE value of two vectors vector Difference(vector accum, vector input, bool *sign);//Takes the ABSOLUTE difference of two vectors vector Add(vector a, vector b, bool *a_sign, bool *b_sign); //Adds two vectors. This function considers //the sign of vectors vector Subtract(vector a, vector b, bool *a_sign, bool *b_sign); //Finds a-b. Considers the sign vector Multiply(vector a, vector b, bool *a_sign, bool *b_sign); //Calculates a*b. Sign is considered vector DivideTwo(vector a); //Finds A/2. Used for the iteration method I used for my division algorithm vector Division(vector a, vector b, bool *a_sign, bool *b_sign, long *a_digit, long *b_digit); //Calculates the division of two vectors. A/B. Only the integer portion. int CompareV(vector a, vector b) // Compares the ABSOLUTE value of the two //vectors, returns 1 if a>b, returns 2 if ab.size()) return 1; else if(a.size()=0;i--) { if(a[i]>b[i]) return 1; else if(a[i] Sum(vector accum, vector input) //Adds the ABSOLUTE value of accum and input { vector v_temp; long temp=0; long n=0; if(accum.size()>input.size()) { n=accum.size(); input.resize(n); } else { n=input.size(); accum.resize(n); } int carrier =0; for(long i = 0; i < n; i++) { temp = accum[i] + input[i] + carrier; carrier =0; while(temp>=1000000000) { temp = temp - 1000000000; carrier++; } v_temp.push_back(temp); } if(carrier!=0) v_temp.push_back(carrier); return v_temp; } vector Difference(vector accum, vector input, bool *sign) // Subtracts the absolute value, |accum|-|input| { vector v_temp; long temp=0; long n=0; switch(CompareV(accum,input)) { case 1://accum bigger than input { input.resize(accum.size()); for(unsigned long i=0; i< accum.size(); i++) { if((accum[i] - input[i])<0) { v_temp.push_back(accum[i] + 1000000000 - input[i]); accum[i+1]--; } else v_temp.push_back(accum[i] - input[i]); } *sign = true; break; } case 2: //input bigger than accum { accum.resize(input.size()); for(unsigned long i=0; i< input.size(); i++) { if(input[i] - accum[i]<0) { v_temp.push_back( input[i] + 1000000000 - accum[i]); input[i+1]--; } else v_temp.push_back( input[i] - accum[i]); } *sign = false; break; } case 3: { v_temp.push_back( 0); *sign = true; break; } } return v_temp; } vector Add(vector a, vector b, bool *a_sign, bool *b_sign) // Adds A and B, will consider the signs { if(((*a_sign == true) && (*b_sign == true))) return Sum(a,b); else if(((*a_sign == true) && (*b_sign == false))) return Difference(a,b,a_sign); else if(((*a_sign == false) && (*b_sign == true))) return Difference(b,a,a_sign); else if(((*a_sign == false) && (*b_sign == false))) { *a_sign = false; return Sum(a,b); } return Sum(a,b); } vector Subtract(vector a, vector b, bool *a_sign, bool *b_sign) //Subtracts, A-B, will consider sign { if(((*a_sign == true) && (*b_sign == true))) return Difference(a,b,a_sign); else if(((*a_sign == true) && (*b_sign == false))) return Sum(a,b); else if(((*a_sign == false) && (*b_sign == true))) { *a_sign = false; return Sum(a,b); } else if(((*a_sign == false) && (*b_sign == false))) return Difference(b,a,a_sign); return Difference(b,a,a_sign); } vector Multiply(vector a, vector b, bool *a_sign, bool *b_sign) { unsigned long long temp = 0 ; long counter =0; vector product(a.size()+ b.size(), 0); if(((*a_sign == true) && (*b_sign==true)) || ((*a_sign == false) && (*b_sign==false))) *a_sign = true; else *a_sign = false; stringstream out; string keep = "0"; string carrier = "0"; a.push_back(0); for(long j=0; j<(long)b.size(); j++) { for(long i=0; i<(long)a.size(); i++) { temp = ((long long)a[i] * (long long)b[j]) + atol(carrier.data()) + product.at(i+j); out.str(""); carrier="0"; keep ="0"; if(temp >= 1E9) { out << temp; carrier = out.str(); keep = carrier.substr(carrier.size()-9, 9); carrier.erase(carrier.size()-9, 9); product.at(i+j) = (atol(keep.data())); temp=0; } else { product.at(i+j) = (long)temp; temp=0; } } } while (product.at(product.size()-1)==0) product.pop_back(); return product; } vector DivideTwo(vector a) { long math=0; long carrier=0; bool temp_sign; long temp_digit; stringstream out; out.str(""); string temp=""; string temp2=""; out << a.at(a.size()-1); temp = out.str(); out.str(""); for(long i=a.size()-2; i>=0;i--) { out << a[i]; temp2 = out.str(); for(int j=temp2.size(); j<9; j++) temp = temp+"0"; temp = temp+ out.str(); out.str(""); } out.str(""); for(long i=0; i<(long)temp.size(); i++) { math = (atol(temp.substr(i,1).data())+(carrier*10)) / 2; carrier = (atol(temp.substr(i,1).data())+(carrier*10)) %2; out< v_temp; v_temp = readInput(temp2, &temp_sign, &temp_digit); return v_temp; } vector Division(vector a, vector b, bool *a_sign, bool *b_sign, long *a_digit, long *b_digit) { unsigned long long temp = 0 ; vector v_temp; bool temp_sign = true; long temp_digit = 0; if(((*a_sign == true) && (*b_sign==true)) || ((*a_sign == false) && (*b_sign==false))) *a_sign = true; else *a_sign = false; stringstream out; string Display; stringstream fix_digit; string accum_digit; *a_digit = 0; fix_digit << a[a.size()-1]; accum_digit = fix_digit.str(); *a_digit = accum_digit.size(); fix_digit.str(""); for(long i=a.size()-2; i>=0; i--) { fix_digit<< a[i]; accum_digit =fix_digit.str(); for(long j = accum_digit.size(); j<9; j++) accum_digit = "0" + accum_digit; *a_digit =*a_digit + accum_digit.size(); fix_digit.str(""); } switch(CompareV(a, b)) // Compares the ABSOLUTE value of the two { case 2: { *a_sign = true; v_temp.push_back(0); return v_temp; break; } case 0: { v_temp.push_back(1); return v_temp; break; } case 1: { long n = *a_digit - *b_digit; string bound=""; vector v_upper; vector v_lower; vector v_mid; vector v_temp; bound = "1"; for(int j=1; j readInput(string stringA, bool *sign, long *digit) //Takes stringA, and turn it into a vector { vector v_temp; if(stringA[0] == '-') { *sign = false; stringA.erase(0,1); } else { *sign = true; } *digit = stringA.size(); for(long i=*digit; i>9; i=i-9) { v_temp.push_back (atol(stringA.substr(i-9, 9).data())); stringA.erase (i-9,9); } v_temp.push_back (atol(stringA.data())); stringA.clear(); return v_temp; } void DisplayValue(vector v_in, bool sign) //cout the values of the vector { stringstream out; string Display; if(sign==false) cout<<'-'; cout << v_in[v_in.size()-1]; for(long i=v_in.size()-2; i>=0; i--) { out<< v_in[i]; Display =out.str(); for(long j = Display.size(); j<9; j++) Display = "0" + Display; cout< accum(1,0); bool accum_sign = true; long accum_digit = 0; vector input(1,0); bool input_sign = true; char input_operation = 0 ; long input_digit = 0; cout<<"Value: "; DisplayValue(accum, accum_sign); while(input_string[0]!='E') { cout << "Enter: "; cin >> input_string; switch(input_string[0]) { case 'C': input_string.erase(0,1); if(checkValid(input_string)) accum = readInput("0", &accum_sign, &accum_digit); break; case 'E': if(checkValid(input_string)) exit(0); break; case '=': input_string.erase(0,1); if(checkValid(input_string)) accum = readInput(input_string, &accum_sign, &accum_digit); break; case '+': input_string.erase(0,1); if(checkValid(input_string)) { input = readInput(input_string, &input_sign, &input_digit); accum = Add(accum,input,&accum_sign, &input_sign); } break; case '-': input_string.erase(0,1); if(checkValid(input_string)) { input = readInput(input_string, &input_sign, &input_digit); accum = Subtract(accum,input,&accum_sign, &input_sign); } break; case '*': input_string.erase(0,1); if(checkValid(input_string)) { input = readInput(input_string, &input_sign, &input_digit); accum = Multiply(accum,input,&accum_sign, &input_sign); } break; case '/': input_string.erase(0,1); if(checkValid(input_string)) { input = readInput(input_string, &input_sign, &input_digit); accum = Division(accum, input, &accum_sign, &input_sign, &accum_digit, &input_digit); } break; default: cout<<"Invalid input"<