题目:
建立学生信息档案,自建input(数据录入)和sort(数据排序)函数完成数据处理。
遇到的问题:
自建函数调用失败直接跳过了,原因不明。
# include<malloc.h>
# include<stdio.h>
struct Student
{
int age;
char name[100];
float score;
};
void sort(int * lon, struct Student ** s)
{
int i;
int j;
struct Student t;
lon = (int *)malloc((*lon) * sizeof(struct Student));
* s = (struct Student *)malloc((*lon)* sizeof(struct Student));
for(i=0; i<(*lon)-1; ++i)
for(j=0; j<(*lon)-1-i; ++j)
if((*s)[j].score < (*s)[j+1].score)
{
t = (*s)[j];
(*s)[j] = (*s)[j+1];
(*s)[j+1] = t;
}
}
void Input(int *len, struct Student ** p)
{
int i;
len = (int *)malloc((*len) * sizeof(struct Student));
* p = (struct Student *)malloc((*len) * sizeof(struct Student));
for(i=0; i<(*len); ++i)
{
printf("请输入第%d名学生信息\n", i+1);
printf("name = ");
scanf("%s", (*p)[i].name);
printf("age = ");
scanf("%d", &(*p)[i].age);
printf("score = ");
scanf("%f", &(*p)[i].score);
}
return;
}
int main(void)
{
int len;
int i;
struct Student * q;
printf("请输入学生人数:\n");
printf("len = ");
scanf("%d", &len);
Input (&len, &q);
sort (&len, &q);
for( i=0; i<len; ++i)
{
printf("第%d名学生的信息", i+1);
printf("name = %s\n", q[i].name);
printf("age = %d\n", q[i].age);
printf("score = %f\n", q[i].score);
}
return 0;
}
以下是执行结果:
请输入学生人数:
len = 3 (仅“3”为手动输入。)
第1名学生的信息Press any key to continue