142 lines
5.9 KiB
C
142 lines
5.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include <ctype.h>
|
|
|
|
float plus(float total, float number);
|
|
float minus(float total, float number);
|
|
float divided(float total, float number);
|
|
float multiplied(float total, float number);
|
|
|
|
int main() {
|
|
char buffer[999];
|
|
char equation[999][20];
|
|
char output[999][10];
|
|
int output_length = 0;
|
|
int eq_index = 0;
|
|
int index = 0;
|
|
while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
|
|
int error = 0;
|
|
int operator_count = 0;
|
|
if (buffer[0] == '\n') break;
|
|
index = 0;
|
|
eq_index = 0;
|
|
int eq_operand_length = 0;
|
|
while (buffer[index] != '\n' && buffer[index] != '\0') {
|
|
|
|
if (isspace(buffer[index])) {
|
|
index++;
|
|
continue;
|
|
}
|
|
if ((isalpha(buffer[index]) || strchr(".+-*/=0123456789\n", buffer[index]) == NULL) && !error) {
|
|
error = 1;
|
|
strcpy(output[output_length], "CHYBA");
|
|
output_length++;
|
|
|
|
}
|
|
if (isdigit(buffer[index]) || (buffer[index] == '.' && eq_operand_length > 0 && isdigit(buffer[index + 1])) || (buffer[index] == '-' && (eq_operand_length == 0 || strchr("+-*/=", buffer[index - 1])))) {
|
|
equation[eq_index][eq_operand_length] = buffer[index];
|
|
eq_operand_length++;
|
|
}
|
|
else if (strchr("+-*/=", buffer[index]) != NULL) {
|
|
if (eq_operand_length > 0) {
|
|
equation[eq_index][eq_operand_length] = '\0';
|
|
eq_index++;
|
|
eq_operand_length = 0;
|
|
}
|
|
equation[eq_index][0] = buffer[index];
|
|
equation[eq_index][1] = '\0';
|
|
eq_index++;
|
|
operator_count++;
|
|
}
|
|
index++;
|
|
}
|
|
if (eq_operand_length > 0) {
|
|
equation[eq_index][eq_operand_length] = '\0';
|
|
eq_index++;
|
|
}
|
|
if (operator_count < 2 && !error) {
|
|
error = 1;
|
|
strcpy(output[output_length], "CHYBA");
|
|
output_length++;
|
|
|
|
}
|
|
float total = 0.0;
|
|
char last_operator = '+';
|
|
int sign = 0;
|
|
int n_of_signs = 0;
|
|
char operators[6] = "+-=*/";
|
|
if (!error) {
|
|
for (int i=0;i<eq_index;i++) {
|
|
n_of_signs = 0;
|
|
for (int l=0; l<5;l++) {
|
|
if (strchr(equation[i], operators[l]) != NULL) {
|
|
if ((strlen(equation[i]) > 1) && (equation[i][0] == '-'))
|
|
continue;
|
|
n_of_signs++;
|
|
}
|
|
}
|
|
if (((n_of_signs == 1) && sign && (strlen(equation[i]) == 1)) || ((n_of_signs == 0) && !sign)) {
|
|
if (!sign) {
|
|
switch (last_operator) {
|
|
case '+':
|
|
total = plus(total, atof(equation[i]));
|
|
break;
|
|
case '-':
|
|
total = minus(total, atof(equation[i]));
|
|
break;
|
|
case '*':
|
|
total = multiplied(total, atof(equation[i]));
|
|
break;
|
|
case '/':
|
|
total = divided(total, atof(equation[i]));
|
|
break;
|
|
case '=':
|
|
if (fabs((round(total * 100) / 100) - atof(equation[i])) < 1e-6) {
|
|
strcpy(output[output_length], "OK");
|
|
output_length++;
|
|
} else {
|
|
strcpy(output[output_length], "ZLE");
|
|
output_length++;
|
|
}
|
|
break;
|
|
}
|
|
} else {
|
|
last_operator = equation[i][0];
|
|
}
|
|
} else {
|
|
strcpy(output[output_length], "CHYBA");
|
|
output_length++;
|
|
break;
|
|
}
|
|
sign = !sign;
|
|
}
|
|
}
|
|
memset(equation, 0, sizeof(equation));
|
|
//printf("%f - %f\n",total, (round(total * 100) / 100));
|
|
}
|
|
for (int i=0; i<output_length; i++) {
|
|
printf("%s\n", output[i]);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
float plus(float total, float number) {
|
|
return total + number;
|
|
}
|
|
|
|
float minus(float total, float number) {
|
|
return total - number;
|
|
}
|
|
|
|
float divided(float total, float number) {
|
|
return total / number;
|
|
}
|
|
|
|
float multiplied(float total, float number) {
|
|
return total * number;
|
|
}
|
|
|