1

このプログラムでは、人々に関する情報を印刷したいと思います。

  • 彼らの年齢
  • 彼らの職種
    • 学生
    • スタッフ
    • またはどちらでもない

職種が学生の場合、学生はコースを受講します。職種がスタッフの場合、講義室があります。

私はこれがによって決定されることを望みますenum typeofpeopleinsert次に、呼び出して要素をリストに入れてから、それらをに出力したいと思いmain()ます。

次のエラーが発生します。

 error: array type has incomplete element type
 part5.c:9:27: error: 'student' undeclared here (not in a function)
 part5.c:9:44: error: 'staff' undeclared here (not in a function)
 part5.c:9:56: error: 'neither' undeclared here (not in a function)

これが私のコードです:

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

 char names[][7]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
  "Harriet"};
 int ages[7]= {22, 24, 106, 6, 18, 32, 24};

 enum typeofpeople type[]={student, student,staff,staff,neither,student,staff};

 char* course[]={"java1","java2","java3"};
 char* rooms[]={"lab1","lab2","lab3"};

 //a coutc for course, and room
 int countc, countr;

 union nextinfo{
   char* course;
   char* rooms;
 };


 /* declare your struct for a person here */
 typedef struct Record{
   char *name;
   int age;
   enum typeofpeople type;
   struct Record *next;
   union nextinfo info;
 }Record;

 //set the head pointer at the start of the list
 Record *headptr = NULL;

 /* compare two string */
 char compare_people_by_name(char *a, char *b)
 {

   return strcmp(a, b);
 }

 int compare_people_by_age(int a, int b)
 {
   if (a<b)
     return -1;
   else if (a>b)
     return 1;
   else
     return 0;  
 }

 void insert (char *s, int n, enum typeofpeople type,int(*compar)(int a, int b)) {

 Record *t, *pnew, *prv;

 prv=NULL;

 pnew=(Record *)malloc(sizeof(struct Record));

 if(pnew == NULL){  
    abort();
    printf("memory allocation fail"); 
    exit(1);  
}else{
    printf("memory allocation to person  - %s - \n", s);      
}

 pnew->name = s;
 pnew->age = n;
 pnew->next = NULL;
 pnew->type=type;

 //check type of people
 if(type==staff){      
   pnew->info.rooms=rooms[countr];
   countr++;
 }else(type==student){   
   pnew->info.course=course[countc];
   countc++;
 }

 //if the list is empty
 if (headptr==NULL)
 { 
    //add the first into the list
    headptr = pnew;
    return;
 }

 // look for the right place to insert
 for (t=headptr;t!=NULL;t=t->next) { 
     if (compar(n,t->age)<0) {        
        pnew->next=t;
        if (prv!=NULL)
           prv->next = pnew;
        else
           headptr=pnew;
        return;
     }
     //break the for loop.
     prv=t; 
 }
 prv->next=pnew;
 return;       
 }

 int main(int argc, char **argv) {

   Record *p, *q;

    countr=0;
    countc=0;

   for (int i=0; i < 7; i++) {
      insert (names[i], ages[i],type[i],compare_people_by_age);
   }

    printf("\n");
   Record *display;
   display=headptr;

   for (p = headptr; p!=NULL; p = p->next) {
     printf("The name is: %s, the age is:%i\n", p->name, p->age);

     if(display->type=staff){

  printf("room is %s\n",rooms);
}else if(display->type=student){
  printf("course is %s\n",course);

}else{
  printf("neither");

}
display=display->next;
   }


   /* To free your linked list: */

   p = headptr;

   while (p!=NULL){

q = p;
p = p->next;
free(q);
   }
 }
4

1 に答える 1

6

この行が問題です:

enum typeofpeople type[]={student, student,staff,staff,neither,student,staff};

次のようなものが必要です。

enum typeofpeople {
   student,
   staff,
   neither
} type[]={student, student,staff,staff,neither,student,staff};

次の問題:

この行は間違っています:

if(display->type=staff){

あなたは本当に欲しい:

if(display->type==staff){
于 2012-11-27T12:07:00.240 に答える