pvjc23/du4/program.c

78 lines
2.0 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
char line[300];
int count = 0;
while (fgets(line, sizeof(line), stdin)) {
count++;
char *start = line, *end = NULL;
int has_operator = 0;
float num1 = 0, num2 = 0, expected_result = 0;
char op, *next;
while (*start != '\n') {
if (*start == '+' || *start == '-' || *start == '*' || *start == '/') {
if (has_operator) {
printf("CHYBA\n");
goto next_line;
}
has_operator = 1;
op = *start;
} else if ((*start < '0' || *start > '9') && *start != ' ' && *start != '=' && *start != '.') {
goto next_line;
}
start++;
}
if (!has_operator) {
printf("CHYBA\n");
goto next_line;
}
num1 = strtof(line, &next);
while (*next == ' ') next++;
if (*next != op) {
printf("KONIEC\n");
goto next_line;
}
next++;
num2 = strtof(next, &next);
while (*next == ' ') next++;
if (*next == '=') {
next++;
expected_result = strtof(next, &next);
while (*next == ' ') next++;
} else {
goto next_line;
}
float result;
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
result = roundf(result * 100) / 100;
if (fabs(result - expected_result) < 0.001) {
printf("OK\n");
} else {
printf("ZLE\n");
}
next_line:
continue;
}
return EXIT_SUCCESS;
}