Přesun souborů z jiného repozitáře.

This commit is contained in:
2026-01-25 17:18:31 +01:00
parent aee44ab3a8
commit 4fbf4c11e5
4 changed files with 336 additions and 2 deletions
+63
View File
@@ -0,0 +1,63 @@
#include "stdio.h"
// ladicí výpisy
#ifdef NDEBUG
#define debug(...)
#else
#define debug(...) do{fprintf(stderr, __VA_ARGS__);}while(0)
#endif
int main() {
int i = 0;
unsigned char buf[8];
char in = '\0';
char in_tmp = 0;
unsigned char coefs[8] = {128, 64, 32, 16, 8, 4, 2, 1};
// hláška k zadání vstupu
printf("Zadejte textovy vstup: ");
// čtení prvního znaku vstupu
in = fgetc(stdin);
// hláška o výstupu
printf("Zakodovany binarni vystup: ");
// cyklus zpracování vstupu
while(in != EOF && in != '\n') {
// ladicí výpis aktuálního znaku
debug("%c\n", in);
// kontrola vstupu
if(in < 0 && in != 255) {
fprintf(stderr, "CHYBA: Neplatny znak na %i. pozici, lze zadat pouze ASCII znaky.\n", i+1);
return -1;
}
// binární hodnota z desítkové reprezentace ASCII znaku
in_tmp = in;
for(int j=0; j<8; j++) {
buf[j] = in_tmp / coefs[j];
debug("DEBUG%i: -%c- -%i- [%i%i%i%i%i%i%i%i]\n", j+1, in, in_tmp, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
in_tmp = in_tmp - (buf[j] * coefs[j]);
}
// výpis osmice binárních znaků
for(int j=0; j<8; j++) {
fputc(buf[j]+'0', stdout);
}
// čtení dalšího ASCII znaku
in = fgetc(stdin);
i++;
}
// odřádkování na konci
fputc('\n', stdout);
return 0;
}