Compare commits

..

2 Commits

Author SHA1 Message Date
mpabi 7bf09994cf update 2024-05-26 07:41:50 +00:00
mpabi d9e57e8ca0 added bubble sort alg. 2024-05-26 07:24:45 +00:00
3 changed files with 42 additions and 35 deletions

View File

@ -1,4 +1,5 @@
#include <stdint.h> #include <stdint.h>
#include "myfunc.h"
int strlen(char *s) { int strlen(char *s) {
char *p = s; char *p = s;
@ -51,12 +52,6 @@ char *alloc(int n)
//pre processor //pre processor
#define LEN (8+2)*10 #define LEN (8+2)*10
struct model {
char * ptr;
uint32_t len ;
};
//alg //alg
// prosta implementacji func. z bibl. std. strok przy uzyciu gpt3.5 // prosta implementacji func. z bibl. std. strok przy uzyciu gpt3.5
// //

View File

@ -1,35 +1,45 @@
#include "myfunc.h" #include "myfunc.h"
#include <cstring>
// Lokalna deklaracja funkcji my_strlen int strncmp(const char* s1, const char* s2, size_t n) {
static int my_strlen(const char *str) { for (size_t i = 0; i < n; i++) {
int length = 0; if (s1[i] != s2[i]) {
while (str[length] != '\0') { return (unsigned char)s1[i] - (unsigned char)s2[i];
length++;
} }
return length; if (s1[i] == '\0' || s2[i] == '\0') {
break;
}
}
return 0;
} }
void count_characters(const char* alfabet, const char* slowo, uint8_t* wynik) { bool compareWords(const char* a, int lenA, const char* b, int lenB) {
// Użycie my_strlen do obliczenia długości alfabetu int minLen = (lenA < lenB) ? lenA : lenB;
int alfabet_length = my_strlen(alfabet); int cmp = strncmp(a, b, minLen);
if (cmp == 0) {
for (int i = 0; i < alfabet_length; ++i) { return lenA < lenB;
wynik[i] = 0; }
return cmp < 0;
} }
for (int i = 0; i < alfabet_length; ++i) { void bubbleSort(model arr[], int n) {
for (int j = 0; slowo[j] != '\0'; ++j) { bool swapped;
if (alfabet[i] == slowo[j]) { for (int i = 0; i < n-1; i++) {
wynik[i]++; swapped = false;
}
} for (int j = 0; j < n-i-1; j++) {
if (!compareWords(arr[j].ptr, arr[j].len, arr[j+1].ptr, arr[j+1].len)) {
// Zamiana miejscami
model temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true;
} }
} }
// #include "myfunc.h" if (!swapped) {
break;
// const char* alfabet = "abcdefghijklmnopqrstuwxyz"; }
// const char* slowo = "mpabi"; }
// int wynik[26] = {0}; // Inicjalizacja tablicy wyników na 0 }
// count_characters(alfabet, slowo, wynik);

View File

@ -3,7 +3,9 @@
#include <stdint.h> #include <stdint.h>
struct model {
void count_characters(const char* alfabet, const char* slowo, uint8_t* wynik); char * ptr;
uint32_t len ;
};
#endif #endif