88 lines
1.6 KiB
C
88 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include<string.h>
|
|
#include<math.h>
|
|
#include<stdlib.h>
|
|
char* compactString(char *src,int leng) {
|
|
char *new_str = (char*)calloc(leng+1,sizeof(char));
|
|
int k=0;
|
|
for(int i=0;i<leng;i++){
|
|
if(src[i]!=' '){
|
|
new_str[k] = src[i];
|
|
k++;
|
|
}
|
|
}
|
|
return new_str;
|
|
}
|
|
|
|
|
|
|
|
double operation(double first,double second,char c){
|
|
double x =0;
|
|
if(c=='*'){
|
|
x=first*second;
|
|
return x;
|
|
}
|
|
else if(c =='-'){
|
|
x=first-second;
|
|
return x;
|
|
}
|
|
else if(c=='/'){
|
|
x=first/second;
|
|
return x;
|
|
}
|
|
x= first+second;
|
|
return x;
|
|
}
|
|
|
|
|
|
double round_to(double value, double eps)
|
|
{
|
|
|
|
return floor(value/eps + 0.5) * eps;
|
|
}
|
|
|
|
double compare_percent(double a, double b, double eps)
|
|
{
|
|
double diff = round_to( (a - b) * 2 / (a + b), eps);
|
|
return diff < 0 ? -1 : diff > 0 ? +1 : 0;
|
|
}
|
|
|
|
int check(char *str){
|
|
for(int i =0;i<strlen(str);i++){
|
|
if(str[i]=='='&&str[i+1]=='='){
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
char str[100][100];
|
|
for(int i =0;fgets(str[i],100,stdin);i++){
|
|
if(str[i][0]=='\n'){
|
|
break;
|
|
}
|
|
char *new_str = compactString(str[i],strlen(str[i]));
|
|
double first =-5,second =-5,res=-5;
|
|
char c='E';
|
|
sscanf(new_str,"%lf%c%lf%*[=]%lf",&first,&c,&second,&res);
|
|
|
|
|
|
if(c!=43&&c!=42&&c!=45&&c!=47||check(new_str)==0){
|
|
printf("CHYBA\n");
|
|
continue;
|
|
}
|
|
double my_res = operation(first,second,c);
|
|
if(compare_percent(res,my_res,0.01)==0){
|
|
printf("OK\n");
|
|
}
|
|
else{
|
|
printf("ZLE\n");
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
} |