pvjc24/a1/program.c
2024-03-28 18:34:49 +00:00

68 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h> // Include string.h for strlen
#define MAX_LINE_LENGTH 100
int main() {
char uloha[MAX_LINE_LENGTH];
while (fgets(uloha, MAX_LINE_LENGTH, stdin) != NULL) {
int i;
int isEmpty = 1;
for (i = 0; uloha[i] != '\0'; i++) {
if (uloha[i] != ' ' && uloha[i] != '\t' && uloha[i] != '\n' && uloha[i] != '\r') {
isEmpty = 0;
break;
}
}
if (isEmpty) {
continue;
}
double cislo1, cislo2;
double vysledok_zadany;
char operacia;
if (sscanf(uloha, "%lf %c %lf = %lf", &cislo1, &operacia, &cislo2, &vysledok_zadany) != 4) {
printf("CHYBA\n");
continue;
}
double vysledok_skutocny;
switch (operacia) {
case '+':
vysledok_skutocny = cislo1 + cislo2;
break;
case '-':
vysledok_skutocny = cislo1 - cislo2;
break;
case '*':
vysledok_skutocny = cislo1 * cislo2;
break;
case '/':
if (cislo2 == 0) {
printf("ZLE\n");
continue;
}
vysledok_skutocny = cislo1 / cislo2;
break;
default:
printf("CHYBA\n");
continue;
}
if (fabs(vysledok_skutocny - vysledok_zadany) < 1e-6) {
printf("OK\n");
} else {
printf("ZLE\n");
}
}
return 0;
}