pvjc24/a1/program.c
2024-03-27 14:57:38 +01:00

78 lines
2.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main() {
char line[100];
double num1, num2, result, expected_result;
char op;
while (fgets(line, sizeof(line), stdin) != NULL) {
if (line[0] == '\n') {
// Stop when the user enters a blank line
break;
}
num1 = num2 = result = 0;
op = '\0';
// Remove any spaces between symbols
for (int i = 0; line[i] != '\0'; i++) {
if (isspace(line[i])) {
for (int j = i; line[j] != '\0'; j++) {
line[j] = line[j + 1];
}
i--; // Move back one step to recheck the current character
}
}
// Parse the line
double temp_expected_result;
sscanf(line, "%lf%c%lf=%lf", &num1, &op, &num2, &temp_expected_result);
// Round the expected result to two decimal places
expected_result = round(temp_expected_result * 100) / 100.0;
// Check if the input is valid
if (op != '+' && op != '-' && op != '*' && op != '/') {
printf("CHYBA\n");
continue;
}
// Check for division by zero
if (op == '/' && num2 == 0) {
printf("ZLE\n");
continue;
}
// Perform the calculation
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
// Check if the result is correct
if (result == expected_result) {
printf("OK\n");
} else {
printf("%lf", expected_result);
printf("%lf", temp_expected_result);
printf("ZLE\n");
}
}
return 0;
}