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
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: "<
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; i 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."<
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
