Ajout des TP

This commit is contained in:
stiti
2024-02-01 13:55:03 +01:00
parent 4fe273c309
commit 113583b37a
228 changed files with 7094 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
{
"files.associations": {
"repetition.h": "c",
"mktime.C": "cpp"
},
"editor.inlineSuggest.showToolbar": "onHover"
}

View File

@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
int main() {
long int nanoseconde;
int seconde;
printf("Entrez le nombre de nanosecondes : ");
scanf("%ld", &nanoseconde);
seconde = nanoseconde / 1000000000;
printf("Le nombre de secondes est : %d \n", seconde);
return 0;
}

View File

@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
int main() {
int seconde;
long int nanoseconde;
printf("Entrez le nombre de secondes : ");
scanf("%d",&seconde);
nanoseconde = seconde * 1000000000;
printf("Le nombre de nanosecondes est : %ld \n", nanoseconde);
return 0;
}

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}

View File

@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/export/home/an23/stiti/Desktop/EntrainementDS2/SUJET1 /EXO 1",
"program": "/export/home/an23/stiti/Desktop/EntrainementDS2/SUJET1 /EXO 1/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

View File

@@ -0,0 +1,37 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wconversion",
"-Wnull-dereference",
"-Wsign-conversion"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false
}

View File

@@ -0,0 +1,14 @@
# BUT PRINCIPAL
repetition_ou_pas : main.o repetition.o
gcc -ansi -pedantic -o repetition_ou_pas repetition.o main.o
repetition.o : repetition.c
gcc -ansi -pedantic -c repetition.c
main.o : main.c repetition.h
gcc -ansi -pedantic -c main.c
clean :
rm -f main.o repetition.o
.PHONY : clean

View File

@@ -0,0 +1,25 @@
#include <stdlib.h>
#include <stdio.h>
#include "../EXO 1/repetition.h"
int main(int argc,char* argv[]){ /* Ou "char** argv" */
int i;
long* tab;
tab = (long*) malloc(argc*sizeof(long));
/* Création d'un tableau de argc - 1 élément (Autrement dit, création d'un tableau du nombre d'éléments donner en argument au programme sans compter le a.out)*/
for(i=1;i<argc;i++){
tab[i-1] = strtol(argv[i],NULL,10);
}
if(repetition(tab,argc-1)==0){
printf("les valeurs sont différentes");
}
if(repetition(tab,argc-1)==1){
printf("les valeurs sont identiques");
}
free(tab);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,13 @@
#include <stdlib.h>
#include <stdio.h>
int repetition(long tab[], int taille){
int i;
for(i=0;i<taille-1;i++){
if(tab[i]!=tab[i+1]){
return 0; /*False*/
}
}
return 1; /*True*/
}

View File

@@ -0,0 +1,6 @@
#ifndef REPETITION_H
#define REPETITION_H
int repetition(long tab[], int taille);
#endif /* REPETITION_H */

View File

@@ -0,0 +1,26 @@
#include <stdlib.h>
#include <stdio.h>
int main(void){
FILE* fichier;
char couleur;
int i,a=5;
fichier = fopen("image","r");
if(fichier==NULL){
fputs("Erreur d'ouverture du fichier",stderr);
return EXIT_FAILURE;
}else{
while(feof(fichier)!=1){
a = fread(&couleur,1,1,fichier);
if(couleur == 0){
printf("\n");
}else{
printf("\33[48;5;%hhum \33[m",couleur);
}
}
}
fclose(fichier);
return EXIT_SUCCESS;
}

Binary file not shown.

View File

@@ -0,0 +1,23 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <math.h>
int main(void){
int i;
struct timeval tv1;
struct timeval tv2;
gettimeofday(&tv1,NULL);
for(i=0;i<1000000;i++){
sqrt(2);
}
gettimeofday(&tv2,NULL);
printf("%dμs",tv2.tv_usec-tv1.tv_usec);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,34 @@
### VARIABLES ###
CC = gcc
CFLAGS = -ansi \
-pedantic
EXE = valeurs_nulles
OFILES = recherche.o \
main.o
### BUT PAR DEFAUT ###
but : ${EXE}
### REGLES ESSENTIELLES ###
recherche.o : recherche.c
main.o : main.c recherche.h
${EXE} : ${OFILES}
$(CC) $(CFLAGS) -o ${EXE} ${OFILES}
### REGLES OPTIONNELLES ###
clean :
-rm -f ${OFILES}
mrproper : clean but
### BUTS FACTICES ###
.PHONY : but clean mrproper
### FIN ###

View File

@@ -0,0 +1,15 @@
#include <stdlib.h>
#include <stdio.h>
#include "recherche.h"
int main(int argc, char* argv[]){ /* ou char** */
int i;
double* tab;
tab = (double*) malloc(argc*sizeof(double));
for(i=0;i<argc-1;i++){
tab[i] = strtod(argv[i+1],NULL);
}
printf("%d valeurs nulles\n",recherche(tab,argc-1));
}

View File

@@ -0,0 +1,13 @@
#include <stdlib.h>
#include <stdio.h>
int recherche(double tab[], int taille){
int valeurs_null = 0, i;
for(i=0;i<taille;i++){
if(tab[i]==0){
valeurs_null++;
}
}
return valeurs_null;
}

View File

@@ -0,0 +1,6 @@
#ifndef RECHERCHE_H
#define RECHERCHE_h
int recherche(double tab[], int taille);
#endif /* RECHERCHE_H */

Binary file not shown.

View File

@@ -0,0 +1,41 @@
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]){
char donnees_fichier1, donnees_fichier2;
int reponse = 1;
FILE* fichier1 = fopen(argv[1],"r");
FILE* fichier2 = fopen(argv[2],"r");
/* Message d'erreur s'il manque des arguments // s'il y a trop d'arguments */
if(argc != 3){
fputs("Erreur : Le nombre d'arguments est incorrect !\n",stderr);
return EXIT_FAILURE;
}
if(fichier1 == NULL){
fputs("Erreur : Impossible d'accéder au fichier 1\n",stderr);
return EXIT_FAILURE;
}
if(fichier2 == NULL){
fputs("Erreur : Impossible d'accéder au fichier 2\n",stderr);
return EXIT_FAILURE;
}
while(feof(fichier1)!=1 && feof(fichier1)!=1){
fread(&donnees_fichier1,1,1,fichier1);
fread(&donnees_fichier2,1,1,fichier2);
if(donnees_fichier1 != donnees_fichier2){
printf("Fichiers pas identiques !\n");
reponse = 0;
break;
}
}
if(reponse == 1){
printf("Fichiers identiques !\n");
}
fclose(fichier1);
fclose(fichier2);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,5 @@
#include <stdlib.h>
int main(void){
}

View File

@@ -0,0 +1,5 @@
#include <stdlib.h>
int main(void){
int a;
}

View File

@@ -0,0 +1,21 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void){
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 500000000; /* 0.5 secondes*/
fputs("E",stderr);
nanosleep(&req,NULL);
fputs("R",stderr);
nanosleep(&req,NULL);
fputs("R",stderr);
nanosleep(&req,NULL);
fputs("E",stderr);
nanosleep(&req,NULL);
fputs("U",stderr);
nanosleep(&req,NULL);
fputs("R",stderr);
}

View File

@@ -0,0 +1,49 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <date au format dd/mm/aa>\n", argv[0]);
return 1;
}
struct tm date;
time_t timestamp;
/* Analyse de la date fournie en argument*/
if (sscanf(argv[1], "%d/%d/%d", &date.tm_mday, &date.tm_mon, &date.tm_year) != 3) {
fprintf(stderr, "Format de date invalide. Utilisez le format dd/mm/aa.\n");
return 1;
}
/*La fonction mktime attend une année en nombre d'années depuis 1900*/
date.tm_year += 100;
/*La fonction mktime attend le mois en nombre de mois depuis janvier*/
date.tm_mon--;
/*Initialisation des autres champs de la structure tm*/
date.tm_hour = 0;
date.tm_min = 0;
date.tm_sec = 0;
date.tm_wday = 0;
date.tm_yday = 0;
date.tm_isdst = -1; /*Indéterminé*/
/*Conversion de la structure tm en timestamp*/
timestamp = mktime(&date);
if (timestamp == -1) {
fprintf(stderr, "Erreur lors de la conversion de la date en timestamp.\n");
return 1;
}
/*Conversion du timestamp en structure tm pour obtenir le jour de la semaine*/
localtime_r(&timestamp, &date);
/*Affichage du jour de la semaine*/
const char *jours[] = {"dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"};
printf("C'est un %s.\n", jours[date.tm_wday]);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fputs("Usage: ./a.out <valeur> <unité>\n",stderr);
return EXIT_FAILURE;
}
double valeur = atof(argv[1]);
char unite = argv[2][0];
if (unite == 'c' || unite == 'C') {
double pouces = valeur / 2.54;
printf("%.2fin\n", pouces);
} else if (unite == 'i' || unite == 'I') {
double centimetres = valeur * 2.54;
printf("%.2fcm\n", centimetres);
} else {
printf("Unité invalide. Utilisez 'cm' ou 'in'.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int resultat_lancer;
unsigned char valeur_aleatoire;
size_t octet_lu;
/* On ouvre le fichier /dev/random */
FILE* fichier_random = fopen("/dev/random", "r");
/* On vérifie qu'il s'ouvre bien*/
if (fichier_random == NULL) {
fputs("Erreur lors de l'ouverture de /dev/random", stderr);
return EXIT_FAILURE;
}
/* On lit un octet depuis /dev/random */
octet_lu = fread(&valeur_aleatoire, sizeof(valeur_aleatoire), 1, fichier_random);
/*On vérifie l'octet lu */
if (octet_lu != 1) {
fputs("Erreur lors de la lecture depuis /dev/random", stderr);
fclose(fichier_random);
return EXIT_FAILURE;
}
/* On ferme le fichier /dev/random */
fclose(fichier_random);
/* On calcule le résultat du dé (entre 1 et 6) */
resultat_lancer = (valeur_aleatoire % 6) + 1;
/* On affiche le résultat */
printf("%d\n", resultat_lancer);
return EXIT_SUCCESS;
}