📄 stuinfomanager.h
字号:
#include <string>
#include <vector>
#include <fstream>
#include "StuInfoVec.h"
#include "SIManagerHelper.h"
namespace StudentInfomationManageStudio
{
const std::string DEFAULT_FILE_NAME="NewStuInfoStore.sdm";
//const std::string SUBJUCT_[]={"英语","数学","程序","物理","体育"};
const std::string SUBJUCT_[]={"Eng","Maths","Prog","Phys","Spts"};
class StuInfoManager :public StuInfoVec
{
protected:
bool isModified;//学生信息是否已被修改还未保存
int toRemvNodes;//做了删除标记的记录数
std::string fileName;
bool MaxToMin;
// static std::string DEFAULT_FILE_NAME;
//菜单选择函数
std::string menuSelect()
{
std::string cmd;
char ch;
cout<<'\\'<<this->fileName<<'>';
cin.clear();
cout.clear();
while(((ch=getchar())==' ')||(ch=='\n'));
do
{
cmd+=ch;
}while(((ch=getchar())!=' ')&&(ch!='\n'));
return cmd;
}
//命令获取函数
std::string NextCmd()
{
std::string cmd;
char ch;
while(((ch=getchar())==' ')||(ch=='\n'));
do
{
cmd+=ch;
}while(((ch=getchar())!=' ')&&(ch!='\n'));
return cmd;
}
//获得操作类型指令
int getCmdType()
{
std::string cmd=this->NextCmd();
if(cmd=="number") {return 0;}
else if(cmd=="name") {return 1;}
else if(cmd=="index") {return 2;}
else if(cmd=="cancel") {return -1;}
else if(cmd=="help") {SIManagerHelper::display();return -2;}
else {cout<<"Command \'"<<cmd<<"\' is unknown!"<<endl
<<"Input \'help\' for help."<<endl;return -2;}
}
//从字符串计算出一符点值,用于排序
float getLynCode(const std::string& str)
{
float code=0;
char ch;
float del=1;
int length=str.length();
for(int i=0;i<length;i++)
{
ch=str[i];
del=1;
for(int j=0;j<i;j++)
del*=32;
code+=(float)((double)(ch-'0')*128/(double)del);
}
return code;
}
//找出给定总分在所有记录中的名次
int getIndex(float sum)
{
Iterator it;
Iterator it_end=this->recVec.end();
int count=0;
for(it=this->recVec.begin();it!=it_end;it++)
{
if((*it)->sum>sum && (*it)->toBeDeleted==false && (*it)->index!=0)
count++;
}
return count+1;
}
//更新排名(增加删除记录时调用)
protected: void countIndex(float resetScoreFrom,bool isAdd)
{
int TSize=this->size();
Record *newRec;
for(int i=0;i<TSize;i++)
{
newRec=this->recVec[i];
if(newRec->sum<resetScoreFrom&&newRec->index!=0)
{
newRec->index += (isAdd? 1:-1);
}
}
isModified=true;
}
//更新排名(装载文件时调用)
protected: void countNewIndex()
{
int TSize=this->size();
Record *newRec;
for(int i=0;i<TSize;i++)
{
newRec=this->recVec[i];
if(newRec->index==0)
{
newRec->index=this->getIndex(newRec->sum);
this->countIndex(newRec->sum,true);
}
}
isModified=true;
}
//显示表头
void dispTable()
{
cout<<" ";
cout.width(12);
cout<<"Number";
cout<<" ";
cout.width(8);
cout<<"Name";
cout<<" ";
cout.width(5);
cout<<"Index";
cout<<" ";
cout.width(3);
cout<<"GD";
cout<<" ";
for(int i=0;i<DEFUALT_SCORE_AMOUNT;i++)
{
cout.width(4);
cout<<SUBJUCT_[i];
cout<<" ";
}
cout.width(4);
cout<<"Sum";
cout<<" ";
cout.width(8);
cout<<"Average";
cout<<endl;
}
public:
//static std::string SUBJUCT_[];
public:
StuInfoManager()
{
fileName=DEFAULT_FILE_NAME;
MaxToMin=true;
}
virtual ~StuInfoManager()
{
this->recVec.clear();
}
//开始成绩
virtual void start()
{
cout<<"Note:Input \'help\' for help."<<endl<<endl;
this->handleMenu();
}
//菜单处理函数管理程序的用户界面操作
virtual void handleMenu()
{
std::string cmd;
bool QuitState=false;
while(!QuitState)
{
cmd=this->menuSelect();
if(cmd=="disps") {displayRecords();}
else if(cmd=="query") {queryRecord();}
else if(cmd=="add") {addRecord();}
else if(cmd=="remove") {removeRecord();}
else if(cmd=="comeback"){removeNote();}
else if(cmd=="modify") {modifyRecord();}
else if(cmd=="sort") {sortRecords();}
else if(cmd=="save") {saveRecords();}
else if(cmd=="load") {loadRecords();}
else if(cmd=="quit") {quit();QuitState=true;}
else if(cmd=="clear") {clear();}
else if(cmd=="dispr") {dispRecord();}
else if(cmd=="recycl") {dispRecycl();}
else if(cmd=="help") {SIManagerHelper::display();}
else {cout<<"Command \'"<<cmd<<"\' is unknown!"<<endl
<<"Input \'help\' for help."<<endl;}
}
}
//显示记录
virtual void displayRecords()
{
this->dispTable();
cout<<this->recVec;
cout<<endl<<this->recVec.size()<<" infos be show."<<endl;
}
//查询记录
virtual void queryRecord()
{
std::string cmd;
int type;
std::string patten;
Iterator found;
cout.clear();
cin.clear();
int count=0;
for(;;)
{
cout<<" \\Query>";
type=this->getCmdType();
switch(type)
{
case -2:break;
case -1:return;
case 0:
case 1:
case 2:
{
cin>>patten;
count=0;
this->dispTable();
found=this->recVec.begin();
while((found=this->findRecord(patten, type, found))!=this->recVec.end())
{
cout<<endl<<*(*found);
count++;
found++;
}
cout<<endl<<count<<"Infos match \'"<<patten<<"\' be found."<<endl;
cout.clear();
cin.clear();
break;
}
default:cout<<endl<<"Unknown type."<<endl;return;
}
}
}
//添加记录
virtual void addRecord()
{
Record *newRec=new Record();
cin>>*newRec;
if(this->findRecord(newRec->number,0,this->recVec.begin())==this->recVec.end())
{
newRec->index=this->getIndex(newRec->sum);
this->countIndex(newRec->sum,true);
StuInfoVec::addRecord(newRec);
}
else
{
cout<<"new info number \'"<<newRec->number<<"\' is already presence."<<endl;
delete newRec;
}
}
//删除记录
virtual void removeRecord()
{
std::string cmd;
int type;
std::string patten;
Iterator found=this->recVec.begin();
int count=0;
type=this->getCmdType();
switch(type)
{
case -2:break;
case -1:return;
case 0:
case 1:
case 2:
{
cin>>patten;
while((found=this->findRecord(patten, type, found))!=this->recVec.end())
{
(*found)->toBeDeleted=true;
this->countIndex((*found)->sum,false);
count++;
found++;
if(type==2)break;
}
cout<<endl<<count<<" Infos match \'"<<patten<<"\' be removed."<<endl;
isModified=true;
return;
}
default:cout<<endl<<"Unknown type."<<endl;
}
}
//标记删除记录
virtual void removeNote()
{
std::string cmd;
int type;
std::string patten;
Iterator found=this->recVec.begin();
int count=0;
type=this->getCmdType();
switch(type)
{
case -2:break;
case -1:return;
case 0:
case 1:
case 2:
{
cin>>patten;
while((found=this->findRecord(patten, type, found))!=this->recVec.end())
{
(*found)->toBeDeleted=false;
(*found)->index=this->getIndex((*found)->sum);
this->countIndex((*found)->sum,false);
count++;
found++;
}
cout<<endl<<count<<" Infos match \'"<<patten<<"\' be comeback."<<endl;
isModified=true;
return;
}
default:cout<<endl<<"Unknown type."<<endl;
}
}
//修改记录
virtual void modifyRecord()
{
std::string cmd;
int type;
std::string patten;
Iterator found=this->recVec.begin();
int count=0;
type=this->getCmdType();
switch(type)
{
case -2:break;
case -1:return;
case 0:
case 1:
case 2:
{
cin>>patten;
while((found=this->findRecord(patten, type, found))!=this->recVec.end())
{
count++;
cout<<endl<<" No."<<count<<" Info be found:";
cout<<endl<<*(*found);
cin.clear();
cout.clear();
this->countIndex((*found)->sum,false);
cin>>*(*found);
(*found)->index=this->getIndex((*found)->sum);
this->countIndex((*found)->sum,true);
found++;
}
cout<<endl<<count<<"Infos match \'"<<patten<<"\' be modifyed."<<endl;
isModified=true;
return;
}
default:cout<<endl<<"Unknown type."<<endl;
}
}
//记录排序
virtual void sortRecords()
{
std::string cmd;
int type;
std::string patten;
Iterator found=this->recVec.begin();
type=this->getCmdType();
switch(type)
{
case -2:break;
case -1:return;
case 0:
case 1:
case 2:
{
int TSize=this->recVec.size();
if(TSize==0) return;
float *floatArray=new float[TSize];
float float_temp;
Record *rec_temp;
for(int i=0;i<TSize;i++)
{
rec_temp=this->recVec[i];
switch(type)
{
case 0: floatArray[i] = this->getLynCode(rec_temp->number); break;
case 1: floatArray[i] = this->getLynCode(rec_temp->name); break;
case 2: floatArray[i] = rec_temp->sum; break;
};
}
for(int j=0;j<TSize;j++)
{
for(int i=0;i<TSize-j-1;i++)
{
if((MaxToMin==true&&floatArray[i]<floatArray[i+1])||(MaxToMin==false&&floatArray[i]>floatArray[i+1]))
{
float_temp=floatArray[i];
floatArray[i]=floatArray[i+1];
floatArray[i+1]=float_temp;
rec_temp=this->recVec[i];
this->recVec[i]=this->recVec[i+1];
this->recVec[i+1]=rec_temp;
}
}
}
delete[]floatArray;
isModified=true;
cout<<endl<<"Infos be sorted.And the direction is "<<(MaxToMin? "Max to Min":"Min to Max")<<"."<<endl;
MaxToMin=!MaxToMin;
return;
}
default:cout<<endl<<"Unknown type."<<endl;
}
}
//保存记录
virtual void saveRecords()
{
std::string fileOut;
cin>>fileOut;
std::ofstream fileStreamOut;
fileStreamOut.open(fileOut.c_str(),std::ios_base::out|std::ios_base::trunc);
if(fileStreamOut)
{
if(StuInfoVec::saveRecords(fileStreamOut)==true)
cout<<"Records successful save to \""<<fileOut<<"\"."<<endl;
else
cout<<"Records failed save to \""<<fileOut<<"\"."<<endl;
this->fileName=fileOut;
}
else
{
cout<<endl<<"File \""<<fileOut<<"\" open fail."<<endl;
}
fileStreamOut.close();
}
//读取记录
virtual void loadRecords()
{
std::string fileIn;
cin>>fileIn;
int recordsBeLoad;
std::ifstream fileStreamIn;
fileStreamIn.open(fileIn.c_str(),std::ios_base::in);
if(fileStreamIn)
{
recordsBeLoad=StuInfoVec::loadRecords(fileStreamIn);
cout<<recordsBeLoad<<" records be loaded."<<endl;
this->countNewIndex();
}
else
{
cout<<endl<<"File \""<<fileIn<<"\" open fail."<<endl;
}
fileStreamIn.close();
}
//结束程序
virtual void quit()
{
this->clear();
}
//清空当前记录信息
virtual void clear()
{
this->recVec.clear();
this->fileName=DEFAULT_FILE_NAME;
//this->removePerform();
}
//显示一条记录的信息
void dispRecord()
{
int index;
cin>>index;
Record *record;
index--;
if(index<0||index>=(int)this->recVec.size())
{
cout<<endl<<"Index "<<index+1<<" out of range."<<endl;
}
else
{
this->dispTable();
record=this->recVec[index];
cout<<*record;
if(record->toBeDeleted==true)
cout<<"This record has been removed!"<<endl;
}
}
//显视已删除记录
void dispRecycl()
{
Iterator it;
Iterator it_end=this->recVec.end();
int count=0;
this->dispTable();
for(it=this->recVec.begin();it!=it_end;it++)
{
if((*it)->toBeDeleted==true)
{
cout<<*(*it);
count++;
}
}
cout<<endl<<count<<" Infos be show."<<endl;
}
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -