友元不解1



#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值