Skip to content
Snippets Groups Projects
Commit 389c2b90 authored by mathias.catala's avatar mathias.catala
Browse files

"first commit"

parent 97ae5251
Branches
No related tags found
No related merge requests found
File added
File added
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <time.h>
#define INIT_DIT_CAPACITY 5
typedef struct {
int day, month, year;
} date_t;
typedef struct {
char name[40];
char surname[40];
date_t birthday;
int phone_number;
} person_t;
typedef struct {
int size;
int capacity;
person_t *content;
} directory_t;
// Prints a person record with the format "Name Surname, day-month-year, phone number"
// Parameter : p - the person
void person_print(person_t p){
printf("%s %s, %d/%d/%d,%d \n",p.name,p.surname,p.birthday.day,p.birthday.month,p.birthday.year,p.phone_number);
}
// Creates a new directory with size 0 and capacity INIT_DIT_CAPACITY
void dir_init(directory_t *dir){
dir->size=0;
dir->capacity= INIT_DIT_CAPACITY;
if (dir->content == NULL) {
dir->content = malloc(sizeof(person_t) * dir->capacity);
} else {
dir->content = realloc(dir->content, sizeof(person_t) * dir->capacity);
}
}
directory_t dir_create(){
directory_t newDirectory;
newDirectory.content = NULL;
dir_init(&newDirectory);
return newDirectory;
}
// Free the memory of the directory, set size and capacity to 0 and content to NULL
// Parameter : dir - the directory
void dir_free(directory_t *dir){
dir->capacity=0;
dir->size=0;
free(dir->content);
}
// Prints the content of a directory each line with the format "[n] person"
// with n being the position in the directory
// Parameters : dir - the directory
void dir_print(directory_t *dir,int n){
if(n==-1){
for (int i = 0; i < dir->size; i++)
{
person_t t = dir->content[i];
printf("[%d] ",i);
person_print(t);
}
}else {
person_t t = dir->content[n];
printf("[%d] ",n);
person_print(t);
}
}
// Erase a directory by setting its size to 0, but letting its capacity untouched
// Parameter : dir - the directory
void dir_erase(directory_t *dir){
dir->size=0;
}
// Add a person to the directory if its capacity is not reached and increase its size
// If the capacity is reached, attempt to increase the capacity before adding the person
// Parameters : dir - the directory, p - the person
// Returns : 0 if success, 1 otherwise
int dir_add_entry(directory_t *dir, person_t p){
if (dir->size >= dir->capacity) {
person_t *new_p = realloc(dir->content, (dir->size + 1) * sizeof(person_t));
if (new_p == NULL) {
return 1;
}
dir->content = new_p;
dir->capacity += 1;
}
dir->content[dir->size] = p;
dir->size++;
return 0;
}
// Parameter : dir - the direcory
// Returns : 0 if success, 1 otherwise
int dir_fill(directory_t *dir){
dir_init(dir);
if(dir_add_entry(dir, (person_t){"Jean", "Paul", (date_t){18, 12, 1967}, 12346890})) return 1;
if(dir_add_entry(dir, (person_t){"Arthur", "Rimbaud", (date_t){1, 1, 2002}, 73645})) return 1;
if(dir_add_entry(dir, (person_t){"Paul", "Albuquerque", (date_t){23, 6, 1902}, 28736474})) return 1;
if(dir_add_entry(dir, (person_t){"Christophe", "Charpilloz", (date_t){11, 4, 2008}, 76253})) return 1;
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);
}
// Delete the entry at position n of the directory if n is smaller than
// the size of the directory, does nothing otherwise
// Parameters : dir - the direcory, n - the position
void dir_delete_entry(directory_t *dir, int n){
if(n < dir->size){
for (int i = 0; i< dir->size-1; i++)
{
dir->content[i]=dir->content[i+1];
}
dir->size--;
}
printf("Contact removed from Annuary.\n");
}
// Searches for an entry with a given surname
// Parameters : dir - the directory, surname - the surname to search
// Returns : the index of a matching entry, a negative number if no match is found
int dir_search_surname(directory_t* dir, char* surname){
for (int i = 0; i < dir->size; i++)
{
person_t p = dir->content[i];
if (strcmp(surname, dir->content[i].surname) == 0) {
return i;
}
}
return -1;
}
int main(){
directory_t directory = dir_create();
dir_fill(&directory);
printf("Available commands : add, del, show, search, quit,\n");
char command[255]=" ";
while(strcmp(command,"quit")!=0){
printf("> ");
scanf("%s", &command);
if(strcmp(command,"add")==0){
person_t new_p;
printf("Enter the name: ");
scanf("%s",&new_p.name);
printf("Enter the 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);
printf("Enter the month of birth: ");
scanf("%s",&new_p.birthday.month);
printf("Enter the year of birth: ");
scanf("%s",&new_p.birthday.year);
printf("Enter the phone number: ");
scanf("%s",&new_p.phone_number);
dir_add_entry(&directory,new_p);
}else if(strcmp(command,"del")==0){
int index;
printf("Enter index to remove: ");
scanf("%s",&index);
dir_delete_entry(&directory,index);
printf("Contact removed from Annuary.\n");
}else if(strcmp(command,"show")==0){
char options[40]="";
printf("enter the name to show or all if you want to show entire directory: \n" );
scanf("%s",options);
int index=-1;
if (strcmp(options,"all")==0){
dir_print(&directory,index);
}else {
index= dir_search_surname(&directory,options);
dir_print(&directory,index);
};
}else if(strcmp(command,"search")==0){
int index=-1;
char Surname[255];
printf(" Enter Surname: ");
scanf("%s",&Surname);
dir_search_surname(&directory,Surname);
if(index>=0){
printf("The Surname index is : %d\n",index);
}else{
printf("The Surname index doesn't exist");
}
}
else if(strcmp(command,"quit")==0){
dir_erase(&directory);
printf("Directory erased");
}
}
dir_free(&directory);
}
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
char *name;
int thread_id;
bool is_valid;
}T_THREAD;
void print_struct_elements(T_THREAD *T) {
printf("\nContents of a structure %s are:\n", T->name);
printf("thread_id: %d\n",T->thread_id);
printf("is_valid: %d\n", T->is_valid);
}
int main(void) {
T_THREAD T1 = {"T1", 123, 1};
T_THREAD T2 = {"T2", 456, 0};
print_struct_elements(&T1);
print_struct_elements(&T2);
return 0;
}
File added
/*
* C Program to Reverse the String using Recursion
*/
#include <stdio.h>
#include <string.h>
void reverse_print(char* str);
void reverse(char [], int, int);
int count(char* str, char c);
int main()
{
char str1[20]= "salut";
int size;
char* str = "Salut les amis\n";
size = strlen(str1);
reverse(str1, 0, size - 1);
reverse_print(str);
printf("Number: %d\n",count(str,'a'));
printf("The string after reversing is: %s\n", str1);
return 0;
}
void reverse_print(char* str){
if(*str){
reverse_print(str+1);
printf("%c",*str);
}
}
int count(char* str, char c){
if(*str!='\0'){
return count(str+1,c)+(*str==c);
}
}
void reverse(char str1[], int index, int size)
{
char temp;
temp = str1[index];
str1[index] = str1[size - index];
str1[size - index] = temp;
if (index == size / 2)
{
return;
}
reverse(str1, index + 1, size);
}
\ No newline at end of file
File added
#include <stdio.h>
#include <math.h>
void main(){
float rayon,puissance;
float perimeter;
float area;
printf("Entrez le rayon du cercle:");
scanf("%f",&rayon);
puissance = rayon*rayon;
perimeter = 2 * 3.14 * rayon;
area = 3.14 * puissance;
printf("le perimètre est: %f \n l'aire est: %f",perimeter,area);
}
\ No newline at end of file
File added
#include <stdio.h>
#include <math.h>
int main(){
float nombre1,nombre2,result;
printf("Entrez le nombre 1");
scanf("%f",&nombre1);
printf("Entrez le nombre 2");
scanf("%f",&nombre2);
result=nombre1*nombre2;
printf("le résultat de la multiplication %f",result);
}
\ No newline at end of file
File added
#include <stdio.h>
#include <math.h>
int main(){
int n =10,p =4;
long q =2;
float x =1.75;
int final = n;
printf("n+q: %d\n", final);
printf("n+x: %f\n", n+x);
printf("n mod p + q: %d\n", n%p + q);
printf("n<p: %d\n", n<p);
printf("n>=p: %d\n", n>=p);
printf("n>q: %d\n", n>p);
printf("q + 3 * (n > p): %d\n", q + 3 * (n > p));
printf("q && n: %d\n",q && n);
printf("(q-2)&& (n-10): %d\n", (q-2)&&(n-10));
printf("x * (q==2) %d\n", x * (q==2));
printf("x * (q=5) %d\n", x * (q=5));
}
\ No newline at end of file
File added
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(){
int max_number;
int user_number=0;
int random_number;
int counter=0;
printf("Entrez le nombre maximum:");
scanf("%d",&max_number);
srand(time(NULL));
while(random_number> max_number|| random_number<0){random_number= rand();
}
printf("nombre généré : %d\n",random_number);
printf("Entrez le nombre désirer\n");
scanf("%d",&user_number);
while(user_number!=random_number){
if(user_number<0 || user_number>max_number ){
printf("Erreur, Entrez un nombre supérieur à 0 et inférieur au nombre maximum\n");
printf("Entrez le nombre désirer : ");
scanf("%d",&user_number);
}
if(user_number< random_number){
printf("plus grand\n");
counter++;
}else{
printf("plus petit\n");
counter++;
}
printf("Entrez une nouvelle valeur: ");
scanf( "%d",&user_number);
}
printf("Voici le nombre d'essai effectués: %d",counter);
}
\ No newline at end of file
File added
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(){
int year;
printf("Entrez une année:");
scanf("%d",&year);
if (year % 4 ==0){
if (year % 100 == 0 ){
if(year % 400 ==0){
printf("l'année: %d est bisextile",year);
}else printf("l'année n'est pas bisextile");
}else printf("l'année: %d est bisextile",year);
}else {
printf("l'année n'est pas bisextile");
}
}
\ No newline at end of file
File added
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main(){
int P_number;
printf("Entrez un nombre:");
scanf("%d",&P_number);
for(int i=0; i<sqrt(P_number); i++){
if (P_number % 2 ==0){
printf("n'est pas un premier");
break;
}else{
printf("est un premier");
break;
}
}
}
\ No newline at end of file
File added
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
bool is_prime(int n){
for(int i=0; i<sqrt(n); i++){
if (n % 2 ==0){
return false;
break;
}else{
return true;
break;
}
}
return true;
}
void swap(int* a, int* b){
int temp ;
temp = *a;
*a = *b;
*b = temp;
}
int ppcm(int a, int b){
int ppcm;
if(i% a==0 && i%a ==0 ){
ppcm = i;
}for (int i=a*b;i>=a;i--){
}
return ppcm;
}
int pgcd(int a, int b){
int pgcd;
for (int i=1; i<= a && i <= b;i++){
if (a % i == 0 && b % i == 0){
pgcd = i;
}
}
return pgcd;
}
int main(){
int a = 12;
int b = 60;
printf("IsPrime : %d\n",is_prime(6));
swap(&a,&b);
printf("Valeur de a : %d et valeur de b: %d\n",a,b);
printf("le pgcd de %d et %d est: %d\n",a,b,pgcd(a,b));
printf("le ppcm de %d et %d est: %d ",a,b,ppcm(a,b));
return 0;
}
\ 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