Aktualizovat „du4/program.c“

This commit is contained in:
Pavlo Tverdyi 2020-04-02 13:21:51 +00:00
parent 4273744aef
commit 08b68b2eab

View File

@ -3,48 +3,83 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
char add(double A, double B, double C,char AC){
float CC=A+B;
if(CC==(float)C || A+B==C)return printf("OK\n");
return printf("ZLE\n"); void add(double a, double b, double c,char ac, int result[],int count){
float cc=a+b;
if(cc==(float)c || a+b==c) result[count]=1;
else result[count]=2;
} }
char subtr(double A, double B, double C,char AC){
if(A-B==C)return printf("OK\n"); double subtr(double a, double b, double c,char ac, int result[],int count){
return printf("ZLE\n"); if(a-b==c) result[count]=1;
else result[count]=2;
} }
char multi(double A, double B, double C,char AC){
if(A*B==C)return printf("OK\n"); double multi(double a, double b, double c,char ac, int result[],int count){
return printf("ZLE\n"); if(a*b==c) result[count]=1;
else result[count]=2;
} }
char divi(double A, double B, double C,char AC){
float CC=A/B; double divi(double a, double b, double c,char ac, int result[],int count){
double Cc=(int)((A/B)*100000); float cc=a/b;
if(CC==C || A/B==C || Cc==(int)(C*100000))return printf("OK\n"); double Cc=(int)((a/b)*100000);
return printf("ZLE\n"); if(Cc==c || a/b==c || cc==(int)(c*100000)) result[count]=1;
else result[count]=2;
} }
int main() int main()
{ {
char *str; char *str;
str=(char*)malloc(100); int *result;
double a,b,c; result=(int*)malloc(100);
char ac,el; double a,b,c;
int ind=0; char ac,el=0;
for(int i=0;el!='\n';i++){ int count=0;
el=getchar(); int eof;
if(isspace(el)){ for(;eof!=EOF;count++){
str=(char*)malloc(20);
eof=scanf("%s",str);
int ind=0;
for(int i=0;i<strlen(str);i++){
if(isspace(str[i])){
continue; continue;
} }
str[ind]=el; str[ind]=str[i];
ind++; ind++;
}
sscanf(str,"%lF%c%lF=%lF ",&a,&ac,&b,&c);
switch(ac){
case '+':
add(a,b,c,ac,result,count);
break;
case '-':
subtr(a,b,c,ac,result,count);
break;
case '*':
multi(a,b,c,ac,result,count);
break;
case '/':
divi(a,b,c,ac,result,count);
break;
default:
result[count]=3;
}
free(str);
} }
sscanf(str,"%lF%c%lF=%lF ",&a,&ac,&b,&c); count--;
if(ac=='+')add(a,b,c,ac); for(int j=0;j<count;j++){
else if(ac=='-')subtr(a,b,c,ac); if(result[j]==1) printf("OK\n");
else if(ac=='*')multi(a,b,c,ac); if(result[j]==2) printf("ZLE\n");
else if(ac=='/')divi(a,b,c,ac); if(result[j]==3) printf("CHYBA\n");
else printf("CHYBA\n"); }
return 0; return 0;
} }