习题 7.3 编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号)、name(姓名)、score[3](3门课的成绩)。用主函数输入这些数据。。。

ゞ 浴缸里的玫瑰 2022-05-17 08:47 308阅读 0赞

C++程序设计(第三版) 谭浩强 习题7.3 个人设计

习题 7.3 编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号)、name(姓名)、score[3](3门课的成绩)。用主函数输入这些数据,用print函数输出这些数据。

代码块:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5. struct Student
  6. {
  7. int num;
  8. string name;
  9. float score[3];
  10. };
  11. void print(Student *s);
  12. int main()
  13. {
  14. Student stu[5], *st=stu;
  15. int i, j;
  16. for (i=0; i<5; i++){
  17. cout<<"Please enter No."<<i+1<<" student num, name, score: ";
  18. cin>>stu[i].num>>stu[i].name;
  19. for (j=0; j<3; cin>>stu[i].score[j++]);
  20. }
  21. print(st);
  22. system("pause");
  23. return 0;
  24. }
  25. void print(Student *s)
  26. {
  27. int i, j;
  28. Student *p;
  29. for (p=s, i=0; p<s+5; p++, i++){
  30. cout<<"Student "<<i+1<<" info: "<<p->num<<' '<<setw(6)<<p->name<<' ';
  31. for (j=0; j<3; cout<<p->score[j++]<<' ');
  32. cout<<endl;
  33. }
  34. cout<<endl;
  35. }

发表评论

表情:
评论列表 (有 0 条评论,308人围观)

还没有评论,来说两句吧...

相关阅读