This commit is contained in:
Peter Sabol 2022-03-31 10:05:03 +02:00
parent f90fcf9a00
commit a0efe44b76
2 changed files with 104 additions and 34 deletions

Binary file not shown.

View File

@ -2,75 +2,145 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <ctype.h>
int main(){ int main(){
int i,j; // pro cyklus int i,j;
double number1, number2, summary, rsum, msum; /*aby se mohlo poèítat s desetinnými èísly*/ double number1, number2, summary, rsum, msum;
char operand, equ; //naèítání znakù char operand, equ;
/* char input[1000];
scanf("%lf", &number1); // nacte prvni cislo, znamenko, druhe cislo char num_str[30] = {0};
scanf("%c", &operand); float numbers[3];
scanf("%lf", &number2); fgets(input, 999, stdin);
scanf("%c", &equ); for (int i = 0, j =0; i < strlen(input); i++) {
scanf("%lf",&msum); char c = input[i];
if (c==' ') continue;
if (c=='+') operand='+';
if (c=='-') operand='-';
if (c=='*') operand='*';
if (c=='/') operand='/';
if (c=='\n') break;
if (isdigit(c)) {num_str[j++] = c - '0';}
else if (j > 0) {
j = 0;
numbers[...] = atof(num_str);
}
}
}
// scanf("%lf", &number1);
//scanf("%lf", &number2);
//scanf("%lf",&msum);
/*pokud se operand rovna deleni a delitel se rovna nule vypise chybu*/ */
if((operand == '/') && (number2 == 0)){ char* input = calloc(1000, 1);
while (1) {
input = fgets(input, 1000, stdin);
if (input == NULL) break;
fflush(stdin);
if (input[0] == '\n' || input[0] == EOF || strlen(input) <= 0)
break;
float values[3] = {0};
char operation = 0;
int k = 0;
int num = strlen(input);
for (int i = 0; i < num; i++) {
if (input[i] == ' ')
continue;
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++) {
temp[j] = input[i++];
}
i--;
values[k++] = (float)atof(temp);
} else if (isdigit(input[i]) == 0 && input[i] != ' ' && input[i] != '\n' && input[i] != '=') {
operation = input[i];
}
}
if (k != 3) {
printf("CHYBA\n");
continue;
}
if (operation == 0 && values[1] < 0) {
operation = '-';
values[1] = abs(values[1]);
}
float result = 0;
////////////////////////////////////////////////
/*if((operand == '/') && (number2 == 0)){
printf("Nulou sa nedá deli<6C>"); printf("Nulou sa nedá deli<6C>");
} }
if(operand !='+' && operand !='-' && operand !='*' && operand !='/'){ if(operand !='+' && operand !='-' && operand !='*' && operand !='/'){
return 1; return 1;
printf("\n"); printf("\n");
} }
if(equ !='='){ */
return 1; msum=values[2];
printf("\n"); if(operation=='+'){
rsum=values[0]+values[1];
}
else if(operand=='+'){
rsum=number1+number2;
if(msum==rsum){ if(msum==rsum){
printf("OK\n"); printf("OK\n");
} }
else{ else{
printf("ZLE\n"); printf("ZLE\n");
} }
printf("%lf\n",rsum); //printf("%lf\n",rsum);
} }
else if(operand=='-'){ else if(operation=='-'){
rsum=number1-number2; rsum=values[0]-values[1];
if(msum==rsum){ if(msum==rsum){
printf("OK\n"); printf("OK\n");
} }
else{ else{
printf("ZLE\n"); printf("ZLE\n");
} }
printf("%lf\n",rsum); //printf("%lf\n",rsum);
} }
else if(operand=='*'){ else if(operation=='*'){
rsum=number1*number2; rsum=values[0]*values[1];
if(msum==rsum){ if(msum==rsum){
printf("OK\n"); printf("OK\n");
} }
else{ else{
printf("ZLE\n"); printf("ZLE\n");
} }
printf("%lf\n",rsum); //printf("%lf\n",rsum);
} }
else if(operand=='/'){ else if(operation=='/'){
rsum=number1/number2; rsum=values[0]/values[1];
if(msum==rsum){ if(msum==rsum){
printf("OK\n"); printf("OK\n");
} }
else{ else{
printf("ZLE\n"); printf("ZLE\n");
} }
printf("%lf\n",rsum); //printf("%lf\n",rsum);
}
} }
} }