打印学生成绩数组(结构体)

来源:互联网 发布:枸杞 怎么泡水 知乎 编辑:程序博客网 时间:2024/04/28 00:46

1

#include<cstdio>#define N 5struct student{    int num;    char name[50];    int score[4];}stu[N];void print(struct student stu[N]){    int i, j;    printf("编号   姓名   成绩1   成绩2   成绩3\n");    for(i = 0; i<N; i++){        printf("%3d%5s", stu[i].num, stu[i].name);        for(j=0; j<3; j++){            printf("%5d", stu[i].score[j]);        }        printf("\n");    }}int main(){    for(int i = 0; i<N; i++){        printf("请输入学生%d信息:\n", i+1);        printf("学生编号:");        scanf("%d", &stu[i].num);        printf("学生姓名:");        scanf("%s", &stu[i].name);        for(int j = 0; j<3; j++){            printf("该学生的第%d门成绩:", j+1);            scanf("%d", &stu[i].score[j]);        }        printf("\n");    }    print(stu);    return 0;}

2

#include<cstdio>#define N 5struct student{    int num;    char name[50];    int score[4];}stu[N];void input(struct student stu[]){    for(int i = 0; i<N; i++){        printf("请输入学生%d信息:\n", i+1);        printf("学生编号:");        scanf("%d", &stu[i].num);        printf("学生姓名:");        scanf("%s", &stu[i].name);        for(int j = 0; j<3; j++){            printf("该学生的第%d门成绩:", j+1);            scanf("%d", &stu[i].score[j]);        }        printf("\n");    }}void print(struct student stu[N]){    int i, j;    printf("编号   姓名   成绩1   成绩2   成绩3\n");    for(i = 0; i<N; i++){        printf("%3d%5s", stu[i].num, stu[i].name);        for(j=0; j<3; j++){            printf("%5d", stu[i].score[j]);        }        printf("\n");    }}int main(){    input(stu);    print(stu);    return 0;}

3.

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;#define N 10struct student{    int num;    char name[50];    float score[3];    float ping;}stu[N];bool cmp(student x, student y){    return x.ping > y.ping;}void input(struct student stu[]){    for(int i = 0; i<N; i++){        printf("请输入学生%d信息:\n", i+1);        printf("学生编号:");        scanf("%d", &stu[i].num);        printf("学生姓名:");        scanf("%s", &stu[i].name);        for(int j = 0; j<3; j++){            printf("该学生的第%d门成绩:", j+1);            scanf("%f", &stu[i].score[j]);        }        printf("\n");    }}void print(struct student stu[N]){    for(int i=0; i<10; i++){        printf("第%d名学生的平均成绩%f\n", i+1, stu[i].ping);    }    printf("最高分学生的数据:%s %d %f %f %f %f", stu[0].name, stu[0].num, stu[0].score[0], stu[0].score[1], stu[0].score[2], stu[0].ping);}int main(){    input(stu);    float sum;    for(int i=0; i<10; i++){        sum = 0;        for(int j=0; j<3; j++){            sum += stu[i].score[j];        }        stu[i].ping = sum/3;    }    sort(stu, stu+10, cmp);    print(stu);    return 0;}
阅读全文
0 0
原创粉丝点击