![]() |
#2
蓝xuan2012-04-01 16:12
#include<iostream.h>
#include<string.h> #include<stdio.h> const int N=4; class Student { public: Student(); void Display(); void Average(); protected: char name[8]; char id[10]; char sex[3]; int course[N]; }; Student::Student() { int i; cin>>name>>id>>sex; for(i=0;i<N;i++) cin>>course[i]; } void Student::Display() { int i; cout<<name<<" "<<id<<" "<<sex<<" "; for(i=0;i<N;i++) cout<<course[i]<<" "; } void Student::Average() { double aver=0; int i; for(i=0;i<N;i++) aver+=course[i]/N; cout<<aver<<endl; } void main() { int n,i; cout<<" Please Input the Number of Students:"<<endl; cin>>n; // k=n; cout<<" Please input"<<n<<" student info: "<<endl<<"Name ID Sex Math English C Computer"; Student s[N]; /* for(i=0;i<n;i++) { a[i].Student(); }*/ cout<<endl; cout<<"Student Information you input:"<<endl; cout<<"Name ID Sex Math English C Computer Average"<<endl;; for(i=0;i<n;i++) { s[i].Display(); s[i].Average(); } } Please Input the Number of Students: 4 Please input4 student info: Name ID Sex Math English C Computer wang 1 f 80 80 80 80 zhang 2 m 85 85 85 85 liang 3 m 75 75 75 75 zong 4 f 86 86 86 86 Student Information you input: Name ID Sex Math English C Computer Average wang 1 f 80 80 80 80 80 zhang 2 m 85 85 85 85 84 liang 3 m 75 75 75 75 72 zong 4 f 86 86 86 86 84 Press any key to continue |
功能:
用动态对象数组实现一个简单的学生成绩管理系统。
定义一个学生类,有姓名、学号、性别、四门功课(Math、English、C、Computer)和平均分等私有数据成员。
用该学生类定义学生对象,学生信息由键盘输入;求出每个学生的平均分;然后显示每个学生的姓名、学号、四门课成绩和平均分。
要求:
1. 用动态对象数组实现;
2. 学生姓名的定义为:char *name;
3. 用构造函数实现学生信息的初始化;
4. 平均分的计算要用一个单独的函数实现;
5. 请严格按照下面的格式进行输入输出。
输入/输出样式:
Please Input the Number of Students:
4
Please input 4 student info: Name ID Sex Math English C Computer
wang 1 f 80 80 80 80
zhang 2 m 85 85 85 85
liang 3 m 75 75 75 75
zong 4 f 86 86 86 86
Student Information you input:
Name ID Sex Math English C Computer Average
wang 1 f 80 80 80 80 80
zhang 2 m 85 85 85 85 85
liang 3 m 75 75 75 75 75
zong 4 f 86 86 86 86 86