Aktualizovat „du4/program.c“

This commit is contained in:
Bohdan Yanchyk 2020-04-03 07:12:50 +00:00
parent 16dd2264e8
commit f01d7d2a30

View File

@ -1,100 +1,128 @@
#include <stdio.h>
#include<string.h>
#include<math.h>
#include <stdarg.h>
#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;
}
#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);
double operation(double first,double second,char c){
double x =0;
if(c=='*'){
x=first*second;
return x;
}
else if(c =='-'){
x=first-second;
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'){
int main(){
int cond;
char fcond=1;
int i;
for(i=0;fcond!=EOF;i++){
fcond=checkend();
if(fcond==EOF)
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){
cond=aget(&list[i].na);
if(cond<0){
printf("CHYBA\n");
continue;
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);
}
double my_res = operation(first,second,c);
if(compare_percent(res,my_res,0.01)==0){
printf("OK\n");
}
else{
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'){
fseek(stdin,-count,SEEK_CUR);
return c;
}
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;
}