HW 04 - Calculator : large numbers

New deadline 10.11.

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.

fgets() function allows flexible reading of stdin

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.

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

Examples

pub00

Standard input Expected output Return value
9
508983428
+
10
5303633367
5812616795
0

pub03

Standard input Expected output Return value
25
1109945105925303633367189
-
13
5303633367189
1109945105920000000000000
0

pub06

Standard input Expected output Return value
8
-53036333
*
55
-5089834282571709651070803999451059296045364409198455770
269946145925289289433504967492618198027807529912597563303491410
0

pub07

Standard input Expected output Return value
19
7170965107080399945
/
10
-4505913015
-1591456622
0

Faulty instances

Standard input Expected output Return value
5
71709
a
10
-4505913015
Error: wrong operator
2
5
Hello
-
10
-4505913015
Error: wrong operands
3
5
71709
-
5
world
Error: wrong operands
3

Evaluation

Upload your solution into BRUTE as a zip archive containing only your program's source files and either a Makefile or readme explaining how to compile it.
  1. Evaluation draft: 2nd-3rd November.
  2. Final evaluation: after the new deadline 10.11.
Your submission must compile without errors with the following compiler flags

-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.

  • 2 points - all operations with only small numbers using built-in types
  • 3 points - addition and subtraction of large numbers
  • 3 points - multiplication of large numbers
  • 2 points - coding style (structured, commented, separated into functions, meaningful naming scheme, no memory leaks etc.)

Bonus

  • 5 points - division of large numbers (round to the closest integer)
courses/be5b99cpl/hw/hw04.txt · Last modified: 2024/10/30 09:45 by ulricji1