pvjc26/du3/program.c
2026-03-20 13:01:56 +00:00

81 lines
1.8 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++] = 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 ye
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("ZLE\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;
}