2 wang2679958237 wang2679958237 于 2015.06.18 13:30 提问

题目:学生顺序表的处理

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

3个回答

devmiao
devmiao   Ds   Rxr 2015.06.18 13: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);
}
cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2015.06.18 18:01
q3733353520
q3733353520   2015.06.18 18:44

1、链表的增删插改
2、文件读写

Csdn user default icon
上传中...
上传图片
插入图片