diff --git a/du4/program b/du4/program index e5fc2da..b240720 100755 Binary files a/du4/program and b/du4/program differ diff --git a/du4/program.c b/du4/program.c index cfb85f3..474081f 100644 --- a/du4/program.c +++ b/du4/program.c @@ -5,12 +5,14 @@ #include -int main() { - int i,j; + +int main(){ + + int i,j; double number1, number2, summary,msum; char operand, equ; float rsum=0; - /* char input[1000]; +/* char input[1000]; char num_str[30] = {0}; float numbers[3]; fgets(input, 999, stdin); @@ -37,7 +39,7 @@ int main() { */ char* input = calloc(1000, 1); while (1) { - + input = fgets(input, 1000, stdin); if (input == NULL) break; @@ -45,9 +47,9 @@ int main() { if (input[0] == '\n' || input[0] == EOF || strlen(input) <= 0) break; - float values[3] = {0}; + float values[3] = {0}; char operation = 0; - + int k = 0; int num = strlen(input); for (int i = 0; i < num; i++) { @@ -58,7 +60,7 @@ int main() { if (isalpha(input[i])) break; - + if (i < strlen(input) && (isdigit(input[i]) || input[i] == '.' || (input[i] == '-' && isdigit(input[i + 1])))) { char temp[100] = {0}; for (int j = 0; i < strlen(input) && (isdigit(input[i]) || input[i] == '.' || input[i] == '-'); j++) { @@ -73,54 +75,26 @@ int main() { } } - + if (k != 3) { printf("CHYBA\n"); continue; } + if (operation == 0 && values[1] < 0) { operation = '-'; values[1] = abs(values[1]); } - - float result = 0; - - switch (operation) { - case '+': { - result = values[0] + values[1]; - break; - } - case '-': { - result = values[0] - values[1]; - break; - } - case '/': { - result = values[0] / values[1]; - break; - } - case '*': { - result = values[0] * values[1]; - break; - } - default: { - printf("CHYBA\n"); - continue; - } - } - - - char result_string[50]; - sprintf(result_string, "%.2f", result); - result = atof(result_string); - - if (result == values[2]) - printf("OK\n"); - else - printf("ZLE\n"); - } - if((operand == '/') && (number2 == 0)){ + + float result = 0; + + + +//////////////////////////////////////////////// + + if((operand == '/') && (number2 == 0)){ printf("Nulou sa nedá deliť"); } if(operand !='+' && operand !='-' && operand !='*' && operand !='/'){ @@ -128,22 +102,35 @@ int main() { printf("\n"); } - //msum=values[2]; - if(operand=='+'){ - // rsum=values[0]+values[1]; + msum=values[2]; + if(operation=='+'){ + rsum=values[0]+values[1]; char rmsum[50]; sprintf(rmsum, "%.2f", msum); msum = atof(rmsum); if(msum==rsum){ - //printf("OK\n"); + printf("OK\n"); } else{ - //printf("ZLE\n"); + printf("ZLE\n"); } - //printf("%lf\n",rsum); + printf("%lf\n",rsum); } - else if(operand=='-'){ - // rsum=values[0]-values[1]; + else if(operation=='-'){ + rsum=values[0]-values[1]; + char rmsum[50]; + sprintf(rmsum, "%.2f", msum); + msum = atof(rmsum); + if(msum==rsum){ + printf("OK\n"); + } + else{ + printf("ZLE\n"); + } + printf("%lf\n",rsum); + } + else if(operation=='*'){ + rsum=values[0]*values[1]; char rmsum[50]; sprintf(rmsum, "%.2f", msum); msum = atof(rmsum); @@ -151,37 +138,22 @@ int main() { //printf("OK\n"); } else{ - //printf("ZLE\n"); + printf("ZLE\n"); } - //printf("%lf\n",rsum); + printf("%lf\n",rsum); } - else if(operand=='*'){ - // rsum=values[0]*values[1]; + else if(operation=='/'){ + rsum=values[0]/values[1]; char rmsum[50]; sprintf(rmsum, "%.2f", msum); msum = atof(rmsum); if(msum==rsum){ - //printf("OK\n"); + printf("OK\n"); } else{ - //printf("ZLE\n"); + printf("ZLE\n"); } - //printf("%lf\n",rsum); + printf("%lf\n",rsum); } - else if(operand=='/'){ - // rsum=values[0]/values[1]; - char rmsum[50]; - sprintf(rmsum, "%.2f", msum); - msum = atof(rmsum); - if(msum==rsum){ - //printf("OK\n"); - } - else{ - //printf("ZLE\n"); - } - //printf("%lf\n",rsum); } - free(input); - - return 0; -} + } \ No newline at end of file