I'm learning C. I tried to solve the following problem, but I had several problems.
I do not control the chain linked list with pointers.
Here's the problem:
We want to write functions for managing the employees of a company. An employee is defined by his name (character string), his personnel number (integer), the number of hours worked (real), and the hourly rate (actual).
- Define the Employee data type
- Functions:
- saisirEmploye; which allows you to enter an employee from the keyboard
- affichEmploye; which displays an employee on the screen
- calculSalaire; which calculates and returns the employee's salary (salary = nbh * rate_h)
- We now want to store a set of employees in a linked list, define for this the node and ListEmp data types.
- ajouterEmploye; which allows you to add a new employee to the list (the addition can be do at the beginning or at the end of the list, your choice)
- saisirListEmploye; which allows you to enter from the keyboard a list of n employees.
- affichListEmploye; which displays a list of employees on the screen.
- totalSalaire; which calculates and returns the total salary of all the employees on the list
- Write a main function performing the following operations:
- declare a list of employees
- enter the number of employees using the keyboard
- enter list
- calculate the total salary of all employees
- display the contents of the list, as well as the total salary
My code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char nom[20];
int mat;
float ht;
float tx ;
}Employe;
typedef struct
{
Employe *employe;
struct ListEmp *suivant;
}ListEmp;
typedef struct
{
ListEmp *premier;
}Elements;
void saisirEmploye(Employe *e)
{
printf("Sasir le nom \n");
scanf("%s",e->nom);
printf("Saisir le matricule \n");
scanf("%d",&e->mat);
printf("Saisir le nombre d’heures travaillees \n");
scanf("%f",&e->ht);
printf("Saisir le taux horaire \n");
scanf("%f",&e->tx);
}
void afficheEmp(Employe e)
{
printf("Nom : %s | Matricule : %d | Nombre d’heures travaillees : %f | Taux horaire : %f \n",e.nom,e.mat,e.ht,e.tx);
}
float calculSalaire(Employe e)
{
float salaire = 0;
salaire = (e.ht)*(e.tx) ;
return salaire;
}
Elements *init()
{
ListEmp *liste = malloc(sizeof(ListEmp));
Elements *elements = malloc(sizeof(Elements));
if (liste == NULL || elements == NULL)
{
exit(EXIT_FAILURE);
}
liste->employe = NULL;
liste->suivant = NULL;
elements->premier = liste;
return elements;
}
void ajouterEmp(Elements *elements, Employe *employe)
{
/* Création du nouvel élément */
ListEmp *nouveau = malloc(sizeof(*nouveau));
if (elements == NULL || nouveau == NULL)
{
exit(EXIT_FAILURE);
}
nouveau->employe = employe;
/* Insertion de l'élément au début de la liste */
nouveau->suivant = elements->premier;
elements->premier = nouveau;
}
int main()
{
// Employes e;
// e.tete=NULL;
// int n=0;
// float t=0;
// printf("Donner le nombre des Emplyes \n"); // saisie du nbr des employes
// scanf("%d ",&n);
// saisirListEmp(&e,n); // saisie de la liste
// t=totalSalaire(e); //calcule du salaire totale
// afficheList(e); // affichage du contenu
// printf("le salaire totale=%f ",t); // affichage du salaire
Employe e;
printf("Saisr un emplye \n");
saisirEmploye(&e);
//printf("Nom emplye : %s \n", e.nom);
afficheEmp(e);
printf("Salaire : %f",calculSalaire(e));
Elements *maListe = init();
ajouterEmp(maListe,e)
return 0;
}
Edited code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char nom[20];
int mat;
float ht;
float tx;
} Employe;
typedef struct Elem
{
Employe employe;
struct Elem *prev;
struct Elem *next;
}Elem;
typedef struct
{
Elem *first;
Elem *last;
}ListEmp;
void saisirEmploye(Employe *e)
{
printf("Sasir le nom \n");
scanf("%s",e->nom);
printf("Saisir le matricule \n");
scanf("%d",&e->mat);
printf("Saisir le nombre d’heures travaillees \n");
scanf("%f",&e->ht);
printf("Saisir le taux horaire \n");
scanf("%f",&e->tx);
}
void afficheEmp(Employe e)
{
printf("Nom : %s | Matricule : %d | Nombre d’heures travaillees : %f | Taux horaire : %f \n",e.nom,e.mat,e.ht,e.tx);
printf("\n");
}
float calculSalaire(Employe e)
{
float salaire = 0;
salaire = (e.ht)*(e.tx) ;
return salaire;
}
ListEmp *init()
{
ListEmp *listEmp = malloc(sizeof(ListEmp));
Elem *elem = malloc(sizeof(Elem));
Employe employe;
if (listEmp == NULL || elem == NULL)
{
exit(EXIT_FAILURE);
}
strcpy(employe.nom, "Hamza");
employe.mat = 123;
elem->employe = employe;
elem->next = NULL;
listEmp->first = elem;
return listEmp;
}
void auDebut(ListEmp *listEmp, Employe employe)
{
printf("1 Au debut \n");
Elem *elem = (Elem*)malloc(sizeof(Elem));
elem->employe = employe;
elem->next = listEmp->last;
elem->prev = NULL;
if(listEmp->last)
listEmp->last->prev = elem;
else
listEmp->last = elem;
listEmp->first = elem;
}
void aLaFin(ListEmp *listEmp, Employe employe)
{
Elem *elem = (Elem*)malloc(sizeof(Elem));
elem->employe = employe;
elem->prev = listEmp->first;
elem->next = NULL;
if(listEmp->first)
listEmp->first->next = elem;
else
listEmp->first = elem;
listEmp->last = elem;
// Elem *elem = (Elem*)malloc(sizeof(Elem));
// if(!elem) exit(EXIT_FAILURE);
// elem->employe = employe;
// elem->next = listEmp->first;
// listEmp->first = elem;
}
void ajouterEmploye(ListEmp *listEmp, Employe employe)
{
char element;
printf("Voulez-vous ajouter le nouvel employe au début de la liste d ou a la fin de la liste f ? \n");
scanf(" %c", &element);
if (element == 'd')
//printf("Au debut \n");
auDebut(listEmp,employe);
else if (element == 'f')
//printf("A la fin \n");
aLaFin(listEmp,employe);
}
void saisirListEmploye(ListEmp *listEmp, int n)
{
Employe employe;
for(int i=1;i<=n;i++)
{
printf("Employe %d\n",i);
saisirEmploye(&employe);
ajouterEmploye(listEmp,employe);
}
}
void affichListEmploye(ListEmp *listEmp)
{
Elem *i = listEmp->first;
while(i != NULL)
{
afficheEmp(i->employe);
i = i->next;
}
}
float totalSalaire(ListEmp *listEmp)
{
float total = 0;
Elem *i = listEmp->first;
while (i != NULL)
{
total += calculSalaire(i->employe);
i = i->next;
}
return total;
}
int main()
{
Employe e;
int n;
//printf("Saisr un emplye \n");
//saisirEmploye(&e);
//printf("Nom emplye : %s \n", e.nom);
//afficheEmp(e);
//printf("Salaire : %f",calculSalaire(e));
//printf("\n\n");
ListEmp *listEmp = init();
//ajouterEmploye(listEmp,e);
printf("Combien d'employes souhaitez-vous ajouter ?\n");
scanf("%d",&n);
saisirListEmploye(listEmp,n);
affichListEmploye(listEmp);
printf("Le salaire total de tous les employés de la liste est : %f", totalSalaire(listEmp));
return 0;
}
The error:
main.c: In function ‘ajouterEmp’:
main.c:87:22: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
nouveau->suivant = elements->premier;
^
main.c: In function ‘main’:
main.c:114:21: error: incompatible type for argument 2 of ‘ajouterEmp’
ajouterEmp(maListe,e)
^
main.c:76:6: note: expected ‘Employe * {aka struct *}’ but argument is of type ‘Employe {aka struct }’
void ajouterEmp(Elements *elements, Employe *employe)
^~~~~~~~~~
main.c:116:2: error: expected ‘;’ before ‘return’
return 0;
^~~~~~
Can you help me please? Thanks!