Initialization#

This commit is contained in:
Kozar 2024-03-27 14:44:36 +01:00
parent a7e68d397f
commit 272be6c4b2

View File

@ -8,55 +8,62 @@ int main() {
double num1, num2, result, expected_result; double num1, num2, result, expected_result;
char op; char op;
// Read the input line while (fgets(line, sizeof(line), stdin) != NULL) {
fgets(line, sizeof(line), stdin); if (line[0] == '\n') {
// Stop when the user enters a blank line
// Remove any spaces between symbols break;
for (int i = 0; line[i] != '\0'; i++) {
if (isspace(line[i])) {
for (int j = i; line[j] != '\0'; j++) {
line[j] = line[j + 1];
}
i--; // Move back one step to recheck the current character
} }
}
// Parse the line num1 = num2 = result = 0;
sscanf(line, "%lf%c%lf=%lf", &num1, &op, &num2, &expected_result); op = '\0';
// Check if the input is valid // Remove any spaces between symbols
if (op != '+' && op != '-' && op != '*' && op != '/') { for (int i = 0; line[i] != '\0'; i++) {
printf("CHYBA\n"); if (isspace(line[i])) {
return 1; for (int j = i; line[j] != '\0'; j++) {
} line[j] = line[j + 1];
}
i--; // Move back one step to recheck the current character
}
}
// Check for division by zero // Parse the line
if (op == '/' && num2 == 0) { sscanf(line, "%lf%c%lf=%lf", &num1, &op, &num2, &expected_result);
printf("ZLE\n");
return 0;
}
// Perform the calculation // Check if the input is valid
switch (op) { if (op != '+' && op != '-' && op != '*' && op != '/') {
case '+': printf("CHYBA\n");
result = num1 + num2; continue;
break; }
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
// Check if the result is correct // Check for division by zero
if (result == expected_result) { if (op == '/' && num2 == 0) {
printf("OK\n"); printf("ZLE\n");
} else { continue;
printf("ZLE\n"); }
// Perform the calculation
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
// Check if the result is correct
if (result == expected_result) {
printf("OK\n");
} else {
printf("ZLE\n");
}
} }
return 0; return 0;