习题 7.3 编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号)、name(姓名)、score[3](3门课的成绩)。用主函数输入这些数据。。。
C++程序设计(第三版) 谭浩强 习题7.3 个人设计
习题 7.3 编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号)、name(姓名)、score[3](3门课的成绩)。用主函数输入这些数据,用print函数输出这些数据。
代码块:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Student
{
int num;
string name;
float score[3];
};
void print(Student *s);
int main()
{
Student stu[5], *st=stu;
int i, j;
for (i=0; i<5; i++){
cout<<"Please enter No."<<i+1<<" student num, name, score: ";
cin>>stu[i].num>>stu[i].name;
for (j=0; j<3; cin>>stu[i].score[j++]);
}
print(st);
system("pause");
return 0;
}
void print(Student *s)
{
int i, j;
Student *p;
for (p=s, i=0; p<s+5; p++, i++){
cout<<"Student "<<i+1<<" info: "<<p->num<<' '<<setw(6)<<p->name<<' ';
for (j=0; j<3; cout<<p->score[j++]<<' ');
cout<<endl;
}
cout<<endl;
}
还没有评论,来说两句吧...