Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to perform addition and subtraction operations with large intege

ID: 3817869 • Letter: W

Question

Write a program to perform addition and subtraction operations with large integers up to 20 digits in length. Your program shall be able to input a positive integer, an operator (+ or -), and another positive integer (assume at least one space between the two items). It then outputs the sum or difference of the two numbers if the operation is legal. For subtraction, you can assume that the first operand is larger than or equal to the second operand unless extra credit option b is attempted. Your program will perform the calculation by implementing the usual paper-and-pencil addition or subtraction algorithm (adding or subtracting one digit at a time with carrying or borrowing as needed). The result is stored in an int array and it is then printed to the screen. Your program must allow the user to continue until the expression 0 % 0 is entered. The following is a sample run of the program and you must follow its user-interface.

[Some information/instructions including your name]

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!

Your program shall reject an invalid operator, an operand with invalid character, or an operand that has a length longer than maximum allowed digits. If the result is an integer with more than the maximum number of digits (i.e., more than 20 digits), then your program shall issue a message saying it has encountered "Integer overflow." You should be able to change the maximum length of the integers by changing only one globally defined named constant. It is probably easier to check out your program with shorter length integers first. Make sure to properly utilize functions in your program or points will be deducted. Here are some suggested functions, but feel free to add or remove as applicable to your design:

convert a string to an int array, add (two big integers), subtract, and output one big integer (do not output leading 0).

Hint: It is easiest to input each operand as a C++ string, but you can also read each digit as value of type char. Do not attempt to read a number as type int because it can be larger than an int can hold. After they are read into the program, the characters should be changed to values of type int and should be stored in an int array (convert a character like ‘1’ to integer value 1).

Extra credit: You can earn up to 4 additional points for implementing one the following features (do not have to implement all features). You can submit one version of your program including extra credit option, but you might want to start with a regular version and save a copy of that version in case there are problems with the extra credit. Clearly specify which extra credit feature in your write up.

a. Allow multiplication (i.e, 1234 * 72).

b. Allow division (i.e, 1234 / 72).

c. Allow positive or negative integers (i.e, -1234 + 72 or 72 - 1234).

Explanation / Answer

Here is the code for question. Addition and subtraction have been implemented. For subtraction, operand 1 should be larger than operand 2. Also remember to give one space between operands and operator. Otherwise program will not recognise operator correctly. For example you should give 1234 + 24 and not 1234+ 24 or 1234+24. The last 2 inputs will not work.

#include <iostream>
#define MAXLEN 20
using namespace std;

bool convert(string operand,int num[],int &size)
{
char ch;
for(int i=0; i<operand.length();i++)
{
ch=operand.at(i);
//check if the character is valid digit
if(ch>='0' && ch<='9')
{
num[i]=ch - '0'; //conver digit character to int value
}
else
return false;
}
size=operand.length();
return true;
}

int add(int num1[],int len1,int num2[],int len2,int ans[],int &len3)
{
int i=len1-1,j=len2-1,k;
int sum,carry,digit1,digit2;
len3=(len1>len2?len1:len2);
k=len3-1;

carry=0;
while(true)
{
if(i>=0)
{
digit1=num1[i];
i--;
}
else
digit1=0;

if(j>=0)
{
digit2=num2[j];
j--;
}
else
digit2=0;

sum=carry+digit1+digit2;


if(sum>9)
{
carry=1;
sum=sum-10;
}
else
carry=0;


ans[k]=sum;

k--;

if(i==-1 && j==-1)
break;
}
if(carry)
{
if(len3<MAXLEN)
{
len3++;
for(int i=len3-1;i>0;i--)
ans[i] = ans[i-1];

ans[0]=carry;
carry=0;
}
}
return carry;
}

//assuming value of 1st operand is larger than 2nd
bool sub(int number1[],int len1,int number2[],int len2,int ans[], int &len3)
{
int i=len1-1,j=len2-1,k;
int digit1,digit2,diff;
int num1[MAXLEN],num2[MAXLEN];

for(int i=0;i< len1;i++)
num1[i]=number1[i];

for(int i=0;i< len2;i++)
num2[i]=number2[i];

if(len2 > len1)
return false;
else
{
len3=len1;
k=len3-1;
while(true)
{
if(i==-1 && j==-1)
break;
if(i>=0)
{
digit1=num1[i];
i--;
}
else
digit1=0;

if(j>=0)
{
digit2=num2[j];
j--;
}
else
digit2=0;

if(digit1 < digit2)
{
digit1+=10; //borrow 10
num1[i]--;
//adjust all previous digits
for(int p=i;p>=0;p--)
{
if(num1[p]==-1)
{
num1[p]=9;
num1[p-1]--;
}
else
break;
}
}

diff=digit1-digit2;

ans[k]=diff;

k--;

}

}
return true;
}

void display(int num[],int len)
{
int i;
//skip over leading zeroes
for(i=0;i<len;i++)
if(num[i]!=0)
break;

//display after leading zeroes are skippped
if(i==len)
cout<<"0";
else
{
for(;i<len;i++)
cout<<num[i];
}
}
int main()
{
string op1, oper,op2;
string valid_operators="+-";
int num1[MAXLEN],num2[MAXLEN],ans[MAXLEN],carry,len1,len2,len3;
bool valid;
while(true)
{

valid=true;
cout<<"Enter an expression: ";
cin>>op1;
cin>>oper;
cin>>op2;

if(op1=="0" && oper=="%" && op2=="0")
break;

if(op1.length()>MAXLEN || op2.length()>MAXLEN) //check the length of operands;
{
cout<<"Operand length larger than "<<MAXLEN<<endl;
continue;
}

//check if operator is one of the valid operators
if(oper.length()==1 && valid_operators.find(oper)==-1)
{
cout<<"Invalid operator "<<oper<<endl;
continue;
}

//only if we are able to convert the operands into int array
if(convert(op1,num1,len1) && convert(op2,num2,len2))
{

if(oper=="+")
{
if(add(num1,len1,num2,len2,ans,len3) != 0)
{
cout<<"Integer overflow"<<endl;
valid=false;
}

}
else if(oper=="-")
{
if(!sub(num1,len1,num2,len2,ans,len3))
{
cout<<"Operand 1 should be larger than operand 2"<<endl;
valid=false;
}
}

if(valid)
{
display(num1,len1);
cout<<" "<<oper<<" ";
display(num2,len2);
cout<<" = ";
display(ans,len3);
cout<<endl;
}
}
else
cout<<"Invalid operand (bad digit)."<<endl;
}

cout<<"Thank you for using my program. Good bye !"<<endl;
return 0;
}

sample output

Enter an expression: 1234 + 72
1234 + 72 = 1306
Enter an expression: 1234 - 72
1234 - 72 = 1162
Enter an expression: 100 + 1
100 + 1 = 101
Enter an expression: 100 - 1
100 - 1 = 99
Enter an expression: 9 + 1
9 + 1 = 10
Enter an expression: 9 - 1
9 - 1 = 8
Enter an expression: 1 - 1
1 - 1 = 0
Enter an expression: 10 - 1
10 - 1 = 9
Enter an expression: 12a - 3
Invalid operand (bad digit).
Enter an expression: 123 / 3
Invalid operator /
Enter an expression: 123 < 3
Invalid operator <
Enter an expression: 12355345577466886874874 + 54554
Operand length larger than 20
Enter an expression: 99999999999999999999 + 1
Integer overflow
Enter an expression: 99999999999999999998 + 1
99999999999999999998 + 1 = 99999999999999999999
Enter an expression: 0 % 0
Thank you for using my program. Good bye !

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote