Question: My Problem: I have had an issue with getting my code to subtract two expresions, stating invalid operands (too large) and (bad digits), and entering
My Problem:
I have had an issue with getting my code to subtract two expresions, stating invalid operands (too large) and (bad digits), and entering an expression of 0 % 0 to end the program.
I can't use int and don't know whether I should use double or float. The program compiles in int, but I am not able to invalidate a large operant of over 20 digits due to int. I am trying to make my program output similar to this example:
Output:
Enter an expression --> 1234 + 72 1234 + 72 = 1306 Enter an expression --> 1234 - 72 1234 - 72 = 1162 Enter an expression --> 123456789012345678901234 - 1 Invalid operand (too large). Enter an expression --> 123456789 123A5 Invalid operand (bad digit). Enter an expression --> 77777777777777777777 + 22222222222222222222 77777777777777777777 + 22222222222222222222 = 99999999999999999999 Enter an expression --> 99999999999999999999 + 1 Integer overflow. Enter an expression --> 0 % 0 Thanks for using my program. Good bye!
This is the code I have so far:
#include using namespace std;
const int Max_Digits = 20;
void Input_Big_Integer(int a[], int& Size_of_A); void Output_Big_Integer(int a[], int Size_of_A); void Output_Big_Integer2(int a[], int Size_of_A); void add(int a[], int& Size_of_A, int b[], int& Size_of_B, int sum[], int& Size_of_Sum); void subtract(int a[], int& Size_of_A, int b[], int& Size_of_B,int diff[], int& Size_of_Diff);
int main() { int a[Max_Digits], b[Max_Digits], sum[Max_Digits], diff[Max_Digits]; int Size_of_A, Size_of_B, Size_of_Sum, Size_of_Diff; char sign; char answer; for (int i=0; i { a[i] = 0; b[i] = 0; sum[i] = 0; } for (int i=Max_Digits-1;i>=0; i--) { a[i] = 0; b[i] = 0; diff[i] = 0; } do { Input_Big_Integer(a, Size_of_A); cout<<"Add a sign"< cin>>sign; Input_Big_Integer(b, Size_of_B); if (sign == '+' ) { add(a, Size_of_A, b, Size_of_B, sum, Size_of_Sum); cout<<" "; Output_Big_Integer(a, Size_of_A); cout<< " + "; Output_Big_Integer(b, Size_of_B); cout<<" = "; Output_Big_Integer(sum, Size_of_Sum); } if (sign == '-') { subtract(a, Size_of_A, b, Size_of_B, diff, Size_of_Diff); cout<<" "; Output_Big_Integer2(a, Size_of_A); cout<< " - "; Output_Big_Integer2(b, Size_of_B); cout<<" = "; Output_Big_Integer2(diff, Size_of_Diff); } cout< cout<<"Enter another Expression? (y or n): "< cin>>answer; } while (answer == 'y' || answer == 'Y'); return 0; }
void Input_Big_Integer(int a[], int& Size_of_A) { char digit[Max_Digits]; char change; int i=0; cout<<" Enter an expression: "< cin.get(change); if(change ==' ') { cin.get(change); } while (isdigit(change) && i { digit[i]=change; i++; cin.get(change); } Size_of_A = i; int j=0; while (i>0) { i--; a[j]=digit[i] - '0'; j++; } }
void Output_Big_Integer(int a[], int Size_of_A) { for(int i=0; i { cout< } } void Output_Big_Integer2(int a[], int Size_of_A) { for (int i=0; i { cout< } } void add(int a[], int& Size_of_A, int b[], int& Size_of_B, int sum[], int& Size_of_Sum) { char Max_Digits; int i; int carry=0; int maxsize=(Size_of_A>Size_of_B)?Size_of_A:Size_of_B; for(i=0; i { sum[i] = (a[i] + b[i] + carry) %10; carry = (a[i] + b[i] + carry) / 10; } if (maxsize==20 && carry>0) { cout<<"Integer overflow."< Size_of_Sum = (Size_of_A>Size_of_B)?Size_of_A:Size_of_B; } else if (maxsize>20) { cout<<"Invalid operand (too large)."< Size_of_Sum = (Size_of_A>Size_of_B)?Size_of_A:Size_of_B; } else if (!maxsize) { cout<<"Invalid operand (bad digit)."< Size_of_Sum = (Size_of_A>Size_of_B)?Size_of_A:Size_of_B; } else if (carry>0) { Size_of_Sum = (Size_of_A>Size_of_B)?Size_of_A:Size_of_B +1; } else { Size_of_Sum = (Size_of_A>Size_of_B)?Size_of_A:Size_of_B; } }
void subtract(int a[], int& Size_of_A, int b[], int& Size_of_B, int diff[], int& Size_of_Diff) { int i; int borrow=0; int maxsize=(Size_of_A for(i=maxsize-1;i>=0; i--) { diff[i] = (a[i] - b[i] - borrow); if (diff<0) { diff+=10; borrow = 1; } else { borrow = 0; } } if (maxsize==20 && borrow>0) { cout<<"Integer overflow."< Size_of_Diff = (Size_of_A } else if (maxsize>20 && borrow>0) { cout<<"Invalid operand (too large)."< Size_of_Diff = (Size_of_A } else if (!isdigit(Max_Digits)) { cout<<"Invalid operand (bad digit)."< Size_of_Diff = (Size_of_A } else if (borrow>0) { Size_of_Diff = (Size_of_A,Size_of_B)?Size_of_A:Size_of_B -1; } else { Size_of_Diff = (Size_of_A } }
Edit: The operations I am using for BigInt are addition and subtraction for large integers up to 20 digits in length
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
