Commit 76c19978 authored by Benjamin Lefebvre's avatar Benjamin Lefebvre
Browse files

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 <stdio.h>
/* 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 <stdio.h>
#include <math.h>
#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 <stdio.h>
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++ : p++;
printf ("C : 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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
#include <math.h>
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 <stdio.h>
int main() {
unsigned char c;
scanf("%hhx",&c);
printf("%c\n",c+'0');
}
#include <stdio.h>
int main() {
}
#include <stdio.h>
int main() {
float x;
scanf("%f",&x);
if (x>0) printf("+");
else if (x<0) printf("-");
else printf("0");
printf("\n");
}
#include <stdio.h>
int main() {
char c;
scanf("%c",&c);
if (c<='z'&&c>='a') printf("%c\n",c-'a'+'A');
}
#include <stdio.h>
int main() {
char c;
scanf("%c",&c);
if (c<='Z'&&c>='A') printf("%c\n",c-'A'+'a');
}
#include <stdio.h>
int main() {
int A,B,C,MAX;
scanf("%d%d%d",&A,&B,&C);
// Méthode a
MAX=C;
if (A>MAX)
MAX=A;
else if (B>MAX)
MAX=B;
printf("%d",MAX);
printf("\n");
// Méthode b
if (A>B)
if (A>C)
printf("%d",A);
else
printf("%d",C);
else
if (B>C)
printf("%d",B);
else
printf("%d",C);
printf("\n");
// Méthode c
MAX=C;
MAX= (A>MAX) ? A : MAX;
MAX= (B>MAX) ? B : MAX;
printf("%d",MAX);
printf("\n");
// Méthode d
printf("%d",(A>B) ? (A>C) ? A : C : (B>C) ? B : C);
printf("\n");
}
#include <stdio.h>
#define SWAP(a,b) {temp=a;a=b;b=temp;}
int main() {
int A,B,C,temp;
scanf("%d%d%d",&A,&B,&C);
if (C>A) SWAP(A,C)
if (C>B) SWAP(B,C)
if (B>A) SWAP(A,B)
printf("A=%d,B=%d,C=%d\n",A,B,C);
}
#include <stdio.h>
#include <math.h>
int main() {
float A,B,C;
scanf("%f%f%f",&A,&B,&C);
if (A==0.f)
if (B==0.f)
if (C==0.f)
printf("x réel");
else
printf("Aucun x");
else
printf("x=%f",-C/B);
else {
float delta=B*B-4.f*A*C;
if (delta<0.f)
printf("Aucun x");
else if (delta==0.f)
printf("x=%f",-B/(2.f*A));
else
printf("x1=%f,x2=%f",(-B-sqrt(delta))/(2.f*A),(-B+sqrt(delta))/(2.f*A));
}
printf("\n");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment