lab 0.1 code

This commit is contained in:
KIX
2025-11-29 04:15:43 -05:00
parent b16b98d7b7
commit 2401b7240c
7 changed files with 211 additions and 51 deletions

28
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

163
str.c
View File

@@ -6,123 +6,184 @@
#define INIT_SZ 20
//Внутренняя функция,
//позволяет убедиться что строка может вместить как минимум newsz символов
void
_str_ensure(str *s, int newsz)
{
int tmp;
newsz += 1; // добавляем единичку чтобы влез нулевой байт
if (newsz < s->capacity)
return;
int tmp;
newsz += 1;
if (newsz < s->capacity)
return;
tmp = s->capacity * 2;
if (tmp > newsz) {
newsz = tmp;
}
s->ptr = xrealloc(s->ptr, newsz);
s->capacity = newsz;
tmp = s->capacity * 2;
if (tmp > newsz) {
newsz = tmp;
}
s->ptr = xrealloc(s->ptr, newsz);
s->capacity = newsz;
}
void
str_init(str *s)
{
assert(s);
// <YOURCODE>
assert(s);
s->len = 0;
s->capacity = INIT_SZ;
s->ptr = xmalloc(INIT_SZ);
s->ptr[0] = '\0';
}
void
str_deinit(str *s)
{
if (s->ptr)
free(s->ptr);
s->ptr = NULL;
if (s->ptr)
free(s->ptr);
s->ptr = NULL;
s->len = 0;
s->capacity = 0;
}
void
str_init_data(str *s, const char *initial)
{
assert(s && initial);
// <YOURCODE>
assert(s && initial);
int len = strlen(initial);
s->len = len;
s->capacity = len + 1;
s->ptr = xmalloc(len + 1);
strcpy(s->ptr, initial);
}
char *
str_data(str *s)
{
assert(s);
// <YOURCODE>
return NULL;
assert(s);
return s->ptr;
}
void
str_set(str *s, char *cstr)
{
assert(s && cstr);
// <YOURCODE>
assert(s && cstr);
int len = strlen(cstr);
_str_ensure(s, len);
strcpy(s->ptr, cstr);
s->len = len;
}
str
str_copy(str *s)
{
assert(s);
// <YOURCODE>
assert(s);
str s2;
str_init_data(&s2, s->ptr);
return s2;
}
int
str_count(str *s, char ch)
{
assert(s);
// <YOURCODE>
return 0;
assert(s);
int count = 0;
for (int i = 0; i < s->len; i++)
{
if (s->ptr[i] == ch)
{count++;}
}
return count;
}
void
str_append(str *s, char *cstr)
{
assert(s && cstr);
// <YOURCODE>
assert(s && cstr);
str_append_n(s, cstr, strlen(cstr));
}
void
str_append_n(str *s, char *cstr, int len)
{
assert(s && cstr);
// <YOURCODE>
assert(s && cstr);
_str_ensure(s, s->len + len);
memcpy(s->ptr + s->len, cstr, len);
s->len += len;
s->ptr[s->len] = '\0';
}
void
str_shrink(str *s, int len)
{
assert(s && len >= 0);
// <YOURCODE>
assert(s && len >= 0);
if (len > s->len)
{return;}
s->len = len;
s->ptr[s->len] = '\0';
}
int
str_find(str *s, char *substr)
{
assert(s && substr);
char *p = strstr(s->ptr, substr);
if (p == NULL)
return -1;
return p - s->ptr;
assert(s && substr);
char *p = strstr(s->ptr, substr);
if (p == NULL)
return -1;
return p - s->ptr;
}
str
str_sub(str *s, int start_idx, int length)
{
assert(s && start_idx >= 0 && length >= 0);
assert(s && start_idx >= 0 && length >= 0);
str s2;
str_init(&s2);
// <YOURCODE>
return s2;
str s2;
str_init(&s2);
if (start_idx >= s->len)
{return s2;}
int true_length = length;
if (start_idx + length > s->len)
{true_length = s->len - start_idx;}
_str_ensure(&s2, true_length);
memcpy(s2.ptr, s->ptr + start_idx, true_length);
s2.len = true_length;
s2.ptr[true_length] = '\0';
return s2;
}
// Hint: you can create temporary string object!
int
str_replace(str *s, char *substr, char *replacement)
{
assert(s && substr && replacement);
// <YOURCODE>
return 0;
}
assert(s && substr && replacement);
int count = 0;
str replaced;
str_init(&replaced);
char *current = s->ptr;
char *found;
int substr_len = strlen(substr);
int replacement_len = strlen(replacement);
while ((found = strstr(current, substr)) != NULL)
{
int prefix_len = found - current;
if (prefix_len > 0)
{str_append_n(&replaced, current, prefix_len);}
str_append_n(&replaced, replacement, replacement_len);
current = found + substr_len;
count++;
}
if (*current != '\0') {
str_append(&replaced, current);
}
str_set(s, replaced.ptr);
str_deinit(&replaced);
return count;
}

BIN
str_test Executable file

Binary file not shown.

6
test/test1 Normal file
View File

@@ -0,0 +1,6 @@
sdsdad adadas
adasdasdsd
adasdadad
adadadadas
adadadadad
asdasdadasd

0
test/test2 Normal file
View File

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;
}