pvjc20/du4/program.c

128 lines
2.7 KiB
C
Raw Normal View History

2020-04-02 23:03:29 +00:00
#include <stdio.h>
2020-04-03 07:12:50 +00:00
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
2020-04-02 23:03:29 +00:00
2020-04-03 07:12:50 +00:00
struct res{
double na,nb,result;
char sign;
}list [10];
2020-04-02 23:03:29 +00:00
2020-04-03 07:12:50 +00:00
int fill(double*,double*,char *,double *);
double count(const double*,const double *,char);
int aget(double *);
int sget(char *);
int efinde(void);
char checkend(void);
2020-04-02 23:03:29 +00:00
2020-04-03 07:12:50 +00:00
int main(){
int cond;
char fcond=1;
int i;
for(i=0;fcond!=EOF;i++){
fcond=checkend();
if(fcond==EOF)
break;
cond=aget(&list[i].na);
if(cond<0){
printf("CHYBA\n");
return 0;}
cond=sget(&list[i].sign);
if(cond<0){
printf("CHYBA\n");
return 0;}
cond=aget(&list[i].nb);
if(cond<0){
printf("CHYBA\n");
return 0;}
cond=efinde();
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'){
printf("1CHYBA %d\n",(int)fcond);
return 0;}*/
if(fcond==-1)
break;
//printf("%lf %c %lf = %lf\n",list[i].na,list[i].sign,list[i].nb,list[i].result);
2020-04-02 23:03:29 +00:00
}
2020-04-03 07:12:50 +00:00
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;
}
2020-04-02 23:03:29 +00:00
}
2020-04-03 07:12:50 +00:00
printf("OK\n");
2020-04-02 23:03:29 +00:00
}
2020-04-03 07:12:50 +00:00
char checkend(void){
char c;
int count=1;
c=getchar();
while((c=getchar())){
++count;
if(c==EOF || c=='\n'){
fseek(stdin,-count,SEEK_CUR);
return c;
2020-04-03 00:22:16 +00:00
}
2020-04-03 07:12:50 +00:00
else if(!isspace(c)){
fseek(stdin,-count,SEEK_CUR);
return c;
2020-04-03 00:29:44 +00:00
}
2020-04-03 00:22:16 +00:00
}
2020-04-03 07:12:50 +00:00
return -2;
2020-04-03 00:22:16 +00:00
}
2020-04-03 07:12:50 +00:00
double count(const double*na,const double *nb,char sign){
2020-04-02 23:03:29 +00:00
2020-04-03 07:12:50 +00:00
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);
2020-04-02 23:03:29 +00:00
2020-04-03 07:12:50 +00:00
return -1;
2020-04-02 23:56:53 +00:00
}
2020-04-03 00:06:25 +00:00
2020-04-03 07:12:50 +00:00
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;
2020-04-02 23:45:12 +00:00
}