diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c50808a229292826f092c6c5b242bd22c37b29e8 Binary files /dev/null and b/.DS_Store differ diff --git a/myTP/.DS_Store b/myTP/.DS_Store index cd31e5f093edd61b84de9210277591ee35a7d660..ca64d0137e4facf36131a396aab0c82bf69740bd 100644 Binary files a/myTP/.DS_Store and b/myTP/.DS_Store differ diff --git a/myTP/Stack_C/stack b/myTP/Stack_C/stack new file mode 100755 index 0000000000000000000000000000000000000000..0c83e35ff1dccddb8a64ffdd1ee2322e6a595f4a Binary files /dev/null and b/myTP/Stack_C/stack differ diff --git a/myTP/Stack_C/stack.c b/myTP/Stack_C/stack.c new file mode 100644 index 0000000000000000000000000000000000000000..3f91f221e02d4d37ae1eedf34096a0599256034a --- /dev/null +++ b/myTP/Stack_C/stack.c @@ -0,0 +1,81 @@ +#include <stdio.h> + +#define MAX_SIZE 10cx fdk + + +struct Stack { + int arr[MAX_SIZE]; + int top; +}; + +// Function to display the current state of the stack +void display(struct Stack *stack) { + printf("Stack:\n"); + for (int i = MAX_SIZE - 1; i >= 0; i--) { + if (i <= stack->top) { + printf("| %3d |\n", stack->arr[i]); + } else { + printf("| |\n"); + } + printf("-------\n"); + } + printf("\n"); +} + + +// Function to initialize the stack +void initialize(struct Stack *stack) { + stack->top = -1; +} + +// Function to check if the stack is empty +int isEmpty(struct Stack *stack) { + return stack->top == -1; +} + +// Function to check if the stack is full +int isFull(struct Stack *stack) { + return stack->top == MAX_SIZE - 1; +} + +// Function to push an element onto the stack +void push(struct Stack *stack, int value) { + if (isFull(stack)) { + printf("Stack overflow\n"); + } else { + stack->arr[++stack->top] = value; + printf("%d pushed to stack\n", value); + // Display the current state of the stack + display(stack); + } +} + +// Function to pop an element from the stack +int pop(struct Stack *stack) { + if (isEmpty(stack)) { + printf("Stack underflow\n"); + return -1; // Assuming -1 represents an error or an invalid value + } else { + // Display the element being popped + printf("%d popped from stack\n", stack->arr[stack->top]); + return stack->arr[stack->top--]; + } +} + + +int main() { + struct Stack myStack; + initialize(&myStack); + + push(&myStack, 10); + push(&myStack, 20); + push(&myStack, 30); + + printf("Top element is %d\n", myStack.arr[myStack.top]); + pop(&myStack); + pop(&myStack); + + printf("Is the stack empty? %s\n", isEmpty(&myStack) ? "Yes" : "No"); + + return 0; +} diff --git a/myTP/Stack_dynamic_C/a.out b/myTP/Stack_dynamic_C/a.out new file mode 100755 index 0000000000000000000000000000000000000000..4860dda810739fb49980491d6f32174affed58eb Binary files /dev/null and b/myTP/Stack_dynamic_C/a.out differ diff --git a/myTP/Stack_dynamic_C/stack_dyn.c b/myTP/Stack_dynamic_C/stack_dyn.c new file mode 100644 index 0000000000000000000000000000000000000000..3a0efba54549bbcd90866d7b85074d0a3e698616 --- /dev/null +++ b/myTP/Stack_dynamic_C/stack_dyn.c @@ -0,0 +1,112 @@ +#include <stdio.h> +#include <stdlib.h> + +// Define a structure for stack nodes +struct Node { + int data; + struct Node* next; +}; + +// Define a structure for the stack +struct Stack { + struct Node* top; +}; + +// Function to initialize an empty stack +void initializeStack(struct Stack* stack) { + stack->top = NULL; +} + +// Function to check if the stack is empty +int isEmpty(struct Stack* stack) { + return stack->top == NULL; +} + +// Function to push a new element onto the stack +void push(struct Stack* stack, int data) { + // Create a new node + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + if (newNode == NULL) { + fprintf(stderr, "Memory allocation error\n"); + exit(EXIT_FAILURE); + } + + // Set the data and link to the current top + newNode->data = data; + newNode->next = stack->top; + + // Update the top to the new node + stack->top = newNode; +} + +// Function to pop an element from the stack +int pop(struct Stack* stack) { + if (isEmpty(stack)) { + fprintf(stderr, "Stack underflow\n"); + exit(EXIT_FAILURE); + } + + // Pop the top element and update the top + struct Node* temp = stack->top; + int data = temp->data; + stack->top = temp->next; + + // Free the memory of the popped node + free(temp); + + return data; +} + +// Function to get the top element of the stack without popping +int peek(struct Stack* stack) { + if (isEmpty(stack)) { + fprintf(stderr, "Stack is empty\n"); + exit(EXIT_FAILURE); + } + + return stack->top->data; +} + +// Function to display the elements of the stack +void display(struct Stack* stack) { + if (isEmpty(stack)) { + printf("Stack is empty\n"); + return; + } + + printf("Stack: "); + struct Node* current = stack->top; + while (current != NULL) { + printf("%d ", current->data); + current = current->next; + } + printf("\n"); +} + +// Function to free the memory used by the stack +void destroyStack(struct Stack* stack) { + while (!isEmpty(stack)) { + pop(stack); + } +} + +int main() { + struct Stack myStack; + initializeStack(&myStack); + + push(&myStack, 1); + push(&myStack, 2); + push(&myStack, 3); + + display(&myStack); + + printf("Top element: %d\n", peek(&myStack)); + + printf("Popped element: %d\n", pop(&myStack)); + + display(&myStack); + + destroyStack(&myStack); + + return 0; +} diff --git a/myTP/TP11/.DS_Store b/myTP/TP11/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c08365eb9c830a76982daa9ba013812484a573aa Binary files /dev/null and b/myTP/TP11/.DS_Store differ diff --git a/myTP/TP11/exo1 b/myTP/TP11/exo1 old mode 100644 new mode 100755 index 921ae220acbebe0ffdff7fa766e956d95862d378..ee694134dc4489fa3ee92daf30ae002568b763a4 Binary files a/myTP/TP11/exo1 and b/myTP/TP11/exo1 differ diff --git a/myTP/TP11/exo1.dSYM/Contents/Resources/DWARF/exo1 b/myTP/TP11/exo1.dSYM/Contents/Resources/DWARF/exo1 index 915531298d328044ef9816cfec1cc53112b11f7e..5424db7eafd085420c03abbcd313d9f0bcb78e96 100644 Binary files a/myTP/TP11/exo1.dSYM/Contents/Resources/DWARF/exo1 and b/myTP/TP11/exo1.dSYM/Contents/Resources/DWARF/exo1 differ diff --git a/myTP/TP12/.DS_Store b/myTP/TP12/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5b5b65f99040dfe3121045e0bd7b8537c8fdc925 Binary files /dev/null and b/myTP/TP12/.DS_Store differ diff --git a/myTP/TP12/a.out b/myTP/TP12/a.out new file mode 100755 index 0000000000000000000000000000000000000000..7642218a383ca9e3ef1f5f89147c48bdb41a9c33 Binary files /dev/null and b/myTP/TP12/a.out differ diff --git a/myTP/TP12/a.out.dSYM/Contents/Info.plist b/myTP/TP12/a.out.dSYM/Contents/Info.plist new file mode 100644 index 0000000000000000000000000000000000000000..3679a65b99a800bdaf3837059265c6d04e11096e --- /dev/null +++ b/myTP/TP12/a.out.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> + <dict> + <key>CFBundleDevelopmentRegion</key> + <string>English</string> + <key>CFBundleIdentifier</key> + <string>com.apple.xcode.dsym.a.out</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundlePackageType</key> + <string>dSYM</string> + <key>CFBundleSignature</key> + <string>????</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + </dict> +</plist> diff --git a/myTP/TP12/a.out.dSYM/Contents/Resources/DWARF/a.out b/myTP/TP12/a.out.dSYM/Contents/Resources/DWARF/a.out new file mode 100644 index 0000000000000000000000000000000000000000..ae23d19bfc8238917bb0a3b806476dbe17dd30b2 Binary files /dev/null and b/myTP/TP12/a.out.dSYM/Contents/Resources/DWARF/a.out differ diff --git a/myTP/TP12/exo1 b/myTP/TP12/exo1 old mode 100644 new mode 100755 index 43eef9692d74500af7726f8daaee835b0fd42796..3d64b9a82eaaac39ce094a5efe02d04daa391ee1 Binary files a/myTP/TP12/exo1 and b/myTP/TP12/exo1 differ diff --git a/myTP/TP12/exo1.c b/myTP/TP12/exo1.c index c7119492dc50111bc5bbb612241d7ddd06933adf..d631a49c601af0d625bf752d5571d7525b7cdaa7 100644 --- a/myTP/TP12/exo1.c +++ b/myTP/TP12/exo1.c @@ -134,7 +134,8 @@ if(dir_add_entry(dir, (person_t){"Christophe", "Charpilloz", (date_t){11, 4, 200 if(dir_add_entry(dir, (person_t){"Emily", "Dickinson", (date_t){19, 8, 1950}, 636363})) return 1; if(dir_add_entry(dir, (person_t){"Ada", "Lovelace", (date_t){12, 12, 2022}, 12345})) return 1; if(dir_add_entry(dir, (person_t){"Qwertz", "Uiop", (date_t){1, 1, 562}, 67879})) return 1; -printf("current dir size : %lu \n", dir->size); +//printf("current dir size : %d \n", dir->size); +return 0; } @@ -185,26 +186,24 @@ int main(){ char command[255]=" "; while(strcmp(command,"quit")!=0){ printf("> "); - scanf("%s", &command); + scanf("%s", command); if(strcmp(command,"add")==0){ person_t new_p; printf("Enter the name: "); - scanf("%s",&new_p.name); + scanf("%s",new_p.name); printf("Enter the surname: "); - scanf("%s",&new_p.surname); + scanf("%s",new_p.surname); printf("Enter the day of birth: "); - scanf("%s",&new_p.birthday.day); - printf("Enter the day of birth: "); - scanf("%s",&new_p.birthday.day); + scanf("%i",&new_p.birthday.day); printf("Enter the month of birth: "); - scanf("%s",&new_p.birthday.month); + scanf("%d",&new_p.birthday.month); printf("Enter the year of birth: "); - scanf("%s",&new_p.birthday.year); + scanf("%d",&new_p.birthday.year); printf("Enter the phone number: "); - scanf("%s",&new_p.phone_number); + scanf("%d",&new_p.phone_number); dir_add_entry(&directory,new_p); @@ -213,7 +212,7 @@ int main(){ int index; printf("Enter index to remove: "); - scanf("%s",&index); + scanf("%d",&index); dir_delete_entry(&directory,index); printf("Contact removed from Annuary.\n"); @@ -240,7 +239,7 @@ int main(){ int index=-1; char Surname[255]; printf(" Enter Surname: "); - scanf("%s",&Surname); + scanf("%s",Surname); dir_search_surname(&directory,Surname); diff --git a/myTP/TP12/exo1.dSYM/Contents/Info.plist b/myTP/TP12/exo1.dSYM/Contents/Info.plist new file mode 100644 index 0000000000000000000000000000000000000000..ea858782144d29f3d6a4a740f9536fd4f66f6feb --- /dev/null +++ b/myTP/TP12/exo1.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> + <dict> + <key>CFBundleDevelopmentRegion</key> + <string>English</string> + <key>CFBundleIdentifier</key> + <string>com.apple.xcode.dsym.exo1</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundlePackageType</key> + <string>dSYM</string> + <key>CFBundleSignature</key> + <string>????</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + </dict> +</plist> diff --git a/myTP/TP12/exo1.dSYM/Contents/Resources/DWARF/exo1 b/myTP/TP12/exo1.dSYM/Contents/Resources/DWARF/exo1 new file mode 100644 index 0000000000000000000000000000000000000000..655b82feaa09fd88f0211970a2443f3f95766798 Binary files /dev/null and b/myTP/TP12/exo1.dSYM/Contents/Resources/DWARF/exo1 differ diff --git a/myTP/TP12/exo1.dSYM/Contents/Resources/Relocations/aarch64/exo1.yml b/myTP/TP12/exo1.dSYM/Contents/Resources/Relocations/aarch64/exo1.yml new file mode 100644 index 0000000000000000000000000000000000000000..158a4375a43d44e146cfc5e9911cfff5c615dc2e --- /dev/null +++ b/myTP/TP12/exo1.dSYM/Contents/Resources/Relocations/aarch64/exo1.yml @@ -0,0 +1,17 @@ +--- +triple: 'arm64-apple-darwin' +binary-path: exo1 +relocations: + - { offsetInCU: 0x26, offset: 0x26, size: 0x8, addend: 0x0, symName: _person_print, symObjAddr: 0x0, symBinAddr: 0x100002C9C, symSize: 0x6C } + - { offsetInCU: 0x4D, offset: 0x4D, size: 0x8, addend: 0x0, symName: _person_print, symObjAddr: 0x0, symBinAddr: 0x100002C9C, symSize: 0x6C } + - { offsetInCU: 0x72, offset: 0x72, size: 0x8, addend: 0x0, symName: _dir_init, symObjAddr: 0x6C, symBinAddr: 0x100002D08, symSize: 0x90 } + - { offsetInCU: 0x96, offset: 0x96, size: 0x8, addend: 0x0, symName: _dir_create, symObjAddr: 0xFC, symBinAddr: 0x100002D98, symSize: 0x2C } + - { offsetInCU: 0xBE, offset: 0xBE, size: 0x8, addend: 0x0, symName: _dir_free, symObjAddr: 0x128, symBinAddr: 0x100002DC4, symSize: 0x38 } + - { offsetInCU: 0xE2, offset: 0xE2, size: 0x8, addend: 0x0, symName: _dir_print, symObjAddr: 0x160, symBinAddr: 0x100002DFC, symSize: 0x178 } + - { offsetInCU: 0x16B, offset: 0x16B, size: 0x8, addend: 0x0, symName: _dir_erase, symObjAddr: 0x2D8, symBinAddr: 0x100002F74, symSize: 0x18 } + - { offsetInCU: 0x18F, offset: 0x18F, size: 0x8, addend: 0x0, symName: _dir_add_entry, symObjAddr: 0x2F0, symBinAddr: 0x100002F8C, symSize: 0xEC } + - { offsetInCU: 0x1E2, offset: 0x1E2, size: 0x8, addend: 0x0, symName: _dir_fill, symObjAddr: 0x3DC, symBinAddr: 0x100003078, symSize: 0x4A8 } + - { offsetInCU: 0x20B, offset: 0x20B, size: 0x8, addend: 0x0, symName: _dir_delete_entry, symObjAddr: 0x884, symBinAddr: 0x100003520, symSize: 0xD0 } + - { offsetInCU: 0x259, offset: 0x259, size: 0x8, addend: 0x0, symName: _dir_search_surname, symObjAddr: 0x954, symBinAddr: 0x1000035F0, symSize: 0x10C } + - { offsetInCU: 0x2C7, offset: 0x2C7, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0xA60, symBinAddr: 0x1000036FC, symSize: 0x438 } +... diff --git a/myTP/TP13/exo1.c b/myTP/TP13/exo1.c index b4c8fc07ec72044b17731389345cade0f4024b4c..7b958fd43ba7a4fce037e20a4efb09addafce416 100644 --- a/myTP/TP13/exo1.c +++ b/myTP/TP13/exo1.c @@ -23,8 +23,6 @@ int main() } void reverse_print(char* str){ - - if(*str){ reverse_print(str+1); diff --git a/myTP/TP13/exo2 b/myTP/TP13/exo2 new file mode 100755 index 0000000000000000000000000000000000000000..5fa24bf5b70e0113f661268602f6c3d7dbb84488 Binary files /dev/null and b/myTP/TP13/exo2 differ diff --git a/myTP/TP13/exo2.c b/myTP/TP13/exo2.c index 64316b8bfe61ff075779780d517ebaaab1f299f6..cdcb9355cae05ef831299aeba2f724571842cda0 100644 --- a/myTP/TP13/exo2.c +++ b/myTP/TP13/exo2.c @@ -1,39 +1,153 @@ #include <stdio.h> -#include <string.h> +#include <stdlib.h> +#include <stdbool.h> +#include <time.h> +// Enumeration representing parts of the maze +typedef enum { WALL, PATH, START, END, USED_PATH } maze_part_t; -typedef enum{WALL, PATH, START, END} maze_part_t; +// Structure representing the maze +typedef struct { + maze_part_t** data; + int n; +} maze_t; -typedef struct{ -maze_part_t** data; -int n; -}maze_t; - - -// Affiche un labyrinthe dans le console +// Function declarations void maze_print(maze_t maze); -// Cette fonction alloue la mémoire pour un labyrinthe -// Initialise toutes les cases à WALL maze_t maze_alloc(int n); -// Libère la mémoire d'un labyrinthe -// met maze.data à NULL void maze_free(maze_t maze); - void maze_create(maze_t maze); +bool dig_maze(maze_t maze, int i, int j); +bool solve_maze(maze_t maze, int i, int j); + +int main() { + // Seed for randomization + srand(time(NULL)); + + // Example usage + int maze_size = 10; // You can change the size as needed + maze_t my_maze = maze_alloc(maze_size); + + // Create and print the maze + maze_create(my_maze); + dig_maze(my_maze,1,1); + maze_print(my_maze); + + // Solve the maze (bonus) + if (solve_maze(my_maze, 0, my_maze.n - 1)) { + printf("\nMaze solved!\n"); + maze_print(my_maze); + } else { + printf("\nNo solution found.\n"); + } + + // Free allocated memory + maze_free(my_maze); + + return 0; +} - - -int main(){ - - maze_print(maze_t maze) +// Function to print the maze +void maze_print(maze_t maze) { + for (int i = 0; i < maze.n; ++i) { + for (int j = 0; j < maze.n; ++j) { + switch (maze.data[i][j]) { + case WALL: + printf("█ "); + break; + case PATH: + printf(" "); + break; + case START: + printf("S "); + break; + case END: + printf("E "); + break; + case USED_PATH: + printf(". "); + break; + } + } + printf("\n"); + } } -void maze_print(maze_t maze){ +// Function to allocate memory for a maze and initialize it with WALLs +maze_t maze_alloc(int n) { + maze_t maze; + maze.n = n; + + // Allocate memory for the maze + maze.data = (maze_part_t**)malloc(n * sizeof(maze_part_t*)); + for (int i = 0; i < n; ++i) { + maze.data[i] = (maze_part_t*)malloc(n * sizeof(maze_part_t)); + } + + // Initialize all cells to WALL + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + maze.data[i][j] = WALL; + } + } + + return maze; +} - int size = maze.n; +// Function to free memory of a maze +void maze_free(maze_t maze) { + for (int i = 0; i < maze.n; ++i) { + free(maze.data[i]); + } + free(maze.data); +} - printf("size : %d", maze.data); +// Function to create the maze following specified criteria +void maze_create(maze_t maze) { + // Set the start and end points + maze.data[maze.n - 1][maze.n / 2] = START; + maze.data[0][maze.n / 2] = END; + // Dig tunnels + dig_maze(maze, maze.n - 1, maze.n / 2); +} +// Function to recursively dig tunnels in the maze +bool dig_maze(maze_t maze, int i, int j) { + // Check if the position is out of bounds or has been visited + if (i < 0 || i >= maze.n || j < 0 || j >= maze.n || maze.data[i][j] != WALL) { + return false; + } + + // If the current position is the end, return true + if (maze.data[i][j] == END) { + return true; + } + + // Change the current position to PATH + maze.data[i][j] = PATH; + + // Define directions (north, south, east, west) + int directions[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; + + // Shuffle the directions randomly + for (int k = 0; k < 4; ++k) { + int rand_dir = rand() % 4; + int new_i = i + directions[rand_dir][0]; + int new_j = j + directions[rand_dir][1]; + + // Recursively dig in the chosen direction + if (dig_maze(maze, new_i, new_j)) { + return true; + } + } + + // If all directions have been tried and no path found, backtrack + maze.data[i][j] = USED_PATH; + return false; +} -} \ No newline at end of file +// Function to solve the maze (bonus) +bool solve_maze(maze_t maze, int i, int j) { + // TODO: Implement maze solving logic using backtracking +} diff --git a/myTP/TP15/Main b/myTP/TP15/Main new file mode 100755 index 0000000000000000000000000000000000000000..928210da032857034c73151a5c5f0caf8d87b890 Binary files /dev/null and b/myTP/TP15/Main differ diff --git a/myTP/TP15/Makefile b/myTP/TP15/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9c8cd560c22e526d95ba46350252b6982edf9135 --- /dev/null +++ b/myTP/TP15/Makefile @@ -0,0 +1,23 @@ +# Compiler +CC = gcc +# Compiler flags +CFLAGS = -Wall + +# Default target: Build the executable and clean object files +all: Main clean_objects + +# Build the executable named "Main" +Main: calculator.o fractions.o + $(CC) $(CFLAGS) -o Main calculator.o fractions.o + +# Compile calculator.c to create calculator.o +calculator.o: calculator.c fractions.h + $(CC) $(CFLAGS) -c calculator.c + +# Compile fractions.c to create fractions.o +fractions.o: fractions.c fractions.h + $(CC) $(CFLAGS) -c fractions.c + +# Target to clean object files +clean_objects: + rm -f *.o diff --git a/myTP/TP15/calculator.c b/myTP/TP15/calculator.c new file mode 100644 index 0000000000000000000000000000000000000000..6c28c387aa51c3ccabea874a260115dba0113ea9 --- /dev/null +++ b/myTP/TP15/calculator.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include "fractions.h" + +int main(int argc, char *argv[]) { + + struct Fraction fraction1 = {1, 2}; + struct Fraction fraction2 = {3, 4}; + struct Fraction result; + + struct Fraction myfraction = {128, 64}; + + printf("Original fraction: "); + display_fraction(myfraction); + + // Reduce the fraction + + printf("Reduced fraction: "); + display_fraction(reduce_fraction(myfraction)); + printf("\n"); + + // power the fraction + printf("powered fraction: "); + display_fraction(power_fraction(myfraction,5)); + printf("\n"); + + + // in place fraction + printf("\n"); + printf("In place add function: "); + fraction_add_inplace(&result, &fraction1, &fraction2); + display_fraction(result); + + // in place fraction + printf("\n"); + printf("In place sub function: "); + fraction_sub_inplace(&result, &fraction1, &fraction2); + display_fraction(result); + + + + + //myfraction = power_fraction(myfraction,5); + //display_fraction(myfraction); + return 0; +} diff --git a/myTP/TP15/executable b/myTP/TP15/executable new file mode 100755 index 0000000000000000000000000000000000000000..9ac584f7c05a51d2e6b0cfc1e465049d5bc18262 Binary files /dev/null and b/myTP/TP15/executable differ diff --git a/myTP/TP15/fractions.c b/myTP/TP15/fractions.c new file mode 100644 index 0000000000000000000000000000000000000000..a32246abdb9374a2290fc91c6336fb862625d88c --- /dev/null +++ b/myTP/TP15/fractions.c @@ -0,0 +1,79 @@ +#include "fractions.h" +#include <stdio.h> +#include <math.h> + + +// Function implementations +void display_fraction(struct Fraction frac) { + + printf("%d/%d ", frac.numerator,frac.denominator); + +} +int calculate_gcd(int a, int b) { + int gcd, remainder; + + if (a < 0) a = -a; + if (b < 0) b = -b; + + while (b != 0) { + remainder = a % b; + a = b; + b = remainder; + } + + gcd = a; + + return gcd; + +} + +struct Fraction reduce_fraction(struct Fraction frac) { + + int gcd = calculate_gcd(frac.denominator,frac.numerator); + + if (gcd != 0) { // Ensure the denominator is not zero before dividing + frac.denominator /= gcd; + frac.numerator /= gcd; + } + return frac; +} + + + +struct Fraction power_fraction(struct Fraction base, int exponent) { + + if (base.numerator <= 0 || exponent < 0) { + printf("Invalid input: numerator or exponent is non-positive.\n"); + return base; + } + + base.denominator = pow(base.denominator,exponent); + base.numerator = pow(base.numerator,exponent); + + return base; +} +void fraction_add_inplace(struct Fraction *result, const struct Fraction *fraction1, const struct Fraction *fraction2) { + // Add fractions: (a/b) + (c/d) = (ad + bc) / bd + + result->denominator = fraction1->denominator * fraction2->denominator; + result->numerator = (fraction1->numerator* fraction2->denominator) + (fraction1->denominator*fraction2->numerator); + + + *result = reduce_fraction(*result); + +} +void fraction_sub_inplace(struct Fraction *result, const struct Fraction *fraction1, const struct Fraction *fraction2) { + // sub fractions: (a/b) - (c/d) = (ad - bc) / bd + + result->denominator = fraction1->denominator * fraction2->denominator; + result->numerator = (fraction1->numerator* fraction2->denominator) - (fraction1->denominator*fraction2->numerator); + + + + *result = reduce_fraction(*result); + + +} + + +// ... other function implementations ... diff --git a/myTP/TP15/fractions.h b/myTP/TP15/fractions.h new file mode 100644 index 0000000000000000000000000000000000000000..f3cd96d6e22213b42eed312d8e0c67aeec053e8e --- /dev/null +++ b/myTP/TP15/fractions.h @@ -0,0 +1,21 @@ +#ifndef FRACTIONS_H +#define FRACTIONS_H + +struct Fraction { + int numerator; + int denominator; +}; + +// Function prototypes +void display_fraction(struct Fraction frac); +struct Fraction reduce_fraction(struct Fraction frac); +int calculate_gcd(int a, int b); +struct Fraction power_fraction(struct Fraction base, int exponent); +void fraction_add_inplace(struct Fraction *result, const struct Fraction *fraction1, const struct Fraction *fraction2) ; +void fraction_sub_inplace(struct Fraction *result, const struct Fraction *fraction1, const struct Fraction *fraction2) ; + + + +// ... other function prototypes ... + +#endif // FRACTIONS_H diff --git a/myTP/TP15/test b/myTP/TP15/test new file mode 100755 index 0000000000000000000000000000000000000000..75379895bbd21ecd36f1c5139e591594be95182f Binary files /dev/null and b/myTP/TP15/test differ diff --git a/myTP/TP15/test.c b/myTP/TP15/test.c new file mode 100644 index 0000000000000000000000000000000000000000..29c881940b6674f6c4d2957d2d91b6370236930a --- /dev/null +++ b/myTP/TP15/test.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <math.h> + +struct Fraction { + double numerator; + double denominator; +}; + +// Tolerance for considering a floating-point number as zero +#define EPSILON 1e-9 + +struct Fraction power_fraction(struct Fraction base, int exponent) { + if (fabs(base.numerator) < EPSILON || exponent < 0) { + printf("Invalid input: numerator is zero or exponent is non-positive.\n"); + return base; + } + + // Handle negative exponent + if (exponent < 0) { + double temp = base.numerator; + base.numerator = base.denominator; + base.denominator = temp; + + base.denominator = pow(base.denominator, -exponent); + base.numerator = pow(base.numerator, -exponent); + } else { + // Positive exponent + base.denominator = pow(base.denominator, exponent); + base.numerator = pow(base.numerator, exponent); + } + + return base; +} + +void display_fraction(struct Fraction frac) { + printf("%lf/%lf\n", frac.numerator, frac.denominator); +} + +int main() { + struct Fraction myfraction = {20.0, 6.0}; + + printf("Original fraction: "); + display_fraction(myfraction); + + // Raise the fraction to the power of -2 + myfraction = power_fraction(myfraction, -2); + + printf("Fraction after raising to the power of -2: "); + display_fraction(myfraction); + + return 0; +} diff --git a/myTP/TP6/tp_06_encore_fonctions.pdf b/myTP/TP6/tp_06_encore_fonctions.pdf deleted file mode 100644 index 25c5b64da51faf75e88a4df74ab01666ba2412f9..0000000000000000000000000000000000000000 Binary files a/myTP/TP6/tp_06_encore_fonctions.pdf and /dev/null differ diff --git a/myTP/read_file/my_file.txt b/myTP/read_file/my_file.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/myTP/read_file/test b/myTP/read_file/test new file mode 100755 index 0000000000000000000000000000000000000000..82e718253bc475f4c78ec4cb06226e8f9b5da9d9 Binary files /dev/null and b/myTP/read_file/test differ diff --git a/myTP/read_file/test.c b/myTP/read_file/test.c new file mode 100644 index 0000000000000000000000000000000000000000..c070d8e252e8d2f78a71fe2cc9bc2217bce82ff0 --- /dev/null +++ b/myTP/read_file/test.c @@ -0,0 +1,37 @@ +#include <stdio.h> + +int main() { + + FILE *fp; + char text[100]; + + // Open the file in write mode ("w") + fp = fopen("my_file.txt", "w"); + + // Check if file opened successfully + if (fp == NULL) { + printf("Error opening file!\n"); + return 1; + } + + // Prompt user for text to write + //printf("Enter text to write: "); + //fgets(text, sizeof(text), stdin); + + // Write text to file + //fprintf(fp, "%s", text); + + + // read file + + fgets(text,100,fp); + + printf("%s",text); + + // Close the file + fclose(fp); + + printf("Successfully wrote text to 'my_file.txt'\n"); + + return 0; +}