Přidat du3/program.c
This commit is contained in:
parent
186c0f6e8a
commit
7870d6c4ef
59
du3/program.c
Normal file
59
du3/program.c
Normal file
@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_LENGTH 100
|
||||
|
||||
int check(char* input);
|
||||
|
||||
int main() {
|
||||
char input[MAX_LENGTH];
|
||||
while (fgets(input,MAX_LENGTH, stdin)) {
|
||||
if (input[0] == '\n') {
|
||||
break;
|
||||
}
|
||||
input[strcspn(input, "\n")] = '\0';
|
||||
int resultat =check(input);
|
||||
if (resultat ==1) {
|
||||
printf("OK\n");
|
||||
} else {
|
||||
if (resultat == -1) {
|
||||
printf("ZLE\n");
|
||||
} else {
|
||||
printf("CHYBA\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int check(char* input) {
|
||||
double number1, number2, resultat, givenResult;
|
||||
char znak;
|
||||
int a=0;
|
||||
a=sscanf(input, " %lf %c %lf = %lf ", &number1, &znak, &number2, &givenResult);
|
||||
if (a != 4) {
|
||||
return 0;
|
||||
}else {
|
||||
switch (znak) {
|
||||
case '+':
|
||||
resultat=number1+number2;
|
||||
break;
|
||||
case '-':
|
||||
resultat=number1-number2;
|
||||
break;
|
||||
case '*':
|
||||
resultat=number1*number2;
|
||||
break;
|
||||
case '/':
|
||||
if (number2 == 0) {
|
||||
return 0;
|
||||
}else{resultat=number1/number2;}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
if (resultat-givenResult ==0 ) {
|
||||
return 1;
|
||||
} else { return -1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user