This commit is contained in:
Debucquoy
2023-10-18 20:27:40 +02:00
parent 4de4dcf2c2
commit b0f02b0d5d
29 changed files with 700 additions and 2 deletions

26
bac2/os/chap2/Makefile Normal file
View File

@ -0,0 +1,26 @@
.PHONY: clean, mrproper
CC = gcc
CFLAGS = -g -Wall
all: ex3 ex4
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
ex3: ex3.o
$(CC) $(CFLAGS) -o $@ $+
ex4: ex4.o
$(CC) $(CFLAGS) -o $@ $+
ex5: ex5.o
$(CC) $(CFLAGS) -o $@ $+
clean:
rm -f *.o core.*
mrproper: clean
rm -f ex3 ex4 ex5
run: ex5
./ex5

Binary file not shown.

14
bac2/os/chap2/ex1.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#define TEST_SIZE 100
int main(void)
{
char* blop[TEST_SIZE];
for (int i = 0; i < TEST_SIZE; ++i) {
blop[i] = malloc(sizeof(char));
printf("%p\n", blop[i]);
}
while(1);
}

34
bac2/os/chap2/ex2.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* expand_tabs(const char* string);
int main(void)
{
const char* str = "This is a text This is another one. ' ' <- space; ' ' <-tab";
printf("%s - %d\n", str, (int)strlen(str));
printf("%s - %d\n", expand_tabs(str),(int) strlen(expand_tabs(str)));
}
char* expand_tabs(const char* string){
int tab_count = 0, letter_count = 0;
for(int i = 0; string[i] != '\0'; i++){
letter_count++;
if(string[i] == '\t')
tab_count++;
}
char* ret = malloc(sizeof(char) * letter_count + 3*tab_count);
char* filler = ret;
for(int i = 0; string[i] != '\0'; i++){
if(string[i] == '\t'){
for (int j = 0; j < 4; ++j) {
*(filler++) = ' ';
}
}else{
*(filler++) = string[i];
}
}
return ret;
}

BIN
bac2/os/chap2/ex3 Executable file

Binary file not shown.

62
bac2/os/chap2/ex3.c Normal file
View File

@ -0,0 +1,62 @@
#include <stdlib.h>
#include <stdio.h>
#define print_vec(vec) for (int i = 0; i < (vec)->size; ++i) { printf("%d, ", (vec)->data[i]); } printf("\n")
struct vec {
int size;
int *data;
};
struct vec* new(unsigned int n);
struct vec* add(const struct vec *v, const struct vec *w);
struct vec* smul(double s, const struct vec *v);
int main(void)
{
struct vec *v = new(4);
v->data[0] = 1;
v->data[1] = 2;
v->data[2] = 3;
v->data[3] = 4;
print_vec(v);
struct vec *w = new(4);
w->data[0] = 5;
w->data[1] = 6;
w->data[2] = 7;
w->data[3] = 8;
print_vec(w);
struct vec *added = add(v, w);
print_vec(added);
struct vec *multiplied = smul(3, v);
print_vec(multiplied);
}
struct vec* new(unsigned int n){
struct vec* ret = malloc(sizeof(struct vec));
ret->size = n;
ret->data = malloc(sizeof(int) * n);
return ret;
}
struct vec* add(const struct vec *v, const struct vec *w){
if(v->size != w->size)
return NULL;
struct vec *ret = new(v->size);
for (int i = 0; i < v->size; ++i) {
ret->data[i] = v->data[i] + w->data[i];
}
return ret;
}
struct vec* smul(double s, const struct vec *v){
struct vec *ret = new(v->size);
for (int i = 0; i < v->size; ++i) {
ret->data[i] = v->data[i] * s;
}
return ret;
}

BIN
bac2/os/chap2/ex3.o Normal file

Binary file not shown.

BIN
bac2/os/chap2/ex4 Executable file

Binary file not shown.

61
bac2/os/chap2/ex4.c Normal file
View File

@ -0,0 +1,61 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#define AT(mat, x, y) ((mat)->data[((x)-1) + ( ((y)-1) * ((mat)->w) )])
#define PRINT_MAT(mat) for (int i = 1; i <= (mat)->h; ++i) { for (int j = 1; j <= (mat)->w; ++j) { printf("%d,", AT((mat),(j), (i))); } printf("\n"); } printf("\n");
typedef struct {
uint8_t w, h;
int *data;
} MAT;
MAT *new(uint8_t, uint8_t);
MAT *mul(MAT*, MAT*);
int main(void)
{
MAT* a = new(3, 2);
MAT* b = new(3, 3);
MAT* mult;
AT(a, 1, 1) = 0; AT(a, 2, 1) = 1; AT(a, 3, 1) = 2;
AT(a, 1, 2) = 3; AT(a, 2, 2) = 4; AT(a, 3, 2) = 5;
/* AT(a, 1, 3) = 6; AT(a, 2, 3) = 7; AT(a, 3, 3) = 8; */
PRINT_MAT(a);
AT(b, 1, 1) = 1; AT(b, 2, 1) = 0; AT(b, 3, 1) = 0;
AT(b, 1, 2) = 0; AT(b, 2, 2) = 1; AT(b, 3, 2) = 0;
AT(b, 1, 3) = 0; AT(b, 2, 3) = 0; AT(b, 3, 3) = 1;
PRINT_MAT(b);
mult = mul(a,b);
PRINT_MAT(mult);
}
MAT *new(uint8_t w, uint8_t h){
MAT* ret = malloc(sizeof(MAT));
ret->w = w;
ret->h = h;
ret->data = malloc(sizeof(int) * w * h);
return ret;
}
MAT *mul(MAT *a, MAT *b){
if(a->w != b->h)
return NULL;
MAT* ret = new(b->w, a->h);
for (int i = 1; i <= a->h; ++i) {
for (int j = 1; j <= b->w; ++j) {
int tmp = 0;
for (int k = 1; k <= a->w; ++k) {
tmp += AT(a, k, i) * AT(b, j, k);
}
AT(ret, j, i) = tmp;
}
}
return ret;
}

BIN
bac2/os/chap2/ex4.o Normal file

Binary file not shown.

BIN
bac2/os/chap2/ex5 Executable file

Binary file not shown.

41
bac2/os/chap2/ex5.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdlib.h>
#include <stdio.h>
struct node{
struct node* prev;
void* data;
};
typedef struct node* LIFO;
LIFO* mklifo ();
void push(LIFO* lst , void*);
void* pop(LIFO* lst);
int main(void)
{
char* t = "test";
LIFO* lifo = mklifo();
push(lifo, t);
printf("%s", (char *)pop(lifo));
}
LIFO *mklifo (){
LIFO* ret = malloc(sizeof(struct node));
return ret;
}
void push(LIFO *lst , void *el){
LIFO* next = mklifo();
(*lst)->data = el;
(*next)->prev = *lst;
lst = next;
}
void *pop(LIFO *lst){
void* el;
(*lst)->data = el;
*lst = (*lst)->prev;
return el;
}

BIN
bac2/os/chap2/ex5.o Normal file

Binary file not shown.