pvjc24/a1/program.c

69 lines
1.4 KiB
C
Raw Permalink Normal View History

2024-03-28 17:47:51 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
2024-03-28 18:39:40 +00:00
#include <string.h>
2024-03-28 17:47:51 +00:00
#define MAX_LINE_LENGTH 100
int main() {
char uloha[MAX_LINE_LENGTH];
while (fgets(uloha, MAX_LINE_LENGTH, stdin) != NULL) {
2024-03-28 18:01:04 +00:00
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;
}
}
2024-03-28 18:29:54 +00:00
2024-03-28 18:01:04 +00:00
if (isEmpty) {
2024-03-28 18:42:00 +00:00
break;
2024-03-28 17:55:23 +00:00
}
2024-03-28 18:34:49 +00:00
double cislo1, cislo2;
2024-03-28 18:29:54 +00:00
double vysledok_zadany;
2024-03-28 17:47:51 +00:00
char operacia;
2024-03-28 18:34:49 +00:00
if (sscanf(uloha, "%lf %c %lf = %lf", &cislo1, &operacia, &cislo2, &vysledok_zadany) != 4) {
2024-03-28 17:47:51 +00:00
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) {
2024-03-28 18:40:23 +00:00
printf("ZLE\n");
2024-03-28 17:47:51 +00:00
continue;
}
2024-03-28 18:34:49 +00:00
vysledok_skutocny = cislo1 / cislo2;
2024-03-28 17:47:51 +00:00
break;
default:
printf("CHYBA\n");
continue;
}
2024-03-28 18:39:40 +00:00
vysledok_skutocny = round(vysledok_skutocny * 100) / 100.0;
2024-03-28 18:01:04 +00:00
if (fabs(vysledok_skutocny - vysledok_zadany) < 1e-6) {
2024-03-28 17:47:51 +00:00
printf("OK\n");
} else {
printf("ZLE\n");
}
}
return 0;
2024-03-28 18:02:29 +00:00
}