Warning
This page is located in archive.

HW 02 - Calculator

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
abc
Error: unknown error
1

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

Late submission

  • Submission is allowed by Sunday (29.10.2023) after the deadline with the following penalties:
    • max pts 7.5
    • Bonus will be only for half points (5 → 2.5 points)
  • Submission is allowed one week after the deadline (25.10.2023) with the following penalties:
    • 50% point reduction (10 → 5 points)
    • Bonus will be only for half points (5 → 2.5 points)
courses/be5b99cpl/hw/hw02.txt · Last modified: 2023/10/25 11:07 by ulricji1