Initialization

This commit is contained in:
Kozar 2024-03-27 14:37:04 +01:00
parent ddb74790ad
commit 3cc550a97b

View File

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