2024-05-22 17:33:42 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
int strlen(char *s) {
|
|
|
|
char *p = s;
|
|
|
|
while (*p != '\0')
|
|
|
|
p++;
|
|
|
|
return p - s;
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:16:34 +00:00
|
|
|
|
2024-05-22 17:33:42 +00:00
|
|
|
void strcpy(char *s, char *t)
|
|
|
|
{
|
|
|
|
while (*s++ = *t++);
|
|
|
|
}
|
|
|
|
#define ALLOCSIZE 10000
|
|
|
|
|
|
|
|
static char allocbuf[ALLOCSIZE];
|
|
|
|
static char *allocp = allocbuf;
|
|
|
|
|
|
|
|
char *alloc(int n)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (n % 4 != 0) {
|
|
|
|
n += 4 - (n % 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (allocbuf + ALLOCSIZE - allocp >= n) {
|
|
|
|
allocp += n;
|
|
|
|
return allocp - n;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:51:39 +00:00
|
|
|
// def. model danych
|
2024-05-22 17:33:42 +00:00
|
|
|
|
2024-05-22 18:51:39 +00:00
|
|
|
//pre processor
|
|
|
|
#define LEN (8+2)*10
|
2024-05-22 17:33:42 +00:00
|
|
|
|
2024-05-22 18:51:39 +00:00
|
|
|
struct model {
|
|
|
|
char * str;
|
|
|
|
uint32_t len ;
|
|
|
|
};
|
2024-05-22 17:33:42 +00:00
|
|
|
|
|
|
|
|
2024-05-22 19:15:48 +00:00
|
|
|
//alg
|
|
|
|
// prosta implementacji func. z bibl. std. strok przy uzyciu gpt3.5
|
|
|
|
//
|
|
|
|
|
|
|
|
#define NULL ((void*) 0)
|
|
|
|
|
|
|
|
//
|
|
|
|
// Funkcja pomocnicza do sprawdzania, czy znak jest wśród delimiterów
|
|
|
|
bool is_delim(char c, const char *delims) {
|
|
|
|
while (*delims) {
|
|
|
|
if (c == *delims) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
delims++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Najprostsza implementacja funkcji strtok
|
|
|
|
char *simple_strtok(char *str, const char *delims) {
|
|
|
|
static char *static_str = (char *) NULL; // Przechowuje wskaźnik do bieżącej pozycji w ciągu
|
|
|
|
|
|
|
|
// Jeśli przekazano nowy ciąg, zaktualizuj static_str
|
2024-05-23 16:16:34 +00:00
|
|
|
if (str == NULL) {
|
|
|
|
return (char *) NULL; // str nie wskazuje na zdanie !!!
|
2024-05-22 19:15:48 +00:00
|
|
|
}
|
2024-05-23 16:16:34 +00:00
|
|
|
static_str = str;
|
2024-05-22 19:15:48 +00:00
|
|
|
|
2024-05-23 16:16:34 +00:00
|
|
|
// " .,mpabi"
|
|
|
|
// ^
|
2024-05-22 19:15:48 +00:00
|
|
|
|
|
|
|
// Pomiń początkowe delimitery
|
|
|
|
while (*static_str && is_delim(*static_str, delims)) {
|
|
|
|
static_str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jeśli doszliśmy do końca ciągu, zwróć NULL
|
|
|
|
if (*static_str == '\0') {
|
|
|
|
return (char *) NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zapisz początek tokenu
|
|
|
|
char *token_start = static_str;
|
|
|
|
|
|
|
|
// Znajdź koniec tokenu
|
|
|
|
while (*static_str && !is_delim(*static_str, delims)) {
|
|
|
|
static_str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jeśli znaleziono delimitery, zamień je na '\0' i zaktualizuj static_str
|
|
|
|
if (*static_str) {
|
|
|
|
*static_str = '\0';
|
|
|
|
static_str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zwróć początek tokenu
|
|
|
|
return token_start;
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:16:34 +00:00
|
|
|
char buf[100];
|
|
|
|
struct model * p = (struct model *) buf; //p[1]
|
2024-05-22 19:15:48 +00:00
|
|
|
////func alg
|
2024-05-22 18:51:39 +00:00
|
|
|
//in: ptr to date
|
|
|
|
//return: count of words
|
|
|
|
int alg (const char * ptr) {
|
2024-05-22 17:33:42 +00:00
|
|
|
|
2024-05-22 20:25:06 +00:00
|
|
|
char bufer[ALLOCSIZE];
|
|
|
|
strcpy(bufer, (char *)ptr);
|
|
|
|
|
|
|
|
const char *delims = " ,.!?:;\n\t";
|
2024-05-22 17:33:42 +00:00
|
|
|
|
2024-05-22 20:25:06 +00:00
|
|
|
int8_t count = 0;
|
2024-05-22 18:51:39 +00:00
|
|
|
|
2024-05-22 20:25:06 +00:00
|
|
|
char *token = simple_strtok(bufer, delims);
|
2024-05-23 16:16:34 +00:00
|
|
|
|
|
|
|
while (token != (char *)NULL) {
|
|
|
|
|
|
|
|
p[count].str = token;
|
|
|
|
p[count].len = strlen(token);
|
|
|
|
|
|
|
|
token = simple_strtok((char *)NULL, delims);
|
2024-05-22 20:25:06 +00:00
|
|
|
count++;
|
|
|
|
}
|
2024-05-23 16:16:34 +00:00
|
|
|
|
2024-05-22 20:25:06 +00:00
|
|
|
return count;
|
|
|
|
}
|
2024-05-22 18:51:39 +00:00
|
|
|
|
2024-05-22 17:33:42 +00:00
|
|
|
int main() {
|
|
|
|
|
2024-06-03 19:57:49 +00:00
|
|
|
char *str = "Lorem ipsum dolor sit amet, consectetur adipiscing";
|
2024-05-22 17:33:42 +00:00
|
|
|
|
2024-05-23 16:16:34 +00:00
|
|
|
alg(str);
|
|
|
|
asm ("nop");
|
2024-05-22 17:33:42 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2024-05-23 17:23:50 +00:00
|
|
|
|
|
|
|
// wynik tego kodu:
|
|
|
|
// p[0].str = If
|
|
|
|
// p[0].len = 2
|
|
|
|
|
|
|
|
// p[1].str = wantered
|
|
|
|
// p[1].len = 8
|
|
|
|
|
|
|
|
// p[2].str = relation
|
|
|
|
// p[2].len = 8
|
|
|
|
|
|
|
|
// p[3].str = no
|
|
|
|
// p[3].len = 2
|
|
|
|
|
|
|
|
// p[4].str = surprise
|
|
|
|
// p[4].len = 8
|
|
|
|
|
|
|
|
// p[5].str = of
|
|
|
|
// p[5].len = 2
|
|
|
|
|
|
|
|
// p[6].str = all
|
|
|
|
// p[6].len = 3
|