Hi. I am working on a stubs only miniature program for school concerning adding integers much too large to be stored as integers. What I have so far seems to work great up until I add 10 integers to 10 integers. Here is what I have so far, and kindly thank anyone who has time to give me a bit of advice. I apologize for the lack of documentation in this program - I haven't really gotten that far yet.
Code:const int MAXCHARS = 80; char numbers[MAXCHARS]; int result[MAXCHARS]; char num1[MAXCHARS]; char num2[MAXCHARS]; char op; int splitPoint; int opFinder = 0; int count1 = 0; int count2 = 0; int x; int ar1Length; int ar2Length; int maxLength; int temp1; int temp2; int tempResult; int carry1 = 0; int carry2; int main() { cout << "This program adds or multiplies numbers" << endl << "much too large to be stored as integers." << endl << "Please refer to the example to enter your problem." << endl << endl << "(EXAMPLE) 100 + 10" << endl << endl << "Calculate: "; cin.getline(numbers, MAXCHARS, '\n'); for(opFinder = 0; opFinder < MAXCHARS; opFinder++) { if((numbers[opFinder] == '+') || (numbers[opFinder] == '*')) { splitPoint = opFinder; op = numbers[opFinder]; } } while(isdigit(numbers[count1])) { num1[count1] = numbers[count1]; numbers[count1] = '\0'; num1[count1+1] = '\0'; cout << num1[count1] << endl; count1++; } ar1Length = count1-1; num1[count1] = '\0'; count1 = 0; count2 = splitPoint + 1; while(numbers[count2] != '\0') { if(numbers[count2] == ' ') { count2++; } num2[count1] = numbers[count2]; numbers[count2] = '\0'; num2[count1 + 1] = '\0'; count1++; count2++; } ar2Length = count1-1; num2[count1] = '\0'; if(ar1Length > ar2Length) {maxLength = ar1Length;} else {maxLength = ar2Length;} cout << "You have entered: " << endl << setw(maxLength+2) << setfill(' ') << num1 << endl << op << setw(maxLength+1) << setfill(' ') << num2 << endl; for(x = 0; x <= maxLength+2; x++) {cout << "-";} cout << endl; x = 0; while((num1[x] != '\0') || (num2[x] != '\0')) { if((num1[x] == '\0') && (num2[x] == '\0')) {break;} else if(num1[x] == '\0') {temp1 = 0;} else if(num2[x] == '\0') {temp2 = 0;} else { temp1 = atoi(&num1[x]); temp2 = atoi(&num2[x]); } if((temp1+temp2) > 9) { tempResult = (temp1 + temp2) % 10; carry1 = (temp1 + temp2) / 10; } tempResult = temp1 + temp2 + carry2; result[x] = tempResult; num1[x] = '\0'; num2[x] = '\0'; result[x+1] = '\0'; cout << result[x] << endl; temp1 = 0; temp2 = 0; carry2 = carry1; } return 0; }



LinkBack URL
About LinkBacks


