Implement a Ripple-Carry Adder using a C++ class. The constructor of your adder
ID: 3650067 • Letter: I
Question
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!".
Submit AT LEAST the following files:
Your main file controling the flow of the program
The prototype for your adder, e.g. adder.h
The implementation for your adder, e.g adder.cpp
Example of program execution:
g++ *.cpp
./a.out
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
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
so far i did this
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
void BinaryTest(char *str)
{
char num[5];
memset (num,'0',4);
strcpy (&num[4 - strlen(str)],str);
strcpy (str,num); }
// check validity
int CheckValidity(char *str)
{
int length;
int ctr;
length = strlen(str);
for (ctr = 0; ctr < length; ctr++)
if ((str[ctr] != '0') && (str[ctr] != '1'))
{ cout << "Please enter only binary 0s and 1s!" << endl;
return 0; }
return 1; }
int main(void)
{ char a[5] = {0};
char b[5] = {0};
char c[5] = {0};
int count,tap,carry,sum;
do
{ cout << "Enter first binary number: ";
cin >> "%4s", a; }
while(! CheckValidity(a));
BinaryTest(a);
do
{
cout << "Enter second binary number: ";
cin >> "%4s", b; }
while(! CheckValidity(b));
BinaryTest(b);
carry = 0;
for(count = 3 ; count> -1; count-- )
{ sum = (a[count] - '0') + (b[count] - '0') + carry;
c[count] = (sum % 2) + '0';
carry = sum / 2; }
tap = 0;
for (count = 0; count < 4; count++)
{ if (c[count] != '0')
tap = 1;
if (tap)
cout << "%c", c[count]; }
cout <<" ";
return 0;
}
Explanation / Answer
C++ differ from C with the input and output functions. Unlike scanf/printf, for cout and cin, you do not need to specify what datatype you are input/outputing. e.g. cout >b;Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.