学生管理系统设计与实现(C++实现)
 一.內容
 
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; }
 
 
#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
 
 
#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; }
五.代碼結果
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
總結
以上是生活随笔為你收集整理的学生管理系统设计与实现(C++实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: pdm大写小写转换
 - 下一篇: 全网最详细的微信小程序开发教程