commit
This commit is contained in:
parent
02723374df
commit
f306485be2
53
du3/program.c
Normal file
53
du3/program.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define MAX 256
|
||||||
|
|
||||||
|
double zaokruhlenie_vysledku(double x) {
|
||||||
|
return round(x * 100.0) / 100.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double vykonaj_operaciu(double a, char op, double b) {
|
||||||
|
if (op == '+') return a + b;
|
||||||
|
if (op == '-') return a - b;
|
||||||
|
if (op == '*') return a * b;
|
||||||
|
if (op == '/') return a / b;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char riadok[MAX];
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (fgets(riadok, MAX, stdin) == NULL)
|
||||||
|
break;
|
||||||
|
if (strcmp(riadok, "\n") == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
double cislo1, cislo2, vysledok;
|
||||||
|
char operacia;
|
||||||
|
int nacitane = sscanf(riadok, "%lf %c %lf = %lf", &cislo1, &operacia, &cislo2, &vysledok);
|
||||||
|
|
||||||
|
if (nacitane != 4) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!(operacia == '+' || operacia == '-' || operacia == '*' || operacia == '/')) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (operacia == '/' && cislo2 == 0) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
double spravnyVysledok = vykonaj_operaciu(cislo1, operacia, cislo2);
|
||||||
|
|
||||||
|
if (zaokruhlenie_vysledku(spravnyVysledok) == zaokruhlenie_vysledku(vysledok)) {
|
||||||
|
printf("OK\n");
|
||||||
|
} else {
|
||||||
|
printf("ZLE\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user