159.102 Computer Science Fundamentals Assignment 2
You have been asked to write a C program that acts as a simple calculator.
The program prompts the user to type in an expression.
The user then types in: number1 (in hexadecimal) an instruction (2 or 3 characters) number2 (in hexadecimal).
There will be a single space before and after the instruction. The input will be correct. No verification is necessary.
All input numbers will be within the range 0x00 to 0xffffffff.
Use the type unsigned int for your numbers.
Use the %x format specifier to input and print hexadecimal values.
The instruction can be one of the following:
add add number1 to number2
sub subtract number2 from number1
and bitwise and of the two numbers
or bitwise or of the two numbers
xor bitwise exclusive or of the two numbers
shl shift the bits of number1, number2 places to the left
shr shift the bits of number1, number2 places to the right
asr arithmetic shift right the bits of number1, number2 places to the right (see below)
rol rotate the bits of number1, number2 places to the left (see below)
ror rotate the bits of number1, number2 places to the right
A one-bit arithmetic shift right, shifts each bit in the number one place to the right.
If the most significant bit of the original number is a 1,the most significant bit of the answer is set to 1, otherwise it is set to 0.
A one-bit rotation left shifts all the bits one place to the left, the most significant bit of the original number becomes the least
significant bit of the answer.
A one-bit rotation right shifts all the bits one place to the right, the least significant bit of the original number becomes the
most significant bit of the answer.
Be careful using the C operators >> and <<. (m >> n) is 'undefined' if n is greater than 31 10 .
(i.e. the answer could be a random value!, but is more likely to be (m >> n&0x1f))
Your program must work with any value of n. (Shift by 1 in a loop n times).
Examples:
Type in an expression: 49 add 41
The answer is: 8a
Type in an expression: 49 sub 41
The answer is: 8
Type in an expression: 2a xor 7d
The answer is: 57
Type in an expression: 49 ror 10
The answer is: 490000
Type in an expression: 80000002 asr 1
The answer is: c0000001
Make sure that your program compiles and links using gcc. Submit your assign2.c electronically.
Marks will be awarded on how many test cases your program handles correctly.
Test your program with different numerical values and instructions before submission.
Get Free Quote!
319 Experts Online