#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
class student{
private:
int stdid;
char name[20];
int math,eng,cpp;
double ave;
static int n;
public:
static double tol;
student(char *nam,int mah,int eg,int cp);
void display()const{
cout<<stdid<<'\t'<<setw(10)<<name<<'\t'<<math<<'\t'
<<eng<<'\t'<<cpp<<'\t'<<ave<<endl;
}
};
int student::n; //静态数据成员n的正式声明
double student::tol; //静态数据成员tol的正式声明
student::student(char *nam,int mah,int eg,int cp){
strcpy(name,nam);
math=mah;
eng=eg;
cpp=cp;
ave=(mah+eg+cp)/3.0;
n++;
stdid=1000+n;
tol=((tol*(n-1)+ave))/n;
}
class node{
private:
student stdt;
node *next;
public:
node(student x,node *y=0):stdt(x),next(y){} //构造函数,带默认参数,有初始化列表
void shownode(){
stdt.display();
}
friend class list; //why!!!!!!!!!!!
};
class list{
private:
node *head;
public:
list(){head=0;}
~list(){
node *p;
while(head){
p=head;
head=head->next;
delete p;
}
}
void crelist();
void showlist();
};
void list::crelist(){
node *p,*q;
char nam[20];
int math,eng,cpp;
cout<<"请输入姓名,若输入0就结束。"<<endl;
cin.getline(nam,20);
for(;strcmp(nam,"0");){
cout<<"请输入该同学的数学、英语和C++的成绩。"<<endl;
cin>>math>>eng>>cpp;
cin.get(); //将遗留在缓冲区内的换行符清除
student stdt(nam,math,eng,cpp);
p=new node(stdt);
if(head==0){
head=p;
q=p;
}
else{
q->next=p;
q=p;
}
cout<<"请输入姓名,若输入0就结束。"<<endl;
cin.getline(nam,20);
}
}
void list::showlist(){
cout<<"学号"<<'\t'<<setw(10)<<"姓名"<<'\t'<<"数学"<<'\t'
<<"英语"<<'\t'<<"C++"<<'\t'<<"平均成绩"<<'\t'<<endl;
node *p=head;
for(;p;){
p->shownode();
p=p->next;
}
cout<<"总平均成绩="<<student::tol<<endl;
}
int main()
{
list lst;
lst.crelist();
lst.showlist();
return 0;
}
友元不解1
最新推荐文章于 2023-06-05 22:53:40 发布