本文实例为大家分享了C++实现简单学生成绩管理系统的具体代码,供大家参考,具体内容如下
manage.h
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//学生
template <class T>
class Student {
template <class T>
friend class StudentManage;
public:
Student() {
next = NULL;
}
private:
int Sid;//学生编号
string Sname;//学生姓名
string Ssex;//学生性别
int Sage;//学生年龄
int Sscore;//学生成绩
Student<T>* next;//指向下一结点的指针
};
template <class T>
class StudentManage {
public:
StudentManage();
~StudentManage();
StudentManage<T>& SInsert(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore);//插入
bool SIsEmpty() const;//判空
int SGetLength() const;//获取长度
bool SGetData(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore);//获取数据
bool SModifyData(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore);//修改
int SFind(const int& sid);//根据id查找
StudentManage<T>& SDelete(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore);//删除
void Sort(T a[]);
void SOutPut(ostream& sout) const;//打印
private:
Student<T>* shead;
};
//班级
template <class T>
class Class {
template <class T>
friend class ClassManage;
public:
Class() {
next = NULL;
}
private:
int Cid;//班级编号
string CName;//班级名称
string CBzrName;//班主任名称
string CXsName;//学生名称
Class<T>* next;
};
template <class T>
class ClassManage {
public:
ClassManage();
~ClassManage();
ClassManage<T>& CInsert(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname);//插入
bool CIsEmpty() const;//判空
int CGetLength() const;//获取长度
bool CGetData(int k, T& cid, string& cname, string& cbzrname, string& cxsname);//获取数据
bool CModifyData(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname);//修改
int CFind(const int& cid);//根据id查找
ClassManage<T>& CDelete(int k, T& cid, string& cname, string& cbzrname, string& cxsname);//删除
void COutPut(ostream& cout) const;//打印
private:
Class<T>* chead;
};
//成绩
template <class T>
class Grade {
template <class T>
friend class GradeManage;
public:
Grade() {
next = NULL;
}
private:
string Gname;
int Gscore;
Grade<T>* next;
};
template <class T>
class GradeManage {
public:
GradeManage();
~GradeManage();
GradeManage<T>& GInsert(int k, const string& gname, const T& gscore);//插入
bool GIsEmpty() const;//判空
int GGetLength() const;//获取长度
bool GGetData(int k, string& gname, T& gscore);//获取数据
bool GModifyData(int k, const string& gname, const T& gscore);//修改
int GFind(const string& gname);//根据课程名称查找
GradeManage<T>& GDelete(int k, string& gname, T& gscore);//删除
void GOutPut(ostream& gout) const;//打印
private:
Grade<T>* ghead;
};
manage.cpp
#include "manage.h"
//学生
template<class T>
inline StudentManage<T>::StudentManage()
{
shead = new Student<T>();
}
template<class T>
StudentManage<T>::~StudentManage()
{
delete shead;
}
template<class T>
StudentManage<T>& StudentManage<T>::SInsert(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore)
{
Student<T>* p = shead;
Student<T>* newNode = new Student<T>;
newNode->Sid = sid;
newNode->Sname = sname;
newNode->Ssex = ssex;
newNode->Sage = sage;
newNode->Sscore = sscore;
int len = SGetLength();
if (k<1 || k>len + 1)
cout << "失败";
else {
for (int i = 1; i < k; i++)
p = p->next;
newNode->next = p->next;
p->next = newNode;
}
return *this;
}
template<class T>
bool StudentManage<T>::SIsEmpty() const
{
return shead->next == NULL;
}
template<class T>
int StudentManage<T>::SGetLength() const
{
int length = 0;
Student<T>* p = shead->next;
while (p) {
length++;
p = p->next;
}
return length;
}
template<class T>
bool StudentManage<T>::SGetData(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore)
{
Student<T>* p = shead->next;
int index = 1;
if (k<1 || k>SGetLength())
return false;
while (p != NULL && index < k) {
index++;
p = p->next;
}
if (p == NULL)
return false;
else {
sid = p->Sid;
sname = p->Sname;
ssex = p->Ssex;
sage = p->Sage;
sscore = p->Sscore;
return true;
}
}
template<class T>
bool StudentManage<T>::SModifyData(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore)
{
Student<T>* p = shead->next;
int index = 1;
if (k<1 || k>SGetLength())
return false;
while (p != NULL && index < k) {
index++;
p = p->next;
}
if (p == NULL)
return false;
else {
p->Sid = sid;
p->Sname = sname;
p->Ssex = ssex;
p->Sage = sage;
p->Sscore = sscore;
return true;
}
}
template<class T>
int StudentManage<T>::SFind(const int& sid)
{
Student<T>* p = shead->next;
int index = 1;
while (p != NULL && p->Sid != sid) {
p = p->next;
index++;
}
if (p != NULL)
return index;
else
return 0;
}
template<class T>
StudentManage<T>& StudentManage<T>::SDelete(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore)
{
if (SGetData(k, sid, sname, ssex, sage, sscore)) {
Student<T>* p = shead;
Student<T>* q = NULL;
for (int i = 1; i < k; i++)
p = p->next;
q = p->next;
p->next = q->next;
delete q;
}
else
cout << "失败";
return *this;
}
template<class T>
void StudentManage<T>::Sort(T a[])
{
int n = SGetLength();
T i, j, temp;
for (i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
temp = a[i];
for (j = i - 1; j >= 0 && a[j] > temp; --j)
a[j + 1] = a[j];
a[j + 1] = temp;
}
}
}
template<class T>
void StudentManage<T>::SOutPut(ostream& sout) const
{
Student<T>* p = shead->next;
while (p != NULL) {
sout << "学生编号:" << p->Sid << "学生姓名:" << p->Sname << "学生性别:" << p->Ssex << "学生年龄:" << p->Sage << "学生总分:" << p->Sscore <<" " << endl;
p = p->next;
}
}
template <class T>
ostream& operator<<(ostream& sout, StudentManage<T>& x) {
x.SOutPut(sout);
return sout;
}
//班级
template<class T>
inline ClassManage<T>::ClassManage()
{
chead = new Class<T>();
}
template<class T>
ClassManage<T>::~ClassManage()
{
delete chead;
}
template<class T>
ClassManage<T>& ClassManage<T>::CInsert(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname)
{
Class<T>* p = chead;
Class<T>* newNode = new Class<T>;
newNode->Cid = cid;
newNode->CName = cname;
newNode->CBzrName = cbzrname;
newNode->CXsName = cxsname;
int len = CGetLength();
if (k<1 || k>len + 1)
cout << "失败";
else {
for (int i = 1; i < k; i++)
p = p->next;
newNode->next = p->next;
p->next = newNode;
}
return *this;
}
template<class T>
bool ClassManage<T>::CIsEmpty() const
{
return chead->next == NULL;
}
template<class T>
int ClassManage<T>::CGetLength() const
{
int length = 0;
Class<T>* p = chead->next;
while (p) {
length++;
p = p->next;
}
return length;
}
template<class T>
bool ClassManage<T>::CGetData(int k, T& cid, string& cname, string& cbzrname, string& cxsname)
{
Class<T>* p = chead->next;
int index = 1;
if (k<1 || k>CGetLength())
return false;
while (p != NULL && index < k) {
index++;
p = p->next;
}
if (p == NULL)
return false;
else {
cid = p->Cid;
cname = p->CName;
cbzrname = p->CBzrName;
cxsname = p->CXsName;
return true;
}
}
template<class T>
bool ClassManage<T>::CModifyData(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname)
{
Class<T>* p = chead->next;
int index = 1;
if (k<1 || k>CGetLength())
return false;
while (p != NULL && index < k) {
index++;
p = p->next;
}
if (p == NULL)
return false;
else {
p->Cid = cid;
p->CName = cname;
p->CBzrName = cbzrname;
p->CXsName = cxsname;
return true;
}
}
template<class T>
int ClassManage<T>::CFind(const int& cid)
{
Class<T>* p = chead->next;
int index = 1;
while (p != NULL && p->Cid != cid) {
p = p->next;
index++;
}
if (p != NULL)
return index;
else
return 0;
}
template<class T>
ClassManage<T>& ClassManage<T>::CDelete(int k, T& cid, string& cname, string& cbzrname, string& cxsname)
{
if (CGetData(k, cid, cname, cbzrname, cxsname)) {
Class<T>* p = chead;
Class<T>* q = NULL;
for (int i = 1; i < k; i++)
p = p->next;
q = p->next;
p->next = q->next;
delete q;
}
else
cout << "失败";
return *this;
}
template<class T>
void ClassManage<T>::COutPut(ostream& cout) const
{
Class<T>* p = chead->next;
while (p != NULL) {
cout << "班级编号:" << p->Cid << "班级:" << p->CName << "班主任姓名:" << p->CBzrName << "学生姓名:" << p->CXsName <<" " << endl;
p = p->next;
}
}
template <class T>
ostream& operator<<(ostream& cout, ClassManage<T>& x) {
x.COutPut(cout);
return cout;
}
//成绩
template<class T>
inline GradeManage<T>::GradeManage()
{
ghead = new Grade<T>();
}
template<class T>
GradeManage<T>::~GradeManage()
{
delete ghead;
}
template<class T>
GradeManage<T>& GradeManage<T>::GInsert(int k, const string& gname, const T& gscore)
{
Grade<T>* p = ghead;
Grade<T>* newNode = new Grade<T>;
newNode->Gname = gname;
newNode->Gscore = gscore;
int len = GGetLength();
if (k<1 || k>len + 1)
cout << "失败";
else {
for (int i = 1; i < k; i++)
p = p->next;
newNode->next = p->next;
p->next = newNode;
}
return *this;
}
template<class T>
bool GradeManage<T>::GIsEmpty() const
{
return ghead->next == NULL;
}
template<class T>
int GradeManage<T>::GGetLength() const
{
int length = 0;
Grade<T>* p = ghead->next;
while (p) {
length++;
p = p->next;
}
return length;
}
template<class T>
bool GradeManage<T>::GGetData(int k, string& gname, T& gscore)
{
Grade<T>* p = ghead->next;
int index = 1;
if (k<1 || k>GGetLength())
return false;
while (p != NULL && index < k) {
index++;
p = p->next;
}
if (p == NULL)
return false;
else {
gname = p->Gname;
gscore = p->Gscore;
return true;
}
}
template<class T>
bool GradeManage<T>::GModifyData(int k, const string& gname, const T& gscore)
{
Grade<T>* p = ghead->next;
int index = 1;
if (k<1 || k>GGetLength())
return false;
while (p != NULL && index < k) {
index++;
p = p->next;
}
if (p == NULL)
return false;
else {
p->Gname = gname;
p->Gscore = gscore;
return true;
}
}
template<class T>
int GradeManage<T>::GFind(const string& gname)
{
Grade<T>* p = ghead->next;
int index = 1;
while (p != NULL && p->Gname != gname) {
p = p->next;
index++;
}
if (p != NULL)
return index;
else
return 0;
}
template<class T>
GradeManage<T>& GradeManage<T>::GDelete(int k, string& gname, T& gscore)
{
if (GGetData(k, gname, gscore)) {
Grade<T>* p = ghead;
Grade<T>* q = NULL;
for (int i = 1; i < k; i++)
p = p->next;
q = p->next;
p->next = q->next;
delete q;
}
else
cout << "失败";
return *this;
}
template<class T>
void GradeManage<T>::GOutPut(ostream& gout) const
{
Grade<T>* p = ghead->next;
while (p != NULL) {
gout << "课程名称:" << p->Gname << "课程成绩:" << p->Gscore<<" " << endl;
p = p->next;
}
}
template <class T>
ostream& operator<<(ostream& gout, GradeManage<T>& x) {
x.GOutPut(gout);
return gout;
}
/*
int main() {
//学生
StudentManage<int> s;
s.SInsert(1, 1, "小明", "男",23 , 290);
s.SInsert(2, 2, "张三", "男", 22, 280);
s.SInsert(3, 3, "小红", "女", 21, 270);
s.SInsert(4, 4, "李四", "男", 24, 260);
cout << s;
int n = s.SGetLength();
cout << "共有" << n << "条数据" << endl;
s.SModifyData(3, 3, "小丽", "女", 21, 270);
cout << s;
int sid, sage, sscore; string sname, ssex;
int sid1, sage1, sscore1; string sname1, ssex1;
s.SGetData(1, sid, sname,ssex, sage, sscore);
cout << "学生编号:" << sid << " 学生姓名:" << sname << " 学生性别:" << ssex << " 学生年龄:" << sage << " 学生总分:" << sscore << endl;
s.SDelete(2, sid1, sname1, ssex1, sage1, sscore1);
cout << "删除学生编号:" << sid1 << " 学生姓名:" << sname1 << " 学生性别:" << ssex1 << " 学生年龄:" << sage1 << " 学生总分:" << sscore1 << endl;
cout << s;
//班级
ClassManage<int> c;
c.CInsert(1,1,"一班","李主任","王麻子");
c.CInsert(2, 2,"一班", "李主任", "十大");
c.CInsert(3, 3,"一班", "李主任", "的撒");
cout << c;
int x = c.CGetLength();
cout << "共有" << x << "条数据" << endl;
c.CModifyData(3, 3, "一班", "张主任", "的撒");
cout << c;
int cid; string cname, cbzrname,cxsname;
c.CGetData(1, cid, cname, cbzrname, cxsname);
cout << "班级编号:" << cid << " 班级:" <<cname << " 班主任姓名:" << cbzrname << " 学生姓名:" << cxsname << endl;
//成绩
GradeManage<int> g;
g.GInsert(1, "c++", 90);
g.GInsert(1, "java", 90);
cout << g;
int t = g.GGetLength();
cout << "共有" << t << "条数据" << endl;
g.GModifyData(2, "python", 92);
cout << g;
string gname;int gscore;
g.GGetData(1, gname, gscore);
cout << "课程名称:" << gname << " 课程成绩:" << gscore << endl;
return 0;
}
*/
int main() {
ofstream outfile;//终端输入--内存--文本
//学生
StudentManage<int> stud;
stud.SInsert(1, 1, "小明", "男", 23, 290);
stud.SInsert(2, 2, "张三", "男", 22, 280);
stud.SInsert(3, 3, "小红", "女", 21, 270);
stud.SInsert(4, 4, "李四", "男", 24, 260);
int sid; string sname; string ssex; int sage; int sscore;
int sid1; string sname1; string ssex1; int sage1; int sscore1;
int sid2; string sname2; string ssex2; int sage2; int sscore2;
//班级
ClassManage<int> cla;
cla.CInsert(1, 1, "一班", "李主任", "王麻子");
cla.CInsert(2, 2, "一班", "李主任", "王五");
cla.CInsert(3, 3, "二班", "李主任", "达摩");
int cid; string cname, cbzrname, cxsname;
int cid1; string cname1, cbzrname1, cxsname1;
int cid2; string cname2, cbzrname2, cxsname2;
//成绩
GradeManage<int> gra;
gra.GInsert(1, "c++", 90);
gra.GInsert(1, "java", 90);
string gname; int gscore;
string gname1; int gscore1;
string gname2; int gscore2;
//主界面
cout << "欢迎来到学生成绩管理系统!" << endl;
cout << "选择1,进入学生信息管理"<<endl;
cout << "选择2,进入班级信息管理" << endl;
cout << "选择3,进入成绩信息管理" << endl;
cout <<"输入999退出" << endl;
int n; cout << "请输入选项:"; cin >> n;
while (n != 999) {
switch (n)
{
case 1:cout << "欢迎来到学生信息管理,输入999退出" << endl;
cout << "1、查看学生信息\n2、增加学生信息\n3、修改学生信息\n4、删除学生信息\n5、保存学生信息\n6、读取学生信息\n7、学生按总成绩排序*" << endl;
int s; cout << "请输入选项:"; cin >> s;
while(s!=999){
switch (s)
{
case 1:cout << "查看学生信息" << endl;
cout << stud << endl;
break;
case 2:cout << "增加学生信息" << endl;
int k;
cout << "请输入学生位置" << endl; cin >> k;
cout << "请输入编号:"; cin >> sid;
cout << "请输入姓名:"; cin >> sname;
cout << "请输入性别:"; cin >> ssex;
cout << "请输入年龄:"; cin >> sage;
cout << "请输入总分:"; cin >> sscore;
stud.SInsert(k, sid, sname, ssex, sage, sscore);
break;
case 3:cout << "修改学生信息" << endl;
int k1;
cout << "请输入要修改的学生位置" << endl; cin >> k1;
cout << "请输入要修改的编号:"; cin >> sid1;
cout << "请输入要修改的姓名:"; cin >> sname1;
cout << "请输入要修改的性别:"; cin >> ssex1;
cout << "请输入要修改的年龄:"; cin >> sage1;
cout << "请输入要修改的总分:"; cin >> sscore1;
stud.SModifyData(k1, sid1, sname1, ssex1, sage1, sscore1);
break;
case 4:cout << "删除学生信息" << endl;
int k2;
cout << "请输入要删除的学生位置" << endl; cin >> k2;
cout << "请输入要删除的编号:"; cin >> sid2;
cout << "请输入要删除的姓名:"; cin >> sname2;
cout << "请输入要删除的性别:"; cin >> ssex2;
cout << "请输入要删除的年龄:"; cin >> sage2;
cout << "请输入要删除的总分:"; cin >> sscore2;
stud.SDelete(k2, sid2, sname2, ssex2, sage2, sscore2);
break;
case 5:cout << "保存学生信息" << endl;
outfile.open("F:\student.txt");
outfile << stud << endl;
outfile.close();
break;
case 6:cout << "读取学生信息" << endl;
{
ifstream in("F:\student.txt", ios::in);
if (!in.fail())
{
while (!in.eof())
{
string str5;
in >> str5;
cout << str5 << endl;
}
}
}
break;
case 7:cout << "学生按总成绩排序" << endl;
break;
default:
break;
}
cout << "请输入选项:"; cin >> s;
}
break;
case 2:cout << "欢迎来到班级信息管理,输入999退出" << endl;
cout << "1、查看班级信息\n2、增加班级信息\n3、修改班级信息\n4、删除班级信息\n5、保存班级信息\n6、读取班级信息" << endl;
int c; cout << "请输入选项:"; cin >> c;
while (c != 999) {
switch (c)
{
case 1:cout << "查看班级信息" << endl;
cout << cla << endl;
break;
case 2:cout << "增加班级信息" << endl;
int k;
cout << "请输入班级位置" << endl; cin >> k;
cout << "请输入班级编号:"; cin >> cid;
cout << "请输入班级名称:"; cin >> cname;
cout << "请输入班主任名称:"; cin >> cbzrname;
cout << "请输入学生名字:"; cin >> cxsname;
cla.CInsert(k, cid, cname, cbzrname, cxsname);
break;
case 3:cout << "修改班级信息" << endl;
int k1;
cout << "请输入班级位置" << endl; cin >> k1;
cout << "请输入班级编号:"; cin >> cid1;
cout << "请输入班级名称:"; cin >> cname1;
cout << "请输入班主任名称:"; cin >> cbzrname1;
cout << "请输入学生名字:"; cin >> cxsname1;
cla.CModifyData(k1, cid1, cname1, cbzrname1, cxsname1);
break;
case 4:cout << "删除班级信息" << endl;
int k2;
cout << "请输入班级位置" << endl; cin >> k2;
cout << "请输入班级编号:"; cin >> cid2;
cout << "请输入班级名称:"; cin >> cname2;
cout << "请输入班主任名称:"; cin >> cbzrname2;
cout << "请输入学生名字:"; cin >> cxsname2;
cla.CDelete(k2, cid2, cname2, cbzrname2, cxsname2);
break;
case 5:cout << "保存班级信息" << endl;
outfile.open("F:\class.txt");
outfile << cla << endl;
outfile.close();
break;
case 6:cout << "读取班级信息" << endl;
{
ifstream in("F:\class.txt", ios::in);
if (!in.fail())
{
while (!in.eof())
{
string str5;
in >> str5;
cout << str5 << endl;
}
}
}
break;
default:
break;
}
cout << "请输入选项:"; cin >> c;
}
break;
case 3:cout << "欢迎来到成绩信息管理,输入999退出" << endl;
cout << "1、查看成绩信息\n2、增加成绩信息\n3、修改成绩信息\n4、删除成绩信息\n5、保存成绩信息\n6、读取成绩信息" << endl;
int g; cout << "请输入选项:"; cin >> g;
while (g != 999) {
switch (g)
{
case 1:cout << "查看成绩信息" << endl;
cout << gra << endl;
break;
case 2:cout << "增加成绩信息" << endl;
int k;
cout << "请输入成绩位置" << endl; cin >> k;
cout << "请输入课程名称:"; cin >> gname;
cout << "请输入课程成绩:"; cin >> gscore;
gra.GInsert(k, gname, gscore);
break;
case 3:cout << "修改成绩信息" << endl;
int k1;
cout << "请输入成绩位置" << endl; cin >> k1;
cout << "请输入课程名称:"; cin >> gname1;
cout << "请输入课程成绩:"; cin >> gscore1;
gra.GModifyData(k1, gname1, gscore1);
break;
case 4:cout << "删除成绩信息" << endl;
int k2;
cout << "请输入成绩位置" << endl; cin >> k2;
cout << "请输入课程名称:"; cin >> gname2;
cout << "请输入课程成绩:"; cin >> gscore2;
gra.GDelete(k2, gname2, gscore2);
break;
case 5:cout << "保存成绩信息" << endl;
outfile.open("F:\grade.txt");
outfile << gra << endl;
outfile.close();
break;
case 6:cout << "读取成绩信息" << endl;
{
ifstream in("F:\grade.txt", ios::in);
if (!in.fail())
{
while (!in.eof())
{
string str5;
in >> str5;
cout << str5 << endl;
}
}
}
break;
default:
break;
}
cout << "请输入选项:"; cin >> g;
}
break;
default:
break;
}
cout << "请输入选项:"; cin >> n;
}
/*
ofstream outfile;//终端输入--内存--文本
outfile.open("F:\student.txt", ios::binary | ios::app | ios::in | ios::out);//注意,里面有iOS::app,表示打开文件后,在写入的文件不会覆盖原文件中的内容,也就是原来文件中的数据会得到保存。
//outfile << "学生编号:" << sid << " 学生姓名: " << sname << " 学生性别:" << ssex << " 学生年龄:" << sage << " 学生总分:" << sscore << endl;
outfile << stud << endl;
outfile.close();
*/
return 0;
}