📄 manager.cpp
字号:
#include<iostream>
#include<string>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <ctype.h>
using namespace std;
//判断学号是不否正确
bool IsSNo(const char *s){
char c;
int i=0;
if(strlen(s)!=8) return false;
while(*(s+i)!='\0'){
c=*(s+i);
if(!isdigit(c)) return false;
else return true;
}
return false;
}
//判断是否执行操作
bool IsSure(const char c){
if(c=='N'||c=='n') return false;
else return true;
}
//判断成绩是否符合约束条件
bool Scores(const double i){
if(i>=0&&i<150) return true;
return false;
}
//判断字符串是否为数字客串
bool IsNumber(const char *s){
bool Numberdoor=0,Indexdoor=0,NePositivedoor=0;
bool Decimaldoor=0;
int i=0;
char c;
while(*(s+i)!='\0'){
c=*(s+i);
if(isdigit(c)) {
Numberdoor=1;
NePositivedoor=1;
}
else if(c=='.'){
if(Decimaldoor==1) return false;
Numberdoor=0;
Decimaldoor=1;
}
else if(c=='+'||c=='-'){
if(NePositivedoor==1) return false;
if(Numberdoor==0) return false;
NePositivedoor=1;
Numberdoor=0;
}
else if(c=='e'||c=='E'){
if(Indexdoor==1) return false;
if(Numberdoor==0) return false;
Indexdoor=1;
Numberdoor=0;
NePositivedoor=0;
Decimaldoor=1;
}
else return false;
i++;
}
if(Numberdoor==0) return false;
return true;
}
//类声明部分
#ifndef NODE_CLASS
#define NODE_CLASS
template <class T>
class Node{
private:
Node<T> *next;//指向后继结点指针
public:
T data;//数据域
Node();
Node(T a);
Node (const T& item,Node<T> *ptrnext=NULL);//构造函数
void InsertAfter(Node<T> *p); //在本结点之后插入一个同类结点p
Node<T> *DeleteAfter(void); //删除本结点的后继结点,并返回其地址
Node<T> *NextNode(void) const; //获取后继结点的地址
};
//类的实现部分
//构造函数,初始化数据和指针成员
template <class T>
Node<T>::Node(){next=NULL;}
template <class T>
Node<T>::Node(T a):data(a){next=NULL;}
template <class T>
Node<T>::Node(const T& item,Node<T> *ptrnext): data(item),next(ptrnext){}
//返回后继结点的指针
template <class T>
Node<T> *Node<T>::NextNode (void) const
{return next;}
template <class T>
void Node<T>::InsertAfter (Node<T> *p){
if(p==NULL) {next=NULL;return;}
// p->next=next; //p结点指针域指向当前结点的后继结点
next=p; //当前结点的指针域指向p
}
//删除当前结点的后继结点,并返回其地址
template <class T>
Node<T> *Node<T>::DeleteAfter (void)
{
Node<T> *tempPtr=next; //将欲删除的结点地址存储到tempPtr中
if(next==NULL) //如果当前结点没有后继结点,则返回NULL
return NULL;
next=tempPtr->next;//使当前结点没有后继结点,则返回NULL
return tempPtr; //返回被删除的结点的地址
}
#endif //NODE___CLASS
//链表类模板声明
#ifndef LINKLIST_CLASS
#define LINKLIST_CLASS
template <class T>
class LiskList{
private:
Node<T> *front,*rear;//表头和表尾指针
Node<T> *prePtr,*currPtr;//记录当前遍历位置的指针,由插入和删除操作更新
int size;//表中的元素个数
int position;//当前元素在表中的位置序号,由函数Reset使用
//Node <T> *GetNode(const T& item,Node <T> *ptrNext=NULL);
//void FreeNode(Node <T> *p);
//void CopyList(const LinkList<T> L);
public:
LiskList(void);
//LinkedList(const LinkedList<T> &L);
~LiskList();//析构函数
//LinkedList<T> &operator=(const LinkedList<T> &L);
int ListSize(void ) const;//返回链表中元素个数(size)
int ListEmpty(void) const;//size等于0时返回TRUE,否则返回FALSE
Node<T> *Reset(int pos=0);//将指针currPtr移动到序号为pos的结点,prevPtr相应移动
//position记录当前结点的序号
void Next(void);//使prePtrt和currPtr移动到下一个结点
int EndOfList(void) const;//currPtr等于NULL时返回TRUE,否则返回FALSE
int CurrentPosition(void) const;//返回数据成员postion
void InsertFront();//在表头插入结点
void InsertFront(T &p);
void InsertRear();//在表尾添加结点
void InsertBefore();//在当前结点之前插入结点
void InsertAfter();//在当前结点之后插入结点
void DeleteFront(void);//删除前结点
void DeleteAt(void);//删除当前结点
void ListAll();//显示所有信息
Node<T> *FindItem();//查找结点
void Find();
void Delete (void);//删除某一结点
bool Modify();
T Data(void){return currPtr->data;}//返回数据域
void ClearList(void);//清空链表:释放所有结点的内存空间。被析构函数、operator =调用
void sorted();
};
//构造函数
template <class T>
LiskList<T>::LiskList(void){
position=0;
size=0;
front=rear=currPtr=prePtr=NULL;
}
//析构函数
template <class T>
LiskList<T>::~LiskList<T>(void){ClearList();}
template <class T>
int LiskList<T>::ListSize(void ) const{
return size;
}
//判断链表是否为空
template <class T>
int LiskList<T>::ListEmpty(void) const{
return front==NULL;
}
//设置当前结点(currPtr)位置
template <class T>
Node<T> *LiskList<T>::Reset(int pos){
int n;
currPtr=front;
prePtr=NULL;
position=pos;
if(pos>=size){cout<<"结点不存在!"<<endl; return NULL;}
for(n=0;n<pos;n++){
prePtr=currPtr;
currPtr=currPtr->NextNode();
}
return currPtr;
}
//判断当前指针(currPtr)是否移到链尾
template <class T>
int LiskList<T>::EndOfList(void) const{
return currPtr==NULL;
}
//移动到一个结点
template <class T>
void LiskList<T>::Next(void){
prePtr=currPtr;
currPtr=currPtr->NextNode();
}
//返回当前指针位置
template <class T>
int LiskList<T>::CurrentPosition(void) const{
int n;
Node<T> *p;
p==front;
for(n=0;p!=currPtr;n++,p=p->NextNode());
return n;
}
//重载插入头结点的函数
template <class T>
void LiskList<T>::InsertFront (T &temp1){
Node<T> *p=new Node<T>(temp1);
Node<T> *p0,*p1;
Node<T> *temp;
if(front==NULL) {front=p;size++; return;}
temp=front;
front=p;
p0=temp->NextNode();
p1=p0;
p->InsertAfter(temp);
for(;p1!=NULL;){
p0=p1;
p1=p1->NextNode();
temp->InsertAfter(p0);
temp=p0;
}
rear=p0;
size++;
}
//插入到头结点之前
template <class T>
void LiskList<T>::InsertFront(){
Node<T> *p=new Node<T>;
Node<T> *p0,*p1;
Node<T> *temp;
cout<<"请按要求输入信息!!!"<<endl;
cin>>p->data;
if(front==NULL) {front=p;size++; return;}
temp=front;
front=p;
p0=temp->NextNode();
p1=p0;
p->InsertAfter(temp);
for(;p1!=NULL;){
p0=p1;
p1=p1->NextNode();
temp->InsertAfter(p0);
temp=p0;
}
rear=p0;
size++;
}
//插入到尾结点之后
template <class T>
void LiskList<T>::InsertRear(){
Node<T> *p=new Node<T>();
if(front==NULL) {cout<<"无数据"<<endl;return;}
if(front->NextNode()==NULL){cout<<"仅有一个数据,不能添加!"<<endl;return ;}
cout<<"请按要求输入信息!!!"<<endl;
cin>>p->data;
Reset(size-1);
currPtr->InsertAfter(p);
rear=p;
size++;
}
template <class T>
void LiskList<T>::InsertBefore(){
Node<T> *p=new Node<T>();
cout<<"请按要求输入信息!!!"<<endl;
cin>>p->data;
prePtr->InsertAfter(p);
size++;
}
//插入到当前结点(currPtr)之后
template <class T>
void LiskList<T>::InsertAfter(){
Node<T> *p=new Node<T>;
if(front==NULL) {cout<<"链表为空!"<<endl; return;}
cout<<"请按要求输入信息!!!"<<endl;
cin>>p->data;
currPtr->InsertAfter(p);
size++;
}
//删除头结点
template <class T>
void LiskList<T>::DeleteFront(void){
front=front->NextNode();
delete currPtr;
currPtr=NULL;
size--;
}
//删除当前结点(currPtr)
template <class T>
void LiskList<T>::DeleteAt(void){
if(front==NULL) {cout<<"无数据"<<endl;return;}
if(front->NextNode()==NULL){cout<<"无数据"<<endl; return;}
if(currPtr==NULL){cout<<"无数据"<<endl; return;}
if(currPtr->NextNode()==NULL) rear=prePtr;
prePtr->DeleteAfter();
delete currPtr;
currPtr=NULL;
size--;
}
//清空链表
template <class T>
void LiskList<T>::ClearList(void){
Node<T> *p;
if(front==NULL) return;
for(p=front;front->NextNode()!=NULL;){
p=front->DeleteAfter();
delete p;
}
delete front;
front=NULL;
size=0;
}
template <class T>
bool LiskList<T>::Modify (){
Node<T> *p;
p=FindItem();
if(p==NULL) {cout<<"数据不存在"<<endl; return false;}
else cin>>p->data;
return true;
}
//查找结点
template <class T>
Node<T> *LiskList<T>::FindItem(){
char tempdata[20],temp[20];
cout<<"请输入姓名!"<<endl;
cin>>temp;
if(front==NULL) {cout<<"无数据"<<endl;return NULL;}
Reset();
for(;currPtr!=NULL;){
currPtr->data.GotName(tempdata);
if(strcmp(temp,tempdata)==0) break;
Next();
}
return currPtr;
}
template <class T>
void LiskList<T>::Find(){
Node<T> *p;
p=FindItem();
if(front==NULL)cout<<"文件中无数据"<<endl;
else if(p==NULL) cout<<"数据不存在"<<endl;
else {cout<<" 姓名 "<<" 学号 "<<"语文 "<<" 英语 "<<" 数学 "<<" 总分 "<<"平均分 "<<endl;cout<<p->data;}
}
//删除某一结点
template <class T>
void LiskList<T>::Delete (void){
Node<T> *p;
int i;
char sure,b[4]={'Y','y','N','n'};
bool suredoor=false;
p=FindItem();
if(front==NULL) return;
if(p==front) {
cout<<"删除的信息如下:"<<endl;
cout<<" 姓名 "<<" 学号 "<<"语文 "<<" 英语 "<<" 数学 "<<" 总分 "<<"平均分 "<<endl;
cout<<p->data<<endl;;
cout<<"确定要删除吗,是请输入Y(y),否则输入N(n): ";
do{cin>>sure;
for(i=0;i<4;i++)
if(b[i]==sure) suredoor=true;
if(suredoor==false)
cout<<"输入错误,请重新输入 ?_?: ";
}while(!suredoor);
if(IsSure(sure)){
DeleteFront();
cout<<"删除成功"<<endl;
}
else cout<<"取消删除操作"<<endl;
return;}
if(p==NULL){cout<<"不存在"<<endl; return;}
if(p==rear) {
cout<<"删除的信息如下:"<<endl;
cout<<" 姓名 "<<" 学号 "<<"语文 "<<" 英语 "<<" 数学 "<<" 总分 "<<"平均分 "<<endl;
cout<<p->data<<endl;
cout<<"确定要删除吗,是请输入Y(y),否则输入N(n): ";
do{cin>>sure;
for(i=0;i<4;i++)
if(b[i]==sure) suredoor=true;
if(suredoor==false)
cout<<"输入错误,请重新输入 ?_?: ";
}while(!suredoor);
if(IsSure(sure)){
DeleteFront();
cout<<"删除成功"<<endl;
}
else cout<<"取消删除操作"<<endl;
return;
}
cout<<"删除的信息如下:"<<endl;
cout<<" 姓名 "<<" 学号 "<<"语文 "<<" 英语 "<<" 数学 "<<" 总分 "<<"平均分 "<<endl;
cout<<p->data<<endl;
cout<<"确定要删除吗,是请输入Y(y),否则输入N(n): ";
do{cin>>sure;
for(i=0;i<4;i++)
if(b[i]==sure) suredoor=true;
if(suredoor==false)
cout<<"输入错误,请重新输入 ?_?: ";
}while(!suredoor);
if(IsSure(sure)){
DeleteFront();
cout<<"删除成功"<<endl;
}
else cout<<"取消删除操作"<<endl;
}
//显示所有结点信息
template <class T>
void LiskList<T>::ListAll(){
if(front==NULL){ cout<<"无数据"<<endl;return;}
int sum=0;
Reset();
while(!EndOfList()){
cout<<Data()<<" ";
cout<<endl;
sum++;
if(sum%20==0) system("pause");
Next();
}
cout<<"学生总数:"<<ListSize()<<endl;
}
//结点按姓名排列
template <class T>
void LiskList<T>::sorted(){
Node<T> *temp1=NULL,*temp2=NULL,*p1=NULL;
Node<T> *p2,*p,*temp3,*temp4,*temp=NULL;
int n=0;
char name[20],tempname[20];
if(front==NULL) return;
if(front->NextNode()->NextNode()==NULL){
p=front;
p->data.GotName(name);
temp4=p->NextNode();
temp4->data.GotName(tempname);
if(strcmp(name,tempname)>0){
front=temp4;
temp4=p->NextNode();
temp4->InsertAfter(p);
p->InsertAfter(temp);
}
return;
}
for(p=front;p->NextNode()!=NULL;p=p->NextNode()){
p2=p;
p->data.GotName(tempname);
strcpy(name,tempname);
while(p2->NextNode()!=NULL){
p1=p2;
p2=p2->NextNode();
p2->data.GotName(tempname);
if(strcmp(name,tempname) >0){
strcpy(name,tempname);
temp1=p1;
temp2=p2;
}
}
if(n==0){
front->data.GotName(tempname);
if(strcmp(tempname,name)>0){
temp4=temp2->NextNode();
temp1->InsertAfter(temp4);
temp2->InsertAfter(front);
p=front=temp2;
}
temp2=NULL;
n=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -