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 from the standard input of your program as follows
<number of digits of the 1st operand> <1st operand> <operator +, -, *, /> <number of digits of the 2nd operand> <2nd operand>
The operands can also be negative numbers starting with - (minus sign).
-
The operands can also have more than 30 digits but less than 100 digits. Thus, more than built-in types.
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, round the result to the closest integer.
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 contain non-digit symbols, 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 or any other tool. No external library is allowed.
getchar() reads a single character from stdin
You can find other possibly useful functions in stdio.h library
Recall the variable-length array based on the user input from lectures.
You can find testing instances here . Files with the suffix .in contain your input with .out your desired output.
.in
.out
pub00.in
$ cat pub00.in | ./main
9 508983428 + 10 5303633367
5812616795
25 1109945105925303633367189 - 13 5303633367189
1109945105920000000000000
8 -53036333 * 55 -5089834282571709651070803999451059296045364409198455770
269946145925289289433504967492618198027807529912597563303491410
19 7170965107080399945 / 10 -4505913015
-1591456622
5 71709 a 10 -4505913015
Error: wrong operator
5 Hello - 10 -4505913015
Error: wrong operands
5 71709 - 5 world
-Wall -Werror -pedantic -std=c99 -O2
The maximum number of points is 10. There is no automatic evaluation; you can test and verify your solution using the provided instances or generate your own.