Skip to content
Extraits de code Groupes Projets
Valider 9e42200e rédigé par CharlyBVO's avatar CharlyBVO
Parcourir les fichiers

fix references aux codes src dans les sections langage C et fichiers

parent f69424fa
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 706 ajouts et 0 suppression
Valeurs de : g1 g2 loc loc2
===================================================
[main1] 0 1 0 1
[main2] 3 1 1252 1234
[f-1a] 7 -1 0 2
[f-1b] 8 0 1 2
[main-for-1] 8 -1 2 1234
[f-2a] 17 -4 0 2
[f-2b] 18 -3 4 2
[main-for-2] 18 -2 3 1234
[f-7a] 19 -49 0 2
[f-7b] 20 -48 49 2
[main3] 60 3 1252 1234
/**************************************
* stack.c
*
* Programme d'exemple implémentant un stack comme structure
* chaînée
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct fraction_t {
int num;
int den;
} fraction;
///AAA
typedef struct node_t
{
struct fraction_t *data;
struct node_t *next;
} node;
struct node_t *stack; // sommet de la pile
// ajoute un élément au sommet de la pile
void push(struct fraction_t *f)
{
struct node_t *n;
n=(struct node_t *)malloc(sizeof(struct node_t));
if(n==NULL)
exit(EXIT_FAILURE);
n->data = f;
n->next = stack;
stack = n;
}
// retire l'élément au sommet de la pile
struct fraction_t * pop()
{
if(stack==NULL)
return NULL;
// else
struct fraction_t *r;
struct node_t *removed=stack;
r=stack->data;
stack=stack->next;
free(removed);
return (r);
}
///BBB
// affiche le contenu de la pile
void display()
{
struct node_t *t;
t = stack;
while(t!=NULL) {
if(t->data!=NULL) {
printf("Item at addr %p : Fraction %d/%d Next %p\n",t,t->data->num,t->data->den,t->next);
}
else {
printf("Bas du stack %p\n",t);
}
t=t->next;
}
}
// exemple
int main(int argc, char *argv[]) {
struct fraction_t demi={1,2};
struct fraction_t tiers={1,3};
struct fraction_t quart={1,4};
struct fraction_t zero={0,1};
// initialisation
stack = (struct node_t *)malloc(sizeof(struct node_t));
stack->next=NULL;
stack->data=NULL;
display();
push(&zero);
display();
push(&demi);
push(&tiers);
push(&quart);
display();
struct fraction_t *f=pop();
if(f!=NULL)
printf("Popped : %d/%d\n",f->num,f->den);
return(EXIT_SUCCESS);
}
///CCC
Bas du stack 0x100100080
Item at addr 0x100100090 : Fraction 0/1 Next 0x100100080
Bas du stack 0x100100080
Item at addr 0x1001000c0 : Fraction 1/4 Next 0x1001000b0
Item at addr 0x1001000b0 : Fraction 1/3 Next 0x1001000a0
Item at addr 0x1001000a0 : Fraction 1/2 Next 0x100100090
Item at addr 0x100100090 : Fraction 0/1 Next 0x100100080
Bas du stack 0x100100080
Popped : 1/4
/**************************************
* static.c
*
* Programme d'exemple d'utilisation de static
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
void f(int i) {
static int sum;
}
int main(int argc, char *argv[]) {
f(1);
f(2);
return(EXIT_SUCCESS);
}
/**************************************
* concat.c
*
* Programme d'exemple pour concaténer
* deux chaînes de caractères
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int * init_vector(int length, int val) {
int *ret=malloc(length*sizeof(int));
for(int i=0;i<length;i++) {
*(ret+i)=val;
}
return(ret);
}
int *unit_matrix(int length) {
int *r=malloc(length*length*sizeof(int));
for(int i=0;i<length;i++) {
for(int j=0;j<length;j++) {
if(i==j) {
*(r+(i*length)+j)=1;
}
else {
*(r+(i*length)+j)=0;
}
}
}
return (r);
}
int main(int argc, char *argv[]) {
int *v;
int *m;
v=init_vector(SIZE,1252);
m=unit_matrix(SIZE);
for(int i=0;i<SIZE;i++) {
printf(" v[%d]=%d",i,*(v+i));
}
printf("\n");
for(int i=0;i<SIZE;i++) {
for(int j=0;j<SIZE;j++) {
printf(" m[%d,%d]=%d",i,j,*(m+(i*SIZE)+j));
}
printf("\n");
}
printf("\n");
free(v);
free(m);
return(EXIT_SUCCESS);
}
/**************************************
* strcpy.c
*
* Programme d'exemple pour strcpy
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
///AAA
#include <string.h>
char *duplicate(char * str) {
int i;
size_t len=strlen(str);
char *ptr=(char *)malloc(sizeof(char)*(len+1));
if(ptr!=NULL) {
for(i=0;i<len+1;i++) {
*(ptr+i)=*(str+i);
}
}
return ptr;
}
///BBB
char *duplicate2(char * str) {
int i;
size_t len=strlen(str);
char str2[len+1];
for(i=0;i<len+1;i++) {
str2[i]=*(str+i);
}
return str2;
}
///CCC
int main(int argc, char *argv[]) {
char str[]="Hello, world";
char *str2=duplicate(str);
char *str3=duplicate2(str);
printf("str: %s\n",str);
printf("str2: %s\n",str2);
printf("str3: %s\n",str3);
return(EXIT_SUCCESS);
}
/**************************************
* structargs.c
*
* exemple de structures comme arguments
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
/* Return t2-t1 in microseconds */
long timeval_diff(struct timeval *t2, struct timeval *t1)
{
long diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
return (diff);
}
///AAA
#define MILLION 1000000
struct large_t {
int i;
char str[MILLION];
};
int sum(struct large_t s1, struct large_t s2) {
return (s1.i+s2.i);
}
int sumptr(struct large_t *s1, struct large_t *s2) {
return (s1->i+s2->i);
}
int main(int argc, char *argv[]) {
struct timeval tvStart, tvEnd;
int err;
int n;
struct large_t one={1,"one"};
struct large_t two={1,"two"};
n=sum(one,two);
n=sumptr(&one,&two);
///BBB
err=gettimeofday(&tvStart, NULL);
if(err!=0)
exit(EXIT_FAILURE);
err=sum(one,two);
err=gettimeofday(&tvEnd, NULL);
if(err!=0)
exit(EXIT_FAILURE);
printf("Durée de sum : %ld microsecondes\n",timeval_diff(&tvEnd, &tvStart));
err=gettimeofday(&tvStart, NULL);
if(err!=0)
exit(EXIT_FAILURE);
err=sumptr(&one,&two);
err=gettimeofday(&tvEnd, NULL);
if(err!=0)
exit(EXIT_FAILURE);
printf("Durée de sumptr : %ld microsecondes\n",timeval_diff(&tvEnd, &tvStart));
}
/**************************************
* typecast.c
*
* Programme d'exemple de conversion entre
* types de données
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
///AAA
int i=1;
float f=1e20;
double d=1e100;
printf("i [int]: %d, [float]:%f, [double]:%f\n",i,(float)i,(double)i);
printf("f [int]: %d, [float]:%f, [double]:%f\n",(int)f,f,(double)f);
printf("d [int]: %d, [float]:%f, [double]:%f\n",(int)d,(float)d,d);
printf("sizeof -> int:%d float:%d double:%d\n",(int)sizeof(int), (int)sizeof(float), (int)sizeof(double));
///BBB
return(EXIT_SUCCESS);
}
/**************************************
* union.c
*
* Programme d'exemple de union
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
///AAA
struct s_t {
int i;
char c;
} s;
union u_t {
int i;
char c;
} u;
///BBB
typedef enum { INTEGER, CHAR } Type;
typedef struct
{
Type type;
union {
int i;
char c;
} x;
} Value;
///XXX
int main(int argc, char *argv[]) {
///CCC
// initialisation
s.i=1252;
s.c='A';
u.i=1252;
// u contient un int
u.c='Z';
// u contient maintenant un char (et u.i est perdu)
///DDD
printf("sizeof(s_t) : %ld\n",sizeof(s));
printf("sizeof(u_t) : %ld\n",sizeof(u));
///EEE
u.i=1252;
printf("char : %c\n", u.c);
///FFF
Value v;
v.type=INTEGER;
v.x.i=1252;
///GGG
return(EXIT_SUCCESS);
}
/**************************************
* var.c
*
* Programme d'exemple pour localiser
* les variables en mémoire
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// affiche sur la sortie standard
printf("Hello, world!\n");
return(EXIT_SUCCESS);
}
/**************************************
* vector.c
*
* Programme de manipulation de vecteurs
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
///AAA
struct vector_t {
int size;
float *v;
};
// initialise le vecteur à la valeur du réel
struct vector_t * init(int, float) ;
// récupère le nième élément
float get(struct vector_t *, int) ;
// fixe la valeur du nième élément
void set(struct vector_t *, int , float);
// supprime un vecteur
void destroy(struct vector_t *);
///BBB
struct vector_t * init(int size, float val)
{
struct vector_t *v=(struct vector_t *)malloc(sizeof(struct vector_t));
v->v=(float *)malloc(size*sizeof(float));
v->size=size;
for(int i=0;i<size;i++) {
*(v->v+i)=val;
}
return v;
}
float get(struct vector_t *v, int i) {
return *(v->v+i);
}
void set(struct vector_t *v, int i, float val) {
if(i<v->size)
*(v->v+i)=val;
}
void destroy(struct vector_t *v) {
free(v->v);
free(v);
}
int main(int argc, char *argv[]) {
struct vector_t *v1=init(10,2.0);
printf("v1[%d]=%f\n",3,get(v1,3));
set(v1,3,17.0);
printf("v1[%d]=%f\n",3,get(v1,3));
destroy(v1);
return(EXIT_SUCCESS);
}
#
# Makefile for source code
#
# Builds all .c files, assuming they are all independant and runs them
# to collect the output on stdout
#
# Automatically builds some of the examples to be included in the text
#
###AAA
myprog: main.o min.o
gcc -std=c99 -o myprog main.o min.o
main.o: main.c min.h
gcc -std=c99 -c main.c
min.o: min.c min.h
gcc -std=c99 -c min.c
###BBB
/**************************************
* errno.c
*
* Programme d'exemple pour errno
*
**************************************/
///AAA
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if(setenv(NULL,NULL,1)!=0) {
fprintf(stderr,"Erreur : errno=%d %s\n",errno,strerror(errno));
}
if(setenv("PATH=","/usr/bin",1)!=0) {
fprintf(stderr,"Erreur : errno=%d %s\n",errno,strerror(errno));
}
}
/**************************************
* fctptr.c
*
* Programme d'exemple pour les pointeurs
* vers des fonctions
*
**************************************/
#include <stdio.h>
#include <stdlib.h>
///AAA
int g=1;
int debug_level;
void empty (char *str) {
return;
}
void oneline(char *str) {
fprintf(stderr,"debug: %s\n",str);
}
void detailed(char *str) {
fprintf(stderr, "debug: %s\n",str);
fprintf(stderr,"g=%d\n",g);
}
void (* debug_print[])(char *) = { empty,
oneline,
detailed };
int main(int argc, char *argv[]) {
if(argc!=2)
return(EXIT_FAILURE);
debug_level=atoi(argv[1]);
if((debug_level<0) || (debug_level>2) )
return(EXIT_FAILURE);
printf("fct debug_print : %p\n",debug_print[debug_level]);
(debug_print[debug_level])("Hello");
return(EXIT_SUCCESS);
}
///BBB
/**************************************
* main.c
*
* Programme d'exemple pour le linker
*
**************************************/
#include "min.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float f1=3.45;
float f2=-4.12;
printf("Minimum(%f,%f)=%f\n",f1,f2,min(f1,f2));
return(EXIT_SUCCESS);
}
/**************************************
* min.c
*
* Programme d'exemple pour le linker
*
**************************************/
#include "min.h"
float min(float a, float b) {
if(a<b)
return a;
else
return b;
}
/**************************************
* min.h
*
**************************************/
#ifndef _MIN_H_
#define _MIN_H_
float min(float, float);
#endif /* _MIN_H */
/**************************************
* module.c
*
**************************************/
#include "module.h"
static float min(float, float);
int num1=0; // accessible hors de module.c
extern int num2; // définie dans un autre module
static int num3=1252; // accessible uniquement dans ce module
float vmin(int n, float *ptr) {
float *p=ptr;
float m=*ptr;
for(int i=1;i<n;i++) {
m=min(m,*p);
p++;
}
return m;
}
static float min(float a, float b) {
if(a<b)
return a;
else
return b;
}
/**************************************
* module.h
*
**************************************/
#ifndef _MODULE_H_
#define _MODULE_H_
float vmin(int, float *);
#endif /* _MODULE_H */
/**************************************
* stresep.c
*
* Implementation partielle de strtol
*
**************************************/
///AAA
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int mystrtol(const char *restrict str,
char **restrict endptr,
int base) {
int val;
int i=0;
int err=false;
while(!err && *(str+i)!='\0')
{
if(!isdigit(*(str+i))) {
err=true;
*endptr=(char *)(str+i);
}
i++;
}
// ...
return val;
}
///BBB
int main(int argc, char *argv[])
{
char *p, *s;
long li;
s = "1252m";
li = mystrtol(s,&p,10);
if(p!=NULL) {
printf("Caractère erronné : %c\n",*p);
// p pointe vers le caractère en erreur
}
printf("Valeur convertie : %s -> %ld\n",s,li);
return(EXIT_SUCCESS);
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter