Compare commits
14 Commits
fe80bfc2fe
...
9adde41415
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9adde41415 | ||
![]() |
8781656616 | ||
![]() |
caf2165e43 | ||
![]() |
e47ce85cf5 | ||
![]() |
710747013d | ||
![]() |
a509424d19 | ||
![]() |
ba57cdda4e | ||
![]() |
0b0f66ddd5 | ||
![]() |
372cdc6be8 | ||
![]() |
11b00c4195 | ||
![]() |
02507b30d5 | ||
![]() |
2a857d1efe | ||
![]() |
c57c78754b | ||
![]() |
f790d3968b |
@ -1,4 +1,3 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -10,14 +9,8 @@
|
||||
#define debug(fmt, ...) fprintf(stdout, "[DEBUG %s:%d %s()] " fmt "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
bool is_numeric(const char* str) {
|
||||
if(*str == '\0') return false;
|
||||
|
||||
for(;*str; str++) {
|
||||
if(!isdigit(*str))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
bool is_in_rgb_range(int color) {
|
||||
return color >= 0 && color <= 255;
|
||||
}
|
||||
|
||||
int invalid_input() {
|
||||
@ -26,73 +19,22 @@ int invalid_input() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
char* input = NULL;
|
||||
size_t len;
|
||||
|
||||
int rgb[3];
|
||||
int r = -1, g = -1, b = -1;
|
||||
|
||||
printf("Zadejte barvu v RGB formatu:\n");
|
||||
|
||||
ssize_t chars_read = getline(&input, &len, stdin);
|
||||
int inputs_read = scanf(" rgb ( %d , %d , %d )", &r, &g, &b);
|
||||
|
||||
if(chars_read == -1) {
|
||||
printf("Failed to read from stdin\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Strip out any whitespaces
|
||||
char* dst = input;
|
||||
for(char * src = input; *src; src++) {
|
||||
if(!isspace(*src)) {
|
||||
*dst++ = *src;
|
||||
}
|
||||
}
|
||||
*dst = '\0';
|
||||
|
||||
int trimmed_len = strlen(input);
|
||||
|
||||
// Check whether the provided input
|
||||
// starts with rgb(
|
||||
// and ends with )
|
||||
if(strncmp(input, "rgb(", 4) != 0 || input[trimmed_len - 1] != ')') {
|
||||
debug("%s not in rgb(xxx) format", input);
|
||||
if(inputs_read != 3) {
|
||||
debug("Not enough inputs read");
|
||||
return invalid_input();
|
||||
}
|
||||
|
||||
input[trimmed_len - 1] = '\0';
|
||||
char* slice = input + 4;
|
||||
|
||||
char* token = strtok(slice, ",");
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
// strtok reached end prematurely
|
||||
if(token == NULL) {
|
||||
debug("Too few numbers");
|
||||
return invalid_input();
|
||||
}
|
||||
|
||||
// Not a number
|
||||
if(!is_numeric(token)) {
|
||||
debug("%s is not a number", token);
|
||||
return invalid_input();
|
||||
}
|
||||
|
||||
int parsed = atoi(token);
|
||||
|
||||
if(parsed < 0 || parsed > 255) {
|
||||
debug("%d out of RGB range", parsed);
|
||||
return invalid_input();
|
||||
}
|
||||
|
||||
rgb[i] = parsed;
|
||||
token = strtok(NULL, ",");
|
||||
}
|
||||
|
||||
if(token != NULL) {
|
||||
debug("Too many numbers");
|
||||
if(!is_in_rgb_range(r) || !is_in_rgb_range(g) || !is_in_rgb_range(b)) {
|
||||
debug("Number not in range of rgb [0, 255]");
|
||||
return invalid_input();
|
||||
}
|
||||
|
||||
printf("#%02X%02X%02X\n", rgb[0], rgb[1], rgb[2]);
|
||||
printf("#%02X%02X%02X\n", r, g, b);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
104
exercise-04-multiplication-table/README.md
Normal file
104
exercise-04-multiplication-table/README.md
Normal file
@ -0,0 +1,104 @@
|
||||
Úkolem je realizovat program, který zobrazí tabulku pro násobilku.
|
||||
|
||||
Vstupem programu je celé číslo - mez n.
|
||||
|
||||
Výstupem programu je tabulka násobilky v rozsahu 1x1 až n x n. Tabulka je zobrazena bez duplicitních hodnot, tedy v trojúhelníkové podobě. Všechny sloupce mají stejnou šířku, šířka je dána počtem číslic v největším součinu v tabulce, zvětšeným o 1. Za posledním číslem na řádce následuje odřádkování, tedy na koncích řádky nejsou žádné bílé znaky navíc.
|
||||
|
||||
Pokud je vstup neplatný, program to musí detekovat a zobrazit chybové hlášení. Chybové hlášení zobrazujte na standardní výstup (ne na chybový výstup). Za chybu považujte:
|
||||
|
||||
- na vstupu není platné celé číslo,
|
||||
- číslo na vstupu je záporné nebo nulové.
|
||||
|
||||
**Ukázka práce programu:**
|
||||
|
||||
```
|
||||
Rozsah:
|
||||
10
|
||||
| 10 9 8 7 6 5 4 3 2 1
|
||||
---+----------------------------------------
|
||||
1| 10 9 8 7 6 5 4 3 2 1
|
||||
2| 20 18 16 14 12 10 8 6 4
|
||||
3| 30 27 24 21 18 15 12 9
|
||||
4| 40 36 32 28 24 20 16
|
||||
5| 50 45 40 35 30 25
|
||||
6| 60 54 48 42 36
|
||||
7| 70 63 56 49
|
||||
8| 80 72 64
|
||||
9| 90 81
|
||||
10| 100
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Rozsah:
|
||||
8
|
||||
| 8 7 6 5 4 3 2 1
|
||||
--+------------------------
|
||||
1| 8 7 6 5 4 3 2 1
|
||||
2| 16 14 12 10 8 6 4
|
||||
3| 24 21 18 15 12 9
|
||||
4| 32 28 24 20 16
|
||||
5| 40 35 30 25
|
||||
6| 48 42 36
|
||||
7| 56 49
|
||||
8| 64
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Rozsah:
|
||||
32
|
||||
| 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
|
||||
----+----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
1| 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
|
||||
2| 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4
|
||||
3| 96 93 90 87 84 81 78 75 72 69 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9
|
||||
4| 128 124 120 116 112 108 104 100 96 92 88 84 80 76 72 68 64 60 56 52 48 44 40 36 32 28 24 20 16
|
||||
5| 160 155 150 145 140 135 130 125 120 115 110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25
|
||||
6| 192 186 180 174 168 162 156 150 144 138 132 126 120 114 108 102 96 90 84 78 72 66 60 54 48 42 36
|
||||
7| 224 217 210 203 196 189 182 175 168 161 154 147 140 133 126 119 112 105 98 91 84 77 70 63 56 49
|
||||
8| 256 248 240 232 224 216 208 200 192 184 176 168 160 152 144 136 128 120 112 104 96 88 80 72 64
|
||||
9| 288 279 270 261 252 243 234 225 216 207 198 189 180 171 162 153 144 135 126 117 108 99 90 81
|
||||
10| 320 310 300 290 280 270 260 250 240 230 220 210 200 190 180 170 160 150 140 130 120 110 100
|
||||
11| 352 341 330 319 308 297 286 275 264 253 242 231 220 209 198 187 176 165 154 143 132 121
|
||||
12| 384 372 360 348 336 324 312 300 288 276 264 252 240 228 216 204 192 180 168 156 144
|
||||
13| 416 403 390 377 364 351 338 325 312 299 286 273 260 247 234 221 208 195 182 169
|
||||
14| 448 434 420 406 392 378 364 350 336 322 308 294 280 266 252 238 224 210 196
|
||||
15| 480 465 450 435 420 405 390 375 360 345 330 315 300 285 270 255 240 225
|
||||
16| 512 496 480 464 448 432 416 400 384 368 352 336 320 304 288 272 256
|
||||
17| 544 527 510 493 476 459 442 425 408 391 374 357 340 323 306 289
|
||||
18| 576 558 540 522 504 486 468 450 432 414 396 378 360 342 324
|
||||
19| 608 589 570 551 532 513 494 475 456 437 418 399 380 361
|
||||
20| 640 620 600 580 560 540 520 500 480 460 440 420 400
|
||||
21| 672 651 630 609 588 567 546 525 504 483 462 441
|
||||
22| 704 682 660 638 616 594 572 550 528 506 484
|
||||
23| 736 713 690 667 644 621 598 575 552 529
|
||||
24| 768 744 720 696 672 648 624 600 576
|
||||
25| 800 775 750 725 700 675 650 625
|
||||
26| 832 806 780 754 728 702 676
|
||||
27| 864 837 810 783 756 729
|
||||
28| 896 868 840 812 784
|
||||
29| 928 899 870 841
|
||||
30| 960 930 900
|
||||
31| 992 961
|
||||
32| 1024
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Rozsah:
|
||||
asdf
|
||||
Nespravny vstup.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Poznámky:
|
||||
|
||||
- Ukázkové běhy zachycují očekávané výpisy Vašeho programu (tučné písmo) a vstupy zadané uživatelem (základní písmo). Zvýraznění tučným písmem je použité pouze zde na stránce zadání, aby byl výpis lépe čitelný. Váš program má za úkol zobrazit text bez zvýrazňování (bez HTML markupu).
|
||||
- Znak odřádkování (\n) je i za poslední řádkou výstupu (i za případným chybovým hlášením).
|
||||
- Při programování si dejte pozor na přesnou podobu výpisů. Výstup Vašeho programu kontroluje stroj, který požaduje přesnou shodu výstupů Vašeho programu s výstupy referenčními. Za chybu je považováno, pokud se výpis liší. I chybějící nebo přebývající mezera/odřádkování je považováno za chybu. Abyste tyto problémy rychle vyloučili, použijte přiložený archiv se sadou vstupních a očekávaných výstupních dat. Podívejte se na videotutoriál (Courses -> Video tutoriály), jak testovací data použít a jak testování zautomatizovat.
|
||||
- Výstup programu může být velmi široký/dlouhý, vyplatí se výstup přesměrovat do souboru a ten si prohlížet v nějakém editoru.
|
61
exercise-04-multiplication-table/main.c
Normal file
61
exercise-04-multiplication-table/main.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int range = -1;
|
||||
|
||||
printf("Rozsah:\n");
|
||||
if (scanf("%d", &range) != 1 || range <= 0) {
|
||||
printf("Nespravny vstup.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int largest_number = range * range;
|
||||
int largest_digit_count = 0;
|
||||
int range_digit_count = 0;
|
||||
|
||||
|
||||
int tmp = largest_number;
|
||||
while(tmp != 0) {
|
||||
largest_digit_count++;
|
||||
tmp /= 10;
|
||||
}
|
||||
|
||||
tmp = range;
|
||||
while(tmp != 0) {
|
||||
range_digit_count++;
|
||||
tmp /= 10;
|
||||
}
|
||||
|
||||
// Header
|
||||
for(int i = range + 1; i > 0; i--) {
|
||||
if(i == range + 1) {
|
||||
printf("%*s|", largest_digit_count, "");
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("%*d", largest_digit_count + 1, i);
|
||||
}
|
||||
putchar('\n');
|
||||
|
||||
// Separator
|
||||
for(int j = 0; j < largest_digit_count; j++) {
|
||||
putchar('-');
|
||||
}
|
||||
putchar('+');
|
||||
|
||||
for(int j = 0; j < (largest_digit_count + 1) * range; j++) {
|
||||
putchar('-');
|
||||
}
|
||||
putchar('\n');
|
||||
|
||||
// Table lines
|
||||
for(int line = 1; line <= range; line++) {
|
||||
printf("%*d|", largest_digit_count, line);
|
||||
|
||||
for(int column = range; column >= line; column--) {
|
||||
printf("%*d", largest_digit_count + 1, column * line);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
1
exercise-04-multiplication-table/test_data/0000_in.txt
Normal file
1
exercise-04-multiplication-table/test_data/0000_in.txt
Normal file
@ -0,0 +1 @@
|
||||
10
|
13
exercise-04-multiplication-table/test_data/0000_out.txt
Normal file
13
exercise-04-multiplication-table/test_data/0000_out.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Rozsah:
|
||||
| 10 9 8 7 6 5 4 3 2 1
|
||||
---+----------------------------------------
|
||||
1| 10 9 8 7 6 5 4 3 2 1
|
||||
2| 20 18 16 14 12 10 8 6 4
|
||||
3| 30 27 24 21 18 15 12 9
|
||||
4| 40 36 32 28 24 20 16
|
||||
5| 50 45 40 35 30 25
|
||||
6| 60 54 48 42 36
|
||||
7| 70 63 56 49
|
||||
8| 80 72 64
|
||||
9| 90 81
|
||||
10| 100
|
13
exercise-04-multiplication-table/test_data/0000_out_win.txt
Normal file
13
exercise-04-multiplication-table/test_data/0000_out_win.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Rozsah:
|
||||
| 10 9 8 7 6 5 4 3 2 1
|
||||
---+----------------------------------------
|
||||
1| 10 9 8 7 6 5 4 3 2 1
|
||||
2| 20 18 16 14 12 10 8 6 4
|
||||
3| 30 27 24 21 18 15 12 9
|
||||
4| 40 36 32 28 24 20 16
|
||||
5| 50 45 40 35 30 25
|
||||
6| 60 54 48 42 36
|
||||
7| 70 63 56 49
|
||||
8| 80 72 64
|
||||
9| 90 81
|
||||
10| 100
|
1
exercise-04-multiplication-table/test_data/0001_in.txt
Normal file
1
exercise-04-multiplication-table/test_data/0001_in.txt
Normal file
@ -0,0 +1 @@
|
||||
8
|
11
exercise-04-multiplication-table/test_data/0001_out.txt
Normal file
11
exercise-04-multiplication-table/test_data/0001_out.txt
Normal file
@ -0,0 +1,11 @@
|
||||
Rozsah:
|
||||
| 8 7 6 5 4 3 2 1
|
||||
--+------------------------
|
||||
1| 8 7 6 5 4 3 2 1
|
||||
2| 16 14 12 10 8 6 4
|
||||
3| 24 21 18 15 12 9
|
||||
4| 32 28 24 20 16
|
||||
5| 40 35 30 25
|
||||
6| 48 42 36
|
||||
7| 56 49
|
||||
8| 64
|
11
exercise-04-multiplication-table/test_data/0001_out_win.txt
Normal file
11
exercise-04-multiplication-table/test_data/0001_out_win.txt
Normal file
@ -0,0 +1,11 @@
|
||||
Rozsah:
|
||||
| 8 7 6 5 4 3 2 1
|
||||
--+------------------------
|
||||
1| 8 7 6 5 4 3 2 1
|
||||
2| 16 14 12 10 8 6 4
|
||||
3| 24 21 18 15 12 9
|
||||
4| 32 28 24 20 16
|
||||
5| 40 35 30 25
|
||||
6| 48 42 36
|
||||
7| 56 49
|
||||
8| 64
|
1
exercise-04-multiplication-table/test_data/0002_in.txt
Normal file
1
exercise-04-multiplication-table/test_data/0002_in.txt
Normal file
@ -0,0 +1 @@
|
||||
32
|
35
exercise-04-multiplication-table/test_data/0002_out.txt
Normal file
35
exercise-04-multiplication-table/test_data/0002_out.txt
Normal file
@ -0,0 +1,35 @@
|
||||
Rozsah:
|
||||
| 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
|
||||
----+----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
1| 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
|
||||
2| 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4
|
||||
3| 96 93 90 87 84 81 78 75 72 69 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9
|
||||
4| 128 124 120 116 112 108 104 100 96 92 88 84 80 76 72 68 64 60 56 52 48 44 40 36 32 28 24 20 16
|
||||
5| 160 155 150 145 140 135 130 125 120 115 110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25
|
||||
6| 192 186 180 174 168 162 156 150 144 138 132 126 120 114 108 102 96 90 84 78 72 66 60 54 48 42 36
|
||||
7| 224 217 210 203 196 189 182 175 168 161 154 147 140 133 126 119 112 105 98 91 84 77 70 63 56 49
|
||||
8| 256 248 240 232 224 216 208 200 192 184 176 168 160 152 144 136 128 120 112 104 96 88 80 72 64
|
||||
9| 288 279 270 261 252 243 234 225 216 207 198 189 180 171 162 153 144 135 126 117 108 99 90 81
|
||||
10| 320 310 300 290 280 270 260 250 240 230 220 210 200 190 180 170 160 150 140 130 120 110 100
|
||||
11| 352 341 330 319 308 297 286 275 264 253 242 231 220 209 198 187 176 165 154 143 132 121
|
||||
12| 384 372 360 348 336 324 312 300 288 276 264 252 240 228 216 204 192 180 168 156 144
|
||||
13| 416 403 390 377 364 351 338 325 312 299 286 273 260 247 234 221 208 195 182 169
|
||||
14| 448 434 420 406 392 378 364 350 336 322 308 294 280 266 252 238 224 210 196
|
||||
15| 480 465 450 435 420 405 390 375 360 345 330 315 300 285 270 255 240 225
|
||||
16| 512 496 480 464 448 432 416 400 384 368 352 336 320 304 288 272 256
|
||||
17| 544 527 510 493 476 459 442 425 408 391 374 357 340 323 306 289
|
||||
18| 576 558 540 522 504 486 468 450 432 414 396 378 360 342 324
|
||||
19| 608 589 570 551 532 513 494 475 456 437 418 399 380 361
|
||||
20| 640 620 600 580 560 540 520 500 480 460 440 420 400
|
||||
21| 672 651 630 609 588 567 546 525 504 483 462 441
|
||||
22| 704 682 660 638 616 594 572 550 528 506 484
|
||||
23| 736 713 690 667 644 621 598 575 552 529
|
||||
24| 768 744 720 696 672 648 624 600 576
|
||||
25| 800 775 750 725 700 675 650 625
|
||||
26| 832 806 780 754 728 702 676
|
||||
27| 864 837 810 783 756 729
|
||||
28| 896 868 840 812 784
|
||||
29| 928 899 870 841
|
||||
30| 960 930 900
|
||||
31| 992 961
|
||||
32| 1024
|
35
exercise-04-multiplication-table/test_data/0002_out_win.txt
Normal file
35
exercise-04-multiplication-table/test_data/0002_out_win.txt
Normal file
@ -0,0 +1,35 @@
|
||||
Rozsah:
|
||||
| 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
|
||||
----+----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
1| 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
|
||||
2| 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4
|
||||
3| 96 93 90 87 84 81 78 75 72 69 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9
|
||||
4| 128 124 120 116 112 108 104 100 96 92 88 84 80 76 72 68 64 60 56 52 48 44 40 36 32 28 24 20 16
|
||||
5| 160 155 150 145 140 135 130 125 120 115 110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25
|
||||
6| 192 186 180 174 168 162 156 150 144 138 132 126 120 114 108 102 96 90 84 78 72 66 60 54 48 42 36
|
||||
7| 224 217 210 203 196 189 182 175 168 161 154 147 140 133 126 119 112 105 98 91 84 77 70 63 56 49
|
||||
8| 256 248 240 232 224 216 208 200 192 184 176 168 160 152 144 136 128 120 112 104 96 88 80 72 64
|
||||
9| 288 279 270 261 252 243 234 225 216 207 198 189 180 171 162 153 144 135 126 117 108 99 90 81
|
||||
10| 320 310 300 290 280 270 260 250 240 230 220 210 200 190 180 170 160 150 140 130 120 110 100
|
||||
11| 352 341 330 319 308 297 286 275 264 253 242 231 220 209 198 187 176 165 154 143 132 121
|
||||
12| 384 372 360 348 336 324 312 300 288 276 264 252 240 228 216 204 192 180 168 156 144
|
||||
13| 416 403 390 377 364 351 338 325 312 299 286 273 260 247 234 221 208 195 182 169
|
||||
14| 448 434 420 406 392 378 364 350 336 322 308 294 280 266 252 238 224 210 196
|
||||
15| 480 465 450 435 420 405 390 375 360 345 330 315 300 285 270 255 240 225
|
||||
16| 512 496 480 464 448 432 416 400 384 368 352 336 320 304 288 272 256
|
||||
17| 544 527 510 493 476 459 442 425 408 391 374 357 340 323 306 289
|
||||
18| 576 558 540 522 504 486 468 450 432 414 396 378 360 342 324
|
||||
19| 608 589 570 551 532 513 494 475 456 437 418 399 380 361
|
||||
20| 640 620 600 580 560 540 520 500 480 460 440 420 400
|
||||
21| 672 651 630 609 588 567 546 525 504 483 462 441
|
||||
22| 704 682 660 638 616 594 572 550 528 506 484
|
||||
23| 736 713 690 667 644 621 598 575 552 529
|
||||
24| 768 744 720 696 672 648 624 600 576
|
||||
25| 800 775 750 725 700 675 650 625
|
||||
26| 832 806 780 754 728 702 676
|
||||
27| 864 837 810 783 756 729
|
||||
28| 896 868 840 812 784
|
||||
29| 928 899 870 841
|
||||
30| 960 930 900
|
||||
31| 992 961
|
||||
32| 1024
|
1
exercise-04-multiplication-table/test_data/0003_in.txt
Normal file
1
exercise-04-multiplication-table/test_data/0003_in.txt
Normal file
@ -0,0 +1 @@
|
||||
asdf
|
2
exercise-04-multiplication-table/test_data/0003_out.txt
Normal file
2
exercise-04-multiplication-table/test_data/0003_out.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Rozsah:
|
||||
Nespravny vstup.
|
@ -0,0 +1,2 @@
|
||||
Rozsah:
|
||||
Nespravny vstup.
|
1
exercise-04-multiplication-table/test_data/0004_in.txt
Normal file
1
exercise-04-multiplication-table/test_data/0004_in.txt
Normal file
@ -0,0 +1 @@
|
||||
-1
|
2
exercise-04-multiplication-table/test_data/0004_out.txt
Normal file
2
exercise-04-multiplication-table/test_data/0004_out.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Rozsah:
|
||||
Nespravny vstup.
|
98
homework-01-two-circles/README.md
Normal file
98
homework-01-two-circles/README.md
Normal file
@ -0,0 +1,98 @@
|
||||
Úkolem je napsat program, který rozhodne vzájemnou polohu 2 kružnic.
|
||||
|
||||
Vstupem programu je zadání dvou kružnic. Každá kružnice je zadaná svým středem (x a y souřadnice) a poloměrem. Čísla jsou zadaná v pořadí x y r, jedná se o desetinná čísla.
|
||||
|
||||
Výstupem programu je rozhodnutí o vzájemné poloze kružnic. Celkem může nastat 6 variant vzájemné polohy:
|
||||
|
||||
- kružnice splývají,
|
||||
- jedna kružnice leží zcela uvnitř druhé,
|
||||
- jedna kružnice se zevnitř dotýká druhé,
|
||||
- kružnice se protínají,
|
||||
- kružnice se dotýkají zvenku,
|
||||
- kružnice leží zcela mimo sebe.
|
||||
|
||||
Program rozhodne o poloze a vypíše ji. Dále, pokud je to relevantní, program vypíše plochu překryvu kružnic. Formát výstupu pro všech 6 variant je uveden v ukázkovém běhu níže. Pozor, za výstupem je odřádkování (\n).
|
||||
|
||||
Program musí být schopen detekovat nesprávný vstup. Pokud je na vstupu nesmyslné zadání, program to zjistí, vypíše chybové hlášení a ukončí se. Formát chybového hlášení je uveden v ukázce níže. Pozor, za případným chybovým hlášením je odřádkování (\n). Chybové hlášení zasílejte na standardní výstup (printf), nezasílejte jej na chybový výstup. Za chybu je považováno:
|
||||
|
||||
- nečíselná hodnota nějaké souřadnice,
|
||||
- nečíselná hodnota poloměru,
|
||||
- poloměr nulový nebo záporný.
|
||||
|
||||
**Ukázka práce programu:**
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 5
|
||||
Zadejte parametry kruznice #2:
|
||||
0 10 5
|
||||
Vnejsi dotyk, zadny prekryv.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 3
|
||||
Zadejte parametry kruznice #2:
|
||||
1.5 1.5 0.8
|
||||
Kruznice #2 lezi uvnitr kruznice #1, prekryv: 2.010619
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 4
|
||||
Zadejte parametry kruznice #2:
|
||||
2 0 2
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 12.566371
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 5
|
||||
Zadejte parametry kruznice #2:
|
||||
100 100 10
|
||||
Kruznice lezi vne sebe, zadny prekryv.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 4.5
|
||||
Zadejte parametry kruznice #2:
|
||||
10 10 15
|
||||
Kruznice se protinaji, prekryv: 37.475800
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
-10.25 -20.5 4
|
||||
Zadejte parametry kruznice #2:
|
||||
-10.25 -20.5 4
|
||||
Kruznice splyvaji, prekryv: 50.265482
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 -2
|
||||
Nespravny vstup.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 12 5
|
||||
Zadejte parametry kruznice #2:
|
||||
3 7 abc
|
||||
Nespravny vstup.
|
||||
```
|
138
homework-01-two-circles/main.c
Normal file
138
homework-01-two-circles/main.c
Normal file
@ -0,0 +1,138 @@
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __PROGTEST__
|
||||
#define debug(...) ((void)0)
|
||||
#else
|
||||
#define debug(fmt, ...) \
|
||||
fprintf(stdout, "[%s:%d %s()] " fmt "\n", __FILE__, __LINE__, __func__, \
|
||||
##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
double clamp(double d) {
|
||||
if (d > 1.0) {
|
||||
return 1.0;
|
||||
} else if (d < -1.0) {
|
||||
return -1.0;
|
||||
} else {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
// https://www.geeksforgeeks.org/dsa/area-of-intersection-of-two-circles/
|
||||
double intersection_area(double a_r, double b_r, double distance) {
|
||||
double a_square = a_r * a_r;
|
||||
double b_square = b_r * b_r;
|
||||
double distance_square = distance * distance;
|
||||
|
||||
double alpha_acos =
|
||||
(distance_square + a_square - b_square) / (2 * a_r * distance);
|
||||
double beta_acos =
|
||||
(distance_square + b_square - a_square) / (2 * b_r * distance);
|
||||
|
||||
double alpha = acos(clamp(alpha_acos)) * 2;
|
||||
double beta = acos(clamp(beta_acos)) * 2;
|
||||
|
||||
double a1 = 0.5 * beta * b_square - 0.5 * b_square * sin(beta);
|
||||
double a2 = 0.5 * alpha * a_square - 0.5 * a_square * sin(alpha);
|
||||
|
||||
double area = a1 + a2;
|
||||
|
||||
debug("Area = %lf", area);
|
||||
|
||||
return area;
|
||||
}
|
||||
|
||||
double circle_area(double radius) { return M_PI * radius * radius; }
|
||||
|
||||
int main() {
|
||||
double a_center_x = 0, a_center_y = 0, a_radius = 0, b_center_x = 0,
|
||||
b_center_y = 0, b_radius = 0;
|
||||
|
||||
printf("Zadejte parametry kruznice #1:\n");
|
||||
|
||||
int inputs_read = scanf("%lf %lf %lf", &a_center_x, &a_center_y, &a_radius);
|
||||
if (inputs_read != 3 || a_radius <= 0) {
|
||||
printf("Nespravny vstup.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("Zadejte parametry kruznice #2:\n");
|
||||
|
||||
inputs_read = scanf("%lf %lf %lf", &b_center_x, &b_center_y, &b_radius);
|
||||
if (inputs_read != 3 || b_radius <= 0) {
|
||||
printf("Nespravny vstup.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
double center_distance =
|
||||
sqrt((b_center_x - a_center_x) * (b_center_x - a_center_x) +
|
||||
(b_center_y - a_center_y) * (b_center_y - a_center_y));
|
||||
|
||||
debug("Distance of centers = %0.60lf", center_distance);
|
||||
debug("Radii difference = %0.60lf", a_radius - b_radius);
|
||||
debug("Radii difference = %0.60lf", b_radius - a_radius);
|
||||
debug("Center + #1 = %0.60lf", a_center_x + a_radius);
|
||||
debug("Center + #2 = %0.60lf", b_center_x + b_radius);
|
||||
|
||||
double radii_sum = a_radius + b_radius;
|
||||
double radii_difference = fabs(a_radius - b_radius);
|
||||
double epsilon = 100 * DBL_EPSILON * fmax(a_radius, b_radius);
|
||||
|
||||
// Both origin points and radii are identical
|
||||
bool are_identical = a_center_x == b_center_x && a_center_y == b_center_y && a_radius == b_radius;
|
||||
|
||||
// d = r1 + r2
|
||||
// d - r1 - r1 < epsilon
|
||||
bool are_touching_from_outside = fabs(center_distance - radii_sum) < epsilon;
|
||||
|
||||
// d = |r1 - r2|
|
||||
// d - |r1 - r2| < epsilon
|
||||
bool are_touching_from_inside = fabs(center_distance - radii_difference) < epsilon;
|
||||
|
||||
// d < |r1 - r2|
|
||||
bool is_fully_inside = center_distance < radii_difference;
|
||||
|
||||
// d < r1 + r2
|
||||
bool are_overlapping = center_distance < radii_sum;
|
||||
|
||||
if (are_identical) {
|
||||
printf("Kruznice splyvaji, prekryv: %lf\n", circle_area(a_radius));
|
||||
}
|
||||
else if (are_touching_from_inside) {
|
||||
if (a_radius > b_radius) {
|
||||
printf(
|
||||
"Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: %lf\n",
|
||||
circle_area(b_radius));
|
||||
} else {
|
||||
printf(
|
||||
"Vnitrni dotyk, kruznice #1 lezi uvnitr kruznice #2, prekryv: %lf\n",
|
||||
circle_area(a_radius));
|
||||
}
|
||||
}
|
||||
else if (are_touching_from_outside) {
|
||||
printf("Vnejsi dotyk, zadny prekryv.\n");
|
||||
}
|
||||
else if (is_fully_inside) {
|
||||
if (a_radius > b_radius) {
|
||||
printf("Kruznice #2 lezi uvnitr kruznice #1, prekryv: %lf\n",
|
||||
circle_area(b_radius));
|
||||
} else {
|
||||
printf("Kruznice #1 lezi uvnitr kruznice #2, prekryv: %lf\n",
|
||||
circle_area(a_radius));
|
||||
}
|
||||
}
|
||||
else if (are_overlapping) {
|
||||
printf("Kruznice se protinaji, prekryv: %lf\n",
|
||||
intersection_area(a_radius, b_radius, center_distance));
|
||||
}
|
||||
else {
|
||||
printf("Kruznice lezi vne sebe, zadny prekryv.\n");
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
2
homework-01-two-circles/test_data/0000_in.txt
Normal file
2
homework-01-two-circles/test_data/0000_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 5
|
||||
0 10 5
|
3
homework-01-two-circles/test_data/0000_out.txt
Normal file
3
homework-01-two-circles/test_data/0000_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnejsi dotyk, zadny prekryv.
|
3
homework-01-two-circles/test_data/0000_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0000_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnejsi dotyk, zadny prekryv.
|
2
homework-01-two-circles/test_data/0001_in.txt
Normal file
2
homework-01-two-circles/test_data/0001_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 3
|
||||
1.5 1.5 0.8
|
3
homework-01-two-circles/test_data/0001_out.txt
Normal file
3
homework-01-two-circles/test_data/0001_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice #2 lezi uvnitr kruznice #1, prekryv: 2.010619
|
3
homework-01-two-circles/test_data/0001_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0001_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice #2 lezi uvnitr kruznice #1, prekryv: 2.010619
|
2
homework-01-two-circles/test_data/0002_in.txt
Normal file
2
homework-01-two-circles/test_data/0002_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 4
|
||||
2 0 2
|
3
homework-01-two-circles/test_data/0002_out.txt
Normal file
3
homework-01-two-circles/test_data/0002_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 12.566371
|
3
homework-01-two-circles/test_data/0002_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0002_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 12.566371
|
2
homework-01-two-circles/test_data/0003_in.txt
Normal file
2
homework-01-two-circles/test_data/0003_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 5
|
||||
100 100 10
|
3
homework-01-two-circles/test_data/0003_out.txt
Normal file
3
homework-01-two-circles/test_data/0003_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice lezi vne sebe, zadny prekryv.
|
3
homework-01-two-circles/test_data/0003_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0003_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice lezi vne sebe, zadny prekryv.
|
2
homework-01-two-circles/test_data/0004_in.txt
Normal file
2
homework-01-two-circles/test_data/0004_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 4.5
|
||||
10 10 15
|
3
homework-01-two-circles/test_data/0004_out.txt
Normal file
3
homework-01-two-circles/test_data/0004_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice se protinaji, prekryv: 37.475800
|
3
homework-01-two-circles/test_data/0004_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0004_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice se protinaji, prekryv: 37.475800
|
2
homework-01-two-circles/test_data/0005_in.txt
Normal file
2
homework-01-two-circles/test_data/0005_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
-10.25 -20.5 4
|
||||
-10.25 -20.5 4
|
3
homework-01-two-circles/test_data/0005_out.txt
Normal file
3
homework-01-two-circles/test_data/0005_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice splyvaji, prekryv: 50.265482
|
3
homework-01-two-circles/test_data/0005_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0005_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice splyvaji, prekryv: 50.265482
|
1
homework-01-two-circles/test_data/0006_in.txt
Normal file
1
homework-01-two-circles/test_data/0006_in.txt
Normal file
@ -0,0 +1 @@
|
||||
0 0 -2
|
2
homework-01-two-circles/test_data/0006_out.txt
Normal file
2
homework-01-two-circles/test_data/0006_out.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Nespravny vstup.
|
2
homework-01-two-circles/test_data/0006_out_win.txt
Normal file
2
homework-01-two-circles/test_data/0006_out_win.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Nespravny vstup.
|
2
homework-01-two-circles/test_data/0007_in.txt
Normal file
2
homework-01-two-circles/test_data/0007_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 12 5
|
||||
3 7 abc
|
3
homework-01-two-circles/test_data/0007_out.txt
Normal file
3
homework-01-two-circles/test_data/0007_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Nespravny vstup.
|
3
homework-01-two-circles/test_data/0007_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0007_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Nespravny vstup.
|
2
homework-01-two-circles/test_data/0008_in.txt
Normal file
2
homework-01-two-circles/test_data/0008_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
9.7e-39 -1.85e-38 4.16e-38
|
||||
4.03e-38 2.3e-39 4.6e-39
|
3
homework-01-two-circles/test_data/0008_out.txt
Normal file
3
homework-01-two-circles/test_data/0008_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 0.000000
|
2
homework-01-two-circles/test_data/0009_in.txt
Normal file
2
homework-01-two-circles/test_data/0009_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
4.03e-38 2.3e-39 4.6e-39
|
||||
9.7e-39 -1.85e-38 4.16e-38
|
3
homework-01-two-circles/test_data/0009_out.txt
Normal file
3
homework-01-two-circles/test_data/0009_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #1 lezi uvnitr kruznice #2, prekryv: 0.000000
|
2
homework-01-two-circles/test_data/0010_in.txt
Normal file
2
homework-01-two-circles/test_data/0010_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
-2.21e-38 -1.11e-38 1.4862e-36
|
||||
1.2253e-36 5.721e-37 1.092e-37
|
3
homework-01-two-circles/test_data/0010_out.txt
Normal file
3
homework-01-two-circles/test_data/0010_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 0.000000
|
Loading…
x
Reference in New Issue
Block a user