diff --git a/2.10.txt b/2.10.txt new file mode 100644 index 0000000..a98cbe7 --- /dev/null +++ b/2.10.txt @@ -0,0 +1,23 @@ + #include + + int lower(int c); + + int main() + { + int i; + + char s[] = "DEERTGAD UIOG\0"; + printf("s = %s\n", s); + + printf("lower s = "); + for (i = 0; s[i] != '\0'; i++) + printf("%c", lower(s[i])); + printf("\n"); + + return 0; + } + + /* lower: преобразование прописных c в строчные, только для ASCII */ + int lower(int c) + { + return (c >= 'A' && c <='Z') ? c +'a'-'A' : c; \ No newline at end of file diff --git a/2.6.txt b/2.6.txt new file mode 100644 index 0000000..c153809 --- /dev/null +++ b/2.6.txt @@ -0,0 +1,38 @@ +#include + +unsigned setbits(unsigned x, int p, int n, unsigned y); +void printfbit(unsigned n); + +int main() +{ + printf("76543210\n\n"); + + unsigned c1 = 'f'; + printfbit(c1); + + unsigned c2 = 'z'; + printfbit(c2); + + printfbit(setbits(c1, 5, 3, c2)); + + return 0; +} + +/* setbits: x получает n правых бит из y, начиная с p-й позиции */ +unsigned setbits(unsigned x, int p, int n, unsigned y) +{ + return ( (~0 << (p+1)) & x ) | ( ~(~0 << (p+1-n)) & x ) | + (~(~0 << n) & y) << (p+1-n); +} + +void printfbit(unsigned n) +{ + for(int i = 7; i >= 0; i--) + { + if(n & (1 << i)) + putchar('1'); + else + putchar('0'); + } + putchar('\n'); +} \ No newline at end of file diff --git a/2.7.txt b/2.7.txt new file mode 100644 index 0000000..d2d92d7 --- /dev/null +++ b/2.7.txt @@ -0,0 +1,34 @@ +#include + +unsigned invert(unsigned x, int p, int n); +void printfbit(unsigned n); + +int main() +{ + printf("76543210\n\n"); + + unsigned c = 'f'; + printfbit(c); + + printfbit(invert(c, 5, 3)); + + return 0; +} + +/* invert: инвертирует n бит из x, начиная с p-й позиции */ +unsigned invert(unsigned x, int p, int n) +{ + return x ^ (~(~0 << n) << (p+1-n)); +} + +void printfbit(unsigned n) +{ + for(int i = 7; i >= 0; i--) + { + if(n & (1 << i)) + putchar('1'); + else + putchar('0'); + } + putchar('\n'); +} \ No newline at end of file diff --git a/3.1.txt b/3.1.txt new file mode 100644 index 0000000..3795af6 --- /dev/null +++ b/3.1.txt @@ -0,0 +1,19 @@ + /* binsearch: найти х в v[0] <= v[1] <= … <= v[n-1] */ +int main(int x, int v[], int n) +{ + int low, high, mid; + low = 0; + high = n - 1 ; + while (low <= high && x != v[mid]) { + mid = (low + high) / 2; + if (x < v[mid]) + high = mid - 1; + else + low = mid + 1; + mid = (low + high) / 2; \ + } + if ( x == v[mid]) + return mid; + else + return -1; /* совпадения нет */ +} \ No newline at end of file diff --git a/3.2.txt b/3.2.txt new file mode 100644 index 0000000..a3614a8 --- /dev/null +++ b/3.2.txt @@ -0,0 +1,65 @@ + #include + + #define MAXLEN 30 + + void escape(char s[], char t[]); + void unescape(char s[], char t[]); + + int main() + { + char input[MAXLEN] = "bla\tbla\nbla\n"; + char output[MAXLEN]; + + printf("Original = %s\n", input); + escape(output, input); + printf("Escaped = %s\n", output); + unescape(input, output); + printf("Unescaped = %s\n", input); + printf("The End\n"); + + return 0; + } + + void escape(char s[], char t[]) + { + int i, j; + + for (i = 0, j = 0; s[i]; i++, j++) + switch (t[i]) { + case '\t': + s[j++] = ' '; + s[j] = 't'; + break; + case '\n': + s[j++] = ' '; + s[j] = 'n'; + break; + default: + s[j] = t[i]; + break; + } + s[j] = t[i]; // \0 ! + } + + void unescape(char s[], char t[]) + { + int i, j; + + for (i = 0, j = 0; s[i]; i++, j++) + switch (t[i]) { + case ' ': + switch (t[++i]) { + case 't': + s[j] = '\t'; + break; + case 'n': + s[j] = '\n'; + break; + } + break; + default: + s[j] = t[i]; + break; + } + s[j] = t[i]; + } \ No newline at end of file diff --git a/3.4.txt b/3.4.txt new file mode 100644 index 0000000..d6e2d5a --- /dev/null +++ b/3.4.txt @@ -0,0 +1,46 @@ +#include + #include + #include + + void itoa(int n, char s[]); + void reverse(char s[]); + + int main(void) { + char buffer[20]; + int i = 35; + + printf("Number: %d\n", i); + itoa(i, buffer); + printf("Buffer : %s\n", buffer); + + printf("INT_MIN: %d\n", INT_MIN); + itoa(INT_MIN, buffer); + printf("Buffer : %s\n", buffer); + + return 0; + } + +/* itoa: преобразование n в строку s */ + void itoa(int n, char s[]) { + int i, sign; + sign = n; /* сохраняем знак */ + + i = 0; + do { /* генерируем цифры в обратном порядке */ + s[i++] = abs(n % 10) + '0'; /* следующая цифра */ + } while ( n /= 10 ); /* исключить ее */ + if (sign < 0) + s[i++] = '-'; + + s[i] = '\0'; + reverse(s); + } + + void reverse(char s[]) { + int c, i, j; + for ( i = 0, j = strlen(s)-1; i < j; i++, j--) { + c = s[i]; + s[i] = s[j]; + s[j] = c; + } + } \ No newline at end of file diff --git a/3.5.txt b/3.5.txt new file mode 100644 index 0000000..4bdb71e --- /dev/null +++ b/3.5.txt @@ -0,0 +1,38 @@ +#include +#include +#include + + +int convert_base(char *p, int b, int s); + +int main() { + char p[] = "111"; + int b = 10; + int s = 8; + + int result = convert_base(p, b, s); + printf("Converted number: %d\n", result); + + return 0; +} +// Функция для конвертации числа из одного основания в другое +int convert_base(char *p, int b, int s) { + int decimal = 0; + int len = strlen(p); + + // Преобразование числа в десятичную систему счисления + for (int i = 0; i < len; i++) { + decimal += (p[i] - '0') * pow(b, len - i - 1); + } + + // Преобразование десятичного числа в нужное основание + int result = 0; + int index = 0; + while (decimal > 0) { + result += (decimal % s) * pow(10, index); + decimal /= s; + index++; + } + + return result; +} \ No newline at end of file