首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求大神帮小弟我修改上

2013-01-21 
求大神帮我修改下/*3 学生链表的处理在一个数据文件中存放若干学生数据记录,每条记录都有如下数据项:学号,

求大神帮我修改下

/*3 学生链表的处理
在一个数据文件中存放若干学生数据记录,每条记录都有如下数据项:学号,姓名,性别,成绩。
编一个程序,采用链接存储结构存储这批数据,并对该数据进行排序。
要求:链表前部为女同学,后部为男同学,并且男女同学都按升序,其运算结果存入数据文件中。*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
int    ID;//学号
char   name[20];//姓名
int    sex;//0代表男,1代表女
double score;//成绩
}datatype;

typedef struct node {
datatype data;
struct node *next;
}node, *Linklist;

Linklist initlist()
{
Linklist p;
p = (Linklist)malloc(sizeof(node));
p->next = NULL;
return p;
}
void createList_female(Linklist head)
{   FILE *fp;
intID;
charname[20]; 
intsex;
double  score;
Linklist p;
if((fp = fopen("records.txt","r")) == NULL)
{
printf("can not open file !");
exit(1);
}
while(!feof(fp)) {
fscanf(fp, "%d %s %lf %d", &ID, &name, &score, &sex);
if (p->data.sex == 1) {
p = (Linklist)malloc(sizeof(node));
p->data.ID = ID;
strcpy(p->data.name, name);
p->data.score = score;
p->data.sex = sex;
}
p->next = head->next;
head->next = p;
}
fclose(fp);
}

void createList_male(Linklist head)
{   FILE *fp;
intID;
charname[20]; 
intsex;
double  score;
Linklist p;
if((fp = fopen("records.txt","r")) == NULL)

printf("can not open file !");
return ;
}
while(!feof(fp)) {
fscanf(fp, "%d %s %lf %d", &ID, &name, &score, &sex);
if (p->data.sex == 0) {
p = (Linklist)malloc(sizeof(node));
p->data.ID = ID;
strcpy(p->data.name, name);
p->data.score = score;
p->data.sex = sex;
p->next = head->next;
}
p->next = head->next;
head->next = p;
}
fclose(fp);
}

void save(Linklist head)
{
FILE *fpout;
Linklist p;
if((fpout = fopen("result.txt","w"))==NULL)
{ printf("can not open file !");
  exit(1);
}
p=head->next;
while(p)
{
   fprintf(fpout,"%d %s %f %d\n",p->data.ID,p->data.name,p->data.score,p->data.sex);
   p=p->next;
}
fclose(fpout);
}

void sort_ID(Linklist head)
{
  Linklist q,p,u;
  p=head->next;
  head->next=NULL;/*利用原表头结点建新的空表*/
  while(p)
  { q=p; /*q为被插入的结点*/
    p=p->next;/*用p记录后继结点*/
    /*遍历新链表查找插入位置*/
    u=head;
    while(u->next!=NULL)/*查找插入位置*/
    { if(u->next->data.ID>q->data.ID)
         break;
      u=u->next; 
    }
    /*插入在u结点的后面*/
    q->next=u->next;


    u->next=q;
  }
}

int main()
{
Linklist female, male, stu;//stu为学生链表 前部为女 后部为男
female = initlist();
male   = initlist();
stu    = initlist();
createList_female(female);
createList_male(male);
sort_ID(female);
sort_ID(male);
stu->next = female;
while (stu)
stu = stu->next;
stu->next = male;
save(stu);
return 0;
}


我不知道怎么改 fp 链表
[解决办法]
/*3 学生链表的处理
在一个数据文件中存放若干学生数据记录,每条记录都有如下数据项:学号,姓名,性别,成绩。
编一个程序,采用链接存储结构存储这批数据,并对该数据进行排序。
要求:链表前部为女同学,后部为男同学,并且男女同学都按升序,其运算结果存入数据文件中。*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
int    ID;//学号
char   name[20];//姓名
int    sex;//0代表男,1代表女
double score;//成绩
}datatype;

typedef struct node {
datatype data;
struct node *next;
}node, *Linklist;

Linklist initlist()
{
Linklist p;
p = (Linklist)malloc(sizeof(node));
p->next = NULL;
return p;
}
void createList_female(Linklist head)
{   FILE *fp;
intID;
charname[20]; 
intsex;
double  score;
Linklist p;
if((fp = fopen("records.txt","r")) == NULL)
{
printf("can not open file !");
exit(1);
}
while(!feof(fp)) {
fscanf(fp, "%d %s %lf %d", &ID, &name, &score, &sex);
if (sex == 1) {//这里判断改了
p = (Linklist)malloc(sizeof(node));
p->data.ID = ID;
strcpy(p->data.name, name);
p->data.score = score;
p->data.sex = sex;
p->next = head->next;
head->next = p;//
}
}
fclose(fp);
}

void createList_male(Linklist head)
{   FILE *fp;
intID;
charname[20]; 
intsex;
double  score;
Linklist p;
if((fp = fopen("records.txt","r")) == NULL)

printf("can not open file !");
return ;
}
while(!feof(fp)) {
fscanf(fp, "%d %s %lf %d", &ID, &name, &score, &sex);
if (sex == 0) {//同上
p = (Linklist)malloc(sizeof(node));
p->data.ID = ID;
strcpy(p->data.name, name);
p->data.score = score;
p->data.sex = sex;
p->next = head->next;
head->next = p;
}
}
fclose(fp);
}

void save(Linklist head)
{
FILE *fpout;
Linklist p;
if((fpout = fopen("result.txt","w"))==NULL)
{ printf("can not open file !");
  exit(1);
}
p=head->next;
while(p)
{
   fprintf(fpout,"%d %s %f %d\n",p->data.ID,p->data.name,p->data.score,p->data.sex);
   p=p->next;
}
fclose(fpout);


}

void sort_ID(Linklist head)
{
  Linklist q,p,u;
  p=head->next;
  head->next=NULL;/*利用原表头结点建新的空表*/
  while(p)
  { q=p; /*q为被插入的结点*/
    p=p->next;/*用p记录后继结点*/
    /*遍历新链表查找插入位置*/
    u=head;
    while(u->next!=NULL)/*查找插入位置*/
    { if(u->next->data.ID>q->data.ID)
         break;
      u=u->next; 
    }
    /*插入在u结点的后面*/
    q->next=u->next;
    u->next=q;
  }
}

int main()
{
Linklist female, male, stu, tmp;//stu为学生链表 前部为女 后部为男
female = initlist();
male   = initlist();
stu    = initlist();
createList_female(female);
createList_male(male);
sort_ID(female);
sort_ID(male);
stu = female;
tmp=stu;//
while (tmp->next)//
tmp = tmp->next;
tmp->next = male->next;//
save(stu);
return 0;
}

热点排行