前言
C++初學者學完封裝、繼承和多态後參考他人寫的學生管理系統,不過此處沒有用到多态的知識。
開發環境:Microsoft Visual C++2010學習版。
如有不對的地方歡迎大家指正。
功能實作
0、退出管理程式
1、增加學生資訊
2、顯示學生資訊
3、删除畢業學生
4、修改學生資訊
5、查找學生資訊
6、按照要求排序
7、儲存到檔案中
8、清空所有文檔
代碼内容
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
#define FILENAME "stuFile.txt"
class Student
{
public:
Student(){}
Student(int id,string name,int math,int chinese,int english)
{
this -> m_Id = id;
this -> m_Name = name;
this -> m_Math = math;
this -> m_Chinese = chinese;
this -> m_English = english;
}
void showInfo()//顯示個人資訊
{
cout << "學号: " << this -> m_Id << "\t姓名: " << this -> m_Name <<
"\t數學: " << this -> m_Math << "\t國文" << this -> m_Chinese << "\t英語"
<< this ->m_English << "\t總成績: "<< this ->getnum() << endl;
}
int getnum()//計算總成績
{
return this -> m_Math + this -> m_Chinese + this ->m_English;
}
int m_Id;//學号
string m_Name;//姓名
int m_Math;//數學
int m_Chinese;//國文
int m_English;//英語
};
class StudentManager
{
public:
StudentManager();// 構造函數
void Show_Menu();//展示菜單
void ExitSystem();//退出系統
int m_StuNum;//記錄學生人數
Student *m_StuArray;//學生數組指針
void Add_Stu();//添加學生
void save();//儲存檔案
bool m_FileIsEmpty;//判斷檔案是否為空 标志
int get_StuNum();//統計檔案中人數
void init_Stu();//初始化學生
void Show_Stu();//顯示學生
void Del_Stu();//删除學生
int IsExist(int id);//判斷學生是否存在,如果存在,傳回學生所在數組中的位置,不存在傳回-1
void Mod_Stu();//修改學生
void Find_Stu();//查找學生
void Sort_Stu();//按照要求排序
void Clean_File();//清空檔案
~StudentManager();// 析構函數
};
StudentManager::StudentManager()// 構造函數
{
//1、檔案不存在
ifstream ifs;
ifs.open(FILENAME,ios::in);//讀檔案
if(! ifs.is_open() )
{
//初始化記錄人數
this -> m_StuNum = 0;
//初始化數組指針
this -> m_StuArray =NULL;
//初始化檔案是否為空
this -> m_FileIsEmpty = true;
ifs.close();
return;
}
//2、檔案存在,資料為空
char ch;
ifs >> ch;
if( ifs.eof() )
{
//檔案為空
//cout << "檔案為空" << endl;
//初始化記錄人數
this -> m_StuNum = 0;
//初始化數組指針
this -> m_StuArray =NULL;
//初始化檔案是否為空
this -> m_FileIsEmpty = true;
ifs.close();
return;
}
//3、檔案存在,并且記錄資料
int num = this -> get_StuNum();
this -> m_StuNum = num;
//開辟空間
this -> m_StuArray = new Student[this -> m_StuNum];
//将檔案中的資料存到數組中
this -> init_Stu();
this -> m_FileIsEmpty = false;
}
void StudentManager::Show_Menu()
{
cout << "********************************************" << endl;
cout << "********* 歡迎使用學生管理系統! **********" << endl;
cout << "************* 0.退出管理程式 *************" << endl;
cout << "************* 1.增加學生資訊 *************" << endl;
cout << "************* 2.顯示學生資訊 *************" << endl;
cout << "************* 3.删除畢業學生 *************" << endl;
cout << "************* 4.修改學生資訊 *************" << endl;
cout << "************* 5.查找學生資訊 *************" << endl;
cout << "************* 6.按照要求排序 *************" << endl;
cout << "************* 7.儲存到檔案中 *************" << endl;
cout << "************* 8.清空所有文檔 *************" << endl;
cout << "********************************************" << endl;
cout << endl;
}
//退出系統
void StudentManager::ExitSystem()
{
cout << "歡迎下次使用" << endl;
system("pause");
exit(0); //退出程式
}
//添加學生
void StudentManager::Add_Stu()
{
cout << "請輸入添加學生數量: " << endl;
int addNum = 0; //儲存使用者的輸入數量
cin >> addNum;
if( addNum > 0 )
{
//添加
//計算添加新空間大小
int newSize = this -> m_StuNum + addNum; //新空間人數 = 原來紀錄人數 + 新增人數
//開辟新空間
Student* newSpace = new Student[newSize];
//将原來空間下資料,拷貝到新空間下
if( this -> m_StuArray != NULL )
{
for(int i = 0; i < this->m_StuNum; i++)
{
newSpace[i] = this -> m_StuArray[i];
}
}
//批量添加新資料
for(int i = 0; i < addNum; i++)
{
int id; //學号
string name; //姓名
int math;
int chinese;
int english;
cout << "請輸入第" << i+1 << "個新學生學号、姓名、數學、國文、英語" << endl;
cin >> id >> name >> math >> chinese >> english;
Student stu(id,name,math,chinese,english) ;
//儲存到數組中
newSpace[this -> m_StuNum + i] = stu;
}
//釋放原有空間
delete [] this -> m_StuArray;
//更改新空間的指向
this -> m_StuArray = newSpace;
//更新新的學生人數
this -> m_StuNum = newSize;
//更新學生不為空标志
this -> m_FileIsEmpty = false;
//提示添加成功
cout << "成功添加" << addNum << "名新學生!" << endl;
}
else
{
cout << "輸入資料有誤" << endl;
}
//按任意鍵後 清屏回到上級目錄
system("pause");
system("cls");
}
//儲存檔案
void StudentManager::save()
{
ofstream ofs;
ofs.open(FILENAME,ios::out);//寫檔案
//将每個人的資料寫入到檔案中
for(int i=0; i < this->m_StuNum; i++)
{
ofs << this -> m_StuArray[i] . m_Id << " "
<< this -> m_StuArray[i] .m_Name << " "
<< this -> m_StuArray[i] . m_Math<< " "
<< this -> m_StuArray[i] . m_Chinese<< " "
<< this -> m_StuArray[i] . m_English << endl;
}
ofs.close();//關閉檔案
cout << "儲存成功!" << endl;
//按任意鍵後 清屏回到上級目錄
system("pause");
system("cls");
}
//統計檔案中人數
int StudentManager::get_StuNum()
{
ifstream ifs;
ifs.open(FILENAME,ios::in);//打開檔案 讀
int id;
string name;
int math;
int chinese;
int english;
int num=0;
while(ifs >> id && ifs >> name && ifs >> math && ifs >> chinese && ifs >> english
)
{
//統計人數變量
num++;
}
return num;
}
//初始化學生
void StudentManager::init_Stu()
{
ifstream ifs;
ifs.open(FILENAME,ios::in);//打開檔案 讀
int id;
string name;
int math;
int chinese;
int english;
int index = 0;
while(ifs >> id && ifs >> name && ifs >> math && ifs >> chinese && ifs >> english
)
{
Student stu(id,name,math,chinese,english);
this -> m_StuArray[index] = stu;
index++;
}
ifs.close();
}
//顯示學生
void StudentManager::Show_Stu()
{
//判斷檔案是否為空
if(this -> m_FileIsEmpty)
{
cout << "檔案不存在或記錄為空" << endl;
}
else
{
for(int i=0; i<this->m_StuNum; i++)
{
this -> m_StuArray[i] .showInfo();
}
}
//按任意鍵清屏
system("pause");
system("cls");
}
//删除學生
void StudentManager::Del_Stu()
{
if(this->m_FileIsEmpty)
{
cout << "檔案不存在或記錄為空" << endl;
}
else
{
cout << "請輸入想要删除學生的學号" << endl;
int id =0;
cin >> id;
int index = this -> IsExist(id);
if( index != -1 )//說明要删除的學生存在
{
for(int i = index; i<this->m_StuNum-1; i++)
{
//資料前移
this -> m_StuArray[i] = this -> m_StuArray[i+1];
}
this -> m_StuNum--; //更新數組中記錄人員個數
cout << "删除成功" << endl;
}
else
{
cout << "删除失敗,未找到該學生" << endl;
}
}
//按任意鍵 清屏
system("pause");
system("cls");
}
//判斷學生是否存在,如果存在,傳回學生所在數組中的位置,不存在傳回-1
int StudentManager::IsExist(int id)
{
int index = -1;
for(int i=0; i<this ->m_StuNum; i++)
{
if(this ->m_StuArray[i] . m_Id == id)
{
index = i;
break;
}
}
return index;
}
//修改學生
void StudentManager::Mod_Stu()
{
if(this->m_FileIsEmpty)
{
cout << "檔案不存在或記錄為空" << endl;
}
else
{
cout << "請輸入修改學生的學号" << endl;
int id;
cin >> id;
int ret = this -> IsExist(id);
if( ret != -1 )
{
//查找到該學号學生
int newId = 0;
string newName = "";
int newMath=0;
int newChinese=0;
int newEnglish=0;
cout << "查到" << id << "号學生,請輸入新學生學号、姓名、數學、國文、英語:" << endl;
cin >> newId >> newName >> newMath >> newChinese >> newEnglish;
Student stu(newId,newName,newMath,newChinese,newEnglish);
//更新資料 到數組中
this -> m_StuArray[ret] = stu;
cout << "修改成功" << endl;
}
else
{
cout << "修改失敗,查無此人" << endl;
}
}
//按任意鍵 清屏
system("pause");
system("cls");
}
//查找學生
void StudentManager::Find_Stu()
{
if(this ->m_FileIsEmpty)
{
cout << "檔案不存在或記錄為空!" << endl;
}
else
{
cout << "請輸入查找方式:" << endl;
cout << "1、按學生學号查找" << endl;
cout << "2、按學生姓名查找" << endl;
int select = 0;
cin >> select;
if( select == 1 )
{
//按照學号查
int id;
cout << "請輸入查找的學生學号: " << endl;
cin >> id;
int ret = this -> IsExist(id);
if( ret != -1 )
{
//找到學生
cout << "查找成功!該學生資訊如下:" << endl;
this -> m_StuArray[ret] . showInfo();
}
else
{
cout << "查找失敗,查無此人" << endl;
}
}
else if( select == 2 )
{
//按照姓名查
string name;
cout << "請輸入查找的學生姓名: " << endl;
cin >> name;
//加入判斷是否查到标志
bool flag = false;//預設未找到學生
for(int i=0; i<this->m_StuNum; i++)
{
if( this -> m_StuArray[i].m_Name == name)
{
cout << "查找成功,學生學号為: "
<< this -> m_StuArray[i].m_Id
<< "号學生資訊如下:" <<endl;
flag = true;
this -> m_StuArray[i] . showInfo();
}
}
if( flag ==false )
{
cout << "查找失敗,查無此人" << endl;
}
}
else
{
cout << "輸入選項有誤!" << endl;
}
}
//按任意鍵 清屏
system("pause");
system("cls");
}
//按照學号排序
void StudentManager::Sort_Stu()
{
if (this->m_FileIsEmpty)
{
cout << "檔案不存在或記錄為空!" << endl;
//按任意鍵 清屏
system("pause");
system("cls");
}
else
{
cout << "--------------------------------------------" << endl;
cout << "請選擇排序種類: " << endl;
cout << "1、按學号進行排序" << endl;
cout << "2、按總成績進行排序" << endl;
int en=0;
cin >> en;
if(en == 1)
{
cout << "--------------------------------------------" << endl;
cout << "請選擇排序方式:" << endl;
cout << "1、按學号進行升序" << endl;
cout << "2、按學号進行降序" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < m_StuNum; i++)
{
int minOrMax = i;
for (int j = i + 1; j < m_StuNum; j++)
{
if (select == 1) //升序
{
if (m_StuArray[minOrMax].m_Id > m_StuArray[j].m_Id)
{
minOrMax = j;
}
}
else //降序
{
if (m_StuArray[minOrMax].m_Id < m_StuArray[j].m_Id)
{
minOrMax = j;
}
}
}
if (i != minOrMax)
{
Student temp = m_StuArray[i];
m_StuArray[i] = m_StuArray[minOrMax];
m_StuArray[minOrMax] = temp;
}
}
cout << "--------------------------------------------" << endl;
cout << "排序成功,排序後結果為:" << endl;
this->Show_Stu();
}
else
{
cout << "--------------------------------------------" << endl;
cout << "請選擇排序方式:" << endl;
cout << "1、按總成績進行升序" << endl;
cout << "2、按總成績進行降序" << endl;
int select1 = 0;
cin >> select1;
for (int i = 0; i < m_StuNum; i++)
{
int minOrMax = i;
for (int j = i + 1; j < m_StuNum; j++)
{
if (select1 == 1) //升序
{
if (m_StuArray[minOrMax].getnum() > m_StuArray[j].getnum() )
{
minOrMax = j;
}
}
else //降序
{
if (m_StuArray[minOrMax].getnum() < m_StuArray[j].getnum() )
{
minOrMax = j;
}
}
}
if (i != minOrMax)
{
Student temp = m_StuArray[i];
m_StuArray[i] = m_StuArray[minOrMax];
m_StuArray[minOrMax] = temp;
}
}
cout << "--------------------------------------------" << endl;
cout << "排序成功,排序後結果為:" << endl;
this->Show_Stu();
}
}
}
//清空檔案
void StudentManager::Clean_File()
{
cout << "确認清空?" << endl;
cout << "1、确認" << endl;
cout << "2、傳回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//打開模式 ios::trunc 如果存在删除檔案并重新建立
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_StuArray != NULL)
{
//删除堆區數組指針
this->m_StuNum = 0;
delete[] this->m_StuArray;
this->m_StuArray = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
StudentManager::~StudentManager()
{
if( this -> m_StuArray != NULL)
{
delete[] this -> m_StuArray;
this -> m_StuArray = NULL;
}
}
int main()
{
//system("color 4");
//執行個體化管理者對象
StudentManager wm;
int choice = 0;
while( true )
{
//調用展示菜單成員函數
wm.Show_Menu();
cout << "請輸入您的選擇:" << endl;
cin >> choice;
switch ( choice )
{
case 0: //退出系統
wm.ExitSystem();
break;
case 1: //增加學生
wm.Add_Stu();
break;
case 2: //顯示學生
wm.Show_Stu();
break;
case 3: //删除學生
wm.Del_Stu();
break;
case 4: //修改學生
wm.Mod_Stu();
break;
case 5: //查找學生
wm.Find_Stu();
break;
case 6: //排序學生
wm.Sort_Stu();
break;
case 7: //儲存資料到檔案中
wm.save();
break;
case 8: //清空文檔
wm.Clean_File();
break;
default:
system( "cls" ) ;//清屏
break;
}
}
system("pause");
return 0;
}