-3

需要帮助来编写正确的代码 请写一个程序来组织一个简单的学生信息数据库。

该程序应允许用户输入一系列学生数据记录。每个学生数据记录都有三个字段:名字、姓氏和年龄。用户应该能够执行以下 4 种操作:1)输入一条记录,2)删除一条记录,3)打印所有记录,以及 4)对所有记录进行排序。所有 4 个选项都应在开头打印。用户应该会收到一个提示,他/她可以在其中输入与他想要执行的操作相对应的数字。输入记录操作将提示用户在同一行输入三个字符串,每个字段一个字符串。打印所有记录操作应该在不同的行上打印每条记录。排序所有记录操作应根据姓氏排序。

我写的代码是:(显示和添加工作,但删除和排序不是!)

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


     struct student
      {
         char fname[29];
         char lname[29];
         int age;
         struct student *next;
      };

      int main()
      {
        int i,n,ch,ps,x,k;
        k=0;
      struct student *h,*t,*t1,*w,*q;
      h=NULL;

      printf("\n/* student data records*/");


    while(1)
     {
          printf("\n1.display\n2:add\n3.delete\n4.exit\n5.sort by first name\n");
            printf("\nenter your choice=");
          scanf("%d",&ch);
          switch(ch)
    {
    case 1:
      if(h==NULL)
       {
        printf("no records are available");
      }
      w=h;
      while(w!=NULL)
     {
        printf("\nfirst name of a student:%s\nlast name of a student:%s\nage of a student:%d\n",
      w->fname,w->lname,w->age);
      w=w->next;
     }
    break;

    case 2:
       printf("\nenter the new record=\t");
       if(h==NULL)
     {
      h=t=(struct student *)malloc(sizeof(struct student));
       printf("\nfirst Name of a student:\t");
        scanf("%s",&t->fname);
        printf("\nlast Name of a student:\t");
        scanf("%s",&t->lname);
        printf("\nage of a student:\t");
        scanf("%d",&t->age);
       t->next=NULL;
    break;
    }
    else
      {
        t1=(struct student *)malloc(sizeof(struct student));
        printf("\nFirst Name of a student:\t");
        scanf("%s",&t1->fname);
        printf("\nlast Name of a student:\t");
        scanf("%s",&t1->lname);
        printf("\nage of a student:\t");
        scanf("%d",&t1->age);
        t1->next=t->next;
        t->next=t1;
        t=t1;
    }
    break;

    case 3:
        printf("enter name of student whos record is to be deleted=\n");
        scanf("%d",&ps);
        t=h;
     while(t->fname!=ps-1)
       {
         t=t->next;
        }
         t1=t->next;
         t->next=t1->next;
         free(t1);
     break;

    case 4:
     exit(0);
     break;

    case 5:
    printf("not working yet");
    {
    void sort( student[],int n)
     { int i,j,comp=0,passes=0;
       student temp;
       for(i=1;i<n;i++)
        {
           passes++;
           for(j=0;j<n-i;j++)
        {      comp++;
               if(st[j].lname < st[j+1].lname)
               {  temp=st[j];
                  st[j]=st[j+1];
                  st[j+1]=temp;
               }
        }
        }
     }
    }
    break;
    }
    }
    }
4

1 回答 1

0

好吧,在案例 5 中,您使用 student 作为数组的名称(或者可能是没有名称的数组的类型?)并且作为 temp 的类型;那么你永远不会使用student,而是使用st,它似乎在任何地方都没有定义。我很惊讶这个编译...

首先猜测一个解决方案:也许你的意思是 sort() 的参数是student st[]

第二个猜测:它不能编译,而且您实际上还没有尝试为链表编写排序代码。

试试这种方法:首先让 delete 工作,但编写它以便它可以选择返回“已删除”记录而不是释放它。然后,您可以通过构建一个新列表来替换旧列表来进行排序,方法是反复查找并不完全删除旧列表中剩余的最大元素,然后将其添加到新列表中,直到旧列表用完为止。

至于删除不起作用,您没有提供任何详细信息,但我注意到h从未更改,并且您假设将找到搜索的名称。

于 2013-08-21T15:20:18.787 回答