Изменить 'du4/program.c'
^^
This commit is contained in:
parent
9fef31bff3
commit
95212f7acd
@ -4,33 +4,32 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main(){
|
||||
char* input = (char*) calloc (100, sizeof(char));
|
||||
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* 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')
|
||||
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'){
|
||||
while(input[counter] != '\0' && input[counter] != '\n'){
|
||||
symbol = input[counter++];
|
||||
while(isspace(symbol))
|
||||
while (isspace(symbol))
|
||||
symbol = input[counter++];
|
||||
if(symbol == '\0' || symbol == '\n')
|
||||
break;
|
||||
switch(symbol){
|
||||
switch (symbol) {
|
||||
case '+':
|
||||
operation = 1;
|
||||
break;
|
||||
@ -47,19 +46,19 @@ int main(){
|
||||
equals = true;
|
||||
break;
|
||||
default:
|
||||
if(isdigit(symbol) || symbol == '.'){
|
||||
if(operation == 0)
|
||||
if (isdigit(symbol) || symbol == '.'){
|
||||
if (operation == 0)
|
||||
number1 += symbol;
|
||||
else if (!equals)
|
||||
else if(!equals)
|
||||
number2 += symbol;
|
||||
else
|
||||
result += symbol;
|
||||
}
|
||||
else {
|
||||
printf("CHYBA");
|
||||
else{
|
||||
printf("CHYBA\n");
|
||||
goto LABEL;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
LABEL:
|
||||
@ -68,18 +67,18 @@ int main(){
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
printf("CHYBA\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user