Add to 13
Implement a calculator that supports four basic operations:
add(a, b) // Returns a + b
subtract(a, b) // Returns a - b
multiply(a, b) // Returns a * b
divide(a, b) // Returns a / b (floating-point division, not integer division)
Important: All functions must handle both integers and floating-point numbers. If the result is a whole number, output it as an integer (e.g., 4 not 4.0). Otherwise, output the decimal value (e.g., 2.5).
Floating-point precision: Due to binary representation limitations, some decimal numbers cannot be stored exactly (e.g., 0.1, 5.1). When the mathematical result should be a clean decimal but floating-point arithmetic introduces small errors (e.g., 1.0999999999999996 instead of 1.1), round to a reasonable precision (10-12 decimal places) before outputting. If the rounded result is a whole number, output it as an integer.
Input Format
- First line: integer
Q(number of queries) - Next
Qlines:a [operator] bwhereaandbare numbers and[operator]is+,-,*, or/
Output Format
Output Q numbers, one per line, representing the result of each query. Omit .0 from whole number results.
Constraints
- The result will fit in a 32-bit signed integer
- All inputs will fit within 5 digits of precision.
Examples
4
6 + 7
6 + 7.2
5 - 4
5.1 - 413
13.2
1
1.12
4.5 * 2
2.5 * 2.59
6.252
5 / 2
4 / 22.5
24
6 + 7
6 + 7.2
5 - 4
5.1 - 413
13.2
1
1.1