diff --git a/du3/README.md b/du3/README.md new file mode 100644 index 0000000..fb5ab1f --- /dev/null +++ b/du3/README.md @@ -0,0 +1 @@ +Na túto úlohu som použil copilot a Claude Haiku 4.5, aby som si ujasnil, čo je 'parse' a ako ho používať. Použil som ho aj na preklad úlohy. \ No newline at end of file diff --git a/du3/program.c b/du3/program.c new file mode 100644 index 0000000..7ff70c3 --- /dev/null +++ b/du3/program.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +int main() { + + char line[256]; + + while (fgets(line, sizeof(line), stdin) != NULL) { + //nothing + line[strcspn(line, "\n")] = 0; +//empty line + + if (strlen(line) == 0) { + break; + } + + //nospace + char pusto[256]; + int j = 0; + for (int i = 0; line[i] != '\0'; i++) { + if (!isspace(line[i]) ) { + + pusto[j = j + 1] = line[i]; + } + } + pusto[j] = '\0'; + + // Parse + double n1; + double n2; + double expect; + char op; + int parsed = sscanf(pusto, "%lf%c%lf=%lf", &n1, &op, &n2, &expect); + + if (parsed != 4) { + printf("CHYBA\n"); + continue; + } +// is valid + if (op != '+' && op != '-' && op != '*' && op != '/') { + printf("CHYBA\n"); + continue; + } + + // resultat + double teraz; + switch (op) { + case '+': + teraz = n1 + n2; + break; + case '-': + teraz = n1 - n2; + break; + case '*': + teraz = n1 * n2; + break; + case '/': + if (n2 == 0.0) { + printf("CHYBA\n"); + continue; + } + teraz = n1 / n2; + break; + } + + // zaokrouhleni + double rounded_teraz = round(teraz * 100.0) / 100.0; + double rounded_expect = round(expect * 100.0) / 100.0; + + if (fabs(rounded_teraz - rounded_expect) < 1e-9) { + printf("OK\n"); + } else { + printf("ZLE\n"); + } + } + return 0; +}