/* Local teaching example: gcc -Wall -Wextra -pedantic calculator.c -o calculator */ #include #include int main(void) { double first, second, result; char operation; printf("Enter an expression, for example 12 + 8: "); if (scanf("%lf %c %lf", &first, &operation, &second) != 3 || !isfinite(first) || !isfinite(second)) { fprintf(stderr, "Enter two finite numbers and an operator.\n"); return 1; } switch (operation) { case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': if (second == 0.0) { fprintf(stderr, "Cannot divide by zero.\n"); return 1; } result = first / second; break; default: fprintf(stderr, "Supported operators: + - * /\n"); return 1; } if (!isfinite(result)) { fprintf(stderr, "Result is outside the supported range.\n"); return 1; } printf("Result: %.12g\n", result); return 0; }