歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++實現學生信息管理系統

C++實現學生信息管理系統

日期:2017/3/1 9:07:50   编辑:Linux編程

利用線性鏈表實現學生成績管理系統,具體功能:輸入、輸出、插入、刪除、查找、追加、讀入、顯示、保存、拷貝、排序、索引、分類合計、退出,並能在屏幕上輸出操作前後的結果。

1. 寫出系統需求分析,並建模。
2. 編程實現,界面友好。
3. 輸出操作前後的結果

1.頭文件

#define MAX_NUM 10
#include <String>
#include <fstream>
using namespace std;

bool quit = false;

struct StuNode{
int num;
int math, eng, yuwen;
int sum;
StuNode *nextstu;
};

class SInfo{
StuNode *StuListHead;
public:
SInfo(); //構造函數
~SInfo(); //析構函數
void CreatSinfo(); //創建學生信息
void StuInsert(int snum, int smath, int seng, int syuwen); //插入學生信息
void StuDelete(int snum); //刪除學生信息
StuNode *StuFind(int snum); //查找學生信息,傳入參數學號
void StuModify(int snum, int smath, int seng, int syuwen); //修改學生信息
void StuCopy(StuNode *ptemp, StuNode *p); //學生信息拷貝
void StuSort(char ch);
void StuClassfy(); //分類合計
void StuRead(); //從文件讀入學生信息
void StuSave(); //保存學生信息到文件
int IsRead();
void StuQuit();
void ShowInfo(); //遍歷輸出學生信息


};

int Systemdoor()
{
string username = "Hecoz", password = "password";
string name, temp;
int number = 3;
while (1)
{
cout << " 用 戶 名:";
cin >> name;
cout << " 密 碼:";
cin >> temp;
if (name != username || temp != password)
{
number--;
if (number >0)
{
cout << " 用戶名/密碼錯誤!你還有" << number << "次機會" << endl;
}
else
cout << "用戶名/密碼錯誤!" << endl, exit(0);

}
else
{
cout << "********************密碼正確********************" << endl<<endl;
return 1;
}
}

}

void ShowMenu()
{
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 << "****** 9.輸出所有學生信息 ******" << endl;
cout << "****** 10.保存現有學生信息 ******" << endl;
cout << "\n\t\n\t\t請選擇:";
}

SInfo::SInfo() //構造函數
{
StuListHead = new StuNode;
StuListHead->nextstu = NULL;
}

SInfo::~SInfo() //析構函數
{
StuNode *p;
while (StuListHead)
{
p = StuListHead;
StuListHead = StuListHead->nextstu;
delete p;
}
StuListHead = NULL;
}

void SInfo::CreatSinfo() //創建學生信息表
{
int n;
StuNode *p, *s;
p = StuListHead;
cout << "請輸入學生人數:";
cin >> n;
for (int i = 1; i <= n; i++)
{
s = new StuNode;
cin >> s->num >> s->math>>s->eng>>s->yuwen;
s->sum = s->math + s->eng + s->yuwen;
s->nextstu = p->nextstu;
p->nextstu = s;
p = p->nextstu;
}
if (p == NULL) //判斷學生信息表是否創建成功
{
cout << "創建失敗請重新創建!" << endl;
CreatSinfo();
}
}

void SInfo::ShowInfo() //遍歷輸出
{
StuNode *p;
cout << "學號" << '\t' << "數學" << '\t' << "英語" << '\t' << "語文" << '\t' << "總分" << endl;
for (p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
cout << p->num << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->sum << endl;
}
}

void SInfo::StuInsert(int snum, int smath,int seng,int syuwen) //插入學生信息(頭插法)
{
StuNode *s,*p;
s = new StuNode;
s->num = snum;
s->math = smath;
s->eng = seng;
s->yuwen = syuwen;
s->sum = s->math + s->eng + s->yuwen;
p = StuListHead;
s->nextstu = p->nextstu;
p->nextstu = s;
}

void SInfo::StuDelete(int snum)
{
StuNode *p, *ptemp;
p = StuListHead;
ptemp = p;
while (p->nextstu && p->num!=snum) //循環終止條件為p->nextstu不為空 而且沒有找到相應學號的學生
{
ptemp = p;
p = p->nextstu;
}
if (p->num == snum)
{
ptemp->nextstu = p->nextstu;
delete p;
}
else
{
cout << "未找到該學生信息!" << endl;
}
}

StuNode *SInfo::StuFind(int snum)
{
StuNode *p;
p = StuListHead->nextstu;
while (p->nextstu && p->num != snum) //循環終止條件為p->nextstu不為空 而且沒有找到相應學號的學生
{
p = p->nextstu;
}
if (p->num == snum)
{
return p;
}
else
{
cout << "未找到該學生信息!" << endl;
return NULL;
}
}

void SInfo::StuModify(int snum, int smath, int seng, int syuwen)
{
StuNode *ItemStu = StuFind(snum); //直接調用查找函數
if (ItemStu != NULL)
{
ItemStu->math = smath;
ItemStu->num = snum;
ItemStu->math = smath;
ItemStu->eng = seng;
ItemStu->yuwen = syuwen;
ItemStu->sum = ItemStu->math + ItemStu->eng + ItemStu->yuwen;
}
}

void SInfo::StuCopy(StuNode *ptemp, StuNode *p) //拷貝學生信息(將p的信息拷貝到ptemp中)
{
if (p == NULL)
{
cout << "拷貝目標為空!" << endl;
}
else
{
ptemp->num = p->num;
ptemp->math = p->math;
ptemp->eng = p->eng;
ptemp->yuwen = p->yuwen;
ptemp->sum = p->sum;
//ptemp->nextstu = p->nextstu; //只是信息拷貝,next不能拷貝否則信息丟失
}
}

void SInfo::StuSort(char ch) //根據 總分排序
{
if (ch == '>')
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->sum > q->sum)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else if (ch == '<')
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->sum < q->sum)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else if (ch == 'o')
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->num < q->num)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else
{
cout << "排序條件出錯!" << endl;
}
}

void SInfo::StuClassfy() //根據學生總分分類
{
int grade[5] = {0};
StuNode *p = StuListHead->nextstu;
while (p != NULL)
{
if (89 < p->math)
{
grade[0]++;
}
else if (79 < p->math && p->math < 90)
{
grade[1]++;
}
else if (69 < p->math && p->math < 80)
{
grade[2]++;
}
else if (59 < p->math && p->math < 70)
{
grade[3]++;
}
else
{
grade[4]++;
}
p = p->nextstu;
}
cout << "A" << '\t' << "B" << '\t' << "C" << '\t' << "D" << '\t' << "E" << endl;
for (int i = 0; i < 5; i++)
{
cout << grade[i] << '\t';
}
cout << endl;
}

void SInfo::StuRead() //從文件讀入數據
{
StuNode *p;
p = StuListHead;
ifstream in("StudentList.txt");
if (!in) { cout << "沒有學生信息,請先錄入學生信息!" << endl; return; }
while (!in.eof())
{
int num, math, eng, yuwen, sum;
in >> num >> math >> eng >> yuwen >>sum;
StuInsert(num,math,eng,yuwen);
}
}

void SInfo::StuSave() //保存學生信息
{
StuNode *p;
p = StuListHead->nextstu;
ofstream out("StudentList.txt");
if (!out) { cout << "不能打開文件!" << endl; return; }
while (p != NULL)
{
out << p->num << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->sum << '\n';
p = p->nextstu;
}
}

void SInfo::StuQuit() //學生信息寫入文件
{
char choice;
cout << "是否保存學生信息:?(Y/N)";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
StuSave();
cout << "學生信息已保存..." << endl;
}
}

2.源程序

#include<iostream>
#include "SInfo.h"
#include<cstdlib>

using namespace std;

int main()
{
Systemdoor();
int x = 100, pnum,pmath,peng,pyuwen;
StuNode *pfind;
SInfo stu;
cout <<" ******************************************" << endl;
cout <<" ******************************************" << endl;
cout <<" ****** ******" << endl;
cout <<" ****** 歡迎進入學生信息管理系統 ******" << endl;
cout <<" ****** ******" << endl;
cout <<" ******************************************" << endl;
cout <<" ******************************************" << endl;
while (x != 0)
{
system("pause");
system("cls"); //清屏
ShowMenu();
cin >> x;
switch (x)
{
case 0:
stu.StuQuit();
break;
case 1:
stu.StuRead();
cout << "讀入學生信息表:" << endl;
stu.ShowInfo();
break;
case 2:
stu.CreatSinfo();
cout << "請核對輸入學生信息!" << endl;
stu.ShowInfo();
break;
case 3:
cout << "請輸入添加學生信息:";
cin >> pnum >> pmath >> peng >> pyuwen;
stu.StuInsert(pnum, pmath, peng, pyuwen);
cout << "更新學生信息表..." << endl;
stu.ShowInfo();
break;
case 4:
cout << "請輸入要刪除學生學號:";
cin >> pnum;
stu.StuDelete(pnum);
cout << "更新學生信息表..." << endl;
stu.ShowInfo();
break;
case 5:
cout << "請輸入要查找學生學號:";
cin >> pnum;
pfind = stu.StuFind(pnum);
cout << "查找學生學號:" << pfind->num <<" 數學 "<<pfind->math<<" 英語 "<<pfind->eng<<" 語文 "<<pfind->yuwen <<" 總分 " << pfind->sum << endl;
break;
case 6:
cout << "請輸入要修改學生學號:";
cin >> pnum;
cout << "請重新輸入學生分數:";
cin >> pmath >> peng >> pyuwen;
stu.StuModify(pnum, pmath, peng, pyuwen);
cout << "修改成功!" << endl;
cout << "更新學生信息表..." << endl;
stu.ShowInfo();
break;
case 7:
cout << "升序排序(1)降序排序(0)學號排序(10):";
cin >> pnum;
if (pnum == 1)
{
stu.StuSort('<');
stu.ShowInfo();
}
else if (pnum == 0)
{
stu.StuSort('>');
stu.ShowInfo();
}
else if (pnum == 10)
{
stu.StuSort('o');
stu.ShowInfo();
}
else
{
cout << "請輸入正確選擇!" << endl;
}
break;
case 8:
stu.StuClassfy();
break;
case 9:
stu.ShowInfo();
break;
case 10:
stu.StuSave();
break;
}
}

system("pause");
return 0;
}

Copyright © Linux教程網 All Rights Reserved