diff --git a/du4/program.c b/du4/program.c index ea71802..e14ab87 100644 --- a/du4/program.c +++ b/du4/program.c @@ -1,158 +1,77 @@ -#include -#include -#include -#include -#include -char array[100]; -char array1[100]; -char number1[100]; -char number2[100]; -char answer[100]; -int p=0; -float zero=0; -char znak1; -char znak2; -float firstN=0; -float secondN=0; -float answer1=0; -float myAnswer=0; -char c; -char r; -char q; -int count = 0; -int scan(char* array2){ -char* r = fgets(array1,100,stdin); -if (r == NULL){ - return -1; -} - else{ - return 0; - } -} +#include +#include +struct try +{ + double number1; + char sign; + double number2; + double final; +}; +void ok(); +void zle(); int main(){ -scan(array1); -int i=0; -while(i<100){ - i=i+1; - if(array1[i]!='\n'){ - count=count+1; - } - if(array1[i]=='\n'){ - if(array1[i+1]=='\n') - { - array1[i]='#'; - break; - } - } - else - { - count=count+1; - } +struct try y1[20]; +int idx =0; +while (5){ + int r = scanf("%lf", &y1[idx].number1); + if (r == -1){ + break; + } + scanf(" %c ", &y1[idx].sign); + scanf("%lf", &y1[idx].number2); + scanf(" = %lf",&y1[idx].final); + idx=idx+1; } -i=0; -int j=0; -int t=0; -while(array1[t]!='#'){ - array[p]=array1[t]; - if(array1[t]=='='){ - t=t+1; - p=p+1; - if(array1[t]==' '){ - array[p]=array1[t]; - t=t+1; - p=p+1; - } - while(array1[t]!=' ') - { - if(array1[t]=='#'){ - break; - } - array[p]=array1[t]; - t=t+1; - p=p+1; - } - while(array[i]!='\0'){ - if(array[i]=='+'||array[i]=='-'||array[i]=='/'||array[i]=='*'){ - break; - } - else if(array[i]=='1'||array[i]=='2'||array[i]=='3'||array[i]=='4'||array[i]=='5'||array[i]=='6'||array[i]=='7'||array[i]=='8'||array[i]=='9'||array[i]=='0'||array[i]=='.'){ - number1[j]= array[i]; - i=i+1; - j=j+1; - } - else if(array[i]==' ' || isalpha(array[i])){i++;} - else{ - printf("CHYBA\n"); - return 0; - } - } - znak1=array[i]; - i=i+1; - j=0; - while(array[i]!='='){ - if(array[i]=='1'||array[i]=='2'||array[i]=='3'||array[i]=='4'||array[i]=='5'||array[i]=='6'||array[i]=='7'||array[i]=='8'||array[i]=='9'||array[i]=='0'||array[i]=='.'){ - number2[j]=array[i]; - j=j+1; - i=i+1; - } - else if(array[i]==' '){ - i=i+1; - } - else{ - i=i+1; - p=p+1; - } +int nikita=0; +while (nikita < idx){ + idx=idx+1; + if(y1[nikita].sign == '+'){ + if (round((y1[nikita].number1 + y1[nikita].number2)*100)/100 == y1[nikita].final) + { + ok(); + break; + } else{ + zle(); + break; + } + } + if(y1[nikita].sign == '-'){ + if (round((y1[nikita].number1 - y1[nikita].number2)*100)/100 == y1[nikita].final){ + ok(); + break; + } else{ + zle(); + break; + } + } + if(y1[nikita].sign == '*'){ + if (round(y1[nikita].number1 * y1[nikita].number2 * 100) / 100 == y1[nikita].final) + { + ok(); + break; + } else + { + zle(); + break; + } + + } + if(y1[nikita].sign == '/'){ + if (round(y1[nikita].number1 / y1[nikita].number2 * 100) / 100 == y1[nikita].final) + { + ok(); + break; + } else{ + zle(); + break; + } + } } - znak2=array[i]; - i++; - j=0; - j=0; -while(array[i]=='1'||array[i]=='2'||array[i]=='3'||array[i]=='4'||array[i]=='5'||array[i]=='6'||array[i]=='7'||array[i]=='8'||array[i]=='9'||array[i]=='0'||array[i]==' '||array[i]=='.'){ - if(array[i]=='1'||array[i]=='2'||array[i]=='3'||array[i]=='4'||array[i]=='5'||array[i]=='6'||array[i]=='7'||array[i]=='8'||array[i]=='9'||array[i]=='0'||array[i]=='.'){ - answer[j]=array[i]; - i=i+1; - j=j+1; - } - else if(array[i]==' '){ - i=i+1; - }else{ - i=i+1; - p=p+1; - } - } - j=0; - float firstN = atof(number1); - float secondN = atof(number2); - float answer1 = atof(answer); - if(znak1=='-'){ - myAnswer=firstN-secondN; - } - else if(znak1=='+'){ - myAnswer=firstN+secondN; - } - else if(znak1=='/'){ - myAnswer=firstN/secondN; - } - else if(znak1=='*'){ - myAnswer=firstN*secondN; - } - zero = myAnswer-answer1; - if(zero>=-0.00001&&zero<=0.00001){ + return 0; +} +void ok(){ printf("OK\n"); - } - else{ - printf("ZLE\n"); - } - i=0; - p=-1; - memset(array,0,100); - } - p=p+1; - t=t+1; - scanf("\n%c",&q); - if(c=='\n'){ - return 0; - } } - return 0; +void zle(){ + printf("ZLE\n"); } \ No newline at end of file