pvjc24/a1/program.c

58 lines
1.6 KiB
C
Raw Normal View History

2024-03-20 14:10:14 +00:00
#include <stdio.h>
#include <string.h>
2024-03-20 14:32:08 +00:00
#include <math.h>
2024-03-20 13:10:12 +00:00
2024-03-20 14:10:14 +00:00
char pole[100];
int main() {
2024-03-20 14:32:08 +00:00
float cislo1, cislo2, vysledok, ocakavany_vysledok;
2024-03-20 14:10:14 +00:00
char znamienko, rovna_sa;
while (1) {
if (fgets(pole, sizeof(pole), stdin) != NULL) {
2024-03-20 14:32:08 +00:00
if (sscanf(pole, "%f %c %f %c %f", &cislo1, &znamienko, &cislo2, &rovna_sa, &ocakavany_vysledok) == 5 && rovna_sa == '=') {
2024-03-20 14:10:14 +00:00
switch (znamienko) {
case '+':
vysledok = cislo1 + cislo2;
break;
case '-':
vysledok = cislo1 - cislo2;
break;
case '*':
vysledok = cislo1 * cislo2;
break;
case '/':
if (cislo2 != 0)
vysledok = cislo1 / cislo2;
else {
2024-03-20 14:33:07 +00:00
printf("ZLE\n");
2024-03-20 14:10:14 +00:00
return 0;
2024-03-20 14:32:08 +00:00
break;
2024-03-20 14:10:14 +00:00
}
break;
default:
printf("Zle znamienko!\n");
return 0;
2024-03-20 14:32:08 +00:00
break;
2024-03-20 14:10:14 +00:00
}
2024-03-20 15:01:15 +00:00
vysledok = roundf(vysledok * 100) / 100;
if (vysledok == ocakavany_vysledok) {
2024-03-20 14:10:14 +00:00
printf("OK\n");
} else {
printf("ZLE\n");
}
} else if (pole[0] == '\n') {
break;
} else {
printf("CHYBA\n");
}
} else {
break;
}
}
return 0;
}