1. 程式人生 > >習題 7.5 有10個學生,每個學生的資料包括學號、姓名、3門課的成績,從鍵盤輸入10個學生資料,要求打印出3門課的總平均成績,以及最高分的學生的資料。

習題 7.5 有10個學生,每個學生的資料包括學號、姓名、3門課的成績,從鍵盤輸入10個學生資料,要求打印出3門課的總平均成績,以及最高分的學生的資料。

C++程式設計(第三版)譚浩強 習題7.5 個人設計

習題 7.5 有10個學生,每個學生的資料包括學號、姓名、3門課的成績,從鍵盤輸入10個學生資料,要求打印出3門課的總平均成績,以及最高分的學生的資料(包括學號、姓名、3門課成績、平均分數)。

程式碼塊:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Student
{
    int num;
    string name;
    float score[3];
    float
aver; }; void input(Student *s, int n); void average(Student *s, int n); void high(Student *s, int n); int main() { Student stu[10], *st=stu; input(st, 10); average(st, 10); high(st, 10); system("pause"); return 0; } void input(Student *s, int n) { Student *p; int i, j; for
(p=s, i=0; p<s+n; p++, i++){ cout<<"Please enter No."<<i+1<<" student num, name, score: "; cin>>p->num>>p->name; for (j=0; j<3; cin>>p->score[j++]); } } void average(Student *s, int n) { Student *p; int i; float
sum, ave=0.0; for (p=s; p<s+n; p++){ sum=0.0; for (i=0; i<3; sum+=p->score[i++]); p->aver=(sum/3); ave+=p->aver; } cout<<"Average= "<<ave/n<<endl; } void high(Student *s, int n) { Student *p, *h; float high; int i; for (p=s, h=p, high=p->aver; p<s+n; p++) if (p->aver>high){ high=p->aver; h=p; } cout<<"Highest student info: "<<h->num<<' '<<h->name<<' '; for (i=0; i<3; cout<<h->score[i++]<<' '); cout<<h->aver<<endl; }