wang2679958237 于 2015.06.18 13:30 提问

3个回答

devmiao      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;
p1 = (struct list *)malloc(sizeof(struct list));
p1->student = stu;
{
p1->next = NULL;
}

else
{
while (p->next != NULL && stu.score >= p->student.score)
{
p2 = p;
p = p->next;
}

if (stu.score < p->student.score)
{
{
p1->next = p;

}

else
{
p2->next = p1;
p1->next = p;
}
}

else
{
p->next = p1;
p1->next = NULL;
}

}

}

struct list *CreatMale(struct list *head,struct Student stu)//男生信息
{
struct list *p;
struct list *ptr;
struct list *q;
struct list *p1,*p2;
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;
{
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;
}

}

}

{
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;

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()
{
FILE *fp;
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)
{
}
}

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)
{
}
}

fclose(fp);