Initial commit

This commit is contained in:
Ninjdai 2024-10-22 13:03:17 +02:00
commit aceb7ebdb8
34 changed files with 811 additions and 0 deletions

131
TP1/part_1.c Normal file
View File

@ -0,0 +1,131 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define TAB_SIZE 7
void afficheTab(int tab[], int size);
int* fibonacci(int n);
void print_sizes();
void exercice_4();
int est_permutation(int* tab, int n);
int * permutation(int n);
char * appliquer_permutation(char * T, int * permutation, int n);
int main(int argc, char *argv[])
{
/*int i;
int *tab = fibonacci(TAB_SIZE);
int * tabptr = tab;
afficheTab(tab, TAB_SIZE);
printf("\n\n");
*/
//print_sizes();
//exercice_4();
// int tab[8] = {0, 1, 2, 3, 4, 5, 6, 7};
// printf("%d\n", est_permutation(tab, 8));
/*int * tab = permutation(TAB_SIZE);
afficheTab(tab, TAB_SIZE);
char bliat[TAB_SIZE] = "Bonjour";
char * bliat_perm = appliquer_permutation(bliat, tab, TAB_SIZE);
for (int i=0; i<TAB_SIZE; i++) {
printf("%c", bliat_perm[i]);
}
printf("\n");
free(bliat_perm);
free(tab);*/
return 0;
}
char * appliquer_permutation(char * T, int * permutation, int n) {
int i;
char * result = malloc(sizeof(char)*n);
for (i=0; i<n; i++) {
result[i] = T[permutation[i]];
}
return result;
}
int * permutation(int n) {
int i, x1, x2, tmp;
int * tab = malloc(sizeof(int)*n);
for (i=0; i<n; i++) {
tab[i]=i;
}
srand(time(NULL));
for (i=0; i<n; i++) {
x1 = rand()%n;
x2 = rand()%n;
tmp = tab[x1];
tab[x1] = tab[x2];
tab[x2] = tmp;
}
return tab;
}
int est_permutation(int* tab, int n) {
int i;
int* checked_tab = malloc(sizeof(int)*n);
for (i=0; i<n; i++) {
if(tab[i]<n && checked_tab[tab[i]]==0){
checked_tab[tab[i]]=1;
} else {
return 0;
}
}
free(checked_tab);
return 1;
}
void afficheTab(int * tabptr, int size) {
int i;
if (tabptr==NULL) {
return;
}
for (i=0; i<size; i++) {
printf("%d, ", tabptr[i]);
}
printf("\n");
}
void exercice_4() {
void * Z = malloc(10*4);
int * X = Z;
//afficheTab(X,10);
//afficheTab((int *)((char *)X+8),8);
free(Z);
}
int* fibonacci(int n) {
int i;
int* tab = malloc(sizeof(int)*n);
tab[0] = 0;
tab[1] = 1;
for (i=2; i<n; i++) {
tab[i]= tab[i-2] + tab[i-1];
}
return tab;
}
void print_sizes() {
printf("char : %lu\n", sizeof(char));
printf("int * : %lu\n", sizeof(int *));
printf("int* : %lu\n", sizeof(int*));
printf("void : %lu\n", sizeof(void));
printf("void * : %lu\n", sizeof(void *));
printf("long int : %lu\n", sizeof(long int));
printf("long int * : %lu\n", sizeof(long int *));
}

BIN
TP1/part_2 Executable file

Binary file not shown.

148
TP1/part_2.c Normal file
View File

@ -0,0 +1,148 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NB_LIGNES 9
#define NB_COLONNES 3
float ** tableau(int n, int m);
void autofill_tableau(float** tab, int n, int m);
void autofill_tableau_int(float** tab, int n, int m, int v);
void fill_tableau(float** tab, int n, int m);
void print_tableau(float** tab, int n, int m);
void print_tableau_int(float** tab, int n, int m);
float ** right_rot(float** tab, int n, int m);
/*
* Libère le tableau
* n: nombre de lignes du tableau
*/
void free_tab(float** tab, int n);
float ** triangle_pascal(int size);
#define PASCAL 5
int main(int argc, char *argv[]) {
srand(time(NULL));
float ** tab = tableau(NB_LIGNES, NB_COLONNES);
autofill_tableau(tab, NB_LIGNES, NB_COLONNES);
//fill_tableau(tab, NB_LIGNES, NB_COLONNES);
print_tableau(tab, NB_LIGNES, NB_COLONNES);
printf("\n\n");
float ** rot_tab = right_rot(tab, NB_LIGNES, NB_COLONNES);
print_tableau(rot_tab, NB_COLONNES, NB_LIGNES);
free_tab(tab, NB_LIGNES);
free_tab(rot_tab, NB_COLONNES);
/*float ** t = triangle_pascal(PASCAL);
print_tableau_int(t, PASCAL, PASCAL);*/
return 0;
}
float ** triangle_pascal(int size) {
int i,j;
float ** tr = tableau(size, size);
autofill_tableau_int(tr, size, size, 0);
tr[0][0] = 1;
for (i=1; i<size; i++) {
for (j=0; j<size; j++) {
tr[i][j] = tr[i-1][j-1]+tr[i-1][j];
}
}
return tr;
}
void free_tab(float** tab, int n) {
int i;
for (i=0; i<n; i++) {
free(tab[i]);
}
free(tab);
}
float ** right_rot(float** tab, int n, int m) {
int lgn, col;
float ** rot_tab = tableau(m, n);
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
rot_tab[col][(m-lgn)] = tab[lgn][col];
}
}
return rot_tab;
}
float ** tableau(int n, int m) {
int i;
int success=1;
void* ptr;
float ** tab = malloc(sizeof(float*)*n);
for (i=0; i<n; i++) {
ptr = malloc(sizeof(float)*m);
if (ptr == 0) {
success=0;
break;
}
tab[i] = ptr;
}
if(success!=0)
return tab;
else
return NULL;
}
#define RAND_RANGE 69
void autofill_tableau(float** tab, int n, int m) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
tab[lgn][col] = (float)rand()/(float)(RAND_MAX/RAND_RANGE);
}
}
}
void autofill_tableau_int(float** tab, int n, int m, int v) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
tab[lgn][col] = v;
}
}
}
void fill_tableau(float** tab, int n, int m) {
int lgn, col;
float tmp;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
printf("Ligne %d / Colonne %d: ", lgn, col);
scanf("%f", &tmp);
tab[lgn][col] = tmp;
}
}
}
void print_tableau(float** tab, int n, int m) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
printf(" %f ", tab[lgn][col]);
}
printf("\n");
}
}
void print_tableau_int(float** tab, int n, int m) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
printf(" %d ", (int)tab[lgn][col]);
}
printf("\n");
}
}

BIN
TP1/part_3 Executable file

Binary file not shown.

70
TP1/part_3.c Normal file
View File

@ -0,0 +1,70 @@
#include "part_3.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Liste * create_list() {
Liste * list = malloc(sizeof(Liste));
list->nb_elements_ = 0;
return list;
}
int list_length(Liste list) {
return list.nb_elements_;
}
void append(Liste * list, int val) {
Element * el = malloc(sizeof(Element));
el->val_ = val;
el->prev_ = list->tail_;
if (list->tail_ != NULL) {
list->tail_->next_ = el;
}
list->tail_ = el;
list->nb_elements_ += 1;
if (list->nb_elements_==1) {
list->head_ = el;
}
}
void append_head(Liste * list, int val) {
Element * el = malloc(sizeof(Element));
list->head_->prev_ = el;
list->head_ = el;
list->nb_elements_ += 1;
}
int first_value(Liste * list) {
return list->head_->val_;
}
int last_value(Liste * list) {
return list->tail_->val_;
}
void print_list(Liste * list) {
if (list->head_ == NULL) {
printf("[]\n");
return;
}
Element* current_el = list->head_;
printf("[");
printf("%lu", (unsigned long)current_el);
while (current_el != NULL) {
printf("%d, ", current_el->val_);
current_el = current_el->next_;
}
printf("]\n");
}
int main(int argc, char *argv[])
{
Liste * list = create_list();
append(list, 69);
print_list(list);
return 0;
}

24
TP1/part_3.h Normal file
View File

@ -0,0 +1,24 @@
typedef struct LListe Liste ;
typedef struct LElement Element ;
struct LListe{
int nb_elements_ ;
Element * head_ ;
Element * tail_ ;
} ;
struct LElement{
/**
* Element suivant / NULL si dernier element
*/
Element * next_ ;
/**
* Element pr´ec´edente
*/
Element * prev_ ;
/**
* Valeur stock´ee dans la cellule.
*/
int val_ ;
};

BIN
TP2/part_1/concatenate Executable file

Binary file not shown.

34
TP2/part_1/concatenate.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: copy <file1> <file2>\n");
exit(-1);
}
FILE * ogf = fopen(argv[1], "r");
if (ogf==NULL) {
perror("Error while opening file1");
}
FILE * tf = fopen(argv[2], "a+");
if (tf==NULL) {
perror("Error while opening file2");
}
fseek(ogf, 0, SEEK_END);
int ogf_size = ftell(ogf);
fseek(ogf, 0, SEEK_SET);
char * f = malloc(sizeof(char) * ogf_size);
fread(f, sizeof(char), ogf_size, ogf);
fwrite(f, sizeof(char), ogf_size, tf);
fclose(ogf);
fclose(tf);
return 0;
}

2
TP2/part_1/copied.log Normal file
View File

@ -0,0 +1,2 @@
UwU
Sussy Baka :3

BIN
TP2/part_1/copy Executable file

Binary file not shown.

35
TP2/part_1/copy.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: copy <file1> <file2>\n");
exit(-1);
}
FILE * ogf = fopen(argv[1], "r");
if (ogf==NULL) {
perror("Error while opening file1");
}
FILE * tf = fopen(argv[2], "w+");
if (tf==NULL) {
perror("Error while opening file2");
}
fseek(ogf, 0, SEEK_END);
int ogf_size = ftell(ogf);
fseek(ogf, 0, SEEK_SET);
char * f = malloc(sizeof(char) * ogf_size);
fread(f, sizeof(char), ogf_size, ogf);
fwrite(f, sizeof(char), ogf_size, tf);
fclose(ogf);
fclose(tf);
return 0;
}

2
TP2/part_1/dummy.log Normal file
View File

@ -0,0 +1,2 @@
UwU
Sussy Baka :3

View File

@ -0,0 +1,16 @@
UwU
Sussy Baka :3
UwU
Sussy Baka :3
UwU
Sussy Baka :3
UwU
Sussy Baka :3
UwU
Sussy Baka :3
UwU
Sussy Baka :3
UwU
Sussy Baka :3
UwU
Sussy Baka :3

View File

@ -0,0 +1,2 @@
UwU
I'm a Sussy Baka :3

View File

@ -0,0 +1,2 @@
UwU
I'm Sussy Baka :3

BIN
TP2/part_1/rm_vowels Executable file

Binary file not shown.

36
TP2/part_1/rm_vowels.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: rm_vowels <file>\n");
exit(-1);
}
FILE * file = fopen(argv[1], "r+");
if (file==NULL) {
perror("Error while opening file");
}
fseek(file, 0, SEEK_END);
int file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char * f = malloc(sizeof(char) * file_size);
fread(f, sizeof(char), file_size, file);
fclose(file);
file = fopen(argv[1], "w+");
for(int i = 0; i<file_size; i++) {
if (!(f[i]=='a' || f[i]=='e' || f[i]=='i' || f[i]=='o' || f[i]=='u' || f[i]=='y' || f[i]=='A' || f[i]=='E' || f[i]=='I' || f[i]=='O' || f[i]=='U' || f[i]=='Y')){
fputc(f[i], file);
}
}
fclose(file);
return 0;
}

BIN
TP2/part_1/tableau Executable file

Binary file not shown.

19
TP2/part_1/tableau.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
int main(int argc, char* argv[]) {
if(argc!=2) {
printf("Usage: tableau <file>");
return -1;
}
int T[512];
for (int i=0;i<512;i++) {
T[i] = i;
}
FILE * F = fopen(argv[1], "w+");
fwrite(T, sizeof(int), 512, F);
return 0;
}

BIN
TP2/part_1/tableau_output Normal file

Binary file not shown.

1
TP2/part_3/out Normal file
View File

@ -0,0 +1 @@
list: [9] 0, 9, 18, 27, 36, 45, 54, 63, 72

BIN
TP2/part_3/serialization Executable file

Binary file not shown.

View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#define INT_N 9
void write_list_to_stream(int * list, int size);
int * read_list_from_stream();
int main(int argc, char *argv[])
{
int * l = malloc(sizeof(int) * INT_N);
for (int i = 0; i<INT_N; i++) {
l[i] = i*INT_N;
}
write_list_to_stream(l, INT_N);
read_list_from_stream();
return 0;
}
void write_list_to_stream(int * list, int size) {
FILE * f = fopen("out", "w+");
char * lst = malloc(9+size%10);
sprintf(lst, "list: [%d] ", size);
fwrite(lst, 1, 10+size/10, f);
free(lst);
for (int i = 1; i<size; i++) {
fprintf(f, "%d, ", list[i-1]);
}
fprintf(f, "%d", list[size-1]);
fclose(f);
}
int * read_list_from_stream() {
FILE * f = fopen("out", "r+");
int size;
fscanf(f, "list: [%d] ", &size);
int * list = malloc(sizeof(int)*size);
for (int i = 1; i<size; i++) {
fscanf(f, "%d, ", &list[i-1]);
}
fscanf(f, "%d", &list[size-1]);
for (int i = 0; i<size; i++) {
printf("%d, ", list[i]);
}
fclose(f);
return list;
}

BIN
TP3/decoupage Executable file

Binary file not shown.

83
TP3/decoupage.c Normal file
View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int * int_list_from_str(char * str);
int int_count_from_str(char * str);
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <string>\n", argv[0]);
return -1;
}
printf("%d ints\n", int_count_from_str(argv[1]));
/*int * list = int_list_from_str(argv[1]);
for (int i=0; i<int_count_from_str(argv[1]); i++) {
printf("%d ", list[i]);
}*/
return 0;
}
int * int_list_from_str(char * str) {
int ico = int_count_from_str(str);
int * list = malloc(sizeof(int)*ico);
int len = strlen(str);
int current_i = 0;
int is_i = 1;
int ic = 0;
for (int i = 0; i < len; i++) {
if (str[i] == ' ') {
if ((i+1 < len && str[i+1] != ' ') || (i+1)==len) {
list[ic] = current_i;
ic++;
}
is_i = 0;
current_i = 0;
} else if (isdigit(str[i])) {
if (is_i) {
current_i *= 10;
} else {
is_i = 1;
}
current_i += (str[i] - '0');
} else if (str[i]=='-' && current_i > 0) {
current_i *= -1;
} else {
return 0;
}
}
return list;
}
int int_count_from_str(char * str) {
int ic = 0;
int current_i = 0;
int is_i = 1;
int len = strlen(str);
for (int i = 0; i < len; i++) {
printf("str[%d]: %c\n", i, str[i]);
if (str[i] == ' ') {
if ((i+1 < len && str[i+1] != ' ') || (i+1)==len) ic++;
is_i = 0;
current_i = 0;
} else if (isdigit(str[i])) {
if (is_i) {
current_i *= 10;
} else {
is_i = 1;
}
current_i += (str[i] - '0');
} else if (str[i]=='-' && current_i > 0) {
current_i *= -1;
} else {
return 0;
}
}
return ic;
}

BIN
TP3/stringprinter Executable file

Binary file not shown.

31
TP3/stringprinter.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(int argc, char *argv[])
{
if (argc!=3) {
printf("Usage: %s <file> <string>", argv[0]);
return -1;
}
FILE * f = fopen(argv[1], "w+");
if (f==NULL) {
printf("Error while opening file %s\n", argv[1]);
return -2;
}
for (int i = 0; i<2000; i++) {
fprintf(f, "%s", argv[2]);
usleep(rand()%100);
}
fclose(f);
return 0;
}
/*
* Si programme executé plusieurs fois sur le même fichier simultanément, deuxième programme lancé prend le pas après un court temps
*/

1
TP3/test Normal file

File diff suppressed because one or more lines are too long

BIN
TP4/ex_4 Executable file

Binary file not shown.

74
TP4/ex_4.c Normal file
View File

@ -0,0 +1,74 @@
#include <stdio.h>
int less(int a, int b);
int more(int a, int b);
int pair_more(int a, int b);
void sort(int * tab, int size_tab, int (* sort_func)(int, int));
void print_tab(int * tab, int size) {
printf("tab[%d] = [", size);
for (int i=0; i<size; i++) {
printf("%d ", tab[i]);
}
printf("]\n");
}
int main(int argc, char *argv[]) {
printf("%d, ", less(5, 6));
printf("%d, ", more(5, 6));
printf("%d\n", pair_more(4, 5));
int tab[10] = {6, 9, 0, 5, 3, 7, 4, 8, 1, 2};
print_tab(tab, 10);
sort(tab, 10, less);
print_tab(tab, 10);
sort(tab, 10, more);
print_tab(tab, 10);
sort(tab, 10, pair_more);
print_tab(tab, 10);
return 0;
}
int less(int a, int b) {
if (a < b) {
return 1;
} else {
return -1;
}
}
int more(int a, int b) {
if (a > b) {
return 1;
} else {
return -1;
}
}
int pair_more(int a, int b) {
if (a%2==0) {
if (b%2!=0) {
return 1;
} else {
return more(a, b);
}
}
return -1;
}
void sort(int * tab, int size_tab, int (* sort_func)(int, int)) {
int i,j, tmp;
for (i=0; i<size_tab-1; i++) {
for (j=i+1; j<size_tab; j++) {
if (sort_func(tab[i], tab[j]) == -1) {
tmp = tab[j];
tab[j] = tab[i];
tab[i] = tmp;
}
}
}
}

BIN
TP4/union_ex1 Executable file

Binary file not shown.

29
TP4/union_ex1.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
typedef union int_test {
int a;
char b[4];
} test;
void print_hex(int n);
int main(int argc, char *argv[])
{
/*test i = {.a=429496729};
for(int j=0; j<4; j++){
printf("%d ", i.b[j]);
}
printf("\n");*/
print_hex(14*256 + 11*16 + 8);
print_hex(4*256 + 2*16 + 0);
return 0;
}
void print_hex(int n) {
test i = {.a=n};
for (int j=0; j<4; j++) {
printf("%d ", i.b[j]);
}
printf("\n");
}

BIN
TP4/union_ex3 Executable file

Binary file not shown.

15
TP4/union_ex3.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
void print_hex(int n);
int main(int argc, char *argv[])
{
print_hex(14*256 + 11*16 + 8); //0xeb8
print_hex(4*256 + 2*16 + 0); //0x420
return 0;
}
void print_hex(int n) {
printf("%x\n", n);
}