| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 635 人关注过本帖
标题:学生类链表
只看楼主 加入收藏
流生
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-11-26
收藏
 问题点数:0 回复次数:0 
学生类链表

朋友,请帮我找找错,谢谢!!!
朋友,请帮我找找错,谢谢!!!
错在哪?
错在哪?

好象是链表没有链起来啊!!!
#include "iostream.h"
#include "string.h"
#include "conio.h"
#include "iomanip.h"
int const M=3;
//定义学生类
class student{
private:
char *name;
int score; //[M];
static int number; //静态数据成员,统计总人数
static double average_marks; //静态数据成员,计算平均分
static long total_marks; //静态数据成员,计算总分
//student *next;
public:
student *next;
student(char *name1="wang",int score1=90); //默认参数的构造函数。
student(const student &s); //申明拷贝构造函数。
~student(); //申明析构函数
void Stu_Init(void);
void ob_print(void) //定义输出学生信息的函数
{
cout<<"The student: "<<name<<"'s score is : "<<score<<endl;
}
void static static_print(void); //申明静态成员函数
student & operator=(const student &s) //重载赋值运算符
{
if(this==&s) return *this;
total_marks-=score;
delete []name;
name=new char[strlen(s.name)+1];
strcpy(name,s.name);
score=s.score;
total_marks+=score;
return *this;
}
};

student::student(char *name1,int score1) //定义构造函数
{
name=new char[strlen(name1)+1];
strcpy(name,name1);
score=score1;
//number++;
//total_marks+=score;
}

void student::static_print(void) //定义静态成员函数
{
cout<<"Now:"<<endl;
cout<<"the number of student is :"<<number<<endl<<endl;
cout<<"the total marks is :"<<total_marks<<endl;
average_marks=double(total_marks)/number;
cout<<"the average marks is :"<<average_marks<<endl;
}

student::student(const student &s) //定义拷贝构造函数。
{
name=new char[strlen(s.name)+1];
strcpy(name,s.name);
score=s.score;
number++;
total_marks+=score;
}

void student::Stu_Init(void)
{
char na[13];
cout<<"请输入学生的名字:"<<endl;
cin>>na;
delete []name;
name=new char[strlen(na)+1];
strcpy(name,na);
cout<<"请输入该学生的分数: "<<endl;
cin>>score;
number++;
total_marks+=score;
cout<<name<<":"<<setw(6)<<score<<endl;
}

student::~student() //定义析构函数
{
delete []name;
number--;
total_marks-=score;
}

int student::number=0;
long student::total_marks=0;
double student::average_marks=0;

main(void)
{
student *head,*p,*q;
p=head=new student;
//p->next=head->next=NULL;
p->ob_print();
if(!p)
{
cout<<"ERROR!!!"<<endl;
return 0;
}
char c;
while(1)
{
head->Stu_Init();
head->ob_print();
cout<<"继续吗?(Y/N)"<<endl;
c=getch();
while(c!='Y'&&c!='y'&&c!='N'&&c!='n') c=getch();
if(c=='n'||c=='N') break;
q=head; head=q->next;
head=new student; head->next=NULL;
}
head=p;
//while(p!=NULL) { p->ob_print(); p=p->next;}
// p->ob_print();
p->next->ob_print();
//student s1("cheng",80),s2("wang",90),s3;
//s1.ob_print(); s2.ob_print(); s3.ob_print();
//student s4=s1; //等价于 student s4(s1) 前者是以“赋值”法调用拷
//贝构造函数,后者是以代入法调用。
//s4.ob_print();
student::static_print();
return 0;
}




搜索更多相关主题的帖子: 链表 学生 
2005-11-29 18:06
快速回复:学生类链表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043350 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved