Aktualizovat „du4/program.c“

This commit is contained in:
Bohdan Yanchyk 2020-04-03 07:34:10 +00:00
parent 0673cb9613
commit 74ee5deb26

View File

@ -1,144 +1,171 @@
/*
gcc -Werror -std=c11 -Wall in-out.c -lm -o in-out -lm && ./in-out
*/
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
struct res{ #include<string.h>
double na,nb,result;
char sign;
}list [10];
int fill(double*,double*,char *,double *); #include<math.h>
double count(const double*,const double *,char);
int aget(double *);
int sget(char *);
int efinde(void);
char checkend(void);
int main(){ #include<stdlib.h>
int cond;
char fcond=1;
int i;
for(i=0;fcond!=EOF;i++){
fcond=checkend();
if(fcond==EOF)
break;
cond=aget(&list[i].na); char* compactString(char *src,int leng) {
if(cond<0){
printf("CHYBA\n"); char *new_str = (char*)calloc(leng+1,sizeof(char));
return 0;}
cond=sget(&list[i].sign); int k=0;
if(cond<0){
printf("CHYBA\n"); for(int i=0;i<leng;i++){
return 0;}
cond=aget(&list[i].nb); if(src[i]!=' '){
if(cond<0){
printf("CHYBA\n"); new_str[k] = src[i];
return 0;}
cond=efinde(); k++;
if(cond<0){
printf("CHYBA\n");
return 0;}
cond=aget(&list[i].result);
if(cond<0){
printf("CHYBA\n");
return 0;}
if((fcond=checkend())!='\n' && fcond>0){
printf("CHYBA\n");
return 0;}
if(fcond==-1)
break;
//printf("%lf %c %lf = %lf\n",list[i].na,list[i].sign,list[i].nb,list[i].result);
}
for(int x=0;x<i;x++){
double fin=count(&list[x].na,&list[x].nb,list[x].sign);
//printf("%lf %lf\n",fin,list[x].result);
if((long)(fin*1000000)!=(long)(list[x].result)*1000000){
printf("ZLE\n");
return 0;
}
}
printf("OK\n");
} }
char checkend(void){
char c;
int count=1;
c=getchar();
while((c=getchar())){
++count;
if(c==EOF || c=='\n'){
int a=c;
if((c=getchar())==EOF){
int b=c;
if((c=getchar())==EOF){
++count;
fseek(stdin,-count,SEEK_CUR);
return c;
}
++count;
fseek(stdin,-count,SEEK_CUR);
return b;
}
++count;
fseek(stdin,-count,SEEK_CUR);
return a;
}
else if(!isspace(c)){
fseek(stdin,-count,SEEK_CUR);
return c;
}
}
return -2;
}
double count(const double*na,const double *nb,char sign){
if(sign=='+')
return *na+*nb+0.00000000001;
else if(sign=='-')
return *na-*nb;
else if(sign=='*')
return *na*(*nb);
else if(sign=='/')
return *na/(*nb);
return -1;
} }
int aget(double *na){ return new_str;
char a[30];
char c;
char *p;
p=a;
while(isspace(c=getchar()));
if(isdigit(c)){
*(p++)=c;
while(isdigit(c=getchar())|| c=='.') *(p++)=c;
*p='\0';
*na=atof(a);
}else
return -1;
fseek(stdin,-1,SEEK_CUR);
return 1;
} }
int sget(char *sign){
char c; double operation(double first,double second,char c){
while(isspace(c=getchar()));
if(c=='+' || c=='-' || c=='/' || c=='*') double x =0;
*sign=c;
else if(c=='*'){
return -1;
return 1; x=first*second;
return x;
} }
int efinde(void){
char c; else if(c =='-'){
while(isspace(c=getchar()));
if(c!='=') x=first-second;
return -1;
return 1; 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){
int flag=0;
char symb[5]="*-+/=";
for(int i =0;i<strlen(str);i++){
for(int k =0;k<5;k++){
for(int j =0;j<5;j++){
if(str[i]==symb[j]&&str[i+1]==symb[k]){
return 0;
}
}
}
if(str[i]=='='){
flag=1;
}
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||str[i]==','){
return 0;
}
}
return flag==1?1:0;
}
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;
} }