Aktualizovat du3/program.c
This commit is contained in:
parent
7575e3d387
commit
8b9cc212e6
@ -1 +1,95 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
void strip_spaces(char *str) {
|
||||||
|
char temp[1000];
|
||||||
|
int j = 0;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (str[i]) {
|
||||||
|
if (isspace(str[i]) && (j == 0 || isspace(temp[j - 1]) || temp[j - 1] == '=' || temp[j - 1] == '+' || temp[j - 1] == '-' || temp[j - 1] == '*' || temp[j - 1] == '/')) {
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
temp[j++] = str[i++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
temp[j] = '\0';
|
||||||
|
strcpy(str, temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_equation(char *line, double *num1, double *num2, char *op, double *result) {
|
||||||
|
strip_spaces(line);
|
||||||
|
|
||||||
|
char *ptr = line;
|
||||||
|
char *endptr;
|
||||||
|
|
||||||
|
*num1 = strtod(ptr, &endptr);
|
||||||
|
if (ptr == endptr) return 0;
|
||||||
|
ptr = endptr;
|
||||||
|
|
||||||
|
while (isspace(*ptr)) ptr++;
|
||||||
|
if (*ptr != '+' && *ptr != '-' && *ptr != '*' && *ptr != '/') return 0;
|
||||||
|
*op = *ptr;
|
||||||
|
ptr++;
|
||||||
|
|
||||||
|
while (isspace(*ptr)) ptr++;
|
||||||
|
*num2 = strtod(ptr, &endptr);
|
||||||
|
if (ptr == endptr) return 0;
|
||||||
|
ptr = endptr;
|
||||||
|
|
||||||
|
while (isspace(*ptr)) ptr++;
|
||||||
|
if (*ptr != '=') return 0;
|
||||||
|
ptr++;
|
||||||
|
|
||||||
|
while (isspace(*ptr)) ptr++;
|
||||||
|
*result = strtod(ptr, &endptr);
|
||||||
|
if (ptr == endptr) return 0;
|
||||||
|
while (isspace(*endptr)) endptr++;
|
||||||
|
if (*endptr != '\0' && *endptr != '\n') return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double calculate(double num1, double num2, char op) {
|
||||||
|
switch (op) {
|
||||||
|
case '+': return num1 + num2;
|
||||||
|
case '-': return num1 - num2;
|
||||||
|
case '*': return num1 * num2;
|
||||||
|
case '/': return num1 / num2;
|
||||||
|
default: return NAN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double round_to_2(double value) {
|
||||||
|
return round(value * 100) / 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char line[1000];
|
||||||
|
|
||||||
|
while (fgets(line, sizeof(line), stdin)) {
|
||||||
|
if (line[0] == '\n') break;
|
||||||
|
|
||||||
|
double num1, num2, expected_result;
|
||||||
|
char op;
|
||||||
|
|
||||||
|
if (!parse_equation(line, &num1, &num2, &op, &expected_result)) {
|
||||||
|
printf("CHYBA\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
double actual_result = calculate(num1, num2, op);
|
||||||
|
actual_result = round_to_2(actual_result);
|
||||||
|
|
||||||
|
if (actual_result == expected_result) {
|
||||||
|
printf("OK\n");
|
||||||
|
} else {
|
||||||
|
printf("ZLE\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user