结构体与数组不同的是结构体能包含不同类型的数据,像一个综合性的仓库,更加灵活。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student
{
int number;
string name;
int grade1;
int grade2;
int grade3;
float score;
}stu[];
int main() {
int num;
int i, j;
cout << "请输入学生人数:" << endl;
cin >> num;
for (i = ; i < num; i++) {
stu[i].score = (stu[i].grade1 + stu[i].grade2 + stu[i].grade3) / ;
cout << "学号:"; cin >> stu[i].number;
cout << "姓名:"; cin >> stu[i].name;
cout << "语文成绩:"; cin >> stu[i].grade1;
cout << "数学成绩:"; cin >> stu[i].grade2;
cout << "英语成绩:"; cin >> stu[i].grade3;
stu[i].score = (stu[i].grade1 + stu[i].grade2 + stu[i].grade3) / ;
}
cout << setw() << "学号:" << setw() << "姓名:" << setw() << "语文成绩:";
cout << setw() << "数学成绩:" << setw() << "英语成绩:" << setw() << "平均成绩:" << endl;
for (i = ; i < num; i++) {
cout << setw() << stu[i].number << setw() << stu[i].name << setw() << stu[i].grade1;
cout << setw() << stu[i].grade2 << setw() << stu[i].grade3 << setw() << stu[i].score << endl;
} return ; }
简单的小程序,会在今后的学习中逐渐完善与丰富这个程序
运行结果如下: