#include #include #include #include #include int main() { char* input = (char*) calloc(100, sizeof(char)); char symbol; int counter; int operation; char* number1 = (char*) calloc(25, sizeof(char)); char* number2 = (char*) calloc(25, sizeof(char)); char* result = (char*) calloc(30, sizeof(char)); char* ptr; bool equals = false; for(int i = 0; fgets(input, 100, stdin) != NULL; i++){ if(!input || !strcmp(input, "") || input[0] == EOF || input[0] == '\n' || input[0] == '\0') break; counter = 0; operation = 0; equals = false; memset(number1, '\0', 25); memset(number2, '\0', 25); while(input[counter] != '\0' && input[counter] != '\n'){ symbol = input[counter++]; while (isspace(symbol)) symbol = input[counter++]; if(symbol == '\0' || symbol == '\n') break; switch (symbol) { case '+': operation = 1; break; case '-': operation = 2; break; case '*': operation = 3; break; case '/': operation = 4; break; case '=': equals = true; break; default: if (isdigit(symbol) || symbol == '.'){ if (operation == 0) strcat(number1, symbol+""); else if(!equals) strcat(number2, symbol+""); else strcat(result, symbol+""); } else{ printf("CHYBA\n"); goto LABEL; } break; } } LABEL: switch(operation){ case 1: printf("%s\n", (strtof(number1, &ptr) + strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) + strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); break; case 2: printf("%s\n", (strtof(number1, &ptr) - strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) - strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); break; case 3: printf("%s\n", (strtof(number1, &ptr) * strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) * strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); break; case 4: printf("%s\n", (strtof(number1, &ptr) / strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) / strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); break; default: printf("CHYBA\n"); } } free(input); return 0; }