无聊写了半个学生管理系统,控制台的需要交作业的应该够了
就只有一点点功能,没有用任何简洁的算法。不过需要交作业的话应该够了,不够的话自己加功能。需要优化的自己优化。代码在dev-C++和vs2008编译通过。
注释也少啊。
代码如下:
subject.h
程序代码:
//科目类 #include <iostream> #include <string> class Subject { public: Subject(); void Display(); // 显示信息,科目名称和成绩 void setsjName(std::string sjName); // 设置科目名称 std::string getsjName(); // 获取科目名称 void setRes(float theRes); // 设置科目成绩 float getRes(); // 获取科目成绩 private: std::string sjName; //科目名称字符串 float theRes; //科目成绩变量 };subject.cpp
程序代码:
#include "subject.h" using namespace std; Subject::Subject() { sjName=""; } void Subject::setsjName(string sjName) { this->sjName=sjName; } string Subject::getsjName() { return this->sjName; } void Subject::setRes(float theRes) { this->theRes=theRes; } float Subject::getRes() { return this->theRes; } void Subject::Display() { cout<<getsjName()<<":"<<getRes()<<endl; }student.h
程序代码:
//学生类 #include <iostream> #include <string> #include "subject.h" const int maxJect=5; //最多科目数 class Student { public: Student(); void Display(); //显示学生信息 Subject& addRes(float res); //添加学生成绩 Subject& addJect(std::string ject); //添加科目 void setstuClass(std::string stuClass); //设置学生班级 std::string getstuClass(); //获取学生班级 void setstuName(std::string stuName); //设置学生名称 std::string getstuName(); //获取学生名称 void setSex(std::string sex); //设置学生性别 std::string getSex(); //获取学生性别 void setstuNumber(int stuNumber);//设置学生学号 int getstuNumber();//获取学生学生 protected: Subject mSubject[maxJect]; //定义科目类 int nextJect; //下一个科目名称 int nextRes;//下一个科目成绩 private: std::string stuClass; //学生班级 std::string stuName;//学生姓名 std::string sex; //学生性别 int stuNumber; //学生学号 };
student.cpp
程序代码:
#include "student.h" using namespace std; Student::Student() { stuNumber=nextRes=nextJect=0; } Subject& Student::addRes(float res) { if (nextRes >= maxJect) { cerr << "越界错误" << endl; throw exception(); } Subject& theSubject=mSubject[nextRes++]; theSubject.setRes(res); return theSubject; } Subject& Student::addJect(string ject) { if (nextJect >= maxJect) { cerr << "越界错误" << endl; throw exception(); } Subject& theSubject=mSubject[nextJect++]; theSubject.setsjName(ject); return theSubject; } void Student::setstuClass(string stuClass) { this->stuClass=stuClass; } string Student::getstuClass() { return stuClass; } void Student::setstuName(string stuName) { this->stuName=stuName; } string Student::getstuName() { return stuName; } void Student::setSex(string sex) { this->sex=sex; } string Student::getSex() { return sex; } void Student::setstuNumber(int stuNumber) { this->stuNumber=stuNumber; } int Student::getstuNumber() { return stuNumber; } void Student::Display() { cout<<getstuClass()<<" * "<<getstuName()<<" * "<<getSex()<<" * "<<getstuNumber()<<endl; for (int i=0;i<nextJect;i++) { cout<<mSubject[i].getsjName()<<":"<<mSubject[i].getRes()<<endl; } }
database.h
程序代码:
#include <iostream> #include <string> #include "student.h" using namespace std; namespace mydb{ const int maxStu=100; /*最多学生数量*/ class Database { public: Database(); Student& addStudent(string stuClass,string stuName,string sex,int stuNumber); //添加一个学生 Student& InitJect(int i,string ject); //初始化科目 Student& AddRes(int i,float res); Student& getStudent(string stuName); //通过姓名查询学生 Student& getStudent(int stuNumber); //通过学号查询学生 void DisPlayStu(string stuName); void DisPlayStu(int stuNumber); /*void delStudent(string stuName);*/ void delStudent(int stuNumber); bool IsStuNumber(int stuNumber); void displayall(); //显示所有学生信息,包括成绩 private: Student mStudent[maxStu]; int nextStu; int nextDbRes; int nextDbJect; int cDbRes; int cDbJect; int stuplase;//学生在链表的位置 }; }database.cpp
程序代码:
#include "database.h" using namespace mydb; Database::Database() { nextDbRes=nextDbJect=nextStu=0; cDbRes=0; cDbJect=0; } Student& Database::addStudent(string stuClass,string stuName,string sex,int stuNumber) { if (nextStu >= maxStu) { cerr << "达到人数上限" << endl; throw exception(); } Student& theStudent=mStudent[nextStu++]; theStudent.setstuClass(stuClass); theStudent.setstuName(stuName); theStudent.setSex(sex); theStudent.setstuNumber(stuNumber); return theStudent; } Student& Database::AddRes(int i,float res) { if (nextDbRes >= i&&nextDbRes>=maxJect) { cerr << "达到科目上限" << endl; throw exception(); } if (cDbRes==i) { nextDbRes++; cDbRes=0; } cDbRes++; Student& theStudent=mStudent[nextDbRes]; theStudent.addRes(res); return theStudent; } Student& Database::getStudent(string stuName) { for (int i=0;i<maxStu;i++) { if (mStudent[i].getstuName()==stuName) { stuplase=i; return mStudent[i]; } } throw exception(); } Student& Database::getStudent(int stuNumber) { for (int i=0;i<maxStu;i++) { if (mStudent[i].getstuNumber()==stuNumber) { stuplase=i; return mStudent[i]; } } throw exception(); } void Database::DisPlayStu(string stuName) { getStudent(stuName).Display(); } void Database::DisPlayStu(int stuNumber) { getStudent(stuNumber).Display(); } void Database::delStudent(int stuNumber) { getStudent(stuNumber); if (stuplase==nextStu) { nextStu--; } else { for (int i=stuplase;i<nextStu;i++) { mStudent[i]=mStudent[i+1]; } nextStu--; } } //void Database::delStudent(string stuName) //{ // getStudent(stuName); // for (int i=stuplase;i<maxStu;i++) // { // mStudent[i]=mStudent[i+1]; // } // nextStu--; //} Student& Database::InitJect(int i,string ject) { if (nextDbJect >= i&&nextDbJect>=maxJect) { cerr << "达到科目上限" << endl; throw exception(); } if (cDbJect==i) { nextDbJect++; cDbJect=0; } cDbJect++; Student& theStudent=mStudent[nextDbJect]; theStudent.addJect( ject); return theStudent; } bool Database::IsStuNumber(int stuNumber) { for (int i=0;i<maxStu;i++) { if (mStudent[i].getstuNumber()==stuNumber) { return false; } } return true; } void Database::displayall() { cout<<"**班级******姓名******性别******学号**"<<endl; for (int i=0;i<nextStu;i++) { mStudent[i].Display(); } }main.cpp
程序代码:
#include <cstdlib> #include <iostream> #include "database.h" using namespace std; using namespace mydb; int gNum; //设置科目数量的全局变量 string className; //班级名称 string initJ[maxJect];//初始化科目 int displayMenu(); //显示操作菜单 int cerrcin(int& istr); bool addStudent(Database& adb);//增加学生 void delStudent(Database& adb); void addRes(Database& adb); void InitJect(Database& adb); void getStudentNum(Database& adb); void getStudentName(Database& adb); int main(int argc, char *argv[]) { Database studentDB; bool done=false; cout<<"欢迎使用文字版本学生成绩管理系统!\n请输入您所在的班级的名称\n>"; cin>>className; InitJect(studentDB); while (!done) { int selection=displayMenu(); switch(selection) { case 1: if ( addStudent(studentDB)) { addRes(studentDB); } break; case 2: delStudent(studentDB); break; case 3: studentDB.displayall(); break; case 4: getStudentNum(studentDB); break; case 5: getStudentName(studentDB); break; case 6: cout<<"输入班级名称"<<endl; cin>>className; InitJect(studentDB); break; case 0: done=true; break; default: cerr<<"未知错误"<<endl; } } system("PAUSE"); return EXIT_SUCCESS; } void InitJect(Database& adb) { int num; cout<<"请初始化科目\n输入科目数量,最多不能超过5科"<<endl; while(cerrcin(num)) { if (num>maxJect) { cout<<"请重新输入,最多不能超过5科"<<endl; continue; } else break; } gNum=num; int j; string ject; for (int i=0;i<num;i++) { cout<<"输入第"<<i+1<<"科目的名称"<<endl; while(cin>>ject) { for (j=0;j<i;j++) { if(initJ[j]==ject) { cout<<"有相同的科目,请重新输入"<<endl; break; } } if(j==i) { j=0; initJ[i]=ject; break; } } } } int displayMenu() { int selection; cout << endl; cout << "学生成绩管理系统" << endl; cout << "-----------------" << endl; cout << "1) 添加一个学生" << endl; cout << "2) 删除一个学生" << endl; cout << "3) 显示所有学生信息" << endl; cout << "4) 通过学号查询学生信息" << endl; cout << "5) 通过姓名查询学生信息" << endl; cout << "6) 换班级" << endl; cout << "0) 退出" << endl; cout << endl; cout << "---> "; cerrcin(selection); return selection; } bool addStudent(Database& adb) { string stuName; string sex; int stuNumber; cout<<"输入姓名"<<endl; cin>>stuName; cout<<"输入性别"<<endl; while(cin>>sex) { if (sex=="男"||sex=="女") { break; } cout<<"输入错误,只能输入‘男’或者‘女’\n"; } cout<<"输入学号"<<endl; while(cerrcin(stuNumber)) { if (!adb.IsStuNumber(stuNumber)) { cout<<"已经存在这个学号了,请重新输入"<<endl; continue; } else break; } try { adb.addStudent(className,stuName,sex,stuNumber); } catch (std::exception ex) { cerr << "请删除学生" << endl; return false; } return true; } void addRes(Database& adb) { float res; string ject; for (int i=0;i<gNum;i++) { ject="a"; cout<<"输入 ’"<<initJ[i]<<"‘科目的成绩"<<endl; while(cin >> res,!cin.eof()) { if (cin.bad()) { cerr<<"输入出现严重错误,将退出程序"; exit(0); } if (cin.fail()) { cerr<<"输入错误重新输入,只能输入数字\n"; cin.clear(); cin.sync(); continue; } if (cin.good()) { if (res<0) { cout<<"请重新输入,没有负数分数"<<endl; continue; } break; } } adb.InitJect(gNum,initJ[i]); adb.AddRes(gNum,res); } } void getStudentNum(Database& adb) { int studentNumber; cout <<"输入学生学号"; cerrcin(studentNumber); try { adb.DisPlayStu(studentNumber); } catch (std::exception ex) { cerr << "没有这个学生" << endl; } } void getStudentName(Database& adb) { string studentName; cout <<"输入学生姓名"; cin >> studentName; try { adb.DisPlayStu(studentName); } catch (std::exception ex) { cerr << "没有这个学生" << endl; } } void delStudent(Database& adb) { int studentNumber; cout <<"输入学生学号"; cerrcin(studentNumber); try { adb.delStudent(studentNumber); } catch (std::exception ex) { cerr << "没有这个学生" << endl; } } int cerrcin(int& istr) { while(cin >> istr,!cin.eof()) { if (cin.bad()) { cerr<<"输入出现严重错误,将退出程序"; exit(0); } if (cin.fail()) { cerr<<"输入错误重新输入,只能输入数字\n--->"; cin.clear(); cin.sync(); continue; } if (cin.good()) { break; } } return 1; }顺便上传一个附件:可以用dev-c++4.9.9.2直接打开编译......
学生成绩管理系统.rar
(128.28 KB)