wang2679958237 2015-06-18 05:30 采纳率: 15.8%
浏览 2587

题目:学生顺序表的处理

在一个数据文件中存放若干学生数据记录,每条记录都有如下数据项:学号,姓名,性别,成绩。
编一个程序,采用 顺序存储结构 存储这批数据,并对该数据进行排序。要求:数组前部为男同学,后部为女同学,并且男女同学都按成绩递减排序,分别计算男生合格率、女生合格率、全班的成绩平均分,并把排序后的学生数据记录及计算结果存入另一个数据文件中。

  • 写回答

3条回答

  • devmiao 2015-06-18 05:32
    关注
     #include "stdio.h"
    #include <malloc.h>
    #include <stdlib.h>
    #include <string>
    
    struct Student
    {
     int num;
     char name[20];
     int  sex;//0女,1男
     int score;
    }stu;
    
    struct list 
    {
     struct Student student;
     struct list *next;
    };
    
    struct list *CreatFemale(struct list *head,struct Student stu)//女生信息
    {
     struct list *p;
     struct list *p1,*p2;
     p = head;
     p1 = (struct list *)malloc(sizeof(struct list));
     p1->student = stu;
     if (head == NULL)
     {
      head = p1;
      p1->next = NULL;
     }
    
     else
     {
      while (p->next != NULL && stu.score >= p->student.score)
      {
       p2 = p;
       p = p->next;
      }
    
      if (stu.score < p->student.score)
      {
       if (p == head)
       {
        head = p1;
        p1->next = p;
    
       }
    
       else
       {
        p2->next = p1;
        p1->next = p;
       }
      }
    
       else
       {
        p->next = p1;
        p1->next = NULL;
       }
    
    }
    
     return head;
    }
    
    struct list *CreatMale(struct list *head,struct Student stu)//男生信息
    {
     struct list *p;
     struct list *ptr;
     struct list *q;
     struct list *p1,*p2;
     q = head;
     ptr = head;
     p1 = (struct list *)malloc(sizeof(struct list));
     p1->student = stu;
     while (q->next != NULL && q->student.sex == 0)
     {
      ptr = q;
      q = q->next;
     }
     p = q;
     if (head == NULL)
     {
      head = p1;
      p1->next = NULL;
     }
    
    else
     {
      while (p->next != NULL && stu.score >= p->student.score)
      {
       p2 = p;
       p = p->next;
    
      }
    
      if (stu.score < p->student.score && p->student.sex == 1)
      {
    
          if (p == q )
       {
        p = p1;
        ptr->next = p1;
        p1->next = q; 
       }
       else
       {
        p2->next = p1;
        p1->next = p;
       }
      }
    
      else
      {
       p->next = p1;
       p1->next = NULL;
      }
    
    }
    
     return head;
    }
    
    void Print(struct list *head)//输出到文件
    {
     FILE *fp;
     char Sex[10];
     fp = fopen("data1.txt","w");
     if (fp == NULL)
     {
      printf("can't open the file!\n");
      exit(0);
     }
     struct list *p;
     struct list *q;
     p = head;
    
     while (p != NULL)
     {
      if (p->student.sex == 0)
      {
       strcpy(Sex,"女");
    
      }
    
      else
      {
       strcpy(Sex,"男");
      }
      fprintf(fp,"%d %s %s %d\n",p->student.num,p->student.name,Sex,p->student.score);
      q = p->next;
      free(p);
      p = q;
     }
    
     fclose(fp);
    }
    
    main()
    {
     struct list *head;
     FILE *fp;
     head = NULL;
     fp = fopen("data.txt","r");
     if (fp == NULL)
     {
      printf("can't open the file!\n");
      exit(0);
     }
    
     while (!feof(fp))
     {
      fscanf(fp,"%d%s%*c%d%d",&stu.num,stu.name,&stu.sex,&stu.score);
      if (stu.sex == 0)
      {
       head = CreatFemale(head,stu);//女
      }
     }
    
     fseek(fp,0L,0);
     while (!feof(fp))
     {
      fscanf(fp,"%d%s%*c%d%d",&stu.num,stu.name,&stu.sex,&stu.score);
      if (stu.sex == 1)
      {
       head = CreatMale(head,stu);//男的
      }
     }
    
     fclose(fp);
    
     Print(head);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器