Question: My Problem: I am adding, subtracting, and multiplying two big integers together in C++. I get it to subtract and multiply two integers, but when

My Problem: I am adding, subtracting, and multiplying two big integers together in C++. I get it to subtract and multiply two integers, but when I do, I get either a wrong number in subtraction or I am missing a number in multiplication.

Also, when I try to get the number to be larger than 20, it ends up skipping the input for the sign and the second expression. I have tried to use long and have tried double, but I end up getting the same issue.

The last issue that I have is when I enter an expression with a letter, it ends up adding the numbers up to the letter and then asking for a sign an expression to add the number after the letter entered. Output:

Here is my code: #include #include using namespace std;

const int Max_Digits = 20;

void Input_Big_Integer(double a[], double& Size_A, double b[], double& Size_B); void Output_Big_Integer(double a[], double Size_A); void Output_Big_Integer2(double b[], double Size_B); void add(double a[], double& Size_A, double b[], double& Size_B, double sum[], double& Size_Sum); void subtract(double a[], double& Size_A, double b[], double& Size_B, double diff[], double& Size_Diff); void multiply(double a[], double& Size_A, double b[], double& Size_B, double prod[], double& Size_Prod);

void Input_Big_Integer(double a[], double& Size_A) { char digit[Max_Digits]; char change; int i=0; cout<<"Enter an expression: "<0) { i--; a[j]=digit[i] - '0'; j++; } }

void Output_Big_Integer(double a[], double Size_A) { for(int i=0; i

void add(double a[], double& Size_A, double b[], double& Size_B, double sum[], double& Size_Sum) { double carry=0; double maxsize=(Size_A>Size_B)?Size_A:Size_B; for(int i=0; i0) { cout<<"Integer overflow."<20) { cout<<"Invalid operand (too large)."<0) { Size_Sum = (Size_A>Size_B)?Size_A:Size_B +1; { Output_Big_Integer(a, Size_A); cout<< " + "; Output_Big_Integer2(b, Size_B); cout<<" = "; Output_Big_Integer(sum, Size_Sum); } } else { Size_Sum = (Size_A>Size_B)?Size_A:Size_B; { Output_Big_Integer(a, Size_A); cout<< " + "; Output_Big_Integer2(b, Size_B); cout<<" = "; Output_Big_Integer(sum, Size_Sum); } } } void subtract(double a[], double& Size_A, double b[], double& Size_B, double diff[], double& Size_Diff) { double borrow=0; double maxsize=(Size_A>Size_B)?Size_A:Size_B; for(int i=maxsize;i>=0; i--) { if(borrow==0) { if (a[i]>=b[i]) { diff[i] = a[i] - b[i]; } else { borrow = 1; diff[i] = (a[i]+10) - b[i]; } } else { borrow = 0; if((a[i]-1)>=b[i]) { diff[i] = (a[i]-1) - b[i]; } } } if (maxsize==20 && borrow>0) { cout<<"Integer overflow."<20 && borrow>0) { cout<<"Invalid operand (too large)."<0) { Size_Diff = (Size_A>Size_B)?Size_A:Size_B +1; { Output_Big_Integer(a, Size_A); cout<< " - "; Output_Big_Integer2(b, Size_B); cout<<" = "; Output_Big_Integer(diff, Size_Diff); } } else { Size_Diff = (Size_A>Size_B)?Size_A:Size_B ; { Output_Big_Integer(a, Size_A); cout<< " - "; Output_Big_Integer2(b, Size_B); cout<<" = "; Output_Big_Integer(diff, Size_Diff); } } }

void multiply(double a[], double& Size_A, double b[], double& Size_B, double prod[], double& Size_Prod) { long tmp; long maxsize=(Size_A>Size_B)?Size_A:Size_B; for(int i=0;i < maxsize;i++) { for(int j=0;j < maxsize;j++) { prod[i+j] += b[i]*a[j]; } } for(int i=0;i < maxsize;i++) { int tmp = prod[i]/10; prod[i] = (int)prod[i]%10; prod[i+1] = prod[i+1] + tmp; } for(int i=maxsize; i>= 0;i--) { if(prod[i] > 0) break; } if (maxsize==20 && tmp>0) { cout<<"Integer overflow."<20) { cout<<"Invalid operand (too large)."<0) { Size_Prod = (Size_A>Size_B)?Size_A:Size_B +1; { Output_Big_Integer(a, Size_A); cout<< " * "; Output_Big_Integer2(b, Size_B); cout<<" = "; Output_Big_Integer(prod, Size_Prod); } } else { Size_Prod = (Size_A>Size_B)?Size_A:Size_B; { Output_Big_Integer(a, Size_A); cout<< " * "; Output_Big_Integer2(b, Size_B); cout<<" = "; Output_Big_Integer(prod, Size_Prod); } } } int main() { double a[Max_Digits], b[Max_Digits], sum[Max_Digits], diff[Max_Digits], prod[Max_Digits]; double Size_A, Size_B, Size_Sum, Size_Diff, Size_Prod; char sign; for (int i=0; i=0; i--) { a[i] = 0; b[i] = 0; diff[i] = 0; } Input_Big_Integer(a, Size_A); cout<<"Enter a sign"<>sign; Input_Big_Integer (b, Size_B); while (Size_A!=0 && sign!='%' && Size_B!=0) { if (sign == '+' ) { add(a, Size_A, b, Size_B, sum, Size_Sum); } if (sign == '-') { subtract(a, Size_A, b, Size_B, diff, Size_Diff); } if (sign == '*') { multiply(a, Size_A, b, Size_B, prod, Size_Prod); } cout<>sign; Input_Big_Integer (b, Size_B); } cout<<"Thanks for using my program. Good bye!"<

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!