From 3cc550a97bbedf3bc07ac818b9b3afe78b835be6 Mon Sep 17 00:00:00 2001 From: ak643du Date: Wed, 27 Mar 2024 14:37:04 +0100 Subject: [PATCH] Initialization --- a1/program.c | 96 +++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/a1/program.c b/a1/program.c index 0b07f64..f112c4b 100644 --- a/a1/program.c +++ b/a1/program.c @@ -8,58 +8,56 @@ int main() { double num1, num2, result, expected_result; char op; - while (fgets(line, sizeof(line), stdin) != NULL) { - num1 = num2 = result = 0; - op = '\0'; + // 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]; - } - i--; // Move back one step to recheck the current character + // 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]; } - } - - // Parse the line - sscanf(line, "%lf%c%lf=%lf", &num1, &op, &num2, &expected_result); - - // Check if the input is valid - if (op != '+' && op != '-' && op != '*' && op != '/') { - printf("CHYBA\n"); - continue; - } - - // Check for division by zero - if (op == '/' && num2 == 0) { - printf("CHYBA\n"); - continue; - } - - // 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"); + i--; // Move back one step to recheck the current character } } + // Parse the line + sscanf(line, "%lf%c%lf=%lf", &num1, &op, &num2, &expected_result); + + // Check if the input is valid + if (op != '+' && op != '-' && op != '*' && op != '/') { + printf("CHYBA\n"); + return 1; + } + + // Check for division by zero + if (op == '/' && num2 == 0) { + printf("CHYBA\n"); + return 1; + } + + // 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; -} +} \ No newline at end of file