|
|
#2
寒风中的细雨2010-12-06 20:28
//下面采用十字链表的形式
//简单示意一下: #include <iostream> #include <string> using namespace std; /*分数类*/ class Score { public: Score( int s=0, Score *dp=NULL, Score *rp=NULL ) {//默认下分数为零 向下、左的指针指向零 sco = s; DownNext_ptr = dp; RighNext_ptr = rp; } //private: int sco;//分 Score *DownNext_ptr;//指向本科目中下个学生的成绩 Score *RighNext_ptr;//指向本学生中下个科目的成绩 }; /*定义科目类*/ class Course { public: Course( string argu_N="未知", Score *sp=0, Course *cp=0 ) { Cou_Name = argu_N;//名称 Sco_ptr = sp; CouNext_ptr = cp; } //private: string Cou_Name;//科目名 Score *Sco_ptr;//指向科目成绩的的指针 Course *CouNext_ptr;//指向下一个科目的指针 }; /*定义学生类*/ class Stud { public: Stud( string id="", string name="", Score *sp=NULL, Stud *stp=NULL ) { S_Id = id;//学号赋值 S_Name = name;//姓名赋值 Sco_ptr = sp; StuNext_ptr = stp; } //private: string S_Id;//学号 string S_Name;//学生姓名 Score *Sco_ptr;//指向成绩的指针 Stud *StuNext_ptr;//指向下一个学生 }; /*定义班级类*/ class Class { public: Class( int i=0, int j=0 ) { Stu_Num = i; Cou_Num = j; Stud_hp = new Stud;//头结点 Cou_hp = new Course;//头结点 } int Create_table();//创建表格 int Show_table();//打印表格 int Delete_Stu();//删除学生 Stud * Search_Stu();//查找学生 Score * Search_Cou();//查找成绩 int Change_table();//修改表格 private: int Stu_Num; //学生数量 int Cou_Num; //科目数量 Stud *Stud_hp;//指向学生的头结点 Course *Cou_hp;//指向科目的头结点 }; int Class::Create_table() { int i=0;// string name; cout << "输入科目名称,科目总数不能超过" << Cou_Num << " 例如:语文 数学 英语 体育..." << endl; for(i=0; i<Cou_Num; ++i) { cout << "输入科目名称: "; cin >> name; Cou_hp->CouNext_ptr = new Course(name, NULL, Cou_hp->CouNext_ptr);//头插入 } string id; Course *ctemp = 0; Score *stemp; int score; for(i=0; i<Stu_Num; ++i) { cout << "输入ID: "; cin >> id; cout << "输入名字: "; cin >> name; Stud_hp->StuNext_ptr = new Stud(id, name, NULL, Stud_hp->StuNext_ptr); ctemp = Cou_hp->CouNext_ptr; while( ctemp ) { cout << "输入" << ctemp->Cou_Name << "成绩: "; cin >> score; //Stud_hp->StuNext_ptr->Sco_ptr = ctemp->Sco_ptr = new Score(score, ctemp->Sco_ptr, Stud_hp->Sco_ptr); if( !Stud_hp->StuNext_ptr->Sco_ptr ) { Stud_hp->StuNext_ptr->Sco_ptr = ctemp->Sco_ptr = new Score(score, ctemp->Sco_ptr); stemp = Stud_hp->StuNext_ptr->Sco_ptr; } else { stemp->RighNext_ptr = ctemp->Sco_ptr = new Score(score, ctemp->Sco_ptr); stemp = stemp->RighNext_ptr; } ctemp = ctemp->CouNext_ptr; } } return 0; } int Class::Show_table() { Course *ctemp = Cou_hp->CouNext_ptr; cout << '\t'; while(ctemp)//打印科目列表头 { cout << '\t' << ctemp->Cou_Name; ctemp = ctemp->CouNext_ptr; } cout << endl; Stud *stemp = Stud_hp->StuNext_ptr; Score *temp; while( stemp ) { cout << "名字:" << stemp->S_Name; temp = stemp->Sco_ptr; while( temp ) { cout <<'\t' << temp->sco; temp = temp->RighNext_ptr; } cout << endl; cout << "学号:" << stemp->S_Id << endl; stemp = stemp->StuNext_ptr; } return 0; } int main() { Class c(2, 3);//表示c班有2个人3门课程 c.Create_table(); c.Show_table(); return 0; } 只有本站会员才能查看附件,请 登录 |
怎样用C++编一个表格,类似成绩单那样的,其中姓名那一列和学科那一行可以用LIST链表的形式实现,并且可以实现增、删、查、改的功能,但对于中间的表体那一部分要怎么实现?、怎么样也可以是它也具有增、删、查、改的功能? 哪位高手可以给我示范一下???跪谢!!!