C programming C programming C programming Midterm (graded out of 100 points) Pro
ID: 3809483 • Letter: C
Question
C programming C programmingC programming
Midterm (graded out of 100 points) Problem 1: 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 x, k, I, &, A, E) Enter a 16 bit unsigned binary number The answer is: (note: the answer should be displayed in both binary and decimal) Enter operator x, I, &, n, 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 Eisentered, 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
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int bin_verify(char []);
int convertBinToDec(char []);
long decimalToBinary(int);
int main()
{
char bin1[33], bin2[33], result[33];
char ch;
float divResult;
int len1, len2, check,dec1,dec2,decResult;
long binResult;
//printf("Entered unsigned binary and their decimal numbers %s = %d and %s = %d: ",bin1,dec1,bin2,dec2);
printf("Enter binary numbers 1 : ");
scanf("%s", bin1);
printf("Enter binary numbers 2 : ");
scanf("%s", bin2);
check = bin_verify(bin1);
if (check==1)
{
printf("Invalid binary number %s. ", bin1);
exit(0);
}
check = bin_verify(bin2);
if (check==1)
{
printf("Invalid binary number %s. ", bin2);
exit(0);
}
dec1 = convertBinToDec(bin1);
dec2 = convertBinToDec(bin2);
while(1)
{
printf("Enter the operation from the operator(+, -, /, *, %,E) ");
scanf("%c", &ch);
switch(ch)
{
case '+':
decResult = (dec1+dec2);
binResult = decimalToBinary(decResult);
printf("%s + %s = %d (%l) ", bin1, bin2,decResult,binResult);
break;
case '-': decResult = dec1 - dec2;
binResult = decimalToBinary(decResult);
printf("%s - %s = %d (%l) ", bin1, bin2,decResult,binResult);
break;
case '*': decResult = dec1 * dec2;
binResult = decimalToBinary(decResult);
printf("%s * %s = %d (%l) ", bin1, bin2,decResult,binResult);
break;
case '/': divResult = dec1 / dec2;
binResult = decimalToBinary(decResult);
printf("%s / %s = %f (%l) ", bin1, bin2,divResult,binResult);
break;
case '%': decResult = dec1 % dec2;
binResult = decimalToBinary(decResult);
printf("%s % %s = %d (%l) ", bin1, bin2,decResult,binResult);
break;
case 'E':
exit(0);
break;
}
}
return 0;
}
long decimalToBinary(int n)
{
int remain;
long bin = 0, i = 1;
while(n != 0)
{
remain = n%2;
n = n/2;
bin= bin + (remain*i);
i = i*10;
}
return bin;
}
int convertBinToDec(char str[])
{
int sum = 0,s= strlen(str)-1;
for(int j=0; s >= 0; s--, ++j)
{
if(str[s] == '1')
{
sum = sum + pow(2,j);
}
}
return sum;
}
int bin_verify(char str[])
{
int i,len;
len = strlen(str);
//printf("String Length:%d ",strlen(str));
if(len != 16)
return 1;
else
{
for (i = 0; i < len; i++)
{
if ((str[i] - '0' != 1 ) && (str[i] - '0' != 0))
{
return 1;
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.