pvjc24/a1/program.c

63 lines
1.4 KiB
C
Raw Normal View History

2024-03-27 13:34:46 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main() {
char line[100];
double num1, num2, result, expected_result;
char op;
2024-03-27 13:37:04 +00:00
// Read the input line
fgets(line, sizeof(line), stdin);
// Remove any spaces between symbols
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];
2024-03-27 13:34:46 +00:00
}
2024-03-27 13:37:04 +00:00
i--; // Move back one step to recheck the current character
2024-03-27 13:34:46 +00:00
}
2024-03-27 13:37:04 +00:00
}
2024-03-27 13:34:46 +00:00
2024-03-27 13:37:04 +00:00
// Parse the line
sscanf(line, "%lf%c%lf=%lf", &num1, &op, &num2, &expected_result);
2024-03-27 13:34:46 +00:00
2024-03-27 13:37:04 +00:00
// Check if the input is valid
if (op != '+' && op != '-' && op != '*' && op != '/') {
printf("CHYBA\n");
return 1;
}
2024-03-27 13:34:46 +00:00
2024-03-27 13:37:04 +00:00
// Check for division by zero
if (op == '/' && num2 == 0) {
2024-03-27 13:39:22 +00:00
printf("ZLE\n");
2024-03-27 13:37:04 +00:00
return 1;
}
2024-03-27 13:34:46 +00:00
2024-03-27 13:37:04 +00:00
// 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;
}
2024-03-27 13:34:46 +00:00
2024-03-27 13:37:04 +00:00
// Check if the result is correct
if (result == expected_result) {
printf("OK\n");
} else {
printf("ZLE\n");
2024-03-27 13:34:46 +00:00
}
return 0;
2024-03-27 13:37:04 +00:00
}