6 Commits

Author SHA1 Message Date
KIX
9e6ad7862c sort func code 2025-11-15 03:59:07 -05:00
KIX
397d263a96 wcl code 2025-11-08 03:48:18 -05:00
KIX
b5f9a34935 atoi 2.0 func code 2025-11-01 06:07:58 -04:00
KIX
01f553fe4c myatoi func code 2025-11-01 04:06:04 -04:00
KIX
ba2e7c99f4 func fib code 2025-10-18 06:06:27 -04:00
KIX
e580f118a1 func sum_array code 2025-10-18 05:39:25 -04:00
12 changed files with 401 additions and 0 deletions

BIN
atoi Executable file

Binary file not shown.

34
atoi.c Normal file
View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
int myatoi(char *s, int l);
int main(int argc, char **argv)
{
printf("Print your string number\n");
char s[100]; scanf("%s", s);
printf("Your converted number is %d\n", myatoi(s, strlen(s)));
return 0;
}
int myatoi(char *s, int l)
{
int result = 0;
int is_pos = 1;
for(int i=0; i < l; i++)
{
if((int)s[i]==45)
{
is_pos = -1;
}
else
{
if((int)s[i] < 48 || (int)s[i] > 57)
{
printf("Not a number\n");
result = 0;
break;
}
result = result * 10 + ((int)s[i] - 48);
}
}
result = result * is_pos;
return result;
}

BIN
atoi_2.0 Executable file

Binary file not shown.

57
atoi_2.0.c Normal file
View File

@@ -0,0 +1,57 @@
#include <stdio.h>
#include <string.h>
int myatoi(char *s, int l, int base);
int main(int argc, char **argv)
{
printf("Print your string number\n");
char s[100]; scanf("%s", s);
printf("Enter your string number base\n");
int base;
scanf("%d", &base);
printf("Your converted number is %d\n", myatoi(s, strlen(s), base));
return 0;
}
int myatoi(char *s, int l, int base)
{
int result = 0;
int is_pos = 1;
int symb;
int d;
for(int i=0; i < l; i++)
{
symb = (int)s[i];
if((int)s[i]=='-')
{
is_pos = -1;
}
else if(base <= 10)
{
if((int)s[i] < '0' || (int)s[i] > '9')
{
printf("Not a number\n");
result = 0;
break;
}
result = result * base + ((int)s[i] - '0');
}
else
{
if (symb >= '0' && symb <= '9')
{
d = symb - '0';
}
else if (symb >= 'A' && symb <= 'Z')
{
d = symb - 'A' + 10;
}
else if (symb >= 'a' && symb <= 'z')
{
d = symb - 'a' + 10;
}
result = result * base + d;
}
}
result = result * is_pos;
return result;
}

BIN
fib Executable file

Binary file not shown.

18
fib.c Normal file
View File

@@ -0,0 +1,18 @@
#include <stdio.h>
int fib(int n);
int main(int argc, char **argv)
{
printf("Print number of fibanachi sum\n");
int n = 1; scanf("%d", &n);
printf("\n%dth number of fibanachi sum is: ", n);
printf("%d\n", fib(n));
return 0;
}
int fib(int n)
{
int array[n];
array[0] = 0; array[1] = 1;
for(int i = 2; i <= n; i++)
{array[i] = array[i-1] + array[i-2];}
return array[n - 1];
}

BIN
sort Executable file

Binary file not shown.

207
sort.c Normal file
View File

@@ -0,0 +1,207 @@
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "macro.h"
typedef void (*sorter_t)(int *arr, size_t sz);
typedef struct {
const char *name;
sorter_t func;
} sort_info_t;
/*
* Написать сортировку вставками (не подглядывая в инторнеты!)
* arr -- целочисленный массив
* sz -- размер массива
*/
void insert_sort(int *arr, size_t sz)
{
int i, j;
for(i = 1; i < sz; i++)
{
int k = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > k)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = k;
}
}
void
bubble_sort(int *arr, size_t sz)
{
int i, j;
int tmp;
for (i = 0; i < sz; i++) {
for (j = i + 1; j < sz; j++) {
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
/*
Функция для слияния двух отсортированных
массивов в один.
a -- указывает на первый элемент первого массива
b -- указывает на первый элемент второго массива
end -- указывает на первый элемент за границами второго массива
Результат записывается во временный массив tmp.
Первый элемент в tmp[0], второй в tmp[1] и т.д.
Пример
массивы a = [1, 4, 5]
b = [2, 3, 6]
a b end
| | |
\/ \/ \/
| 1 | 4 | 5 | 2 | 3 | 6 |
после вызова _merge в tmp должен получиться массив
| 1 | 2 | 3 | 4 | 5 | 6 |
*/
static void
_merge(int *tmp, int *a, int *b, int *end)
{
int tmp1 = 0, a1 = 0, b1 = 0;
int size_1 = b - a, size_2 = end - b;
while(a1 < size_1 && b1 < size_2)
{
if(a[a1] < b[b1])
{
tmp[tmp1++] = a[a1++];
}
else
{
tmp[tmp1++] = b[b1++];
}
}
while(a1 < size_1)
{
tmp[tmp1] = a[a1];
tmp1++; a1++;
}
while(b1 < size_2)
{
tmp[tmp1] = b[b1];
tmp1++; b1++;
}
}
static void
_merge_sort_int(int *tmp, int *arr, size_t n)
{
int half = n / 2; //индекс, который делит массивы пополам
if (half > 1)
_merge_sort_int(tmp, arr, half);
if (n - half > 1)
_merge_sort_int(tmp + half, arr + half, n - half);
// "слить" два отсортированных массива в один
//tmp - where new sorted array
_merge(tmp, arr, arr + half, arr + n);
// Скопировать данные из временного хранилища в
// оригинальный массив
memcpy(arr, tmp, n * sizeof(*tmp));
}
void
merge_sort(int *arr, size_t sz)
{
int *tmp;
if (sz < 2)
return;
tmp = malloc(sizeof(*arr) * sz);
assert(tmp);
_merge_sort_int(tmp, arr, sz);
free(tmp);
}
void
print_arr(int *arr, size_t n)
{
printf("{");
for (int i = 0; i < n; i++) {
printf("%d, ", arr[i]);
}
printf("}\n");
}
#define _S(item) {#item, item}
bool
test_all()
{
#include "test_arrays.h"
TEST_ARR_INIT(testcases);
sort_info_t sorters[] = {
_S(insert_sort),
//_S(bubble_sort),
_S(merge_sort),
};
int *temparr = NULL;
int temparr_len = 0;
int allok = 1;
for (int j = 0; j < ARRSZ(sorters); j++) {
sort_info_t cursort = sorters[j];
printf("[+] %s\n", cursort.name);
for (int i = 0; i < ARRSZ(testcases); i++) {
int len = testcases[i].len;
printf("[+] testing %s\n", testcases[i].name);
if (len > temparr_len) {
// TODO: EER????
temparr_len = len;
temparr = realloc(temparr, temparr_len * sizeof(*temparr));
assert(temparr);
}
memcpy(temparr, testcases[i].unsorted, sizeof(*temparr) * len);
cursort.func(temparr, len);
int res = memcmp(temparr, testcases[i].sorted, sizeof(*temparr) * len);
if (res != 0) {
allok = 0;
printf("[-] arrays does not match \n");
printf("your\n");
print_arr(temparr, len);
printf("original\n");
print_arr(testcases[i].sorted, len);
break;
}
}
}
if (allok) {
printf("[+] All OK!\n");
}
if (temparr)
free(temparr);
return allok == 1;
}
int
main(int argc, const char *argv[])
{
test_all();
return 0;
}

BIN
sum_array Executable file

Binary file not shown.

20
sum_array.c Normal file
View File

@@ -0,0 +1,20 @@
#include <stdio.h>
int sum(int array[], int size);
int main(int argc, char **argv)
{
int array[] = {2,2,2,2,2};
int size = sizeof(array)/sizeof(array[0]);
printf("Array size is %d\n{", size);
for(int i = 0; i < size; i++)
{printf("%d,",array[i]);}
printf("}\nResult of summing the array:%d\n", sum(array, size));
return 0;
}
int sum(int array[], int size)
{
int sum = 0;
for(int i = 0; i < size; i++)
{sum += array[i];}
return sum;
}

BIN
wcl Executable file

Binary file not shown.

65
wcl.c Normal file
View File

@@ -0,0 +1,65 @@
#include <stdio.h>
#include <ctype.h>
int countLines(const char* fname);
int countBytes (const char* fname);
int countWords (const char* fname);
int main(int argc, char *argv[])
{
int nlines = 0;
int nbytes = 0;
int nwords = 0;
for(int i=1; i < argc; i++)
{
nlines = countLines(argv[i]);
nbytes = countBytes(argv[i]);
nwords = countWords(argv[i]);
printf("%s: lines - %d, bytes - %d, words - %d\n", argv[i], nlines, nbytes, nwords);
}
return 0;
}
int countLines(const char* fname)
{
FILE* file = fopen(fname, "r");
int lines = 0;
char buffer[1024];
while (fgets(buffer, sizeof(buffer), file) != NULL)
{lines++;}
fclose(file);
return lines;
}
int countBytes (const char* fname)
{
FILE* file = fopen(fname, "r");
int bytes = 0;
int c;
while ((c = fgetc(file)) != EOF)
{
bytes++;
}
return bytes;
}
int countWords (const char* fname)
{
FILE* file = fopen(fname, "r");
int words = 0;
int c;
int is_in_word = 0;
while ((c = fgetc(file)) != EOF)
{
if (isspace(c))
{is_in_word = 0;}
else
{
if (is_in_word == 0)
{
words++;
is_in_word = 1;
}
}
}
return words;
}