刚开始学C,希望大家帮我下
刚刚写的,基本符合要求
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
struct student{
char stu_num[10];
char stu_name[10];
float stu_grade;
};
int get_stu(struct student *stu,int i)//输入第i个学生的信息,存入数组stu中
{
student stu_temp;
printf("Enter the num of the student:");
scanf("%10s",&stu_temp.stu_num);
printf("Enter the name of student:");
scanf("%10s",&stu_temp.stu_name);
printf("Enter the grade of the student:");
scanf("%f",&stu_temp.stu_grade);
stu[i-1]=stu_temp;
return 0;
}
int order_stu(struct student *stu,int n)//按成绩排序,降序排列
{
student stu_temp;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(stu[j].stu_grade>stu[i].stu_grade)
{
strcpy(stu_temp.stu_num,stu[i].stu_num);
strcpy(stu_temp.stu_name,stu[i].stu_name);
stu_temp.stu_grade=stu[i].stu_grade;
strcpy(stu[i].stu_num,stu[j].stu_num);
strcpy(stu[i].stu_name,stu[j].stu_name);
stu[i].stu_grade=stu[j].stu_grade;
strcpy(stu[j].stu_num,stu_temp.stu_num);
strcpy(stu[j].stu_name,stu_temp.stu_name);
stu[j].stu_grade=stu_temp.stu_grade;
}
}
}
return 0;
}
float average_stu(struct student *stu,int n)//计算平均成绩
{
float average,sum=0;
for(int i=0;i<n;i++)
{
sum+=stu[i].stu_grade;
}
average=sum/(float)n;
return average;
}
int print_stu(struct student *stu,int n)//打印输出学生信息
{
for(int i=0;i<n;i++)
{
printf("%10s % 10s %10f\n",stu[i].stu_num,stu[i].stu_name,stu[i].stu_grade);
}
return 0;
}
int main()
{
int n;
printf("输入学生个数:");
scanf("%d",&n);
struct student stu[n];
memset(stu,0,n*sizeof(student));
for(int i=0;i<n;i++)
{
get_stu(stu,i+1);
}
order_stu(stu,n);
print_stu(stu,n);
printf("平均成绩为:%f\n",average_stu(stu,n));
printf("最高分和最低分的差为:%f\n",stu[0].stu_grade-stu[n-1].stu_grade);
getch();
return 0;
}
[此贴子已经被作者于2007-7-16 10:33:16编辑过]