(PROGRAM THIS IN C, NOT C++) Write a program which can accept binary numbers and
ID: 3808148 • Letter: #
Question
(PROGRAM THIS IN C, NOT C++)
Write a program which can accept binary numbers and certain operations to calculate the results of those operations. The user should see something like:
Enter a 16 bit unsigned binary number:
Enter operator (+, -, *, %, /, <<, >>, |, &, ^, E)
Enter a 16 bit unsigned binary number:
The answer is: (note: the answer should be displayed in both binary and decimal)
Enter operator (+, -, *, %, /, <<, >>, |, &, ^, E)
Enter a 16 bit unsigned binary number:
The answer is: (note: this should perform the operation on the previous answer and the newly entered number)
The program should continue in this manner, until E is entered as the operator. When E is entered, the program should terminate.
Hints: partition up the problem. Create subroutines to:
• Accept input from the user and verify that they have entered a 16 bit binary number (note: you’ll need to handle binary numbers as strings)
• Convert a string holding a binary number to an unsigned integer
• Convert an unsigned integer to a string holding a binary representation of that number
• Accept two numbers and an operator, perform the noted operation (hint: only capture the first < from << or > from >>, doing so will allow you to handle the operator as a simple character)
Hint #2: use fgets and sscanf to take user input. scanf alone will be tough to get correct.
Explanation / Answer
include #include #include #include #include #include using namespace std; int main() { unsigned short b = BOOST_BINARY( 10010 ); char buf[sizeof(b)*8+1]; printf("hex: %04x, dec: %u, oct: %06o, bin: %16s ", b, b, b, itoa(b, buf, 2)); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.