Aktualizovat „du4/program.c“

This commit is contained in:
Bohdan Yanchyk 2020-04-02 23:03:29 +00:00
parent 6e56740129
commit 2a07c7ea29

View File

@ -1,62 +1,62 @@
#include <stdio.h> #include <stdio.h>
#include<string.h> #include<string.h>
#include<stdlib.h> #include<stdlib.h>
char* compactString(char *src,int leng) { char* compactString(char *src,int leng) {
char *new_str = (char*)calloc(leng+1,sizeof(char)); char *new_str = (char*)calloc(leng+1,sizeof(char));
int k=0; int k=0;
for(int i=0;i<leng;i++){ for(int i=0;i<leng;i++){
if(src[i]!=' '){ if(src[i]!=' '){
new_str[k] = src[i]; new_str[k] = src[i];
k++; k++;
} }
} }
return new_str; return new_str;
} }
double operation(double first,double second,char c){ double operation(double first,double second,char c){
if(c=='*'){ if(c=='*'){
return first*second; return first*second;
} }
else if(c =='-'){ else if(c =='-'){
return first-second; return first-second;
} }
else if(c=='/'){ else if(c=='/'){
return first/second; return first/second;
} }
return first+second; return first+second;
} }
int main() int main()
{ {
char str[100]; char str[100];
memset(str,0,100); memset(str,0,100);
fgets(str,100,stdin); fgets(str,100,stdin);
char *new_str = compactString(str,strlen(str)); char *new_str = compactString(str,strlen(str));
printf("%s\n",new_str); printf("%s\n",new_str);
double first =-5,second =-5,res=-5; double first =-5,second =-5,res=-5;
char c='E'; char c='E';
sscanf(new_str,"%lf%c%lf%*[=]%lf",&first,&c,&second,&res); sscanf(new_str,"%lf%c%lf%*[=]%lf",&first,&c,&second,&res);
printf("%lf %lf %lf",first,second,res); printf("%lf %lf %lf",first,second,res);
if(c=='E'||first==-5||second==-5||res==-5){ if(c=='E'||first==-5||second==-5||res==-5){
printf("CHYBA\n"); printf("CHYBA\n");
return 0; return 0;
} }
double my_res = operation(first,second,c); double my_res = operation(first,second,c);
if(my_res==res){ if(my_res==res){
printf("OK\n"); printf("OK\n");
} }
else{ else{
printf("ZLE\n"); printf("ZLE\n");
} }
return 0; return 0;
} }