pvjc20/du4/program.c

138 lines
3.0 KiB
C

/*
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{
double na,nb,result;
char sign;
}list [10];
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);
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' && 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){
++count;
fseek(stdin,-count,SEEK_CUR);
return c;
}
++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){
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;
}