Update 'du4/program.c'
This commit is contained in:
parent
b21597ee93
commit
2fc2076427
136
du4/program.c
136
du4/program.c
@ -1,91 +1,87 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int ch = getchar();
|
char line[300];
|
||||||
char priklad[300];
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
while (fgets(line, sizeof(line), stdin)) {
|
||||||
|
count++;
|
||||||
|
char *start = line, *end = NULL;
|
||||||
|
int has_operator = 0;
|
||||||
|
float num1 = 0, num2 = 0, expected_result = 0;
|
||||||
|
char op, *next;
|
||||||
|
|
||||||
for (int idx = 0; 1; idx++) {
|
while (*start != '\n') {
|
||||||
priklad[idx] = ch;
|
if (*start == '+' || *start == '-' || *start == '*' || *start == '/') {
|
||||||
if (ch == '\n') {
|
if (has_operator) {
|
||||||
count++;
|
printf("CHYBA\n");
|
||||||
}
|
goto next_line;
|
||||||
ch = getchar();
|
|
||||||
if ((priklad[idx] == '\n' && ch == '\n') || ch == EOF) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char *start = priklad, *end = NULL;
|
|
||||||
|
|
||||||
for (int c = 0; c < count; c++) {
|
|
||||||
int skip = 0, z = 0;
|
|
||||||
|
|
||||||
for (int idx = 0; start[idx] != '\n'; idx++) {
|
|
||||||
if (start[idx] == '+' || start[idx] == '-' || start[idx] == '*' || start[idx] == '/') {
|
|
||||||
z = 1;
|
|
||||||
}
|
|
||||||
if ((start[idx] < '0' || start[idx] > '9') && start[idx] != ' ' && start[idx] != '=' && start[idx] != '+' && start[idx] != '-' && start[idx] != '*' && start[idx] != '/' && start[idx] != '.') {
|
|
||||||
skip = 1;
|
|
||||||
printf("CHYBA\n");
|
|
||||||
while ((*start) != '\n') {
|
|
||||||
start++;
|
|
||||||
}
|
}
|
||||||
start++;
|
has_operator = 1;
|
||||||
break;
|
op = *start;
|
||||||
}
|
} else if ((*start < '0' || *start > '9') && *start != ' ' && *start != '=' && *start != '.') {
|
||||||
}
|
printf("CHYBA\n");
|
||||||
|
goto next_line;
|
||||||
if (skip == 1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (z == 0) {
|
|
||||||
while ((*start) != '\n') {
|
|
||||||
start++;
|
|
||||||
}
|
}
|
||||||
start++;
|
start++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!has_operator) {
|
||||||
printf("CHYBA\n");
|
printf("CHYBA\n");
|
||||||
continue;
|
goto next_line;
|
||||||
}
|
|
||||||
float num1 = strtof(start, &end);
|
|
||||||
if (end == start) {
|
|
||||||
printf("KONIEC\n");
|
|
||||||
}
|
|
||||||
while ((*start) != '+' && (*start) != '-' && (*start) != '/' && (*start) != '*') {
|
|
||||||
start++;
|
|
||||||
}
|
|
||||||
char znak = (*start);
|
|
||||||
start++;
|
|
||||||
|
|
||||||
float num2 = strtof(start, &end);
|
|
||||||
start = end;
|
|
||||||
while (((*start) < '0' || (*start) > '9') && (*start) != '-') {
|
|
||||||
start++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float vysledok2 = strtof(start, &end);
|
num1 = strtof(line, &next);
|
||||||
start = end;
|
while (*next == ' ') next++;
|
||||||
start++;
|
if (*next != op) {
|
||||||
float vysledok = 0;
|
printf("CHYBA\n");
|
||||||
|
goto next_line;
|
||||||
if (znak == '-') {
|
|
||||||
vysledok = num1 - num2;
|
|
||||||
} else if (znak == '+') {
|
|
||||||
vysledok = num1 + num2;
|
|
||||||
} else if (znak == '*') {
|
|
||||||
vysledok = num1 * num2;
|
|
||||||
} else if (znak == '/') {
|
|
||||||
vysledok = num1 / num2;
|
|
||||||
}
|
}
|
||||||
vysledok = (round(vysledok * 100)) / 100;
|
next++;
|
||||||
if (vysledok2 - vysledok < 0.001 && vysledok2 - vysledok > -0.001) {
|
num2 = strtof(next, &next);
|
||||||
|
while (*next == ' ') next++;
|
||||||
|
if (*next == '=') {
|
||||||
|
next++;
|
||||||
|
expected_result = strtof(next, &next);
|
||||||
|
while (*next == ' ') next++;
|
||||||
|
} else {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
goto next_line;
|
||||||
|
}
|
||||||
|
if (*next != '\n') {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
goto next_line;
|
||||||
|
}
|
||||||
|
|
||||||
|
float result;
|
||||||
|
switch (op) {
|
||||||
|
case '+':
|
||||||
|
result = num1 + num2;
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
result = num1 - num2;
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
result = num1 * num2;
|
||||||
|
break;
|
||||||
|
case '/':
|
||||||
|
if (num2 == 0) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
goto next_line;
|
||||||
|
}
|
||||||
|
result = num1 / num2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result = roundf(result * 100) / 100;
|
||||||
|
if (fabs(result - expected_result) < 0.001) {
|
||||||
printf("OK\n");
|
printf("OK\n");
|
||||||
} else {
|
} else {
|
||||||
printf("ZLE\n");
|
printf("ZLE\n");
|
||||||
}
|
}
|
||||||
|
next_line:
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user