4条回答 默认 最新
- CSDN专家-Fay 2021-06-15 09:25关注
参考下代码:
#include<iostream> #include<string> #include<stdlib.h> #include<iomanip>//用setw()函数,控制台页面按照指定字符对齐 using namespace std; int choice=1; void menu(); class Node{ private: string name;//学生姓名 int age;//学生年龄 int num;//学生学号 string cla_ss;//学生班级 string teacher;//学生教师 int score;//学生排名 public: Node *prev;//前指针 Node *next;//后指针 Node(string name1="小明",int age1=18,int num1=0,string cla_ss1="计算机5班",string teacher1="王老师",int score1=1){//构造函数初始化 name=name1; age=age1; num=num1; cla_ss=cla_ss1; teacher=teacher1; score=score1; } ~Node(){ //此处进行了析构 } void changeall(){ string name1; cout<<"请输入学生姓名"<<endl; cin>>name1; name=name1; int age1; cout<<"请输入学生年龄"<<endl; cin>>age1; age=age1; int num1; cout<<"请输入学生学号"<<endl; cin>>num1; num=num1; string cla_ss1; cout<<"请输入学生班级"<<endl; cin>>cla_ss1; cla_ss=cla_ss1; string teacher1; cout<<"请输入学生老师"<<endl; cin>>teacher1; teacher=teacher1; int score1; cout<<"请输入学生排名"<<endl; cin>>score1; score=score1; } string getname(){return name;} int getage(){return age;} int getnum(){return num;} string getclass(){return cla_ss;} string getteacher(){return teacher;} int getscore(){return score;} void showall(){ cout<<setw(15)<<name<<setw(15)<<age<<setw(15)<<num<<setw(15)<<cla_ss<<setw(15)<<teacher<<setw(15)<<score<<endl; } void changeit(){ int a; cout<<"请问要修改哪项信息?"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"| |"<<endl; cout<<"| 1.姓名 2.年龄 3.学号 4.班级 5.教师 6.排名 |"<<endl; cout<<"| |"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl; cin>>a; switch(a){ case 1: {string name1; cout<<"请输入学生姓名:"<<endl; cin>>name1; name=name1; cout<<"修改成功!!!"<<endl; break;} case 2: {int age1; cout<<"请输入学生年龄:"<<endl; cin>>age1; age=age1; cout<<"修改成功!!!"<<endl; break;} case 3: {int num1; cout<<"请输入学生学号:"<<endl; cin>>num1; num=num1; cout<<"修改成功!!!"<<endl; break;} case 4: {string cla_ss1; cout<<"请输入学生班级:"<<endl; cin>>cla_ss1; cla_ss=cla_ss1; cout<<"修改成功!!!"<<endl; break;} case 5: {string teacher1; cout<<"请输入学生老师:"<<endl; cin>>teacher1; teacher=teacher1; cout<<"修改成功!!!"<<endl; break;} case 6: {int score1; cout<<"请输入学生排名:"<<endl; cin>>score1; score=score1; cout<<"修改成功!!!"<<endl; break;} system("pause"); system("cls"); } } class List{//整个链表的管理工作 整个链表的初始化和回收 private: Node *p; Node *q,*head,*tail; public: List(){//构建空链表 p=new Node; p->prev=NULL; p->next=NULL; q=p; head=p; tail=p; } ~List(){ Node *temp; while(head) { temp=head; head=head->next; delete temp; }//链表删除 } Node* GetHead(){ return head; } Node* GetTail(){ return tail; } }; class Set:public List{//集合操作的实现 private: List list0; Node *a,*b; public: Set(){ a=list0.GetHead(); b=list0.GetTail(); } //移除元素 void RemoveHead(){//移除第一元素 Node *temp; temp=a; a=a->next; delete temp; } void RemoveTail(){//移除最后一个元素 Node *temp,*temp1; temp=b; temp1=temp->prev; temp1->next=NULL; delete temp; } void RemoveAll(){//移除所有元素 Node *temp; while(a->next){ temp=a; a=a->next; delete temp; } temp=a; a=new Node; b=a; delete temp; List list0; cout<<"所有元素已移除"<<endl; } //添加元素的操作 void operator +(Node *head){//运算符重载函数 并 Node *head1=head; b->next=head1; head1->prev=b; Node *temp1,*temp2,*p,*q; temp1=a; temp2=head1; while(temp1) { while(temp2) { if(temp1==temp2) { Node *del; del=temp2; p=temp2->prev; q=temp2->next; p->next=q; q->prev=p; temp2=temp2->next; delete del; } else temp2=temp2->next; } temp1=temp1->next; } cout<<"集合取并集完成!!!"<<endl; } void operator -(Node *head){//运算符重载函数 差 Node *temp1,*temp2,*p,*q; Node *head1=head; temp1=a; temp2=head1; while(temp2) { while(temp1) { if(temp1->prev==NULL) { if(temp1==temp2) { Node *temp=temp1; temp1=temp1->next; temp1->prev=NULL; a=temp1; delete temp; } else temp1=temp1->next; } else { if(temp1==temp2) { Node *temp,*before,*after; temp=temp1; before=temp1->prev; after=temp1->next; before->next=after; after->prev=before; delete temp1; temp1=after; } else temp1=temp1->next; } } temp2=temp2->next; } } void And(Node *head){//集合操作函数 交 Node *temp1,*temp2,*p,*q,*item; Node *head1=head; temp1=a; temp2=head1; while(temp1) { while(temp2) { if(temp1->prev==NULL)//temp1前一个是空的 { if(temp2==temp1) { temp1=temp1->next; break; } else { if(temp2->next) temp2=temp2->next; else { Node *temp3=temp1; temp1=temp1->next; a=temp1; delete temp3; } } temp2=head1; } else//temp1前一个不是空的 { if(temp2==temp1) { temp1=temp1->next; break; } else { if(temp2->next) temp2=temp2->next; else { Node *temp3=temp1; Node *p,*q; p=temp1->prev; q=temp1->next; temp1=temp1->next; p->next=q; q->prev=p; delete temp3; } } } } } } void AddHead(){//在表头添加元素或者链表 函数重载 int n; cout<<"请问要添加多少学生?"<<endl; cin>>n; system("cls"); if(n==1) { a->changeall(); system("cls"); } if(n>1) { Node *temp1,*temp; a->changeall(); system("pause"); system("cls"); temp=new Node; temp->changeall(); a->next=temp; temp->prev=a; temp1=temp; b=temp; b->next=NULL; system("pause"); system("cls"); for(int i=2;i<n;i++) { temp=new Node; temp->changeall(); temp1->next=temp; temp->prev=temp1; temp1=temp; system("pause"); system("cls"); } b=temp; b->next=NULL; } if(n<=0) { cout<<"输入的参数错误!!!"<<endl; } } void AddTail(){//在表尾添加元素或者链表 函数重载 int n; cout<<"请问要添加多少学生?"<<endl; cin>>n; system("cls"); if(n==1) { Node *temp,*temp1; temp1=new Node; temp=b; temp->next=temp1; temp1->prev=temp; temp1->next=NULL; temp1->changeall(); system("pause"); system("cls"); } if(n>1) { Node *item,*temp,*head1; item=new Node; temp=item; head1=item; item->changeall(); system("pause"); system("cls"); item=new Node; temp->next=item; temp->prev=NULL; item->prev=temp; temp=item; item->changeall(); system("pause"); system("cls"); for(int i=2;i<n;i++) { item=new Node; temp->next=item; item->prev=temp; temp=item;//item是尾指针 item->changeall(); system("pause"); system("cls"); } b->next=head1;//并操作。剔除重复信息 head1->prev=b; } if(n<=0) { cout<<"参数错误!!!"<<endl; } } //遍历操作 Node *GetNext(Node *p){//返回下一个元素的位置 Node *temp; temp=p->next; return temp; } Node *GetPrev(Node *p){//返回前一个元素的位置 Node *temp; temp=p->prev; return temp; } void RemoveAt(Node *temp1){//移除指定下标值的元素 Node *temp; Node *p,*q; temp=temp1; if(temp->prev!=NULL&&temp->next!=NULL) { p=temp->prev; q=temp->next; p->next=q; q->prev=p; delete temp;} if(temp->prev!=NULL&&temp->next==NULL) { p=b; b=b->prev; delete p; } if(temp->next!=NULL&&temp->prev==NULL) { p=a; a=a->next; delete p; } if(temp->prev==NULL&&temp->next==NULL) { delete temp; List list; list0=list; a=list0.GetHead(); b=list0.GetTail(); } } //查询操作 Node *Find(){//返回指定值的元素的位置 int k; Node *temp; temp=a; cout<<"请问要按什么查找?"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"| |"<<endl; cout<<"| 1.姓名 2.学号 3.排名 |"<<endl; cout<<"| |"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl; cin>>k; switch(k){ case 1: { string name1; cout<<"请输入学生姓名:"<<endl; cin>>name1; while(temp) { if(temp->getname()==name1) { return temp; break; } else temp=temp->next; } return NULL; cout<<"并没有此项信息!!!"<<endl; } break; case 2: { int num1; cout<<"请输入学生学号:"<<endl; cin>>num1; while(temp) { if(temp->getnum()==num1) { return temp; break; } else temp=temp->next; } return NULL; cout<<"并没有此项信息!!!"<<endl; } break; case 3: { int score1; cout<<"请输入学生排名:"<<endl; cin>>score1; while(temp) { if(temp->getscore()==score1) { return temp; break; } else temp=temp->next; } return NULL; cout<<"并没有此项信息!!!"<<endl; } break; default: cout<<"参数错误!!!"<<endl; break; } } //状态测试 int GetCount(){//返回链表长度 int n=0; Node *temp=a; while(temp) { n++; temp=temp->next; } return n; } int IsEmpty(){//判断链表是否为空 if(a->next==NULL&&a->getname()=="小明"&&a->getscore()==1&&a->getage()==18&&a->getclass()=="计算机5班"&&a->getteacher()=="王老师") return 1;//链表为空 else return 0; }//链表不为空 //元素的具体操作 void seek(){ int judge; judge=IsEmpty(); int i=1; do{ if(judge==1) { cout<<"链表为空,请先添加信息!!!"<<endl; insert(); break; } else { Node *temple; temple=Find(); system("cls"); if(temple!=NULL) { cout<<"学生信息如下:"<<endl; cout<<setw(15)<<"姓名"<<setw(15)<<"年龄"<<setw(15)<<"学号"<<setw(15)<<"班级"<<setw(15)<<"教师"<<setw(15)<<"排名"<<endl; cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; temple->showall(); cout<<"---------------------------------------------------------------------------------------------------------------"<<endl;} system("pause"); system("cls"); cout<<"1.继续查找 2.返回主菜单"<<endl; cin>>i; system("cls"); } }while(i==1); } void modify(){ int judge; judge=IsEmpty(); int i=1; do{ if(judge==1) cout<<"链表为空,请先添加信息!!!"<<endl; else { cout<<"首先查找该学生"<<endl; Node *temple; temple=Find(); if(temple!=NULL) temple->changeit(); } system("cls"); cout<<"--------------------------"<<endl; cout<<"|1.继续修改 2.返回主菜单|"<<endl; cout<<"--------------------------"<<endl; cin>>i; }while(i==1); } void insert(){ int judge; judge=IsEmpty(); int i=1; do{ if(judge==1) { cout<<"目前有0条信息"<<endl; AddHead(); break; } else { cout<<"目前共有"<<GetCount()<<"位学生的信息"<<endl; AddTail(); cout<<"--------------------------"<<endl; cout<<"|1.继续添加 2.返回主菜单|"<<endl; cout<<"--------------------------"<<endl; cin>>i; } }while(i==1); } void del(){ int judge; judge=IsEmpty(); int i=1; do{ if(judge==1) { cout<<"目前有0条信息,请首先添加学生生信息"<<endl; insert(); } else { cout<<"1.删除所有信息 2.删除部分信息"<<endl; int sign; cin>>sign; system("cls"); if(sign==1) { RemoveAll(); } if(sign==2) { Node *temp; cout<<"首先查找信息"<<endl; temp=Find(); system("cls"); RemoveAt(temp); } } cout<<"-------------------------------"<<endl; cout<<"|1.继续删除信息 2.返回主菜单|"<<endl; cout<<"-------------------------------"<<endl; cin>>i; }while(i==1); } void display(){ Node *temp=a; if(IsEmpty()!=1) { if(temp->next){ cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; cout<<setw(15)<<"姓名"<<setw(15)<<"年龄"<<setw(15)<<"学号"<<setw(15)<<"班级"<<setw(15)<<"教师"<<setw(15)<<"排名"<<endl; cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; while(temp->next){ temp->showall(); cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; temp=temp->next; } temp->showall(); cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; cout<<"以上为所有学生信息"<<endl; } else{ cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; cout<<setw(15)<<"姓名"<<setw(15)<<"年龄"<<setw(15)<<"学号"<<setw(15)<<"班级"<<setw(15)<<"教师"<<setw(15)<<"排名"<<endl; cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; temp->showall(); cout<<"---------------------------------------------------------------------------------------------------------------"<<endl; cout<<"以上为所有学生信息"<<endl;} } else cout<<"目前没有学生信息,请添加学生信息"<<endl; system("pause"); system("cls"); } void tongji(){ int n; cout<<"--------------------------------------------------------"<<endl; cout<<"|1.统计学生总数 2.统计特定教师学生数目 3.统计以上所有|"<<endl; cout<<"--------------------------------------------------------"<<endl; cin>>n; if(n==1) { int num1=0; Node *temp1=a; while(temp1->next!=NULL) { num1++; temp1=temp1->next; } cout<<"学生总数为:"<<num1<<endl; system("pause"); system("cls"); } if(n==2) { int num2=0; string tname; cout<<"请输入教师姓名"<<endl; cin>>tname; Node *temp2=a; while(temp2->next!=NULL){ if(temp2->getteacher()==tname) { num2++; temp2=temp2->next; } else temp2=temp2->next; } cout<<"该教师有学生"<<num2<<"名"<<endl; system("pause"); system("cls"); } if(n==3) { int num1=0; Node *temp1=a; while(temp1->next!=NULL) { num1++; temp1=temp1->next; } cout<<"学生总数为:"<<num1<<endl; int num2=0; string tname; cout<<"请输入教师姓名"<<endl; cin>>tname; Node *temp2=a; while(temp2->next!=NULL){ if(temp2->getteacher()==tname) { num2++; temp2=temp2->next; } else temp2=temp2->next; } cout<<"该教师有学生"<<num2<<"名"<<endl; } system("pause"); system("cls"); } }; Set ABC; int main() { do{ menu(); }while(choice!=0); } void menu() { system("cls"); cout<<"*****************************************************************\n"; cout<<"*\t\t\t学生信息管理系统\t\t\t*\n"; cout<<"*****************************************************************\n"; cout<<"*\t\t\t系统功能选择菜单\t\t\t*\n"; cout<<"*---------------------------------------------------------------*\n"; cout<<"* 0.退出菜单 *\n"; cout<<"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\n"; cout<<"* 1.查询信息 2.修改信息 *\n"; cout<<"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\n"; cout<<"* 3.增加信息 4.删除信息 *\n"; cout<<"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\n"; cout<<"* 5.显示信息 6.统计信息 *\n"; cout<<"*---------------------------------------------------------------*\n"; cout<<"* 请选择菜单编号 : *\n"; cout<<"*****************************************************************\n"; cin>>choice; system("cls"); switch(choice) { case 0: break; case 1: ::ABC.seek(); break; case 2: ::ABC.modify(); break; case 3: ::ABC.insert(); break; case 4: ::ABC.del(); break; case 5: ::ABC.display(); break; case 6: ::ABC.tongji(); break; default: { cout<<"选择错误,请重新选择"<<endl; system("pause"); system("cls"); } break; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报