Skip to content
Snippets Groups Projects
Commit bc199c3f authored by thibault.chatillo's avatar thibault.chatillo
Browse files

finito

parent ca6b65b7
Branches master
No related tags found
No related merge requests found
......@@ -2,27 +2,43 @@
#include <stdlib.h>
#include <string.h>
typedef struct _element {
char c;
struct _element *next;
} element;
int main(int argc, char **argv) {
// read user input
char str[16];
scanf("%s", str);
int len = strlen(str);
// cli args
if (argc == 1) {
printf("need more arguments");
return EXIT_FAILURE;
float moyenne(int i,float*arr){
if(i==14){
return arr[i]/15;
}else{
return arr[i]/15+moyenne(i+1,arr);
}
}
void swap(float*a,float*b){
float tmp=*a;
*a=*b;
*b=tmp;
}
void inverse(int i,float* arr){
if(i>7){
return;
}
swap(&arr[i],&arr[14-i]);
inverse(i+1,arr);
}
int main() {
float change[15];
for(int i=0;i<15;i++){
scanf("%f",&change[i]);
}
// printf("")
printf("moyenne: %.3f\ninversion:\n",moyenne(0,change));
inverse(0,change);
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
// do stuff with atof/atoi (argv[++i])
}
for(int i=0;i<15;i++){
printf("%.2f\n",change[i]);
}
return EXIT_SUCCESS;
......
main: main.o
gcc -o $@ $< -fsanitize=address -fsanitize=leak
cc=gcc
CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak
LFLAGS=-fsanitize=address -fsanitize=leak -lm
main.o: main.c
gcc -c -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak $<
clean:
rm -f *.o main
rebuild: clean main
\ No newline at end of file
main: tree.o main.o
$(cc) -o $@ $^ $(CFLAGS) $(LFLAGS)
clean:
rm -f *.o *.x
File added
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"
typedef struct _element {
char c;
struct _element *next;
} element;
int main() {
int main(int argc, char **argv) {
node_t *tree = bst_create();
int input[15];
// read user input
char str[16];
scanf("%s", str);
int len = strlen(str);
// cli args
if (argc == 1) {
printf("need more arguments");
return EXIT_FAILURE;
for(int i=0;i<15;i++){
scanf("%d",&input[i]);
tree = bst_insert(tree, input[i]);
}
// manque de temps je fais l'affichage seulement
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
// do stuff with atof/atoi (argv[++i])
}
}
bst_to_array_incr(tree);
bst_to_array_decr(tree);
return EXIT_SUCCESS;
bst_destroy(tree);
}
\ No newline at end of file
#include "tree.h"
// typedef struct _node_t {
// int val;
// struct _node_t *left;
// struct _node_t *right;
// } node_t;
node_t *test(node_t *tree) {
if (tree->left != NULL) {
printf("%d\n",tree->val);
return bst_find_min_node(tree->left);
} else {
return tree;
}
}
void incr(node_t *tree) {
if (tree == NULL) {
return;
}
incr(tree->left);
printf("%d\n", tree->val);
incr(tree->right);
}
void swap(node_t* a,node_t*b){
node_t tmp=*a;
*a=*b;
*b=tmp;
}
void reverse_tree(node_t* tree){
if(tree==NULL){
return;
}
if(tree->right!=NULL&&tree->left!=NULL){
swap(tree->right,tree->left);
}
reverse_tree(tree->right);
reverse_tree(tree->left);
}
int* bst_to_array_incr(node_t *tree){
//pas assez de temps donc j'affiche seulement les valeurs dans l'ordre
printf("ordre croissant:\n");
incr(tree);
printf("\n");
// int *values=malloc(sizeof(int)*15);
return NULL;
}
int* bst_to_array_decr(node_t *tree){
//pareil
printf("ordre decroissant (presque):\n");
reverse_tree(tree);
incr(tree);
// int *values=malloc(sizeof(int)*15);
return NULL;
}
// peite gauche et plus grand droite
bool bst_is_empty(node_t *tree) { return tree == NULL; }
static node_t *create_node(int val) {
node_t *node = malloc(sizeof(node_t));
if (node == NULL) { // memory error
return NULL;
}
node->left = NULL;
node->right = NULL;
node->val = val;
return node;
}
node_t *bst_create() { return NULL; }
void bst_destroy(node_t *tree) {
if (!bst_is_empty(tree)) {
if (tree->left != NULL) {
bst_destroy(tree->left);
}
if (tree->right != NULL) {
bst_destroy(tree->right);
}
free(tree);
}
}
void bst_print(node_t *tree, int depth) { // affiche l'arbre ordre croissant
if (tree == NULL) {
return;
}
bst_print(tree->left, depth + 1);
for (int i = 0; i < depth; i++) {
printf("-------");
}
printf(" %d \n\n", tree->val);
bst_print(tree->right, depth + 1);
}
// insertion de val dans l'arbre et retourne l'arbre (ou NULL si problème)
node_t *bst_insert(node_t *tree, int val) {
if (bst_is_empty(tree)) { // root
tree = create_node(val);
} else {
if (val > tree->val) { // right
if (tree->right == NULL) {
tree->right = create_node(val);
} else {
tree->right = bst_insert(tree->right, val);
}
} else { // left
if (tree->left == NULL) {
tree->left = create_node(val);
} else {
tree->left = bst_insert(tree->left, val);
}
}
}
return tree;
}
// la valeur val est-elle présente dans l'arbre?
bool bst_is_present(node_t *tree, int val) {
return bst_search(tree, val) != NULL;
}
// retourne le noeud où la valeur val se trouve (NULL si absent)
node_t *bst_search(node_t *tree, int val) {
if (!bst_is_empty(tree)) {
if (val == tree->val) { // found it
return tree;
} else if (val > tree->val) { // right
return bst_search(tree->right, val);
} else { // left
return bst_search(tree->left, val);
}
}
return NULL;
}
// l'arbre est-il un arbre binaire de recherche?
bool bst_is_bst(node_t *tree) {
if(tree==NULL){
return true;
}
if (tree->left != NULL && tree->left->val > tree->val) {
return false;
}
if (tree->right != NULL && tree->right->val < tree->val) {
return false;
}
return bst_is_bst(tree->left) && bst_is_bst(tree->right);
}
node_t *bst_find_min_node(node_t *tree) {
if (tree->left != NULL) {
return bst_find_min_node(tree->left);
} else {
return tree;
}
}
int bst_find_min(node_t *tree) { return bst_find_min_node(tree)->val; }
node_t *bst_find_max_node(node_t *tree) {
if (tree->right != NULL) {
return bst_find_min_node(tree->right);
} else {
return tree;
}
}
int bst_find_max(node_t *tree) { return bst_find_max_node(tree)->val; }
int count_child(node_t *node) {
int nb = (node->left != NULL) + (node->right != NULL);
// printf("%d child(s)\n", nb);
return nb;
}
node_t *bst_delete(node_t *tree, int val) {
if (!bst_is_empty(tree)) {
if (val > tree->val) {
tree->right = bst_delete(tree->right, val); // go right
} else if (val < tree->val) {
tree->left = bst_delete(tree->left, val); // go left
// found node
} else if (count_child(tree) == 2) {
node_t *next = bst_find_min_node(tree->right);
tree->val = next->val;
tree->right = bst_delete(next, next->val);
} else { // 1 or 0 childs
node_t *tmp = tree;
if (tmp->left != NULL) { // left
tree = tree->left;
} else if (tmp->right != NULL) { // right
tree = tree->right;
} else {
tree = NULL;
}
free(tmp);
}
}
return tree;
}
#ifndef _STACK_H_
#define _STACK_H_
#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
typedef struct _node_t {
int val;
struct _node_t *left;
struct _node_t *right;
} node_t;
//BINARY SEARCH TREE (BST)
// Fonctions de création, destruction et affichage
node_t *bst_create(); // création d'un arbre vide (retourne NULL)
void bst_destroy(node_t *tree); // détruit l'arbre et vide la mémoire
void bst_print(node_t *tree, int depth); // affiche l'arbre (voir plus bas)
// insertion de val dans l'arbre et retourne l'arbre (ou NULL si problème)
node_t *bst_insert(node_t *tree, int val);
// efface le premier élément contenant la valeur val dans l'arbre
// et retourne l'arbre (ne fait rien si val est absente)
node_t *bst_delete(node_t *tree, int val);
// la valeur val est-elle présente dans l'arbre?
bool bst_is_present(node_t *tree, int val);
// retourne le noeud où la valeur val se trouve (NULL si absent)
node_t *bst_search(node_t *tree, int val);
// l'arbre est-il un arbre binaire de recherche?
bool bst_is_bst(node_t *tree);
// retourne le noeud avec la valeur minimale de l'arbre (NULL s'il y a pas)
node_t *bst_find_min_node(node_t *tree);
// retourne la valeur la plus petite stockée dans l'arbre (ou MIN_INT)
int bst_find_min(node_t *tree);
// retourne le noeud avec la valeur maximale de l'arbre (NULL s'il y a pas)
node_t *bst_find_max_node(node_t *tree);
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
int bst_find_max(node_t *tree);
int count_child(node_t *node);
int* bst_to_array_incr(node_t *tree);
int* bst_to_array_decr(node_t *tree);
int* bst_find_min_and_free(node_t *tree);
#endif
\ No newline at end of file
File added
......@@ -2,28 +2,45 @@
#include <stdlib.h>
#include <string.h>
typedef struct _element {
char c;
struct _element *next;
} element;
int main(int argc, char **argv) {
// read user input
char str[16];
scanf("%s", str);
int len = strlen(str);
// cli args
if (argc == 1) {
printf("need more arguments");
return EXIT_FAILURE;
int my_strlen(char* str){
int i=0;
while(str[i]!='\0'){
i++;
}
return i;
}
void hamming_distance(char*s1,char*s2){
int len1=my_strlen(s1);
if(len1!=my_strlen(s2)){
printf("Distance %s - %s: Impossible\n",s1,s2);
return;
}
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
// do stuff with atof/atoi (argv[++i])
int cnt=0;
for(int i;i<len1;i++){
if(s1[i]!=s2[i]){
cnt++;
}
}
printf("Distance %s - %s: %d\n",s1,s2,cnt);
}
int main() {
char str0[40];
char str1[40];
char str2[40];
scanf("%s", str0);
scanf("%s", str1);
scanf("%s", str2);
hamming_distance(str0,str1);
hamming_distance(str0,str2);
hamming_distance(str1,str2);
printf("\n");
return EXIT_SUCCESS;
}
\ No newline at end of file
File added
......@@ -2,28 +2,88 @@
#include <stdlib.h>
#include <string.h>
typedef struct _element {
char c;
struct _element *next;
} element;
int main(int argc, char **argv) {
// read user input
char str[16];
scanf("%s", str);
int len = strlen(str);
// cli args
if (argc == 1) {
printf("need more arguments");
return EXIT_FAILURE;
typedef struct _etudiante {
char *name;
int hash;
} etudiante;
etudiante* new_student(char* name){
int len=strlen(name);
int hash=0;
for(int i=0;i<len;i++){
hash+=(43*name[i])%len;
}
etudiante *etu=malloc(sizeof(etudiante));
etu->name=name;
etu->hash=hash;
return etu;
}
etudiante* shallow_copy(etudiante* stu){
return stu;
}
etudiante* deep_copy(etudiante* stu){
etudiante *etu=malloc(sizeof(etudiante));
etu->name=stu->name;
etu->hash=stu->hash;
return etu;
}
void print_student(etudiante* stu){
printf("%s, %d\n",stu->name,stu->hash);
}
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
// do stuff with atof/atoi (argv[++i])
}
void delete_student(etudiante* stu){
free(stu);
}
void set_name(etudiante* stu,char* newName){
int len=strlen(newName);
int hash=0;
for(int i=0;i<len;i++){
hash+=(43*newName[i])%len;
}
stu->name=newName;
stu->hash=hash;
}
void doStuffWithStudent(char* name){
printf("welcome %s\n",name);
etudiante *s = new_student(name);
print_student(s);
etudiante* s_deep = deep_copy(s);
set_name(s_deep, "deep_student");
print_student(s_deep);
print_student(s);
etudiante* s_shallow = shallow_copy(s);
set_name(s_shallow, "shallow_student");
print_student(s);
print_student(s_deep);
print_student(s_shallow);
//free the students !
delete_student(s);
delete_student(s_deep);
printf("\n");
}
int main() {
char name1[40];
char name2[40];
scanf("%s", name1);
scanf("%s", name2);
doStuffWithStudent(name1);
doStuffWithStudent(name2);
return EXIT_SUCCESS;
}
\ No newline at end of file
main: main.o
gcc -o $@ $< -fsanitize=address -fsanitize=leak
main.o: main.c
gcc -c -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak $<
clean:
rm -f *.o main
rebuild: clean main
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _element {
char c;
struct _element *next;
} element;
int main(int argc, char **argv) {
// read user input
char str[16];
scanf("%s", str);
int len = strlen(str);
// cli args
if (argc == 1) {
printf("need more arguments");
return EXIT_FAILURE;
}
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
// do stuff with atof/atoi (argv[++i])
}
}
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment