【问题描述】结构体应用。全班有30位同学,某学期的课程数量为4门,利用结构体存储学生的基本信息(包括学号、姓名、4门成绩、绩点),通过从键盘上输入30个同学的学号、姓名、4门成绩,计算出每个同学的绩点,输出所有同学的信息(学号、姓名、4门成绩、绩点),并输出绩点最高的同学的学号和姓名。其中,存储学生信息的结构体为: struct stdinfo { char no[20]; char name[10]; float score[4]; float gpa; //绩点 }; 这里假设每个人的绩点为各科绩点的总和,单科成绩可通过下述公式转换为对应绩点值。 image.png 例如,成绩50分对应绩点0,成绩62对应绩点1.2,成绩85对应3.5 【输入形式】 【输出形式】标题名Id、Name、Score1、Score2、Score3、Score4、GPA用\t分隔; 一行中的数据也用\t分隔; 【输入/输出样例】红色部分代表输入的信息。 Please enter students number:5 Please input all student information: 101 s01 88 68 76 80 102 s02 78 80 88 69 103 s03 50 68 62 55 104 s04 90 99 92 95 105 s05 62 71 80 75 Id Name Score1 Score2 Score3 Score4 gpa 101 s01 88.00 68.00 76.00 80.00 11.20 102 s02 78.00 80.00 88.00 69.00 11.50 103 s03 50.00 68.00 62.00 55.00 3.00 104 s04 90.00 99.00 92.00 95.00 17.60 105 s05 62.00 71.00 80.00 75.00 8.80 The highest gpa is 17.60, Id is 104, name is s04.
时间: 2023-01-31 13:06:55 浏览: 183
C语言 结构体应用
以下是参考代码:
```
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 35;
struct stdinfo
{
char no[20];
char name[10];
float score[4];
float gpa;
};
stdinfo students[N]; // 存储学生信息的数组
int main()
{
int n;
cout << "Please enter students number:";
cin >> n;
cout << "Please input all student information:" << endl;
for (int i = 0; i < n; i++)
{
cin >> students[i].no >> students[i].name;
for (int j = 0; j < 4; j++)
cin >> students[i].score[j];
}
cout << "Id" << "\t" << "Name" << "\t" << "Score1" << "\t" << "Score2" << "\t" << "Score3" << "\t" << "Score4" << "\t" << "gpa" << endl;
for (int i = 0; i < n; i++)
{
students[i].gpa = 0; // 初始化绩点
for (int j = 0; j < 4; j++)
{
// 根据单科成绩计算绩点
if (students[i].score[j] >= 90)
students[i].gpa += 4.0;
else if (students[i].score[j] >= 85)
students[i].gpa += 3.7;
else if (students[i].score[j] >= 82)
students[i].gpa += 3.3;
else if (students[i].score[j] >= 78)
students[i].gpa += 3.0;
else if (students[i].score[j] >= 75)
students[i].gpa += 2.7;
else if (students[i].score[j] >= 72)
students[i].gpa += 2.3;
else if (students[i].score[j] >= 68)
students[i].gpa += 2.0;
else if (students[i].score[j] >= 64)
students[i].gpa += 1.5;
else if (students[i].score[j] >= 60)
students[i].gpa += 1.0;
}
printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].no, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].score[3], students[i].gpa);
}
// 找出
阅读全文