bez medzier a s viacerymy medzerami naraz

This commit is contained in:
Aleš Novysedlák 2025-03-10 14:04:58 +01:00
parent ed35d59346
commit 100f8e2e89

View File

@ -2,45 +2,58 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <ctype.h>
float plus(float total, float number); float plus(float total, float number);
float minus(float total, float number); float minus(float total, float number);
float divided(float total, float number); float divided(float total, float number);
float multiplied(float total, float number); float multiplied(float total, float number);
int main() { int main() {
char buffer[999]; char buffer[999];
char row[999];
char equation[999][20]; char equation[999][20];
char output[999][10]; char output[999][10];
int output_length = 0; int output_length = 0;
int index = 0;
int eq_index = 0; int eq_index = 0;
int index = 0;
while (fgets(buffer, sizeof(buffer), stdin) != NULL) { while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
if (buffer[0] == '\n') break; if (buffer[0] == '\n') break;
index = 0; index = 0;
eq_index = 0; eq_index = 0;
int eq_operand_length = 0; int eq_operand_length = 0;
while (buffer[index] != '\n') { while (buffer[index] != '\n' && buffer[index] != '\0') {
if (buffer[index] != ' ') {
if (isspace(buffer[index])) {
index++;
continue;
}
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]; equation[eq_index][eq_operand_length] = buffer[index];
eq_operand_length++; eq_operand_length++;
} }
else { else if (strchr("+-*/=", buffer[index]) != NULL) {
if (eq_operand_length > 0) {
equation[eq_index][eq_operand_length] = '\0'; equation[eq_index][eq_operand_length] = '\0';
eq_index++;
eq_operand_length = 0; eq_operand_length = 0;
}
equation[eq_index][0] = buffer[index];
equation[eq_index][1] = '\0';
eq_index++; eq_index++;
} }
index++; index++;
} }
if (eq_operand_length > 0) {
equation[eq_index][eq_operand_length] = '\0';
eq_index++;
}
float total = 0.0; float total = 0.0;
char last_operator = '+';
int sign = 0; int sign = 0;
char out[10] = "";
int n_of_signs = 0; int n_of_signs = 0;
char operators[6] = "+-=*/"; char operators[6] = "+-=*/";
char last_operator = '+'; for (int i=0;i<eq_index;i++) {
for (int i=0;i<eq_index+1;i++) {
n_of_signs = 0; n_of_signs = 0;
for (int l=0; l<5;l++) { for (int l=0; l<5;l++) {
if (strchr(equation[i], operators[l]) != NULL) { if (strchr(equation[i], operators[l]) != NULL) {
@ -65,22 +78,20 @@ int main() {
total = divided(total, atof(equation[i])); total = divided(total, atof(equation[i]));
break; break;
case '=': case '=':
/*if (total == atof(equation[i]))*/if (fabs((round(total*100)/100) - atof(equation[i])) < 1e-6) { total = round(total * 100) / 100;
if (fabs(total - atof(equation[i])) < 1e-6) {
strcpy(output[output_length], "OK"); strcpy(output[output_length], "OK");
output_length++; output_length++;
} } else {
else {
strcpy(output[output_length], "ZLE"); strcpy(output[output_length], "ZLE");
output_length++; output_length++;
} }
break; break;
} }
} } else {
else {
last_operator = equation[i][0]; last_operator = equation[i][0];
} }
} } else {
else {
strcpy(output[output_length], "CHYBA"); strcpy(output[output_length], "CHYBA");
output_length++; output_length++;
break; break;
@ -88,8 +99,6 @@ int main() {
sign = !sign; sign = !sign;
} }
memset(equation, 0, sizeof(equation)); memset(equation, 0, sizeof(equation));
//printf("%f", total);
} }
for (int i=0; i<output_length; i++) { for (int i=0; i<output_length; i++) {
printf("%s\n", output[i]); printf("%s\n", output[i]);
@ -98,7 +107,6 @@ int main() {
return 0; return 0;
} }
float plus(float total, float number) { float plus(float total, float number) {
return total + number; return total + number;
} }
@ -114,3 +122,4 @@ float divided(float total, float number) {
float multiplied(float total, float number) { float multiplied(float total, float number) {
return total * number; return total * number;
} }