lab 0.1 code
This commit is contained in:
28
.vscode/tasks.json
vendored
Normal file
28
.vscode/tasks.json
vendored
Normal 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
163
str.c
@@ -6,123 +6,184 @@
|
|||||||
|
|
||||||
#define INIT_SZ 20
|
#define INIT_SZ 20
|
||||||
|
|
||||||
//Внутренняя функция,
|
|
||||||
//позволяет убедиться что строка может вместить как минимум newsz символов
|
|
||||||
void
|
void
|
||||||
_str_ensure(str *s, int newsz)
|
_str_ensure(str *s, int newsz)
|
||||||
{
|
{
|
||||||
int tmp;
|
int tmp;
|
||||||
newsz += 1; // добавляем единичку чтобы влез нулевой байт
|
newsz += 1;
|
||||||
if (newsz < s->capacity)
|
if (newsz < s->capacity)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
tmp = s->capacity * 2;
|
tmp = s->capacity * 2;
|
||||||
if (tmp > newsz) {
|
if (tmp > newsz) {
|
||||||
newsz = tmp;
|
newsz = tmp;
|
||||||
}
|
}
|
||||||
s->ptr = xrealloc(s->ptr, newsz);
|
s->ptr = xrealloc(s->ptr, newsz);
|
||||||
s->capacity = newsz;
|
s->capacity = newsz;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
str_init(str *s)
|
str_init(str *s)
|
||||||
{
|
{
|
||||||
assert(s);
|
assert(s);
|
||||||
// <YOURCODE>
|
s->len = 0;
|
||||||
|
s->capacity = INIT_SZ;
|
||||||
|
s->ptr = xmalloc(INIT_SZ);
|
||||||
|
s->ptr[0] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
str_deinit(str *s)
|
str_deinit(str *s)
|
||||||
{
|
{
|
||||||
if (s->ptr)
|
if (s->ptr)
|
||||||
free(s->ptr);
|
free(s->ptr);
|
||||||
s->ptr = NULL;
|
s->ptr = NULL;
|
||||||
|
s->len = 0;
|
||||||
|
s->capacity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
str_init_data(str *s, const char *initial)
|
str_init_data(str *s, const char *initial)
|
||||||
{
|
{
|
||||||
assert(s && initial);
|
assert(s && initial);
|
||||||
// <YOURCODE>
|
int len = strlen(initial);
|
||||||
|
s->len = len;
|
||||||
|
s->capacity = len + 1;
|
||||||
|
s->ptr = xmalloc(len + 1);
|
||||||
|
strcpy(s->ptr, initial);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
str_data(str *s)
|
str_data(str *s)
|
||||||
{
|
{
|
||||||
assert(s);
|
assert(s);
|
||||||
// <YOURCODE>
|
return s->ptr;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
str_set(str *s, char *cstr)
|
str_set(str *s, char *cstr)
|
||||||
{
|
{
|
||||||
assert(s && cstr);
|
assert(s && cstr);
|
||||||
// <YOURCODE>
|
int len = strlen(cstr);
|
||||||
|
_str_ensure(s, len);
|
||||||
|
strcpy(s->ptr, cstr);
|
||||||
|
s->len = len;
|
||||||
}
|
}
|
||||||
|
|
||||||
str
|
str
|
||||||
str_copy(str *s)
|
str_copy(str *s)
|
||||||
{
|
{
|
||||||
assert(s);
|
assert(s);
|
||||||
// <YOURCODE>
|
str s2;
|
||||||
|
str_init_data(&s2, s->ptr);
|
||||||
|
return s2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
str_count(str *s, char ch)
|
str_count(str *s, char ch)
|
||||||
{
|
{
|
||||||
assert(s);
|
assert(s);
|
||||||
// <YOURCODE>
|
int count = 0;
|
||||||
return 0;
|
for (int i = 0; i < s->len; i++)
|
||||||
|
{
|
||||||
|
if (s->ptr[i] == ch)
|
||||||
|
{count++;}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
str_append(str *s, char *cstr)
|
str_append(str *s, char *cstr)
|
||||||
{
|
{
|
||||||
assert(s && cstr);
|
assert(s && cstr);
|
||||||
// <YOURCODE>
|
str_append_n(s, cstr, strlen(cstr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
str_append_n(str *s, char *cstr, int len)
|
str_append_n(str *s, char *cstr, int len)
|
||||||
{
|
{
|
||||||
assert(s && cstr);
|
assert(s && cstr);
|
||||||
// <YOURCODE>
|
_str_ensure(s, s->len + len);
|
||||||
|
memcpy(s->ptr + s->len, cstr, len);
|
||||||
|
s->len += len;
|
||||||
|
s->ptr[s->len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
str_shrink(str *s, int len)
|
str_shrink(str *s, int len)
|
||||||
{
|
{
|
||||||
assert(s && len >= 0);
|
assert(s && len >= 0);
|
||||||
// <YOURCODE>
|
if (len > s->len)
|
||||||
|
{return;}
|
||||||
|
s->len = len;
|
||||||
|
s->ptr[s->len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
str_find(str *s, char *substr)
|
str_find(str *s, char *substr)
|
||||||
{
|
{
|
||||||
assert(s && substr);
|
assert(s && substr);
|
||||||
char *p = strstr(s->ptr, substr);
|
char *p = strstr(s->ptr, substr);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
return p - s->ptr;
|
return p - s->ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
str
|
str
|
||||||
str_sub(str *s, int start_idx, int length)
|
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 s2;
|
||||||
str_init(&s2);
|
str_init(&s2);
|
||||||
// <YOURCODE>
|
|
||||||
return 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
|
int
|
||||||
str_replace(str *s, char *substr, char *replacement)
|
str_replace(str *s, char *substr, char *replacement)
|
||||||
{
|
{
|
||||||
assert(s && substr && replacement);
|
assert(s && substr && replacement);
|
||||||
// <YOURCODE>
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
6
test/test1
Normal file
6
test/test1
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
sdsdad adadas
|
||||||
|
adasdasdsd
|
||||||
|
adasdadad
|
||||||
|
adadadadas
|
||||||
|
adadadadad
|
||||||
|
asdasdadasd
|
||||||
0
test/test2
Normal file
0
test/test2
Normal file
65
wcl.c
Normal file
65
wcl.c
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user