I created a class for display for each fabrication name the employee and piece. I use structure also pointer, but always the first line gives me name of the employee is null and piece is empty.
My result is like this
FICHE DE GESTION DES IDEMNITES DES EMPLOYES DE MP (par journee)
-----------------------------------------------------------------
Nom Atelier de Fabrication 1: 1 Nom Responsable Atelier 1 : 1
Nom NPF IndiceP IndP
(null) 1 . 40.000000
empname 1 A 20.000000
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "h3.h"
MP MOTO[20];
MP *p1;
EMPLOYE *p2;
float TIndFabrication=0;
int NAF;
char ***NomE;
char **NomAF;
char **NomRA;
char **IndiceP;
char *saisir()
{
char *TABL,INTRO[20];
scanf("%s",&INTRO);
TABL=malloc(strlen(INTRO)+1);
if(TABL)
strcpy(TABL,INTRO);
else {
printf("pas d'espace mémoire!!");
exit(-1);
}
}
float traitement_1(char IndiceP,int NPF)
{
float Indp;
int taux;
if(IndiceP=='A')
taux=20;
else if(IndiceP=='B')
taux=10;
else if(IndiceP=='C')
taux=5;
else
printf("Erreur!!");
Indp=taux*NPF;
return Indp;
}
float traitement_2(MP MOTO[20])
{
for(p1=MOTO;p1<MOTO+NAF;p1++)
{
for(p2=p1->EMP;p2<p1->EMP+p1->NEF;p2++)
{
p1->TIndp+= p2->Indp;
}
}
return p1->TIndp; ;
}
float traitement_3(MP MOTO[20])
{
for(p1=MOTO;p1<MOTO+NAF;p1++)
{
TIndFabrication+=p1->TIndp;
}
return TIndFabrication;
}
void afficher( MP MOTO[20],char ***NomE,char **IndiceP,char **NomAF,char **NomRA)
{
FILE* fichier=NULL;
fichier=fopen("f3.txt","a");
if(fichier==NULL)
{
fputs("\n ce fichier n existe pas\n",fichier);
}
else
{
fputs("\nFICHE DE GESTION DES IDEMNITES DES EMPLOYES DE MP (par journee)\n",fichier);
for(p1=MOTO;p1<MOTO+NAF;p1++)
{
fputs("-----------------------------------------------------------------\n",fichier);
fprintf(fichier," Nom Atelier de Fabrication %d: %s\t Nom Responsable Atelier %d : %s\n",p1-MOTO+1,NomAF[p1-MOTO],p1-MOTO+1,NomRA[p1-MOTO]);
fputs(" Nom\tNPF\tIndiceP\t\tIndP\n",fichier);
When is use plus the one employee code display the first line
(null) 1 . 40.000000
and second is correct
for (p2=p1->EMP; p2<p1->EMP+p1->NEF; p2++)
{
fprintf(fichier,"\t\t\t\t%s\t%d\t%c\t\t%f\n", NomE[p1-MOTO][p2-(p1->EMP)],p2->NPF,IndiceP[p1-MOTO][p2-(p1->EMP)],p2->Indp);
}
fputs("\n-----------------------------------------------------------------\n",fichier);
fprintf(fichier," Total Indemnites %fDH",p1->TIndp);
fputs("\n-----------------------------------------------------------------\n",fichier);
}
fprintf(fichier,"\nTotaux des indemnites des employes du groupe MP pendant la journee : %.2fDH",TIndFabrication);
}
fclose(fichier);
}
void libereE( MP MOTO[20],char ***NomE,char **NomAF,char **NomRA)
{
for(p1=MOTO;p1<MOTO+NAF;p1++)
{
free(NomAF[p1-MOTO]);
free(NomRA[p1-MOTO]);
for(p2=p1->EMP;p2<p1->EMP+p1->NEF;p2++)
{
free(NomE[p1-MOTO][p2-p1->EMP]);
}
}
}
void main()
{
char INTRO[20];
int taux;
MP MOTO[20];
printf("Entrer le nombre d'atelier de fabrication:");
scanf("%d",&NAF);
NomAF=malloc(NAF*sizeof(*NomAF));
NomRA=malloc(NAF*sizeof(*NomRA));
NomE=malloc(NAF*sizeof(*NomE));
IndiceP=malloc(NAF*sizeof(*IndiceP));
for(p1=MOTO;p1<MOTO+NAF;p1++)
{
p1->TIndp=0.0;
printf("entrer le nom de l'atelier %d:",p1-MOTO+1);
NomAF[p1-MOTO]=saisir();
printf("entrer le nom de responsable de l'atelier %d :",p1-MOTO+1);
NomRA[p1-MOTO]=saisir();
printf("entrer le nombre des employers d'atelier %d :\n",p1-MOTO+1);
scanf("%d",&p1->NEF);
for(p2=p1->EMP;p2<p1->EMP+p1->NEF;p2++)
{
NomE[p1-MOTO]=malloc( (p1->NEF)*sizeof(*NomE) );
IndiceP[p1-MOTO]=malloc( (p1->NEF)*sizeof(*IndiceP) );
printf("entrer le nom d'emloyer %d au sein de l'atlelier %d: ",p2-p1->EMP+1,p1-MOTO+1);
NomE[p1-MOTO][p2-p1->EMP]=saisir();
printf("entrer le nombre des pieces fabrique par l'employer %d au sein de l'atlelier %d:\n ",p2-p1->EMP+1,p1-MOTO+1);
scanf("%d",&p2->NPF);;
printf("entrer l'indice des pieces (A,B ou C):");
scanf("%s",&IndiceP[p1-MOTO][p2-p1->EMP] );
p2->Indp=traitement_1( IndiceP[p1-MOTO][p2-p1->EMP] ,p2->NPF);
}
}
p1->TIndp=traitement_2(MOTO);
TIndFabrication=traitement_3 (MOTO);
afficher( MOTO,NomE,IndiceP,NomAF,NomRA);
libereE(MOTO,NomE,NomAF,NomRA);
}
Class the structur
main.ch3.h
#ifndef def_H3_H_
#define H3_H
typedef struct EMPLOYE EMPLOYE;
struct EMPLOYE{
int NPF;
float Indp;
};
typedef struct MP MP;
struct MP {
float TIndp;
int NEF;
struct EMPLOYE EMP[20];
};
char* saisir();
float traitement_1 (char IndiceP,int NPF);
float traitement_2( MP MOTO[20]);
float traitement_3( MP MOTO[20]);
void afficher( MP MOTO[20],char ***NomE,char **IndiceP,char **NomAF,char **NomRA);
void libereE( MP MOTO[20],char ***NomE,char **NomAF,char **NomRA);
#endif