?? stuinfomanager.cpp
字號:
case '2':
{
int num = removePerform();
toRemvNodes=0;
cout << "一共刪除了" << num << "條記錄\n"
<< "現在還剩下" << size() <<"條記錄" << endl;
if (num != 0)
isModified = true;
}
break;
case '3':
removeNote(false);
break;
case '4':
{
Iterator it;
Iterator it_end = pastEnd();
for (it = first(); it != it_end;it++)
(*it)->toBeDeleted = false;
toRemvNodes = 0;
cout << "所有刪除標記已經被取消" << endl;
}
}
}
/*****************************************************
*功能: 標記指定記錄為"待刪除"或取消該標記
*參數: isToRemove,為true表示標記指定記錄為"待刪除"
* 為false表示取消指定記錄的"待刪除"標記
*******************************************************/
void StuInfoManager::removeNote(bool isToRemove)
{
if (empty())
{
cout << "當前沒有任何記錄." << endl;
return;
}
if (!isToRemove && toRemvNodes == 0)
{
cout << "當前沒有被做了刪除標記的項目" << endl;
return;
}
char choi[5];
char pattern[20];
int type;
while(true)
{
cout << "請輸入如何找到欲更改標記的記錄的方式:(直接輸入回車則結束移除操作) \n"
<< "1.按學號\n"
<< "2.按姓名\n"
<< "3.按名次\n";
cin.getline(choi,5);
//直接輸入回車則結束查詢
if (choi[0] == '\0') break;
if (choi[0] == '1' )
{
cout << "請輸入欲查詢的學生的學號: ";
type = 0;
}else if (choi[0] == '2')
{
cout << "請輸入欲查詢的學生的姓名: ";
type =1;
} else
{
cout << "請輸入欲查詢的學生的名次: ";
type = 2;
}
cin.getline(pattern,20);
//找到匹配的記錄
bool gotMatch = false;
Iterator it =findRecord(pattern,type,first());
Iterator it_end = pastEnd();
while (it != it_end)
{
//如果要取消標記而當前記錄沒有標記,或者是要做標記而
//當前記錄已經被標記,則繼續下一條記錄
if ((!isToRemove && (*it)->toBeDeleted) ||
(isToRemove && (*it)->toBeDeleted))
{
it = findRecord(pattern,type,++it);
continue;
}
gotMatch = true;
cout << "找到一條匹配的記錄: \n";
dispTable();
dispRecord(*it);
cout << "確定要改變這條記錄的標記嗎? (Y/N)";
cin.getline(choi,5);
//改變這條記錄的標記
if (choi[0] !='n' && choi[0] != 'N')
(*it)->toBeDeleted = !(*it)->toBeDeleted;
if (isToRemove)
toRemvNodes++;
else
toRemvNodes--;
//取下一條匹配的記錄
it = findRecord(pattern,type,++it);
}
if (!gotMatch)
cout << "沒有找到匹配的記錄" << endl;
else
{
if (toRemvNodes != 0)
cout << "當前有" << toRemvNodes
<< "條記錄做了刪除標記" << endl;
else
cout << "現在已經沒有被做了刪除標記的記錄了\n";
}
}
}
/*******************************
* 功能: 修改指定學生的信息
*******************************/
void StuInfoManager::modifyRecord()
{
if(empty())
{
cout <<"沒有可供修改的記錄"<<endl;
return;
}
char choi[5];
char pattern[20];
char name[20];
int type;
string str;
while(true)
{
cout <<"請輸入如何找到欲修改的記錄的方式:(直接輸入回車則結束移除操作)\n"
<<"1.按學號\n"
<<"2.按姓名\n"
<<"3.按名次\n";
cin.getline(choi,5);
//直接輸入回車則結束查詢
if (choi[0]=='\0') break;
if (choi[0]=='1')
{
cout<<"請輸入欲查詢的學生的學號:";
type=0;
}else if (choi[0]=='2')
{
cout <<"請輸入欲查詢的學生的姓名:";
type=1;
}else
{
cout <<"請輸入欲查詢的學生的名次: ";
type=2;
}
cin.getline(pattern,20);
//找到匹配的記錄
Iterator it=findRecord(pattern,type,first());
Iterator it_end=pastEnd();
if (it==it_end)
{
cout <<"沒有匹配的記錄。"<<endl;
continue;
}
while (it !=it_end)
{
cout <<"沒有一條匹配的記錄:\n";
dispTable();
dispRecord(*it);
cout <<"確定要修改這條記錄?[Y/n]";
cin.getline(choi,5);
//修改該記錄
if (choi[0]!='n'&&choi[0]!='N')
{
int ncount=0;
int tmpi=(*it)->index;
cout <<"下面請重新輸入該學生的信息:\n";
cout <<"請輸入學生:";
cin >>(*it)->number;
cin.ignore();
cout <<"請輸入姓名:";
cin.getline(name,20);
(*it)->name=name;
printf("請輸入性別 (0為女,1為男):");
cin >>str;
cin.ignore();
if (str[0]=='0')
(*it)->gender="女";
else
(*it)->gender="男";
float sum=0;
for (int j=0;
j<NUM_SUBJECT;j++)
{
cout <<"請輸入" <<subject_[j] <<"成績:";
cin >>(*it)->score[j];
sum+=(*it)->score[j];
}
cin.ignore();
(*it)->sum=sum;
(*it)->average=sum/NUM_SUBJECT;
//修改其他記錄的名次,
//將原來名次排在被修改記錄之后、而其sum大于等于
//修改后記錄的sum的記錄的名次減1,
//將原來名詞排在被修改記錄之前或相同、而其sum小于
//修改后記錄的sum的記錄的名次減1
ncount=0;
Iterator itl;
for (itl=first(); itl!=it_end;itl++)
{
if (itl==it) continue;
if ((*itl)->index>tmpi&&(*itl)->sum>=sum)
(*itl)->index--;
else if ((*itl)->index<=tmpi&&(*itl)->sum<sum)
(*itl)->index++;
if ((*itl)->sum>sum)
ncount++;
}
//ncount是總分比當前學生的總分大的學生的個數
//當前學生的名次是ncount+1
(*it)->index=ncount+1;
isModified=true;
}
//取下一條匹配的記錄
it=findRecord(pattern,type,++it);
}
cout <<"已找完所有的記錄\n" <<endl;
}
}
/**************************************************
*功能: 按照學號、姓名或名次對記錄進行排序
*結果: 各個記錄在數組中存儲的位置會發生變化
**************************************************/
void StuInfoManager::sortRecords()
{
if (empty())
{
cout <<"沒有可供排序的記錄!"<<endl;
return;
}
string str;
cout <<"請輸入您希望進行排序的方式:\n"
<<"1.按學號進行升序排序\n"
<<"2.按學號進行降序排序\n"
<<"3.按姓名進行升序排序\n"
<<"4.按姓名進行降序排序\n"
<<"5.按名次進行升序排序\n"
<<"6.按名次進行降序排序\n"
<<"7.按錯了,我并不想進行排序 "<<endl;
cin >>str;
cin.ignore();
if (str[0]<'1'||str[0]>'6') return;
//進行排序
int i,j;
int numStus=size();
for (i=0;i<numStus-1; i++)
{
for (j=i+1;j<numStus; j++)
{
if ((str[0]=='1'&&
recVec[i]->number>recVec[j]->number) ||
(str[0]=='2'&&
recVec[i]->number<recVec[j]->number)||
(str[0]=='3'&&recVec[i]->name>recVec[j]->name) ||
(str[0]=='4'&&recVec[i]->name>recVec[j]->name) ||
(str[0]=='5'&&recVec[i]->index>recVec[j]->index) ||
(str[0]=='6'&&recVec[i]->index>recVec[j]->index))
{
swap(recVec[i],recVec[j]);//這是STL的一個函數
}
}
}
cout <<"排序已經完成"<<endl;
isModified=true;
}
/******************************************
*功能:將學生信息保存至指定文件
*注意:該函數重載了基類中的函數
***************************************/
void StuInfoManager::saveRecords()
{
if (empty())
{
cout << "沒有記錄可存! " << endl;
return;
}
char name[40];
string fname;
cout << "請輸入襖保存的文件名(直接回車選擇文件sname): ";
cin.getline(name,40);
fname = name;
if (fname.empty())
fname = "sname";
ofstream of (fname.c_str(),ios_base::out);
if (!of)
{
cout << "不能存入文件! " << endl;
return;
}
StuInfoVec::saveRecords(of);
of.close();
cout << size() << "條記錄已經存入文件,請繼續操作." <<endl;
isModified = false;
}
/*************************************************
*功能:從指定文件讀取記錄追加到當前記錄末尾
*注意:該函數重載了基類中的函數
********************************************/
void StuInfoManager::loadRecords()
{
char choi[5];
if (!empty())
{
cout << "請選擇是要覆蓋現有記錄(Y),還是要將讀取的"
<< "記錄添加到現有的記錄之后(N)?\n"
<< "直接回車則覆蓋現有記錄\n";
cin.getline(choi,5);
if (choi[0] == 'n' || choi[0] == 'N')
{
//將讀取的記錄添加到現有記錄之后
isModified = true;
}
else
{
clear();
}
}
string fname;
char fname1[40];
cout << "請輸入要讀取的文件名(直接回車選擇文件sname):";
cin.getline(fname1,40);
fname = fname1;
if (fname.empty())
fname = "sname";
ifstream ifs(fname.c_str(),ios_base::in);
if (!ifs)
{
cout << "打不開文件!請重新選擇" << endl;
return;
}
int num = StuInfoVec::loadRecords(ifs);
ifs.close();
cout << "取入" << num << "條記錄.\n"
<< "現在一共有" << size() << "條記錄" << endl;
}
void StuInfoManager::quit()
{
clear();
cout << "退出操作,再見!" <<endl;
delete this;
exit(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -