c语言,结构体 学生成绩

Python014

c语言,结构体 学生成绩,第1张

#include #include #define STU_NUM 10//学生数 #define SCORE_NUM 6//每个学生的科目数 typedef struct /*定义结构体数组*/ { char num[20]/*学号*/ char name[20]/*姓名*/ float score[SCORE_NUM]/*成绩*/ float total//总分 float average//平均分 } StudentStudent stu[STU_NUM]//输入学生信息 void input() { int i,jprintf("请输入%d个学生的信息:\n",STU_NUM)for(i=0iij--) { if(stu[j].total>stu[j-1].total) { tStu=stu[j]stu[j]=stu[i]stu[i]=tStu} } } } void main() { input()process()sort()output()} 经供参考,具体自己调试。

#include <stdio.h>

#include <string.h>

#define STU_NUM 10//学生数

#define SCORE_NUM 6//每个学生的科目数

typedef struct /*定义结构体数组*/

{

char num[20]/*学号*/

char name[20]/*姓名*/

float score[SCORE_NUM]/*成绩*/

float total//总分

float average//平均分

} Student

Student stu[STU_NUM]

//输入学生信息

void input()

{

int i,j

printf("请输入%d个学生的信息:\n",STU_NUM)

for(i=0i<STU_NUMi++)

{

printf("学号:")

scanf("%s",stu[i].num)

printf("姓名:")

scanf("%s",stu[i].name)

for(j=0j<SCORE_NUMj++)

{

printf("科目%d的成绩:",j+1)

scanf("%f",&stu[i].score[j])

}

}

}

//输出学生信息

void output()

{

int i,j

//打印表头

printf("学号\t姓名\t")

for(j=0j<SCORE_NUMj++)

{

printf("科目%d\t",j+1)

}

printf("总分\t平均分\n")

//打印所有学生信息

for(i=0i<STU_NUMi++)

{

printf("%s\t%s\t",stu[i].num,stu[i].name)

for(j=0j<SCORE_NUMj++)

{

printf("%3.2f\t",stu[i].score[j])

}

printf("%3.2f\t%3.2f\n",stu[i].total,stu[i].average)

}

}

//计算总分和平均分

void process()

{

int i,j

for(i=0i<STU_NUMi++)

{

stu[i].total=0

for(j=0j<SCORE_NUMj++)

{

stu[i].total+=stu[i].score[j]

}

stu[i].average=stu[i].total/SCORE_NUM

}

}

//排序并输出

void sort()

{

Student tStu

int i,j

for(i=0i<STU_NUMi++)

{

for(j=STU_NUM-1j>ij--)

{

if(stu[j].total>stu[j-1].total)

{

tStu=stu[j]

stu[j]=stu[i]

stu[i]=tStu

}

}

}

}

void main()

{

input()

process()

sort()

output()

}

经供参考,具体自己调试。