HW 02 - Calculator : small numbers

The task is to write a simple program that performs the following basic integer arithmetic operations: addition, subtraction, multiplication and division.

You will get the operands and operator from the standard input of your program as follows

<1st operand>
<operator +, -, *, />
<2nd operand>

You will also be always and correctly provided with an output format specifier as a command line argument. It will be a single character specifying how your program should print the result of the arithmetic operation.

  1. d decimal
  2. x hexadecimal with capital letters for digits and 0x prepended
  3. o octal with 0 prepended

The operands can also be negative numbers starting with - (minus sign). The operands and the results of performed operations will fit in the standard integer types. The operands can be in three different representations. Both operands are always expressed in the same representation.

  1. decimal: digits 0,1,2,3,4,5,6,7,8,9
  2. hexadecimal: always starts with 0x, digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
  3. octal: always starts with 0, digits 0,1,2,3,4,5,6,7

For example: 13 (decimal) = 0xD (hexadecimal) = 015 (octal)

Your program is expected to print the result of the desired arithmetic operation on the standard output, and the return value shall be 0. In the case of a division operation, cut off the digits after the decimal point.

However, there might be situations when an unsupported operation (meaning not one of the listed above) is entered. In such situations, the program prints Error: wrong operator and returns 2. Also, any of the operands on the input might not be a valid number and those situations should result in printing Error: wrong operands and returning 3. In any other erroneous situation, print Error: unknown error and return 1.

You can check your solution using Python, WolframAlpha, a calculator or any other tool. No external library is allowed.

scanf() function allows flexible reading of stdin

You can find other possibly useful functions in stdio.h library

You can find testing instances here . Files with the suffix .in contain your input. Files with the suffix .out.d contain the expected decimal output, out.x hexadecimal and out.o octal.

When you have a file pub00.in containing the program input, you can pass it to your program in like below for hexadecimal output
$ cat pub00.in | ./main x

Examples

Standard input Expected output Return value CMD argument
508983428
+
5303633367
5812616795
0
d
0x276EE45596AF2C
-
0x4D2D92ED095
0x276A117C67DE97
0
x
-0312242455
*
-0143322
047220326706352
0
o
717096510708039
/
-4505913015
-0x26DA9
0
x

Faulty instances

Standard input Expected output Return value
71709
a
-4505913015
Error: wrong operator
2
Hello
-
-4505913015
Error: wrong operands
3
71709
+
world
Error: wrong operands
3
abc
Error: unknown error
1

Evaluation

Upload your solution into BRUTE as a zip archive containing only the file main.c.
  1. Evaluation draft: 19-20th October.
  2. Final evaluation: after the deadline.

The maximum number of points is 5. There is no automatic evaluation; you can test and verify your solution using the provided instances or generate your own.

  • 1.5 points - decimal representation
  • 2.5 points - hexadecimal and octal representation
  • 1 point - coding style (structured, commented with doxygen or similar, separated into functions, meaningful naming scheme, no memory leaks etc.)
courses/be5b99cpl/hw/hw02.txt · Last modified: 2024/10/10 15:57 by ulricji1