du3
This commit is contained in:
parent
a6a8b725d2
commit
fea4c4881b
1
du3/README.md
Normal file
1
du3/README.md
Normal file
@ -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.
|
||||||
80
du3/program.c
Normal file
80
du3/program.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user