Solved: Write a program to perform addition and subtractio…




Solved: Write a program to perform addition and subtractio…

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.

  1. Allow multiplication (i.e, 1234 * 72).
  2. Allow division (i.e, 1234 / 72).

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


error: Content is protected !!