- # include <iostream.h>
- # include<iomanip.h>
- class stu
- {
- int no;
- char name[10];
- class cmarks
- {
- float Eng;int xfEng;
- float Chi;int xfChi;
- float Mat;int xfMat;
- public:
- float aver;
- void setdata()
- {
- float En,Ch,Ma;
- int xfEn,xfCh,xfMa;
- cout<<"English mark:"; cin>>En;
- setmarksE(En);
- cout<<"English credit:"; cin>>xfEn;
- setxfE(xfEn);
- cout<<"Chinese mark:"; cin>>Ch;
- setmarksC(Ch);
- cout<<"Chinese credit:"; cin>>xfCh;
- setxfC(xfCh);
- cout<<"Maths mark:"; cin>>Ma;
- setmarksM(Ma);
- cout<<"Maths credit:"; cin>>xfMa;
- setxfM(xfMa);
- }
- void setmarksE(float E)
- {Eng=E;}
- void setxfE(int xfE)
- {
- xfEng=xfE;
- }
- void setmarksC(float C)
- {
- Chi=C;
- }
- void setxfC(int xfC)
- {
- xfChi=xfC;
- }
- void setmarksM(float M)
- {
- Mat=M;
- }
- void setxfM(int xfM)
- {
- xfMat=xfM;
- }
- float getEng()
- {
- return Eng;
- }
- float getChi()
- {
- return Chi;
- }
- float getMat()
- { return Mat;
- }
- float getave()
- {
- return (Eng*xfEng+Chi*xfChi+Mat*xfMat)/(xfEng+xfChi+xfMat);
- }
- }marks;
- public:
- void setstd()
- {
- cout<<"NO:"; cin>>no;
- cout<<"Name:"; cin>>name;
- marks.setdata();
- }
- void disp1()
- {
- cout<<"NO. ";
- cout<<"Name ";
- cout<<"English mark ";
- cout<<"Chinese mark ";
- cout<<"maths mark ";
- cout<<"average mark ";
- cout<<endl;
- }
- void disp2()
- {
- cout<<setiosflags(ios::left);
- cout<<setiosflags(ios::fixed); //删除科学标记
- cout<<setprecision(2); //输出精度设置
- cout<<setw(5)<<no<<" ";
- cout<<setw(6)<<name<<" ";
- cout<<setw(12)<<marks.getEng()<<" ";
- cout<<setw(12)<<marks.getChi()<<" ";
- cout<<setw(10)<<marks.getMat()<<" ";
- cout<<setw(10)<<marks.getave();
- }
- };
- void main()
- {
- int n;
- cout<<"请输入学生人数,n="; //也可直接给定数组大小stu A[2];
- cin>>n;
- stu *ptr=new stu[n]; //动态创建对象数组
- for(int i=0;i<n;i++)
- {
- ptr[i].setstd();
- }
- ptr[0].disp1();
- for(i=0;i<n;i++)
- {
- ptr[i].disp2();
- cout<<endl;
- }
- delete[] ptr; //删除整个对象数组
- }