首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >集合和指针-来自不兼容指针类型的错误分配

集合和指针-来自不兼容指针类型的错误分配
EN

Stack Overflow用户
提问于 2020-07-05 09:22:39
回答 1查看 153关注 0票数 0

我正在学习C。我试图解决以下问题,但我有几个问题。

我不使用指针控制链表。

这里有个问题:

我们想写一些函数来管理公司的员工。员工由其姓名(字符串)、员工数(整数)、工作时数(实)和小时薪(实际)来定义。

定义员工数据type

  • Functions:

  • saisirEmploye;,它允许您从keyboard
  • affichEmploye;中输入雇员,后者在screen
  • calculSalaire;上显示一个雇员,该雇员计算并返回员工的薪资(screen
  • calculSalaire;= nbh * rate_h)
  1. We现在希望将一组雇员存储在链接列表中,为此定义节点和ListEmp数据types.
  • ajouterEmploye;),它允许您向列表中添加一个新员工(可以在列表的开头或末尾进行添加,您的choice)
  • saisirListEmploye;允许您从键盘上输入一个n employees.
  • affichListEmploye;列表,该列表在screen.
  • totalSalaire;上显示员工列表)。它计算并返回list
  1. Write上所有员工的总薪资--执行以下operations:
  • declare的主要函数-- employees
  • enter列表、使用keyboard
  • enter list
  • calculate的雇员人数、所有employees
  • display的总薪资、列表的内容以及总薪资

我的代码:

代码语言:javascript
运行
复制
#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;
}

编辑代码:

代码语言:javascript
运行
复制
#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;
}

错误:

代码语言:javascript
运行
复制
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;
  ^~~~~~

你能帮帮我吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-07-05 10:36:00

关于您的类型错误:

代码语言:javascript
运行
复制
typedef struct {
    char  nom[20];
    int   mat;
    float ht;
    float tx;
} Employe;

但是后来您使用了Employes而不是Employe,例如在

代码语言:javascript
运行
复制
void ajouterEm (Employes *e, Elem nemp) { /* ... */ }

ElementsElem在这里也一样。这就是为什么会出现未知类型错误的原因。在……里面

代码语言:javascript
运行
复制
ajouterEmp(maListe, e)

指令后还必须有分号。还应该注意指针(例如Employes*)和整个结构(没有*)传递给函数的位置。

此错误修复后的编辑:

代码语言:javascript
运行
复制
typedef struct{
    Employe *employe;
    struct ListEmp *suivant;
} ListEmp;

类型名称ListEmp已在suivant中的定义中使用,但仅在下面的行中定义。使用

代码语言:javascript
运行
复制
typedef struct _ListEmp {
    Employe *employe;
    struct _ListEmp *suivant;
} ListEmp;

而不是。该结构获得名称_ListEmp,然后将struct _ListEmp定义为ListEmp。的第三个错误

代码语言:javascript
运行
复制
ajouterEmp(maListe, e)

必须传递指针,但Employe e是一个完整的数据结构。写

代码语言:javascript
运行
复制
ajouterEmp(maListe, &e);

然后添加分号。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62738911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档