好长的一段代码 开始只是 有几个数据输出错误 后来改就越错了 高手帮忙看看哈
#include <stdio.h>#include <stdlib.h>
enum SEX{man, female};
struct student_info
{
char no[9];
char name[20];
enum SEX sex;
int age;
int classno;
float grade;
};
typedef struct student_info STUDENT;
STUDENT *getstuinfo(int i);
void sortstuinfo(STUDENT **pstu, int num);
void freememory(STUDENT **pstu, int num);
void main()
{
STUDENT **pstu;
int i, num;
printf("please input the number of the students:");
scanf("%d", &num);
if(num<=0)
return;
pstu=(STUDENT **)malloc(num*sizeof(STUDENT *));
if(pstu==NULL)
{
printf("no enouge memory!\n");
return;
}
for(i=0;i<num;i++)
{
pstu[i]=getstuinfo(i);
if(pstu==NULL)
{
printf("not enouge memory!\n");
return;
}
}
sortstuinfo(pstu, num);
printf("\n................sort the result.....................\n");
for(i=0;i<num;i++)
printf("%8s%8s%6s%5d%5d%8.1f\n", pstu[i]->no, pstu[i]->name, (pstu[i]->sex==man)?"man":"female", pstu[i]->age, pstu[i]->classno, pstu[i]->grade");
freememory(pstu, num);
getch();
}
STUDENT *getstuinfo(int i)
{
STUDENT *p;
char sex;
p=(STUDENT *)malloc(sizeof(STUDENT));
if(p==NULL)
return NULL;
printf("\n........input NO. %d student's information...........\n", i+1);
printf("no: ");
scanf("%s", p->no);
printf("name: ");
scanf("%s", p->name);
fflush(stdin);
while(1)
{
printf("please input sex(m/f): ");
scanf("%c", &sex);
if(sex=='m' || sex=='f')
break;
fflush(stdin);
}
p->sex=(sex=='m')?man:female;
printf("age: ");
scanf("%d", &p->age);
printf("classno: ");
scanf("%d", &p->classno);
printf("grade: ");
scanf("%f", &p->grade);
return p;
}
void sortstuinfo(STUDENT **pstu, int num)
{
STUDENT *p;
int i, j, k;
for(i=0;i<num-1;i++)
{
k=i;
for(j=i+1;j<num;j++)
if(pstu[k]->grade<pstu[j]->grade)
k=j;
if(k!=1)
{
p=pstu[i];
pstu[i]=pstu[k];
pstu[k]=p;
}
}
}
void freememory(STUDENT **pstu, int num)
{
int i;
for(i=0;i<num;i++)
free(pstu[i]);
free(pstu);
}