Commit 76c19978 by Benjamin Lefebvre

### TD de langage C

parent f01e264a
 # TD1 de Langage C ## INTRODUCTION ### 1. ```c if (var == 0) return (var >= autreVar ? var : autreVar) else return (var <= encoreAutreVar ? var : encoreAutreVar) ``` ### 2. ```c 10 + b a + c ``` ### 3. ```c printf("Ligne 1\n"); printf("Ligne 3\n"); ``` ### 4. ```c printf("Ligne 1\n"); printf("Ligne 3\n"); printf("Ligne 4\n"); ``` ## EXERCICE 1 analyseur.c ```c char gLigne [1024]; int SuiteSymbol [1024]; int ErrLexicale = 0; extern void AnalyseLexicale(void); extern int AnalyseSyntaxique(int *); main() { for (;;) { ... AnalyseLexicale(); if (!ErrLexicale) { ... } else if (AnalyseSyntaxique(&i)) ... } } } ``` lex.c ```c extern char gLigne []; extern int SuiteSymbol []; extern int ErrLexicale; void AnalyseLexicale() { ... } ``` syntax.c ```c externe char gLigne []; extern int SuiteSymbol []; extern int ErrLexicale; int AnalyseSyntaxique(int *i) { ... } static int Instruction(int *i) { ... } static int Expression(int *i) { ... } static int Facteur(int *i) { ... } ... ``` ## EXERCICE 2 ### 1. ```c #define abs(x) ((x) > 0? (x) : -(x)) ``` ### 2. ```c #define min(a,b) (a < b ? a : b) ``` ### 3. ```c #define max(a,b) (a > b ? a : b) ``` ### 4. ```c #define min3(a, b, c) min(a, min(b, c)) ``` ### 5. ```c #define max3(a, b, c) max(a, max(b, c)) ``` ### 6. ```c #define absmin(a, b) min(abs(a), abs(b)) ``` OU ```c #define absmin(a, b) min(((a) > 0 ? (a) : -(a)),((b) > 0 ? (b) : -(b))) ``` ### 7. ```c #define absmax(a, b) max(abs(a), abs(b)) ``` OU ```c #define absmax(a, b) max(((a) > 0 ? (a) : -(a)), ((b) > 0 ? (b) : -(b))) ``` ## EXERCICE 3 racinesEqn.h ```c #include /* fonction de calcul du discriminant */ float dis(float a, float b, float c) { return (b * b - 4 * a * c); } /* fonction calculant les racines reelles */ void rac2(float r, float r1) { printf ("2 racines reelles : %f et %f\n", r + r1, r - r1); } /* fonction calculant les racines complexes */ void complex(float r, float r1) { printf ("2 racines complexes : %f + %f i et %f - %f i\n", r, r1, r, r1); } /* fonction calculant la racine double */ void racd(float r) { printf (" racine double : %f\n", r); } ``` main.c ```c #include #include #include "racinesEqn.h" #define DEBUG_MODE 1 #if DEBUG_MODE #define DEBUG(x) printf("Valeur de x:" int main () { float a, b, c, r, r1; double rdis; float res; printf("calcul des racines de ax2 + bx + c\n\n"); /* saisie des coefficients */ printf("saisissez les coefficients a b et c\n"); scanf("%f %f %f", &a, &b, &c); if (a == 0) { printf(" Equation du premier degre \n"); printf(" La solution est x = %f \n", -c / b); exit(0); } r = -b/(2 * a); switch (res < 0 ? -1 : (res >0 ? 1 : 0)) { case 1: rdis = sqrt(res); r1 = rdis / ( 2 * a); rac2(r,r1); break; case -1: rdis = sqrt(-res); r1 = rdis / ( 2 * a); complex(r,r1); break; case 0: racd(r); break; } return 0; } ``` \ No newline at end of file
 # TD2 de Langage C ## Exercice 1 ### a) 10000*(taille d'une valeur) ### b) 10000*(2 octets) = 20000octets = 20ko ## Exercice 2 1o 4o 8o 4o 2o ## Exercice 3 n=10, p=5, q=10, r n=10, p=10, q=10, r=1 n=5, p=5, q=5, r=1 n=15, p=10, q=5, r=1 n=15, p=11, q=10, r=1 n=16, p=11, q=15, r=1 n=6, p=2, q=1 n=6, p=3, q=1 n=6, p=2, q=0 n=6, p=3, q=1 ## Exercice 4 trois valeurs de x sont 3.5* 3.46*3.456 trois valeurs de n sont 345* 345*345
 #include int main () { int n=10, p=5, q=10,r;r = n == (p = q); printf ("A : n = %d p = %d q= %d r = %d \n", n, p, q, r); n = p = q =5; n+=p+=q; printf ("B : n = %d p = %d q= %d \n", n, p, q); q = n

p ? n++ : q++; printf ("D : n = %d p = %d q= %d \n", n, p, q); }

 #include int main() { int heures,minutes,secondes; scanf("%d%d%d",&heures,&minutes,&secondes); int total = heures*3600+minutes*60+secondes; printf("Total en secondes: %d\n",total); heures=total/3600; total-=heures*3600; minutes=total/60; total-=minutes*60; secondes=total; printf("%dh:%dm:%ds\n",heures,minutes,secondes); }
 #include int main () { int n,p,q; n=5; p=2; q= n++>p || p++ !=3; printf ("A : n = %d p = %d q= %d \n", n, p, q); n=5; p=2; q= n++ < p || p++ != 3; printf ("B : n = %d p = %d q= %d \n", n, p, q); n=5;p=2; q= ++n == 3 && ++p == 3; printf ("C : n = %d p = %d q= %d \n", n, p, q); n=5; p=2; q = ++n == 6 && ++p == 3; printf ("D : n = %d p = %d q= %d \n", n, p, q); }
 #include int main() { float x=3.456; int n=345; printf ("trois valeurs de x sont %4.1f*%5.2f*%.3f\n", x,x,x) ; printf ("trois valeurs de n sont %4d*%5d*%d\n ", n,n,n) ; }
 #include #include const double pi = 3.141592635; const double g = 9.81; const double G = 6.673e-11; int main() { double h; scanf("%lf",&h); double t = 2*pi*sqrt(h/g); printf("%lf \n",t); double m1, m2,r; scanf("%lf%lf%lf",&m1,&m2,&r); double F = G*m1*m2/(r*r); printf("%lf",F); }
 #include int main() { unsigned char c; scanf("%hhx",&c); printf("%c\n",c+'0'); }
 #include int main() { }