pvjc21/du4/program.c

82 lines
2.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
int main(){
char* input = (char*) calloc (100, sizeof(char));
char symbol = '\0';
int counter = 0;
int operation = 0;
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); i++){
counter = 0;
operation = 0;
equals = false;
memset(number1, '\0', 25);
memset(number2, '\0', 25);
while (input[counter] != '\0'){
symbol = input[counter++];
while(isspace(symbol))
symbol = input[counter++];
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)
number1 += symbol;
else if (!equals)
number2 += symbol;
else
result += symbol;
}
else if(!isdigit(symbol)){
printf("CHYBA");
goto LABEL;
}
break;
}
}
LABEL:
switch(operation){
case 1:
printf("%s\n", (strtof(number1, &ptr) + strtof(number2, &ptr) == strtof(result, &ptr)) ? "OK" : "ZLE");
break;
case 2:
printf("%s\n", strtof(number1, &ptr) - strtof(number2, &ptr) == strtof(result, &ptr)) ? "OK" : "ZLE");
break;
case 3:
printf("%s\n", strtof(number1, &ptr) * strtof(number2, &ptr) == strtof(result, &ptr)) ? "OK" : "ZLE");
break;
case 4:
printf("%s\n", strtof(number1, &ptr) / strtof(number2, &ptr) == strtof(result, &ptr)) ? "OK" : "ZLE");
break;
default:
printf("CHYBA");
}
}
return 0;
}