81 lines
1.7 KiB
C
81 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include <ctype.h>
|
|
|
|
int main() {
|
|
|
|
char line[256];
|
|
|
|
while (fgets(line, sizeof(line), stdin) != NULL) {
|
|
//nothing
|
|
line[strcspn(line, "\n")] = 0;
|
|
//empty line
|
|
|
|
if (strlen(line) == 0) {
|
|
break;
|
|
}
|
|
|
|
//nospace
|
|
char pusto[256];
|
|
int j = 0;
|
|
for (int i = 0; line[i] != '\0'; i++) {
|
|
if (!isspace(line[i]) ) {
|
|
|
|
pusto[j = j + 1] = line[i];
|
|
}
|
|
}
|
|
pusto[j] = '\0';
|
|
|
|
// Parse
|
|
double n1;
|
|
double n2;
|
|
double expect;
|
|
char op;
|
|
int parsed = sscanf(pusto, "%lf%c%lf=%lf", &n1, &op, &n2, &expect);
|
|
|
|
if (parsed != 4) {
|
|
printf("CHYBA\n");
|
|
continue;
|
|
}
|
|
// is valid
|
|
if (op != '+' && op != '-' && op != '*' && op != '/') {
|
|
printf("CHYBA\n");
|
|
continue;
|
|
}
|
|
|
|
// resultat
|
|
double teraz;
|
|
switch (op) {
|
|
case '+':
|
|
teraz = n1 + n2;
|
|
break;
|
|
case '-':
|
|
teraz = n1 - n2;
|
|
break;
|
|
case '*':
|
|
teraz = n1 * n2;
|
|
break;
|
|
case '/':
|
|
if (n2 == 0.0) {
|
|
printf("CHYBA\n");
|
|
continue;
|
|
}
|
|
teraz = n1 / n2;
|
|
break;
|
|
}
|
|
|
|
// zaokrouhleni
|
|
double rounded_teraz = round(teraz * 100.0) / 100.0;
|
|
double rounded_expect = round(expect * 100.0) / 100.0;
|
|
|
|
if (fabs(rounded_teraz - rounded_expect) < 1e-9) {
|
|
printf("OK\n");
|
|
} else {
|
|
printf("ZLE\n");
|
|
}
|
|
}
|
|
return 0;
|
|
}
|