du3
This commit is contained in:
parent
1a2eff3de4
commit
311a5f0a6d
51
du3/program.c
Normal file
51
du3/program.c
Normal file
@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
char line[256];
|
||||
|
||||
while (fgets(line, sizeof(line), stdin) != NULL) {
|
||||
if (line[0] == '\n' || line[0] == '\r') break;
|
||||
|
||||
double a, b, result;
|
||||
char op;
|
||||
char eq;
|
||||
char extra;
|
||||
|
||||
int n = 0;
|
||||
int parsed = sscanf(line, " %lf %c %lf %c %lf %c%n", &a, &op, &b, &eq, &result, &extra, &n);
|
||||
|
||||
if (parsed != 5 || eq != '=') {
|
||||
printf("CHYBA\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (op != '+' && op != '-' && op != '*' && op != '/') {
|
||||
printf("CHYBA\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (op == '/' && b == 0.0) {
|
||||
printf("CHYBA\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
double actual;
|
||||
if (op == '+') actual = a + b;
|
||||
else if (op == '-') actual = a - b;
|
||||
else if (op == '*') actual = a * b;
|
||||
else actual = a / b;
|
||||
|
||||
double rounded_actual = round(actual * 100.0) / 100.0;
|
||||
double rounded_result = round(result * 100.0) / 100.0;
|
||||
|
||||
if (fabs(rounded_actual - rounded_result) < 1e-9) {
|
||||
printf("OK\n");
|
||||
} else {
|
||||
printf("ZLE\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user