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 @@
/* #include <stdio.h>
gcc -Werror -std=c11 -Wall in-out.c -lm -o in-out -lm && ./in-out
*/
#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; char* compactString(char *src,int leng) {
int i;
for(i=0;fcond!=EOF;i++){ char *new_str = (char*)calloc(leng+1,sizeof(char));
fcond=checkend();
if(fcond==EOF) int k=0;
break;
for(int i=0;i<leng;i++){
cond=aget(&list[i].na);
if(cond<0){ if(src[i]!=' '){
printf("CHYBA\n");
return 0;} new_str[k] = src[i];
cond=sget(&list[i].sign);
if(cond<0){ k++;
printf("CHYBA\n");
return 0;} }
cond=aget(&list[i].nb);
if(cond<0){ }
printf("CHYBA\n");
return 0;} return new_str;
cond=efinde();
if(cond<0){ }
printf("CHYBA\n");
return 0;} double operation(double first,double second,char c){
cond=aget(&list[i].result);
if(cond<0){ double x =0;
printf("CHYBA\n");
return 0;} if(c=='*'){
if((fcond=checkend())!='\n' && fcond>0){
printf("CHYBA\n"); x=first*second;
return 0;}
if(fcond==-1) return x;
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++){ else if(c =='-'){
double fin=count(&list[x].na,&list[x].nb,list[x].sign);
//printf("%lf %lf\n",fin,list[x].result); x=first-second;
if((long)(fin*1000000)!=(long)(list[x].result)*1000000){
printf("ZLE\n"); return x;
return 0;
} }
}
printf("OK\n"); else if(c=='/'){
} x=first/second;
char checkend(void){
char c; return x;
int count=1;
c=getchar(); }
while((c=getchar())){
++count; x= first+second;
if(c==EOF || c=='\n'){
int a=c; return x;
if((c=getchar())==EOF){
int b=c; }
if((c=getchar())==EOF){
++count; double round_to(double value, double eps)
fseek(stdin,-count,SEEK_CUR);
return c; {
}
++count; return floor(value/eps + 0.5) * eps;
fseek(stdin,-count,SEEK_CUR);
return b; }
}
++count; double compare_percent(double a, double b, double eps)
fseek(stdin,-count,SEEK_CUR);
return a; {
}
else if(!isspace(c)){ double diff = round_to( (a - b) * 2 / (a + b), eps);
fseek(stdin,-count,SEEK_CUR);
return c; return diff < 0 ? -1 : diff > 0 ? +1 : 0;
}
} }
return -2;
} int check(char *str){
double count(const double*na,const double *nb,char sign){
int flag=0;
if(sign=='+')
return *na+*nb+0.00000000001; char symb[5]="*-+/=";
else if(sign=='-')
return *na-*nb; for(int i =0;i<strlen(str);i++){
else if(sign=='*')
return *na*(*nb); for(int k =0;k<5;k++){
else if(sign=='/')
return *na/(*nb); for(int j =0;j<5;j++){
return -1; 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;
int aget(double *na){
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;
while(isspace(c=getchar()));
if(c=='+' || c=='-' || c=='/' || c=='*')
*sign=c;
else
return -1;
return 1;
}
int efinde(void){
char c;
while(isspace(c=getchar()));
if(c!='=')
return -1;
return 1;
} }