Telechargé par Oussama Safi

Chapitre N°1 Rappel

publicité
Algorithmique et programmation 2
Chapitre 1:Rappel
1. Introduction
2. Variable, saisie, calcul, affichage
3. Structures alternatives
4. Structures itératives
Prof: R. EL AYACHI
1
MIPC: S3
Département d’Informatique
Introduction
• Un programme en c : un programme écrit en
langage c qui traduit un algorithme décrivant la
solution d’un problème.
• Il est composé de:
• Directives de préprocesseur (#)
• Déclarations
• Fonctions
• Fonction principale (main)
•Instruction
• Bloc
• Commentaires (/* texte*/ , // ligne)
Prof: R. EL AYACHI
MIPC: S3
2
Département d’Informatique
Introduction (suite)
Exemple:
# include<stdio.h>
Directive
float Rayon,Pi=3.14;
Bibliothèque
Déclaration de données
float Surface(float r)
{ return (r*r*Pi);}
Fonction
main()
Fonction principale
{
float Res;
Scanf("%f",&Rayon);
Bloc
Res = Surface(Rayon);
Instruction
/* Appel de la fonction*/
printf ("%f",Res);
}
Prof: R. EL AYACHI
Commentaire
3
MIPC: S3
Département d’Informatique
Donnée
• Donnée:
constituée
de
(identificateur, valeur et type)
trois
attributs
• Mots réservés au langage c:
auto
break
case
char
const
continue
default
do
Prof: R. EL AYACHI
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
4
MIPC: S3
Département d’Informatique
Donnée (suite)
• Type:
Nom
Taille
char
1 octet
int
short int
long int
unsigned int
2 octets (et plus)
2 octets
4 octets
2 octets
float
double
long Double
4 octets
8 octets
10 octets
• Catégories de donnée: variable et constante
Prof: R. EL AYACHI
5
MIPC: S3
Département d’Informatique
Donnée (suite)
• Variable:
type identificateur_var ;
Exemple: float surface ;
• Constante:
const type identificateur_const = valeur ;
Exemple: const float Pi = 3.14;
• Remarques:
•On peut utiliser # et define pour définir une
constante (#define Pi 3.14)
• Deux types de variable: globale et locale
Prof: R. EL AYACHI
6
MIPC: S3
Département d’Informatique
Calcul
• Expression: combinaison d’opérandes et
d’opérateurs.
•Affectation: stockage du résultat d'une
expression dans une variable.
identificateur_variable = expression ;
Exemple: surface = rayon*rayon*3.14;
Prof: R. EL AYACHI
7
MIPC: S3
Département d’Informatique
Calcul (suite)
• Opérateurs:
• Arithmétiques
• Relationnels
• Logiques
• Incrémentation / décrémentation
•…
Prof: R. EL AYACHI
8
MIPC: S3
Département d’Informatique
Calcul (suite)
• Opérateurs arithmétiques:
Prof: R. EL AYACHI
Opérateur
Signification
+
*
/
%
Addition
Soustraction
Multiplication
Division
Reste de la
division entière
9
MIPC: S3
Département d’Informatique
Calcul (suite)
• Opérateurs relationnels:
Prof: R. EL AYACHI
Opérateur
Signification
==
!=
<
>
<=
>=
Egal
Différent
Inférieur
Supérieur
Inférieur ou égal
Supérieur ou égal
10
MIPC: S3
Département d’Informatique
Calcul (suite)
• Opérateurs logiques:
Opérateur
!
||
&&
Signification
Non
OU
ET
• Opérateurs d’incrémentation et décrémentation:
Opérateur
++
--
Prof: R. EL AYACHI
Signification
Augmente d’une unité la variable
Diminue d’une unité la variable
11
MIPC: S3
Département d’Informatique
Saisie (Entrées)
•Instruction d’entrée: acquérir et placer une
valeur dans une variable.
scanf ("format",&variable) ;
•Exemple:
scanf ("%f",&Rayon) ;
Fonction de
lecture
Prof: R. EL AYACHI
Format
Adresse
Identificateur
de la variable
12
MIPC: S3
Département d’Informatique
Saisie (Entrées)
•Formats associés aux types:
T ype
Form at
in t o u s h o r t
%d
lo n g
%D
f lo a t o u d o u b le
%f
char
%c
Prof: R. EL AYACHI
13
MIPC: S3
Département d’Informatique
Affichage (Sorties)
•Instruction de sortie: afficher sur l’écran (un
message, le contenu d’une variable ou le
résultat d’une expression)
printf (" Message format",variable ou expression) ;
• Exemple:
printf ("Le double de %d est %d ",R,2*R) ;
Message
Prof: R. EL AYACHI
Format
Variable
Expression
14
MIPC: S3
Département d’Informatique
Structures alternatives
• Opérateur conditionnel (1)
• Structure de choix simple (2)
• Structure de choix alternatif (3)
• Structure de choix multiples (4)
Prof: R. EL AYACHI
15
MIPC: S3
Département d’Informatique
Structures alternatives
(1) Opérateur conditionnel
Syntaxe:
Test
Test est vrai
Test est faux
(Expression logique) ? ExpressionSiVrai : ExpressionSiFaux
L'expression conditionnelle débute par un test suivi du
caractère ? puis la valeur lorsque le test est vrai puis le caractère
: et enfin la valeur lorsque le test est faux.
Exemple:
renvoie le produit de i par 2 si i est strictement
positif et le résultat de l’ajout de 2 à i si i est
négatif ou nul.
(i>0) ? i*2 : i+2
Prof: R. EL AYACHI
16
MIPC: S3
Département d’Informatique
Structures alternatives (suite)
(2) Structure de choix simple
if (conditions)
{
instruction 1;
instruction 2;
…
instruction n;
}
Prof: R. EL AYACHI
17
MIPC: S3
Département d’Informatique
Structures alternatives (suite)
(3) Structure de choix alternatif
if (conditions)
{
instructions 1;
}
else
{
instructions 2;
}
Prof: R. EL AYACHI
18
MIPC: S3
Département d’Informatique
Structures alternatives (suite)
(4) Structure de choix multiples
switch (variable)
{
case valeur 1: instructions 1;
break;
…
case valeur n: instructions n;
break;
default: instructions n+1;
}
Prof: R. EL AYACHI
19
MIPC: S3
Département d’Informatique
Exercice 1
Ecrire un programme en c qui affiche la
mention équivalente à une moyenne
obtenue à partir de trois notes saisies au
clavier.
Prof: R. EL AYACHI
20
MIPC: S3
Département d’Informatique
Structures itératives
• Boucle (1)
• Boucle (2)
• Boucle (3)
Prof: R. EL AYACHI
21
MIPC: S3
Département d’Informatique
Structures itératives (suite)
Boucle (1)
Initiations
Conditions
for (expressions1; expressions2; expressions3)
{
Incrémentations
instruction 1;
ou bien
instruction 2;
Décrémentations
…
instruction n;
}
Prof: R. EL AYACHI
22
MIPC: S3
Département d’Informatique
Structures itératives (suite)
Boucle (2)
while (conditions)
{
instruction 1;
instruction 2;
…
instruction n;
}
Prof: R. EL AYACHI
23
MIPC: S3
Département d’Informatique
Structures itératives (suite)
Boucle (3)
do{
instruction 1;
instruction 2;
…
instruction n;
}while(conditions);
Prof: R. EL AYACHI
24
MIPC: S3
Département d’Informatique
Exercice 2
Ecrire un programme en c qui affiche si un
entier saisi au clavier est un nombre parfait
ou non.
Prof: R. EL AYACHI
25
MIPC: S3
Département d’Informatique
Exercice 3
Ecrire un programme en c qui affiche les
nombres parfaits inférieurs strictement à
100.
Prof: R. EL AYACHI
26
MIPC: S3
Département d’Informatique
Exercice 4
Ecrire un programme en c qui affiche les
quatre premiers nombres parfaits.
Prof: R. EL AYACHI
27
MIPC: S3
Département d’Informatique
Exercice 5
Ecrire un programme en langage C qui
détermine la Nième valeur Un (N étant
fourni en donnée) de la suite de
« FIBONACCI » définie comme suit :
U1=1
U2=1
Un= Un-1+ Un-2
pour n>2
Prof: R. EL AYACHI
28
MIPC: S3
Département d’Informatique
Téléchargement