نسمة طيف
اهلا وسهلا بك زائرنا الكريم
منتديات نسمة طيف ترحب بك أجمل ترحيب
ونتمنى لك وقتاً سعيداً مليئاً بالحب كما يحبه الله ويرضاه
فأهلاً بك في هذا المنتدى المبارك إن شاء الله
ونرجوا أن تفيد وتستفيد منا
وشكراً لتعطيرك المنتدى بباقتك الرائعة من مشاركات مستقبلية
لك منا أجمل المنى وأزكى التحيات والمحبة
نسمة طيف
اهلا وسهلا بك زائرنا الكريم
منتديات نسمة طيف ترحب بك أجمل ترحيب
ونتمنى لك وقتاً سعيداً مليئاً بالحب كما يحبه الله ويرضاه
فأهلاً بك في هذا المنتدى المبارك إن شاء الله
ونرجوا أن تفيد وتستفيد منا
وشكراً لتعطيرك المنتدى بباقتك الرائعة من مشاركات مستقبلية
لك منا أجمل المنى وأزكى التحيات والمحبة
نسمة طيف
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

نسمة طيف

منتدى عربي يشمل جميع المواضيع اهلا بيكم في منتدى نسمة طيف
 
الرئيسيةدخولأحدث الصورالتسجيل
المواضيع الأخيرة
» الدال علي الخير كفاعله
langage c ++++ Emptyالأربعاء أبريل 17, 2024 6:19 pm من طرف ملاك الطيف

» صلى الله عليه وسلم
langage c ++++ Emptyالأربعاء أبريل 17, 2024 6:07 pm من طرف ملاك الطيف

» https://www.facebook.com/groups/1599246743680231
langage c ++++ Emptyالسبت ديسمبر 04, 2021 1:21 pm من طرف ملاك الطيف

» https://www.facebook.com/groups/1599246743680231
langage c ++++ Emptyالسبت ديسمبر 04, 2021 1:19 pm من طرف ملاك الطيف

» اللهم بلغنا يوم عرفة
langage c ++++ Emptyالإثنين يوليو 19, 2021 12:22 am من طرف ملاك الطيف

» قصة رائعة جدا
langage c ++++ Emptyالإثنين يوليو 19, 2021 12:14 am من طرف ملاك الطيف

» السلام عليكم ورحمة الله وبركاته
langage c ++++ Emptyالأحد يوليو 18, 2021 11:53 pm من طرف ملاك الطيف

» اللهم بلغنا رمضان
langage c ++++ Emptyالإثنين ديسمبر 28, 2020 2:12 pm من طرف ملاك الطيف

» سجل حضورك بالكلمة الطيبة لا اله الا الله
langage c ++++ Emptyالإثنين ديسمبر 28, 2020 2:11 pm من طرف ملاك الطيف

أفضل 10 أعضاء في هذا المنتدى
ملاك الطيف
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
رغد
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
روايات انسان
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
زهرة الياسمين
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
دموع الورد
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
المحب
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
البرنسيسه
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
دموع انسان
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
محمد15
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 
نسمة طيف
langage c ++++ I_vote_rcaplangage c ++++ I_voting_barlangage c ++++ I_vote_lcap 

 

 langage c ++++

اذهب الى الأسفل 
كاتب الموضوعرسالة
chahid
عضو مبدع
عضو  مبدع
chahid


عدد المساهمات : 78
تاريخ التسجيل : 24/02/2011

langage c ++++ Empty
مُساهمةموضوع: langage c ++++   langage c ++++ Emptyالإثنين نوفمبر 19, 2012 11:56 am

CHAPITRE 8

LES TYPES DE VARIABLES COMPLEXES


LES DECLARATIONS DE TYPE SYNONYMES: TYPEDEF



On a vu les types de variables utilisés par le langage C: char, int, float, pointeur; le chapitre 9 traitera des fichiers (type FILE).

Le programmeur a la possibilité de créer ses propres types: Il suffit de les déclarer en début de programme (après les déclarations des bibliothèques et les « define ») avec la syntaxe suivante:

Exemples:

typedef int entier; /* on définit un nouveau type "entier" synonyme de "int" */

typedef int vecteur[3]; /* on définit un nouveau type "vecteur" synonyme */
/* de "tableau de 3 entiers" */

typedef float *fpointeur; /* on définit un nouveau type "fpointeur" synonyme */
/* de "pointeur sur un réel */

Utilisation: La portée de la déclaration de type dépend de l'endroit où elle est déclarée: dans main(), le type n'est connu que de main(); en début de programme, le type est reconnu dans tout le programme.

#include <stdio.h>

typedef int entier;
typedef float point[2];

void main()
{
entier n = 6;
point xy;
xy[0] = 8.6;
xy[1] = -9.45;

etc ...

}

Exercice VIII_1: Afficher la taille mémoire d'un point à l'aide de l'opérateur sizeof.
Exercice VIII_2: Définir le type typedef char ligne[80];
Déclarer dans main() un pointeur de ligne; lui attribuer de la place en mémoire (pour 5 lignes). Ecrire une fonction qui effectue la saisie de 5 lignes puis une autre qui les affiche. Les mettre en oeuvre dans main().
Attention, le fichier d’en-tête doit contenir la déclaration du nouveau type.


LES STRUCTURES


Le langage C autorise la déclaration de types particuliers: les structures . Une structure est constituée de plusieurs éléments de même type ou non.

Exemple:

Déclaration:

typedef struct /* On définit un type struct */
{
char nom[10];
char prenom[10];
int age;
float note;
}
fiche;

Utilisation:

On déclare des variables par exemple: fiche f1,f2;

puis, par exemple: strcpy(f1.nom,"DUPONT");
strcpy(f1.prenom,"JEAN");
f1.age = 20;
f1.note = 11.5;

L'affectation globale est possible avec les structures: on peut écrire: f2 = f1;

Exercice VIII_3: Créer la structure ci-dessus, saisir une fiche, l'afficher.


STRUCTURES ET TABLEAUX


On peut définir un tableau de structures (mais ceci est assez peu utilisé) :

Exemple: (à partir de la structure définie précédemment)
Déclaration: fiche f[10]; /* on déclare un tableau de 10 fiches */

Utilisation: strcpy(f[i].nom,"DUPONT") /* pour un indice i quelconque */
strcpy(f[i].prenom,"JEAN");
f[i].age = 20;
f[i].note = 11.5;


Exercice VIII_4: Créer une structure point{int num;float x;float y;}
Saisir 4 points, les ranger dans un tableau puis les afficher.


STRUCTURES ET POINTEURS


On peut déclarer des pointeurs sur des structures. Cette syntaxe est très utilisée en langage C, elle est notamment nécessaire lorsque la structure est un paramètres modifiable dans la fonction.

Un symbole spécial a été créé pour les pointeurs de structures, il s'agit du symbole ->

Exemple: (à partir de la structure définie précédemment)

Déclaration: fiche *f; /* on déclare un pointeur de fiche */

Utilisation: f = (fiche*)malloc(sizeof(fiche)); /* réserve de la place */
strcpy(f->nom,"DUPONT");
strcpy(f->prenom,"JEAN");
f->age = 20;
f->note = 11.5;

Exercice VIII_5: Reprendre l'exercice VIII_2: Une fonction saisie permet de saisir un point, une fonction affiche permet d'afficher un point.

CORRIGE DES EXERCICES

Exercice VIII_1:

#include <stdio.h>
#include <conio.h>

typedef float point[2];

void main()
{
printf("TAILLE D'UN POINT: %2d OCTETS\n",sizeof(point));
printf("\nPOUR SORTIR FRAPPER UNE TOUCHE ");
getch();
}

Exercice VIII_2:

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include "c:\bc5\courc_C\teach_c\chap8\chap8.h" /* fichier d'en-tete */

/* cette declaration est normalement dans le fichier µ/
/*d’en-tete typedef char ligne[80]; */

void saisie (ligne *tx)
{
int i;
printf("\n SAISIE DU TEXTE\n\n");
for (i=0;i<5;i++)
{
printf("LIGNE Num %d ",i);
scanf("%s",tx+i); /* saisie de la ligne nøi */
}
}

void affiche(ligne *tx)
{
int i;
printf("\n\n\n AFFICHAGE DU TEXTE\n\n");
for(i=0;i<5;i++)
printf("%s\n",tx+i);
}

void main()
{
ligne *texte;
texte = malloc(sizeof(ligne)*5);/* reserve de la place pour 5 lignes */
saisie(texte);
affiche(texte);
free(texte);
printf("\nPOUR SORTIR FRAPPER UNE TOUCHE ");
getch();
}

Exercice VIII_3:

#include <stdio.h>
#include <conio.h>

typedef struct
{
char nom[10];
char prenom[10];
int age;
float note;
}
fiche;

void main()
{
fiche f;
printf("SAISIE D'UNE FICHE \n");
printf("NOM: ");gets(f.nom);
printf("PRENOM: ");gets(f.prenom);
printf("AGE: ");scanf("%d",&f.age);
printf("NOTE: ");scanf("%f",&f.note);
printf("\n\nLECTURE DE LA FICHE:\n");
printf("NOM: %s PRENOM: %s AGE: %2d NOTE: %4.1f",
f.nom,f.prenom,f.age,f.note);
printf("\n\nPOUR SORTIR FRAPPER UNE TOUCHE ");
getch();
}

Exercice VIII_4:

#include <stdio.h>
#include <conio.h>

typedef struct {int num;float x;float y;} point;

void main()
{
point p[4]; /* tableau de points */
int i;
float xx,yy;

/* saisie */
printf("SAISIE DES POINTS\n\n");
for(i=0;i<4;i++)
{
printf("\nRELEVE N¯%1d\n",i);
p[i].num = i;
printf("X= ");scanf("%f",&xx);
printf("Y= ");scanf("%f",&yy);
p[i].x = xx;p[i].y = yy;
}

/* relecture */
printf("\n\nRELECTURE\n\n");
for(i=0;i<4;i++)
{
printf("\nRELEVE N¯%1d",p[i].num);
printf("\nX= %f",p[i].x);
printf("\nY= %f\n",p[i].y);
}
printf("\n\nPOUR SORTIR FRAPPER UNE TOUCHE ");
getch();
}

Exercice VIII_5:

#include <stdio.h>
#include <conio.h>
#include "c:\bc5\courc_C\teach_c\chap8\chap8.h" /* fichier d'en-tete */

void saisie(point *pp,int i)
{
float xx,yy;
printf("\nRELEVE Nø%1d\n",i);
printf("X= ");scanf("%f",&xx);
printf("Y= ");scanf("%f",&yy);
pp->num = i;pp->x = xx;pp->y = yy;
}

void affiche(point *pp)
{
printf("\nRELEVE Nø%1d",pp->num);
printf("\nX= %f",pp->x);
printf("\nY= %f\n",pp->y);
}

void main()
{
point p[4]; /* tableau de points */
int i;
printf("SAISIE:\n\n");
for(i=0;i<4;i++)saisie(&p[i],i);
printf("\n\nRELECTURE:\n\n");
for(i=0;i<4;i++)affiche(&p[i]);
printf("\n\nPOUR SORTIR FRAPPER UNE TOUCHE ");
getch();
}
الرجوع الى أعلى الصفحة اذهب الى الأسفل
 
langage c ++++
الرجوع الى أعلى الصفحة 
صفحة 1 من اصل 1

صلاحيات هذا المنتدى:لاتستطيع الرد على المواضيع في هذا المنتدى
نسمة طيف :: قسم البرامج و التطوير :: دروس في الاعلاميات-
انتقل الى: