du3
This commit is contained in:
parent
e56ddc2dba
commit
68fe8dde34
102
du3/program.c
Normal file
102
du3/program.c
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char riadok[100];
|
||||||
|
|
||||||
|
while (fgets(riadok, sizeof(riadok), stdin)) {
|
||||||
|
|
||||||
|
// prázdny riadok -> KONIEC
|
||||||
|
if (riadok[0] == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *r = riadok;
|
||||||
|
char *end;
|
||||||
|
|
||||||
|
// preskoč medzery
|
||||||
|
while (*r == ' '){
|
||||||
|
r++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. číslo
|
||||||
|
float a = strtof(r, &end);
|
||||||
|
if (r == end) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
r = end;
|
||||||
|
|
||||||
|
while (*r == ' '){
|
||||||
|
r++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// operátor
|
||||||
|
char or = *r;
|
||||||
|
if (!(or == '+' || or == '-' || or == '*' || or == '/')) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
r++;
|
||||||
|
|
||||||
|
while (*r == ' '){
|
||||||
|
r++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. číslo
|
||||||
|
float b = strtof(r, &end);
|
||||||
|
if (r == end) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
r = end;
|
||||||
|
|
||||||
|
while (*r == ' '){
|
||||||
|
r++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// '='
|
||||||
|
if (*r != '=') {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
r++;
|
||||||
|
|
||||||
|
while (*r == ' '){
|
||||||
|
r++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// výsledok
|
||||||
|
float c = strtof(r, &end);
|
||||||
|
if (r == end) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
r = end;
|
||||||
|
|
||||||
|
// výpočet
|
||||||
|
float cislo;
|
||||||
|
|
||||||
|
if (or == '+') cislo = a + b;
|
||||||
|
else if (or == '-') cislo = a - b;
|
||||||
|
else if (or == '*') cislo = a * b;
|
||||||
|
else {
|
||||||
|
if (b == 0) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
cislo = a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// porovnanie s toleranciou
|
||||||
|
if (fabs(cislo - c) <= 0.001) {
|
||||||
|
printf("OK\n");
|
||||||
|
} else {
|
||||||
|
printf("ZLE\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user