Implement a Ripple-Carry Adder using a C++ class. The constructor of your adder must take an integer parameter, indicating of the maximum length (i.e. number of bits) that are supported. Keep all class members that are not accessed "from the outside" private. You must have at least 1 private class member. Implement an add-function in the adder class that takes 2 arguments containing the numbers that are to be added. The type of these arguments may vary depending on your implementation. In your main class create a single instance of your adder that you will use to calculate the sum of 2 numbers. Use 5 as the parameter for maximum length.

Once the program is started, it will print out the promt "ripple_carry> " (> is followed by a whitespace):

./a.out

ripple_carry>

You will implement the commands "add" and "quit":

Add

Add takes 2 arguments, which represent the numbers that must be added. The result will be on a new line without any leading zeros. Then repeat the prompt.

ripple_carry> add 101 11
1000
ripple_carry> add 001 100
101
ripple_carry> add 01 00111
1000
ripple_carry>

Quit

Exit the program

ripple_carry> quit

Error Handling

  • If the command received from the user input is not supported, print out an error message starting with "Error!".
  • If one of the numbers received from the user input is negative, print out an error message starting with "Error!".
  • If one of the numbers received from the user input exceeds the maximum allowed length, print out an error message starting with "Error!".
  • If the result overflows the maximum allowed length, print out an error message starting with "Error!".
  • If a number is not a binary number, print out an error message starting with "Error!".

Example of program execution:

ripple_carry> add 11 1110
10001
ripple_carry> add 001 1010
1011
ripple_carry> add 1 1
10
ripple_carry> subtract
Error! Not a valid command.
ripple_carry> hi
Error! Not a valid command.
ripple_carry> add -101 1110
Error! Number is negative.
ripple_carry> add 1011 -1
Error! Number is negative.
ripple_carry> add -01 1
Error! Number is negative.
ripple_carry> add 1010101 01
Error! Number exceeds 5 bits.
ripple_carry> add 1 11111111
Error! Number exceeds 5 bits.
ripple_carry> add 11111 1
Error! Overflow!
ripple_carry> add 10101 10101
Error! Overflow!
ripple_carry> add 101 12
Error! Number is not binary.
ripple_carry> add 200 101
Error! Number is not binary.
ripple_carry> quit
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.