pvjc20/du4/program.c

93 lines
1.8 KiB
C
Raw Normal View History

2020-04-02 23:03:29 +00:00
#include <stdio.h>
#include<string.h>
2020-04-02 23:23:43 +00:00
#include<math.h>
2020-04-02 23:03:29 +00:00
#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){
2020-04-02 23:23:43 +00:00
double x =0;
2020-04-02 23:03:29 +00:00
if(c=='*'){
2020-04-02 23:23:43 +00:00
x=first*second;
return x;
2020-04-02 23:03:29 +00:00
}
else if(c =='-'){
2020-04-02 23:23:43 +00:00
x=first-second;
return x;
2020-04-02 23:03:29 +00:00
}
else if(c=='/'){
2020-04-02 23:23:43 +00:00
x=first/second;
return x;
2020-04-02 23:03:29 +00:00
}
2020-04-02 23:23:43 +00:00
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;
2020-04-02 23:03:29 +00:00
}
2020-04-03 00:22:16 +00:00
int check(char *str){
2020-04-03 00:24:29 +00:00
char symb[5]="*-+/=";
2020-04-03 00:22:16 +00:00
for(int i =0;i<strlen(str);i++){
2020-04-03 00:24:29 +00:00
for(int k =0;k<5;k++){
2020-04-03 00:26:25 +00:00
for(int j =0;j<5;j++){
if(str[i]==symb[j]&&str[i+1]==symb[k]){
return 0;
}
2020-04-03 00:24:29 +00:00
}
2020-04-03 00:22:16 +00:00
}
}
return 1;
}
2020-04-02 23:03:29 +00:00
int main()
{
2020-04-02 23:29:32 +00:00
char str[100][100];
2020-04-02 23:56:53 +00:00
for(int i =0;fgets(str[i],100,stdin);i++){
2020-04-03 00:06:25 +00:00
if(str[i][0]=='\n'){
break;
}
2020-04-02 23:56:53 +00:00
char *new_str = compactString(str[i],strlen(str[i]));
2020-04-02 23:29:32 +00:00
double first =-5,second =-5,res=-5;
char c='E';
2020-04-03 00:22:16 +00:00
sscanf(new_str,"%lf%c%lf%*[=]%lf",&first,&c,&second,&res);
2020-04-02 23:29:32 +00:00
2020-04-02 23:03:29 +00:00
2020-04-03 00:22:16 +00:00
if(c!=43&&c!=42&&c!=45&&c!=47||check(new_str)==0){
2020-04-02 23:29:32 +00:00
printf("CHYBA\n");
2020-04-03 00:07:44 +00:00
continue;
2020-04-02 23:29:32 +00:00
}
double my_res = operation(first,second,c);
if(compare_percent(res,my_res,0.01)==0){
printf("OK\n");
}
else{
printf("ZLE\n");
2020-04-03 00:06:25 +00:00
}
2020-04-02 23:03:29 +00:00
2020-04-02 23:56:53 +00:00
}
2020-04-03 00:06:25 +00:00
2020-04-02 23:03:29 +00:00
return 0;
2020-04-02 23:45:12 +00:00
}