Search
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.
d
x
0x
o
0
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.
-
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.
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.
.in
.out.d
out.x
out.o
pub00.in
$ cat pub00.in | ./main x
508983428 + 5303633367
5812616795
0x276EE45596AF2C - 0x4D2D92ED095
0x276A117C67DE97
-0312242455 * -0143322
047220326706352
717096510708039 / -4505913015
-0x26DA9
71709 a -4505913015
Error: wrong operator
2
Hello - -4505913015
Error: wrong operands
3
71709 + world
abc
Error: unknown error
1
main.c
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.