学生管理系统设计与实现(C++实现)

来源:互联网 发布:中国银联和银联数据 编辑:程序博客网 时间:2024/04/29 07:55

一.内容

1、设计一个学生类Student,包括姓名,学号,性别,籍贯,年龄,出生日期和住址,其中"出生日期"定义为一个"日期"类(具有属性:year(年),month(月),date(日))内嵌子对象。

2、具有数据的录入、显示、保存、查询(按学号或姓名查询)、修改和删除功能。

3、对Student类重载"=="运算符和"="运算符,"=="运算符判断两个Student类对象的id属性是否相等;"="运算符实现Student类对象的赋值操作。

二.环境

语言:C++

平台:VS2013

三.设计思路或方案

根据实验要求把整个系统分化成不同的模块,每个模块完成一个特定的子功能。最后把这些模块结合起来组成一个整体,逐一实现各个功能。

流程图如下:




根据流程图可以看出,设计学生管理系统采取先局部后整体,首先从最小的时间类Date开始设计,然后把Date类对象放入学生类Student作为其中的一个私有数据成员,完成对Student类的设计。为了增强系统的模拟性以及程序的健壮性,引入异常机制的管理,创建Date_Exception类从C++异常类logic_error进行共有继承,对于不合法的Date输入进行重新操作(比如一年只有12个月且为整数,输入其他非法数字选择重新输入或者退出当前操作(如录入或者修改等))。对于基础类的准备工作大概已经完成,接下来则根据需求分别设计数据的录入、显示、保存、查询(按学号或姓名查询)、修改和删除功能。

四.程序清单

学生管理系统实现与设计分为四个文件分别为:

main.cpp存有主函数以及关键函数增删改查的声明和实现,是程序运行的主体。

Student.h主要存有Date类,Student类,Date_Exception类声明和简单实现

Student.cpp主要存有3个类内复杂成员函数实现

Interface.h开始和结束界面的声明和实现


文件main.cpp

#include "Student.h"  #include "interface.h"  #include <iostream>  #include <cstdlib>  #include <iomanip>  #include <fstream>    using namespace std;  const int MAX = 200;//数组大小     void menu_display();//显示菜单  void print_students_info(Student *stu);//显示全体学生信息  void read_student_info(Student *stu);//录入学生信息  int search_by_number(Student *stu, const int num);//根据学号查询  void search_by_name(Student *stu, const string name);//根据姓名查询 考虑重名  void modify_student(Student *stu, int num, int choice);//修改学生信息  void delete_student(Student *stu, int num);//删除学生信息  void write_file(Student *stu, const string file_name);//保存到文件    int main()  {      Front();//开始的界面         Date date[4];      date[1].setDate(1996, 2, 3);      date[2].setDate(1995, 12, 1);      date[3].setDate(1997, 6, 6);      Student students[MAX];      students[1].setStudent("徐一", 1, "男", date[1], "西安", "江苏");      students[2].setStudent("刘二", 2, "女", date[2], "西安", "陕西");      students[3].setStudent("张三", 3, "男", date[3], "西安", "浙江");        int flag = 1;      while (flag) {          menu_display();          cout << "输入你想执行的操作:";          char ch;          cin >> ch;          switch (ch) {          case '1':              cout << endl;              cout << "显示当前全体学生信息:" << endl;              print_students_info(students);              cout << endl;              break;          case '2':              cout << endl;              cout << "输入你想录入的学生信息:(学号根据当前学生人数自动排序)" << endl;              read_student_info(students);              cout << "显示当前全体学生信息:" << endl;              print_students_info(students);              cout << endl;              break;          case '3':              cout << endl;              cout << "1.根据学号查询\t2.根据姓名查询\t3.退出:";              int choice;              cin >> choice;              if (choice == 1) {                  cout << "请输入查找的学号:";                  int num;                  cin >> num;                  int i = search_by_number(students, num);                  if (i == 0)                      cout << "找不到此学生信息" << endl << endl;                  else{                      cout << "学生信息如下:" << endl;                      students[i].printStudent();                      cout << endl;                  }              }              else if (choice == 2) {                  cout << "请输入查找的姓名:";                  string name;                  cin >> name;                  search_by_name(students, name);                  cout << endl;              }              else {                  cout << endl;                  break;              }              break;          case '4':          {                      cout << endl;                      cout << "请输入你想修改的学号:";                      int i;                      cin >> i;                      int n = 1;                      while (students[n].getName() != "Andy") {                          n++;                      }//求现在的学生人数                      if (i > n - 1) {                          cout << "没有此学生信息!" << endl;                          cout << endl;                      }                      else{                          cout << "输入你想修改的属性(1.性别\t2.出生日期\t3.地址\t4.籍贯):";                          int no;                          cin >> no;                          modify_student(students, i, no);                          cout << "显示当前全体学生信息:" << endl;                          print_students_info(students);                          cout << endl;                      }                      break;          }          case '5':              cout << endl;              cout << "请输入你想删除的学号:";              int i0;              cin >> i0;              delete_student(students, i0);              cout << "显示当前全体学生信息:" << endl;              print_students_info(students);              cout << endl;              break;          case '6':          {                      cout << endl;                      cout << "输入你想写入的文件名:";                      string file_name;                      cin >> file_name;                      write_file(students, file_name);                      cout << endl;                      break;          }          case '7':              cout << endl;              cout << "退出系统!";              cout << endl;              flag = 0;              break;          }//switch      }//while      cout << endl;        Back();//结束界面       return 0;  }    void menu_display()  {      for (int i = 0; i<10; i++)          cout << "*";      cout << "学生管理系统";      for (int i = 0; i<10; i++)          cout << "*";      cout << endl;      cout << "* 1.显示" << "\t\t" << "2.录入 *" << endl;      cout << "* 3.查询" << "\t\t" << "4.修改 *" << endl;      cout << "* 5.删除" << "\t\t" << "6.打印 *" << endl;      cout << "* 7.退出" << "\t\t" << "       *" << endl;      for (int i = 0; i<32; i++)          cout << "*";      cout << endl;  }    void print_students_info(Student *stu)  {      int num = 1;      while (stu[num].getName() != "Andy") {          num++;      }      cout << "姓名" << "   " << "学号" << "  " << "性别" << setw(12) << "出生日期" << setw(12) << "地址" << setw(12) << "籍贯" << endl;      for (int i = 1; i < num; i++) {          if (stu[i].getName() == "无") {              cout << endl;              continue;          }          stu[i].printStudent();      }  }    void read_student_info(Student *stu)  {      string name, sex, address, native_place;      Date date;      cout << "姓名:";      cin >> name;      cout << "性别:";      cin >> sex;      try {          cout << "出生日期:";          cin >> date;      }      catch (Date_Exception &e) {          cout << e.getSide() << e.what() << endl;          cout << "是否要重新输入(y or n)";          char ch;          cin >> ch;          if (ch == 'y') {              cout << "出生日期:";              cin >> date;          }          else{              cout << "录入信息失败!" << endl;              return;          }      }      cout << "地址:";      cin >> address;      cout << "籍贯:";      cin >> native_place;      int i = 1;      while (stu[i].getName() != "Andy") {          i++;      }      stu[i].setStudent(name, i, sex, date, address, native_place);      cout << "录入学生信息成功!" << endl;  }    int search_by_number(Student *stu, const int num)  {      int n = 1;      while (stu[n].getName() != "Andy") {          ++n;      }      if (num > n - 1)          return 0;      int i = 1;      while (i < n) {          if (i == num)              return i;          i++;      }      return 0;  }    void search_by_name(Student *stu, const string name)  {      int n = 1;      while (stu[n].getName() != "Andy") {          n++;      }      int i = 1;      int flag = 0;      while (i++ < n) {          if (stu[i].getName() == name) {              stu[i].printStudent();              flag = 1;          }      }      if (flag == 0)          cout << "找不到此学生信息!" << endl;  }    void modify_student(Student *stu, int num, int choice)  {      //choice:1.性别\t2.出生日期\t3.地址\t4.籍贯      switch (choice) {      case 1:      {                cout << "输入性别:";                string sex1;                cin >> sex1;                stu[num].setSex(sex1);                break;      }      case 2:      {                Date date;                try {                    cout << "输入出生日期:";                    cin >> date;                }                catch (Date_Exception &e) {                    cout << e.getSide() << e.what() << endl;                    cout << "是否要重新输入(y or n)";                    char ch;                    cin >> ch;                    if (ch == 'y') {                        cout << "出生日期:";                        cin >> date;                    }                    else{                        cout << "修改信息失败!" << endl;                        return;                    }                }                stu[num].setDate(date);                break;      }        case 3:      {                cout << "输入地址:";                string address;                cin >> address;                stu[num].setAddress(address);                break;      }      case 4:      {                cout << "输入籍贯:";                string native_place;                cin >> native_place;                stu[num].setNativePlace(native_place);                break;      }        }      cout << "修改信息成功!" << endl;  }    void delete_student(Student *stu, int num)  {      Date date(0, 0, 0);      stu[num].setStudent("无", 0, "无", date, "无", "无");      cout << "删除信息成功!" << endl;  }    void write_file(Student *stu, const string file_name)  {      //格式("Andy", 1, "男", date, "西安", "江苏")      ofstream output;      output.open(file_name);      if (output.fail()) {          cout << "can't open the file" << endl;      }      int n = 1;      while (stu[n].getName() != "Andy") {          n++;      }      for (int i = 1; i < n; i++) {          if (stu[i].getName() == "无") {              output << endl;              continue;          }          output << setw(5) << stu[i].getName() << setw(8) << stu[i].getNumber() << setw(13) << stu[i].getSex() << setw(17)              << stu[i].getDate() << setw(24) << stu[i].getAddress() << setw(29) << stu[i].getNativePlace() << endl;      }      output.close();      cout << "数据写入文件成功!" << endl;  }  


文件interface.h

#ifndef _INTERFACE_H_  #define _INTERFACE_H_   #include <iostream>  #include <windows.h>  using namespace std;    void gotoxy(int x, int y)//设置坐标   {      COORD c;      c.X = x;      c.Y = y;      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);  }    bool setcolor(WORD wAttributes)//设置颜色   {      HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);      if (hConsole == INVALID_HANDLE_VALUE)          return false;      bool bResult = SetConsoleTextAttribute(hConsole, wAttributes);      return bResult;  }    void Front()//开始的界面   {      gotoxy(22, 10);      setcolor(10);      cout << "学生";      setcolor(12);      cout << "管理";      setcolor(0x0007);      cout << "系统";      setcolor(9);      cout << "设计";      setcolor(0x0006);      cout << "实现";      gotoxy(42, 13);      setcolor(15);      cout << "BY 徐洋 and 雷婕";      Sleep(3000);      system("cls");  }    void Back()//结束的界面   {      system("cls");      setcolor(15);      gotoxy(20, 8);      cout << "感谢你使用*学生管理系统*" << endl << endl;      int i;      for (i = 0; i < 10; i++){          gotoxy(20 + i, 9);          cout << "欢迎你下次使用!!!";          Sleep(200);          if (i != 9){              gotoxy(20 + i, 9);              putchar(' ');          }      }      Sleep(3000);      exit(0);  }    #endif  


文件Student.h

#ifndef _STUDENT_H_  #define _STUDENT_H_    #include <iostream>  #include <string>  #include <iomanip>  #include <stdexcept>  using namespace std;    class Date_Exception :public logic_error {  private:      int var_;  public:      Date_Exception(int var) :logic_error("不是正常值!")      {          var_ = var;      }      double getSide() const { return var_; }  };    class Date {  private:      int year_;      int month_;      int day_;  public:      Date() :year_(0), month_(0), day_(0) {}      Date(int year, int month, int day)      {          year_ = year;          month_ = month;          day_ = day;      }      void setDate(int year, int month, int day)      {          if (year < 1990 || year>2016)              throw Date_Exception(year);          year_ = year;          if (month < 1 || month>12)              throw Date_Exception(month);          month_ = month;          if (day < 1 || day>31)              throw Date_Exception(day);          day_ = day;      }      int getYear() const { return year_; }      int getMonth() const { return month_; }      int getDay() const { return day_; }  };    class Student {  private:      string name_;//姓名      int number_;//学号      string sex_;//性别      Date date_;//出生日期      string address_;//住址      string native_place_;//籍贯  public:      //default value("Andy", 1, "男",date, "西安", "江苏")      Student() :name_("Andy"), number_(1), sex_("男"), date_(0, 0, 0), address_("西安"), native_place_("江苏") {}      Student(string name, int number, string sex, Date date, string address, string native_place);        void setStudent(string name, int number, string sex, Date date, string address, string native_place);      void setSex(string sex) { sex_ = sex; }      void setDate(Date date) { date_ = date; }      void setAddress(string address) { address_ = address; }      void setNativePlace(string native_place) { native_place = native_place; }        string getName() const { return name_; }      int getNumber() const { return number_; }      string getSex() const { return sex_; }      Date getDate() const { return date_; }      string getAddress() const { return address_; }      string getNativePlace() const { return native_place_; }        bool operator ==(Student &other);      Student& operator =(Student &other);      friend ostream &operator <<(ostream &out, const Date &other);      friend istream &operator >>(istream &in, Date &other);      void printStudent();  };    ostream &operator <<(ostream &out, const Date &other);  istream &operator >>(istream &in, Date &other);    #endif // !_STUDENT_H  

文件Student.cpp

#include "Student.h"    Student::Student(string name, int number, string sex, Date date, string address, string native_place) :name_(name),  number_(number), sex_(sex), date_(date), address_(address), native_place_(native_place) { }    void Student::setStudent(string name, int number, string sex, Date date, string address, string native_place)  {      name_ = name;      number_ = number;      sex_ = sex;      date_ = date;      address_ = address;      native_place_ = native_place;  }    bool Student::operator ==(Student &other)  {      return (name_ == other.name_&&number_ == other.number_&&sex_ == other.sex_);  }    Student& Student::operator=(Student &other)  {      name_ = other.name_;      number_ = other.number_;      sex_ = other.sex_;      date_ = other.date_;      address_ = other.address_;      native_place_ = other.native_place_;      return *this;  }    ostream &operator <<(ostream &out, const Date &other)  {      out << other.getYear() << "-" << other.getMonth() << "-" << other.getDay();      return out;  }    istream &operator >>(istream &in, Date &other)  {      int year, month, day;      in >> year >> month >> day;      other.setDate(year, month, day);      return in;  }    void Student::printStudent()  {      cout << name_ << setw(6) << number_ << setw(6) << sex_ << setw(9) << date_ << setw(12) << address_ << setw(12) << native_place_ << endl;  }  

五.代码结果










0 0
原创粉丝点击