"Couse.h" #ifndef COURSE_H #define COURSE_H #include#include #include #include #include using namespace std; class Course{ private: string courseName; double score; bool isBixiu; public: Course *next; //构造函数 Course(string nameVal,double scoreVal,bool isVal,Course *nextVal = NULL); Course(Course* nextVal = NULL); void setCourseName(string ); void setScore(double ); void setIsBixiu(bool); bool getIsBixiu(); string getCourseName(); double getScore(); }; #endif
//"deleteStudent.cpp" #include"record.h" bool VerifyDelete(); void Record::deleteStudent(){ /*system("cls"); char search_string[20]; current_ptr = head_ptr; if(current_ptr ==NULL){ cout<<"没有要删除的记录"<getName()==search_string){ if(flag==0)cout<<"找到记录 !"< getName()<<"\t\t"< getID()< getName()<<"\t"< getID()<<"\t记录已经删除\n"; DeleteNode(); getPause(); } else cout< getName()<<"\t"< getID()<<"\t该记录未删除"< next; } if(flag == 0) cout<<"该记录不存在,请核实后继续操作 !"< >choice; if(choice=="1")this->searchByName(); else if(choice=="2")this->searchByID(); else if(choice=="3")this->mainPage(); else { error(); return ; } if(VerifyDelete()) DeleteNode(); cout<<"\n信息已经成功删除。"< >yesno; if(yesno == "y" ) return 1; else if(yesno =="n") return 0; else { char pause; cout<<"\n输入有误。"< next == NULL) DeleteNodeAtEnd();//在表尾删除 else DeleteNodeAtMiddle();//在链表中部删除 } } void Record::DeleteNodeAtHead(){ if(head_ptr->next ==NULL){ //如果只有头结点 delete head_ptr; head_ptr = tail_ptr = current_ptr = NULL; return; } head_ptr = head_ptr->next; head_ptr->prior = NULL; delete current_ptr; current_ptr = head_ptr; return; } void Record::DeleteNodeAtEnd(){ tail_ptr = tail_ptr->prior; tail_ptr->next = NULL; delete current_ptr; current_ptr = NULL; return; } void Record::DeleteNodeAtMiddle(){ Student *temp_ptr; temp_ptr = current_ptr; current_ptr = current_ptr->next;//删除完自动跳到下一结点 temp_ptr->prior->next = current_ptr; current_ptr->prior = temp_ptr ->prior; delete temp_ptr; return; }
//“Student.h 文件” #ifndef STUDENT_H #define STUDENT_H #include"course.h" class Student{ private: string name; string ID; public: //文档信息 string sex; string phone; string birthday; string reword; string punishment; string activity; string hobby; //统计信息成员变量(暂且定义为public,简化代码。) double total; double ave; int unpassed; int courseNum; bool needUpdate; Course* head; Course* tail; Course* fence;//使用教材中的定义 //学生条目的存储使用双向链表实现 Student* next;//指向上一个学生 Student* prior;//指向后一个学生 Student();//构造函数 void setName(string valName); void setID(string setID); string getName(); string getID(); //与课程添加删除相关的函数声明 //课程信息用单向量表实现 void clear();//清空学生的所有课程信息 void setStart();//指针置前 void setEnd(); void nextItem(); bool getValue(string& name,double& score,bool& is); bool insert(string name,double score,bool is); bool remove(string &name,double &score,bool& is); void pre(); bool setPos(string name); void print(); bool find(string name); }; #endif
//"main.cpp" #include"record.h" Record myrecord; int main(){ myrecord.head_ptr = NULL; myrecord.tail_ptr = NULL; myrecord.loadCourse();//将该班级的必修课程科目载入数组Bixiu[string] 中,之后有用 if(myrecord.loadFile()) myrecord.mainPage(); else { cout<<"\n没有导入文件,退出。"<>choice; myrecord.userChoice(choice);//处理选择 }while(choice!="0"); }
"Record.h 文件" #ifndef RECORD_H #define RECORD_H #include"student.h" class Record{ public: Student* head_ptr; Student* tail_ptr; Student* current_ptr; vectorBixiu; //暂存的必修课程名称 char pause; string className; void userChoice(string choice);//用户选择函数 void insertStudent(); //插入学生信息函数 void deleteStudent(); //删除学生信息 void insertCourse(); //添加课程 void deleteCourse(); //删除课程 void inputScore(); //输入课程分数 void calculate(); //计算统计 void list(); //列表显示 bool search(); //信息搜索 void sort(); //信息排序 bool loadFile(); //文件载入 void saveFile(); //文件存储 void modify(); //信息修改 void outputTXT(); //txt文档输出 void insertNode(Student *); //插入结点子函数声明 void InsertNodeAtHead(Student* new_ptr);//插入表头的子函数声明 void InsertNodeAtEnd(Student* new_ptr); //插入表尾的子函数声明 void DeleteNode(); //删除链表结点 void DeleteNodeAtHead(); //在表头删除 void DeleteNodeAtEnd(); //在表尾删除 void DeleteNodeAtMiddle(); //在链表中部删除 //查找辅助子函数 bool searchByName(); bool searchByID(); //插入辅助子函数 void insertXuanxiu(); void insertBixiu(); void insertSingleBixiu(); //删除辅助子函数 void deleteXuanxiu(); void deleteBixiu(); //显示辅助子函数 void comuCOUT(); void studentCOUT(); void courseCOUT(); void error(); void listOUT(); void getPause(); bool requireList(); void listSingle(); //文档输出辅助子函数 void comuTXT(); void paperTXT(); void courseTXT(); void docTXT(); void listTXT(); //统计计算辅助子函数 void calForEach();//每一名学生的成绩信息统计 void calForCourse(); //控制辅助子函数 void mainPage(); bool onlyNumber(string PhoneNumber); bool onlySex(string sex); void fileInput(); //修改辅助函数 void modifyDetail(); void modifyDoc(); void modifyCourse(); //初始化辅助函数 void loadCourse();//将该班级的必修课程科目载入数组Bixiu[string] 中 void fileOutput(); void zero();//成绩统计清零 }; #endif
//"calculate.cpp " #include"record.h" void Record::calculate(){ string choice; system("cls"); cout<<<" ----------------计算统计信息-----------------"< < >choice; if(choice == "1"){ system("cls"); cout<<" ----对某个学生成绩信息进行计算统计----"< >choice; if(choice=="1"){ if(searchByName()){ system("cls"); this->calForEach(); cout<<"**************************************************************"< < getName()< < getID()< getName()<<" 学号:"< getID()< print(); cout< <<" 该学生所修科目总数:"< courseNum<<" 不及格总数:"< unpassed< total<<" 平均成绩为:"< ave< calForEach(); cout<<"**************************************************************"< < getName()< < getID()< getName()<<" 学号:"< getID()< print(); cout< <<" 该学生所修科目总数:"< courseNum<<" 不及格总数:"< unpassed< total<<" 平均成绩为:"< ave< >tempName; int studentNum = 0; double total = 0; double max = 0; double min = 100; double ave = 0; int unpassed = 0; bool isVal=false; double rate = 0.0; bool isHasStudent = false; current_ptr=head_ptr; while(current_ptr!=NULL){ if(current_ptr->find(tempName)){ isHasStudent = true; cout< <<"********"< getName()<<" "< fence->next->getScore()<<"********"< fence->next->getIsBixiu(); studentNum++;//选课人数统计 double tempScore = current_ptr->fence->next->getScore(); if(tempScore<60)unpassed++;//不及格人数统计 total+=tempScore;//总分数统计 if(tempScore max)max = tempScore;//最高成绩统计 ave = total/(double)studentNum;//平均成绩统计 rate = (double)(studentNum-unpassed)/studentNum;//及格率计算 } current_ptr = current_ptr->next; } if(isHasStudent){ cout<<"\n 课程名称:"< <<" 课程属性:"; if(isVal)cout<<"必修课"< fence = current_ptr->head; while(current_ptr->fence->next != NULL){ current_ptr->courseNum++; //课程总数统计 current_ptr->total+=current_ptr->fence->next->getScore(); //总分相加 if(current_ptr->fence->next->getScore()<60) //不及格科目统计 current_ptr->unpassed++; current_ptr->fence = current_ptr->fence->next; } current_ptr->ave = current_ptr->total/(double)current_ptr->courseNum;//平均分统计 } void Record::zero(){ current_ptr->total = 0; current_ptr->courseNum = 0; current_ptr->unpassed = 0; }
//"course.cpp" #include"course.h" Course::Course(string nameVal,double scoreVal,bool isVal,Course* nextVal){ courseName = nameVal; score = scoreVal; isBixiu = isVal; next = nextVal; } Course::Course(Course* nextVal){ next = nextVal; } void Course::setCourseName(string valName){ courseName = valName; } void Course::setScore(double valScore){ score = valScore; } string Course::getCourseName(){ return courseName; } double Course::getScore(){ return score; } void Course::setIsBixiu(bool isVal){ isBixiu = isVal; } bool Course::getIsBixiu(){ return isBixiu; }
//"deleteCourse.cpp" #include"record.h" void Record::deleteCourse(){ system("cls"); string choice; system("cls"); cout<<<" -------------课程删除----------------"< < >choice; if(choice == "1") deleteBixiu(); else if(choice =="2") deleteXuanxiu(); else if(choice=="3")mainPage(); else { error(); return; } } void Record::deleteBixiu(){ string search_string; cout<<"\n请输入要删除的课程的名称:"; cin>>search_string; current_ptr = head_ptr; while(current_ptr!=NULL){ if(current_ptr->find(search_string)&¤t_ptr->fence->next != NULL){ if(!current_ptr->fence->next->getIsBixiu()){ //判断该课程是否为必修 cout<<"\n该课程为选修课程,请重新进行正确的选择。"< remove(tempName,tempScore,tempIs); cout<<"\n"< getName()<<"的"< <<"课程已经删除 "< next; } getPause(); } void Record::deleteXuanxiu(){ if(searchByName()){ string search_string; cout<<"\n请输入要删除的课程的名称:"; cin>>search_string; if(current_ptr->find(search_string)&¤t_ptr->fence->next != NULL){ if(current_ptr->fence->next->getIsBixiu()){ //判断该课程是否为选修 cout<<"\n该课程为必修课程,不能对给别学生进行课程删除操作。"< remove(tempName,tempScore,tempIs); cout< < getName()<<"的"< <<"已经删除成功 "<
//"fileIO.cpp" #include"record.h" void Record::outputTXT(){ string choice; system("cls"); cout<<<" ---------------打印输出------------------"< < >choice; if(choice=="1") listTXT(); else if(choice=="2") comuTXT(); else if(choice=="3") paperTXT(); else if(choice=="4") courseTXT(); else if(choice=="5") docTXT(); else if(choice=="6") mainPage(); else error(); } void Record::listTXT(){ ofstream outfile; string fileName; fileName = this->className+"班级基本信息表.txt"; outfile.open(fileName,ios::out); if(outfile){ current_ptr = head_ptr; if(head_ptr ==NULL){ cout<<"记录为空,还未添加记录 !"< loadCourse();//将该班级的必修课程科目载入数组Bixiu[string] 中 //system("cls"); outfile<<"*********************全部学生基本信息列表**********************"< < calForEach();//调用函数对该学生的成绩进行统计 outfile<<"|"< < getName()< <<"|"< < getID()< <<"|"; for(int i=0;i find(Bixiu[i]); outfile< < fence->next->getScore()< <<"|"; } outfile< next; }while(current_ptr!=NULL); cout<<"\n文件生成成功。"< className+"班级同学录.txt"; outfile.open(fileName,ios::out); if(outfile){ int i=1; current_ptr = head_ptr; do{ outfile< <<"第"< <<"个同学"< getName()<<" 学号:"< getID()< sex<<" 电话号码:"< phone<<" 爱好:"< hobby< activity< reword< punishment< next; i++; }while(current_ptr!=NULL); cout<<"\n文件创建成功。"< >x; if(x == "n"){ getPause(); return ; } else if(x =="y"){ ofstream outfile; string fileName; fileName = current_ptr->getName()+"成绩单文件.txt"; outfile.open(fileName,ios::out); if(outfile){ outfile< <<" ---------------该同学的课程信息如下------------"< getName()<<" 学号:"< getID()< < fence = current_ptr->head; if(current_ptr->fence->next == NULL) outfile<<"该同学目前还未选择任何课程 !"< fence->next != NULL){ outfile<<" "< < < fence->next->getCourseName()< < fence->next->getScore(); if(current_ptr->fence->next->getIsBixiu())outfile< <<"必修课"< fence = current_ptr->fence->next; } outfile< <<"\n 该学生所修科目总数:"< courseNum<<" 不及格总数:"< unpassed< total<<" 平均成绩为:"< ave< >tempName; ofstream outfile; string fileName; fileName = className+tempName+"成绩单文件.txt"; outfile.open(fileName,ios::out); if(outfile){ int studentNum = 0; double total = 0; double max = 0; double min = 100; double ave = 0; int unpassed = 0; bool isVal; double rate; outfile<<"*************"< <<"成绩单************"< find(tempName)){ outfile< getName()<<" "< fence->next->getScore()< fence->next->getIsBixiu(); studentNum++;//选课人数统计 double tempScore = current_ptr->fence->next->getScore(); if(tempScore<60)unpassed++;//不及格人数统计 total+=tempScore;//总分数统计 if(tempScore max)max = tempScore;//最高成绩统计 ave = total/(double)studentNum;//平均成绩统计 rate = (double)(studentNum-unpassed)/studentNum;//及格率计算 } current_ptr = current_ptr->next; } outfile<<"\n课程属性:"; if(isVal)outfile<<"必修课"< >x; if(x == "n"){ getPause(); return ; } else if(x =="y"){ ofstream outfile; string fileName; fileName = current_ptr->getName()+"档案文件.txt"; outfile.open(fileName,ios::out); if(outfile){ outfile<<"**************************************************************"< < getName()<<" 学号:"< getID()< sex<<" 电话号码:"< phone<<" 爱好:"< hobby< activity< reword< punishment< sex<<" 电话号码:"< phone<<" 爱好:"< hobby< activity< reword< punishment<
//"init.cpp" #include"record.h" void Record::loadCourse(){ //将该班级的必修课程科目载入数组Bixiu[string] 中 this->Bixiu.clear(); if(head_ptr != NULL){ head_ptr->fence = head_ptr->head; while(head_ptr->fence->next != NULL){ if(head_ptr->fence->next->getIsBixiu()) this->Bixiu.push_back(head_ptr->fence->next->getCourseName()); head_ptr->fence = head_ptr->fence->next; } } } bool Record::onlyNumber(string numbers){ bool isOnly = true; for(int i=0;i'9'){ isOnly = false; break; } } return isOnly; } bool Record::onlySex(string sex){ if(sex=="男"||sex=="女") return true; else return false; }
//"inputScore.cpp" #include"record.h" void Record::inputScore(){ double tempScore; cout<<"\n\n请输入"<getName()<<"同学的"< fence->next->getCourseName()<<"成绩:"; cin>>tempScore; current_ptr->fence->next->setScore(tempScore); /*cout< <<"\n成绩输入完毕。"< fence->next->getCourseName()< < fence->next->getScore(); if(current_ptr->fence->getIsBixiu())cout< <<"必修课"<
//"insertCourse.cpp" #include"record.h" void inputMes(); void Record::insertCourse(){ string choice; system("cls"); cout<<<" ------------增加课程---------------"< < >choice; if(choice == "1") insertBixiu(); else if(choice =="2")insertXuanxiu(); else if(choice =="3") mainPage(); else error(); } void Record::insertXuanxiu(){ if(searchByName()){ string newCourseName; double newCourseScore; cout<<"\n添加的选修科目名称:"; cin>>newCourseName; cout<<"\n该选修课程的成绩:"; cin>>newCourseScore; current_ptr->insert(newCourseName,newCourseScore,false); cout<<"\n已经为该同学添加了"< <<"选修课程 。"< >newCourseName; current_ptr = head_ptr; while(current_ptr != NULL){ current_ptr->insert(newCourseName,0,true); this->inputScore(); zero(); current_ptr = current_ptr->next; } cout<<"\n已经为每一名同学添加了"< <<"必修课程 。"< loadCourse(); if(Bixiu.size() ==0){ cout<<"\n该班级未有必修课程,无需添加必修课程"< >tempScore; current_ptr->insert(Bixiu[i],tempScore,true); } }
//"insertNode.cpp" #include"record.h" void Record::insertNode(Student* new_ptr){ system("cls"); //friend_node *temp_ptr; //情况一:双向链表为空 if(head_ptr==NULL){ new_ptr->next = NULL; new_ptr->prior = NULL; head_ptr = new_ptr; tail_ptr = new_ptr; current_ptr= new_ptr; return; } //情况二:如果链表中只有一个结点 if(head_ptr->next==NULL){ if(new_ptr->getName() < head_ptr->getName()) InsertNodeAtHead(new_ptr); else InsertNodeAtEnd(new_ptr); return; } //情况三:链表中不为一个结点 if(head_ptr->next!=NULL){ current_ptr = head_ptr; while((current_ptr!=NULL)//这两个判断条件不能颠倒,否则会出现严重错误 !!! &&(new_ptr->getName() > current_ptr->getName())) current_ptr = current_ptr->next; if(current_ptr == head_ptr) InsertNodeAtHead(new_ptr); else if(current_ptr == NULL ) InsertNodeAtEnd(new_ptr); else//如果没有查找完整个表,并且满足排序条件推出, //欲插入结点应该插入current_ptr之前 { new_ptr->next = current_ptr; new_ptr->prior = current_ptr->prior; current_ptr->prior->next = new_ptr; current_ptr->prior = new_ptr; } return ; } } //插入表头子函数实现 void Record::InsertNodeAtHead(Student* new_ptr){ new_ptr->next = head_ptr; new_ptr->prior = NULL; head_ptr->prior = new_ptr; head_ptr = new_ptr; } //插入表尾子函数实现 void Record::InsertNodeAtEnd(Student* new_ptr){ new_ptr->prior = tail_ptr; new_ptr->next = NULL; tail_ptr->next = new_ptr; tail_ptr = new_ptr; }
//"insertStudent.cpp" #include"record.h" #includeusing namespace std; Student *new_ptr; void inputMes();//基本信息输入的子函数 void Record::insertStudent(){ new_ptr = new Student; if(new_ptr!=NULL){ inputMes();//调用子函数 //insertNode(new_ptr); //输入文档信息 。。。 cout<<"\n是否需要输入文档信息?(y/n)"; string tempChar; cin>>tempChar; if(tempChar == "y") fileInput(); else if(tempChar == "n"){ cout<<"\n如果以后需要添加该学生的文档信息,可以在主菜单里的“9_信息修改”中进行操作。"< current_ptr = new_ptr; this->insertSingleBixiu(); cout<<"\n是否需要继续添加选修课程?(y/n):"; cin>>tempChar; while(tempChar == "y"){ string newCourseName; double newCourseScore; cout<<"\n添加的选修科目名称:"; cin>>newCourseName; cout<<"\n该选修课程的成绩:"; cin>>newCourseScore; current_ptr->insert(newCourseName,newCourseScore,false); cout<<"\n已经为该同学添加了"< <<"选修课程 。"< >tempChar; } if(tempChar == "n"){ cout<<"\n以后还可以在主界面中“3_添加课程信息”一栏中进行课程的添加。"< >tempName; new_ptr->setName(tempName); //输入学号 cout<<"学号:"; string tempID; cin>>tempID; new_ptr->setID(tempID); } void Record::fileInput(){ string tempInput; cout<<"\n请输入性别:"; string temp; cin>>temp; if(onlySex(temp)){ current_ptr->sex = temp; } else{ cout<<"信息输入有误,请注意:性别只能是男或者女。"< >temp; if(onlyNumber(temp)){ current_ptr->phone = temp; } else{ cout<<"信息输入有误,请注意:电话号码只能是数字。"< >new_ptr->hobby; cout<<"\n请输入该生曾经参加过的活动:"; cin>>new_ptr->activity; cout<<"\n请输入该生曾经获得过的奖励:"; cin>>new_ptr->reword; cout<<"\n请输入该生曾经遭受过的惩罚:"; cin>>new_ptr->punishment; }
//"list.cpp" #include"record.h" using namespace std; void Record::list(){ system("cls"); string choice; cout<<<" --------------屏幕显示---------------"< < >choice; if(choice=="1") listOUT(); else if(choice=="2") comuCOUT(); else if(choice=="3") studentCOUT(); else if(choice=="4") courseCOUT(); else if(choice=="5") mainPage(); else error(); } void Record::listOUT(){ current_ptr = head_ptr; if(head_ptr ==NULL){ cout<<"记录为空,还未添加记录 !"< loadCourse();//将该班级的必修课程科目载入数组Bixiu[string] 中 system("cls"); cout<<"*********************全部学生基本信息列表**********************"< < calForEach();//调用函数对该学生的成绩进行统计 cout<<"|"< < getName()< <<"|"< < getID()< <<"|"; for(int i=0;i find(Bixiu[i]); cout< < fence->next->getScore()< <<"|"; } cout< next; }while(current_ptr!=NULL); getPause(); } void Record::comuCOUT(){ system("cls"); int n = 1; int i; current_ptr = head_ptr; if(head_ptr ==NULL){ cout<<"记录为空,还未添加记录 !"< next; i++; //}while(current_ptr!=NULL&&i<=n); if(current_ptr!=NULL){ getPause(); } else cout<<"文件结束"< >choice; if(choice=="1"){ if(searchByName())listSingle(); getPause(); } else if(choice == "2"){ if(searchByID())listSingle(); getPause(); } else if(choice=="3")list(); else if(choice=="4")mainPage(); else error(); } void Record::courseCOUT(){ calForCourse(); } void Record::error(){ cout<<"\n输入有误。"< calForEach(); cout<<"**************************************************************"< < getName()< < getID()< getName()<<" 学号:"< getID()< print(); cout< <<" 该学生所修科目总数:"< courseNum<<" 不及格总数:"< unpassed< total<<" 平均成绩为:"< ave<
//"loadFile.cpp" #include"record.h" bool Record::loadFile(){ string fileName; string choice; cout<<" --------------------班级文件载入系统----------------------"<< >choice; ifstream infile; Student *new_ptr; if(choice =="1") { fileName ="FRIENDS.dat"; this->className = "FRIENDS"; } else if(choice=="2"){ cout<<"\n请输入你要打开的班级文件,输入班级名称即可:"; cin>>this->className; fileName = className+".dat"; } else if(choice=="3") return false; else { error(); return false; } int end_loop = 0; infile.open(fileName,ios::in); if(infile){ do{ new_ptr = new Student; if(new_ptr!=NULL){ string tempName; infile>>tempName; new_ptr->setName(tempName); if(new_ptr->getName() != "文件结束" &&new_ptr->getName() != ""){ string tempID; infile>>tempID; new_ptr->setID(tempID); //内层循环读取课程信息 infile>>tempName; double tempScore; bool tempIs; while(tempName != "课程结束"){ infile>>tempScore; infile>>tempIs; new_ptr->insert(tempName,tempScore,tempIs); infile>>tempName; } //读取文档信息 infile>>new_ptr->sex; infile>>new_ptr->phone; infile>>new_ptr->hobby; infile>>new_ptr->activity; infile>>new_ptr->reword; infile>>new_ptr->punishment; insertNode(new_ptr); } else{ delete new_ptr; end_loop = 1; } } else{ cout<<"\n没有成功从磁盘导入文件 !"< >tempString; if(tempString == "y"){ outfile.open(fileName,ios::out); cout<<"\n班级文件创建成功。"<
//"modify.cpp" #include"record.h" void Record::modify(){ system("cls"); cout<<" ----对某个学生信息进行修改----"<>choice; bool isFind = false; if(choice=="1")isFind = searchByName(); else if(choice =="2")isFind = searchByID(); else if(choice =="3")mainPage(); else { cout<<"输入有误。"< >choice; string tempString; if(choice=="1"){ cout<<"\n请输入新的学生姓名"; cin>>tempString; current_ptr->setName(tempString); cout<<"\n学生信息修改成功。"< >tempString ; current_ptr->setID(tempString); cout<<"\n学生信息修改成功。"< >choice; if(choice=="1"){ cout<<"\n请输入性别:"; string temp; cin>>temp; if(onlySex(temp)){ current_ptr->sex = temp; cout<<"\n信息修改成功。"< >temp; if(onlyNumber(temp)){ current_ptr->phone = temp; cout<<"\n信息修改成功。"< >current_ptr->hobby; cout<<"\n信息修改成功。"< >current_ptr->activity; cout<<"\n信息修改成功。"< >current_ptr->reword; cout<<"\n信息修改成功。"< >current_ptr->punishment; cout<<"\n信息修改成功。"< calForEach(); cout< <<"\n ---------------该同学的课程信息如下------------"< print(); cout< <<"\n 该学生所修科目总数:"< courseNum<<" 不及格总数:"< unpassed< total<<" 平均成绩为:"< ave< >search_string; if(current_ptr->find(search_string)){ cout<<"\n请输入要修改的分数:"; double tempScore; cin>>tempScore; current_ptr->fence->next->setScore(tempScore); cout<<"\n信息修改成功。"< >tempChar; if(tempChar == "y")modifyCourse(); else if(tempChar != "n"){ error(); return; } this->zero();//课程信息有改动,所以需要重新计算其课程信息 } }
//"saveFile.cpp" #include"record.h" void Record::saveFile(){ ofstream outfile; string fileName = this->className+".dat"; outfile.open(fileName,ios::out); if(outfile){ current_ptr = head_ptr; while(current_ptr!=NULL){ outfile<getName()< getID()< fence = current_ptr->head; while(current_ptr->fence->next != NULL){ outfile< fence->next->getCourseName()< fence->next->getScore()< fence->next->getIsBixiu()< fence = current_ptr->fence->next; } outfile<<"课程结束"< sex< phone< hobby< activity< reword< punishment< next; } outfile<<"文件结束"<
//"search.cpp" #include"record.h" bool Record::search(){ string choice; system("cls"); cout<<<" -----------信息查找---------"< < >choice; if(choice == "1"){ bool temp = searchByName(); if( temp&&requireList()){ listSingle(); getPause(); } return temp; } else if(choice=="2"){ bool temp = searchByID(); if(temp&&requireList()){ listSingle(); getPause(); } return temp; } else if(choice=="3") mainPage(); else error(); return false; } bool Record::searchByName(){ //system("cls"); int flag =0; string search_string; current_ptr = head_ptr; if(current_ptr==NULL){ cout<<"学生记录为空 !"< >search_string; while(current_ptr!=NULL){ if(current_ptr->getName()==search_string){ if(flag==0)cout<<"\n找到记录"< getID()< next; } if(flag == 0){ cout<<"\n无该记录"< >search_ID; while(current_ptr!=NULL){ if(current_ptr->getID()==search_ID){ if(flag==0)cout<<"\n找到记录"< getName()<<" "; cout<<"\n学号为:"< getID()< next; } if(flag == 0){ cout<<"\n无该记录"< >choice; if(choice=="y")return true; else if(choice =="n")return false; else error(); return false; }
//"student.cpp" #include"student.h" using namespace std; Student::Student(){ head = tail = fence = new Course; this->unpassed = 0; this->total = 0.0; this->ave = 0.0; this->courseNum = 0; this->needUpdate = true; this->sex = "无"; this->phone="无"; this->hobby ="无"; this->punishment="无"; this->reword ="无"; this->activity="无"; } void Student::setName(string valName){ name = valName; } void Student::setID(string valID){ ID = valID; } string Student::getName(){ return name; } string Student::getID(){ return ID; } /* 需要实现的关于kecheng单向链表的函数 void clear(); void setStart();//指针置前 void setEnd(); void next(); bool getValue(string name,double score); bool insert(string name,double score); bool remove(string &name,double &score); void pre(); bool setPos(string name); void print(); bool find(string name); */ void Student::clear(){ while(head != NULL){ fence = head; head = head->next; delete fence; } head = tail = fence = new Course; } void Student::setStart(){ fence = head; } void Student::setEnd(){ fence = tail; } void Student::nextItem(){ if(fence != tail){ fence = fence->next; } else cout<<"\n这是最后一门课程,没有课程了 !"<next ==NULL) return false; //不要忘记fence指向的是当前元素的上一个位置 nameVal = fence->next->getCourseName(); scoreVal =fence->next->getScore(); isVal = fence->next->getIsBixiu(); return true; } bool Student::insert(string nameVal,double scoreVal,bool isVal){ //fence = tail;//暂时处理,追加到最后,暂且不排序 fence->next = new Course(nameVal,scoreVal,isVal,fence->next); if(fence == tail)tail = fence->next; return true; } bool Student::remove(string& nameVal,double& scoreVal,bool& isVal){ if(fence->next == NULL)return false; nameVal = fence->next->getCourseName(); scoreVal = fence->next->getScore(); isVal = fence->next->getIsBixiu(); Course* temp = fence->next; fence->next = temp->next; if(temp == tail) tail = fence; delete temp; return true; } void Student::pre(){ Course* temp = head; while(temp->next != fence) temp = temp->next; fence = temp; } bool Student::setPos(string nameVal){ fence = head; while(fence->next->getCourseName() != nameVal && fence->next!=NULL) fence = fence->next; if(fence->next == NULL){ cout<<"\n该学生没有选修该课程!"< next!=NULL&&fence->next->getCourseName() !=nameVal ) fence = fence->next; if(fence->next == NULL){ //cout<<"\n该学生没有选修该课程!"< getName()<<"同学没有选修该课程。"< next == NULL) cout<<"\n该同学目前还未选择任何课程 !"< next != NULL){ cout<<" "< < < next->getCourseName()< < next->getScore(); if(fence->next->getIsBixiu())cout< <<"必修课"< next; } }
//"userChoice.cpp" #include"record.h" #includeusing namespace std; void Record::userChoice(string choice){ if(choice=="1") insertStudent(); else if(choice=="2") deleteStudent(); else if(choice=="3") insertCourse(); else if(choice=="4") deleteCourse(); else if(choice=="5") calculate(); else if(choice=="6") list(); else if(choice=="7") search(); else if(choice=="8") outputTXT(); else if(choice=="9") modify(); else if(choice=="0") saveFile(); else error(); }