Question

Make a program in C that takes in four parameters,
1) string of +,- or * for addition,subtraction, or multiplication
2) 32 bit two's complement integers where a 'b' in front of it means binary, 'o' means octal 'd' means decimal, and 'x' means hexadecimal
3) another 32 bit two's complement number with the same base as the first number, so if first number was a binary the second would also be a binary
4) the type of base you want the output to be in, could be binary, hexadecimal, octal, or decimal
Example runs on terminal

un 3 out Put o EXAMPLE t diiliIH M d1122つつ22AL Nun り1110


Can't use printf.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<math.h>

int decimalToBinary(int n)

{

int num = 0;

for (int i = 31; i >= 0; i--) {

int k = n >> i;

if (k & 1)

num += 1 * pow(10, i);

else

num += 0 * pow(10, i);

}

return num;

}

int binaryToDecimal(int n)

{

int num = n;

int dec_value = 0;

// Initializing base value to 1, i.e 2^0

int base = 1;

int temp = num;

while (temp)

{

int last_digit = temp % 10;

temp = temp / 10;

dec_value += last_digit * base;

base = base * 2;

}

return dec_value;

}

int decimalToOctal(int n)

{

// array to store octal number

int octalNum[100];

int result=0;

// counter for octal number array

int i = 0;

while (n != 0) {

// storing remainder in octal array

octalNum[i] = n % 8;

n = n / 8;

i++;

}

// printing octal number array in reverse order

for (int j = i - 1; j >= 0; j--)

result += octalNum[j] * pow(10, i);

return result;

}

int octalToDecimal(int n)

{

int num = n;

int dec_value = 0;

// Initializing base value to 1

int base = 1;

int temp = num;

while (temp) {

// Extracting last digit

int last_digit = temp % 10;

temp = temp / 10;

dec_value += last_digit * base;

base = base * 8;

}

return dec_value;

}

int hexadecimalToDecimal(int num)

{

int remainder;

int result = 0;

for (int count = 0; num > 0; count++)

{

remainder = num % 10;

num = result + remainder * pow(16, count);

result = num / 10;

}

return result;

}

int decimalToHexadecimal(int num)

{

int remainder;

int result = 0;

for (int count = 0; num > 0; count++)

{

remainder = num % 16;

result = result + remainder * pow(10, count);

num = num / 16;

}

return result;

}

void iprint(int n)

{

if (n > 9)

{

int a = n / 10;

n -= 10 * a;

iprint(a);

}

putchar('0' + n);

}

int main()

{

char operatorType, firstNumType, secNumType, outputType;

int firstNum, secNum, result;

scanf("%c %c %d %c %d %c", &operatorType, &firstNumType, &firstNum, &secNumType, &secNum, &outputType);

int secNumInDecimal=0, firstNumInDecimal=0;

switch (firstNumType)

{

case 'b':

firstNumInDecimal = binaryToDecimal(firstNum);

break;

case 'd':

firstNumInDecimal = firstNum;

break;

case 'o':

firstNumInDecimal = octalToDecimal(firstNum);

break;

case 'x':

firstNumInDecimal = hexadecimalToDecimal(firstNum);

break;

default:

break;

}

switch (secNumType)

{

case 'b':

secNumInDecimal = binaryToDecimal(secNum);

break;

case 'd':

secNumInDecimal = secNum;

break;

case 'o':

secNumInDecimal = octalToDecimal(secNum);

break;

case 'x':

secNumInDecimal = hexadecimalToDecimal(secNum);

break;

default:

break;

}

//do calculation

switch (operatorType)

{

case '+':

result = firstNumInDecimal + secNumInDecimal;

break;

case '-':

result = firstNumInDecimal - secNumInDecimal;

break;

case '*':

result = firstNumInDecimal * secNumInDecimal;

break;

default:

break;

}

switch (outputType)

{

case 'b':

result = decimalToBinary(result);

break;

case 'd':

result = result;

break;

case 'o':

result = decimalToOctal(result);

break;

case 'x':

result = decimalToHexadecimal(result);

break;

default:

break;

}

iprint(result);

getchar();

getchar();

return 0;

}

//output:

Source1.cpp x CheggSep 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 (Global Scope) main0 break; case; result -firstNumInDecimal secNiumTnnerimal. break; İ CAUsers\surya\Documents\Visual Studio 201 7.projects\CheggSep\DebugChegg Sep.exe default: break; switch (outputType) case b: result decimalToBinary(result) break; case d: result -result; break; case 0 resultdecimalTooctal(result); break case X result decimalToHexadecimal(re break default: break; iprint (result); getchar(); getcharO: return e;

Add a comment
Know the answer?
Add Answer to:
Make a program in C that takes in four parameters, 1) string of +,- or *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 1.25 Convert the following decimal numbers to unsigned binary numbers Exercise 1.31 Repeat Exercise 1.29,...

    Exercise 1.25 Convert the following decimal numbers to unsigned binary numbers Exercise 1.31 Repeat Exercise 1.29, but convert to 8-bit sign/magnitude numbers KExercise 1.32 Repeat Exercise 1.30, but convert to 8-bit sign/magnitude numbers (a) 4210 (b) 6310 Exercise 1.33 Convert the following 4-bit two's complement numbers to 8-bit two's complement numbers. (c) 22910 (d) 84510 (a) 0101 b) 1010 XExercise 1.26 Convert the following decimal numbers to unsigned binary numbers. Exercise 1.34 Convert the following 4-bit two's complement numbers to...

  • CS 3503-06 Homework 1 Due: 11:59pm, Friday, Jan. 24. Please show the details of your work....

    CS 3503-06 Homework 1 Due: 11:59pm, Friday, Jan. 24. Please show the details of your work. Please submit in D2L using the associated link. Problems (total: 100 points) Representation of signed numbers (5 x 4 = 20 points) In an 8-bit system, find out the binary representation for the following numbers using sign-and-magnitude, ones’ complement, and two’s complement, respectively: 55 -47 In a 4-bit system, find out the binary representation for the following numbers using sign-and-magnitude, ones’ complement, and two’s...

  • question 1 part 2 and 3 thank you (47) Naruto Notone C Sign In er Sign...

    question 1 part 2 and 3 thank you (47) Naruto Notone C Sign In er Sign Up | Ch ® UFC & MMA × Secure I https://piazza-resourcess3.amazonaws.com/jgopch0cb93d8/j .pdfAWSAccessKeyld-AKAILDNRL/4ALKBWOHA8lexpires-15200435/2&Signature-ol9aXG9 /UAKIHS0QUwMeyBX.. ☆ ミ quations must be properly tyne-set including superscript-s expunents, Always watch the course websile for updates on the assignments. Question 1 (4 points) Show you work I. Convert 2727 into a 32-bit two's complement binary number 2. Convert -5795 into a 16-bit two's complement binary number 3. Add the above...

  • 1. Convert the binary number 10101102 to octal, decimal, and hexadecimal numbers. 2. Convert the decimal...

    1. Convert the binary number 10101102 to octal, decimal, and hexadecimal numbers. 2. Convert the decimal number 236.7510 to binary,octal, and hexadecimal numbers. 3. Add the following two binary numbers: 100111102 and 011110112. Remember to show any carries that are generated along the way. 4. Repeat the previous question, but this time subtract the second binary number from the first. Remember to show any borrows that are required along the way. 5. Determine the encoding of the decimal number 28610...

  • 5. Write a Complete C++ program which can produce the same result as shown below, i.e....

    5. Write a Complete C++ program which can produce the same result as shown below, i.e. convert numbers procedurally from decimal to binary, octal and hexadecimal. Microsoft Office 365 Excel supports number conversion from one base to another, for example from Decimal to Binary, Octal, Hexadecimal etc. One such example screen shot is attached here: Binar Deci mal Hexadeci Octal mal y 010 0 0 11 2 10 2 3/11 3 4 100 4. 4 5101 5 5 6110 6...

  • Question 1.1. (TCO 1) Which number system has a radix of two? (Points : 4) Hexadecimal...

    Question 1.1. (TCO 1) Which number system has a radix of two? (Points : 4) Hexadecimal Binary Decimal Octal Question 2.2. (TCO 1) Convert 24 base 10 to hexadecimal. (Points : 4) 1A 18 20 30 Question 3.3. (TCO 1) If FF h is converted to decimal, the result is _____. (Points : 4) 100 200 255 256 Question 4.4. (TCO 1) Convert decimal 103 to an 8-bit binary number. (Points : 4) 1110 0100 0100 0000 0110 0111 0110...

  • (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the...

    (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the left of the decimal point and 3 to the right. a.      What is the range of the expressible numbers?    b.      What is the precision?    c.       What is the error?    ______________________________________________________________________________   (3 pts) Convert this unsigned base 2 number, 1001 10112, to each base given below   (Note: the space in the binary string is purely for visual convenience) Show your work. Using...

  • and then print their average. Write a C program that accepts a string of text from...

    and then print their average. Write a C program that accepts a string of text from the user and prints back the string without any of the vowels. For example, if the user entered "Hello, world", the program should print "Hll, wrld" Write a C program that accepts an integer in decimal from the user and prints the number of 1' bits in the integers binary representation. For example, if the user entered 14, the program should print 3 because...

  • QUESTION 1 Convert 1 0110 1010 from binary to decimal, assuming nine-bit two's complement binary representation....

    QUESTION 1 Convert 1 0110 1010 from binary to decimal, assuming nine-bit two's complement binary representation. Provide only the number. For a negative number provide the negative sign. Example 79 NOT +79 or 78 Decimal Example -79 NOT negative 79 or -79 Decimal QUESTION 2 Count the next 10 numbers in base 5 starting after 3434. (Don't include 3434) Provide you answer with only numbers and spaces. No extra spaces or words Example  410 411 412   NOT 410, 411, 412

  • Please show that it works and show the output. Description Develop a program (in C++) that...

    Please show that it works and show the output. Description Develop a program (in C++) that first asks the user to input a signed number, for example 48, then converts it to binary number by using Two's Complement regulations. Do the same for the second inputted number, eg. 17. Then compute the summation of these two numbers, output the results in binary format first, then if there is an overflow, display error message; otherwise, convert the result to decimal value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT