本文主要是介绍学生信息管理系统-书本实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写在前面
本例是南邮本科教材<<面向对象程序设计及C++(第2版)>>
(就是下面这本,朱立华、俞琼、郭剑、朱建主编)中第六章后面的小 Project,简单的学生信息管理系统,没有 String、没有 vector,简单的类实现以及运算符重载操作。代码并不难,只是作为热身练习,不过修复 Bug 倒也耗费了不少时间。这里只做记录,mark 自己水过来的本科课程,以及现在重新回来填坑。
这里基本是重现了课本上的代码,顺带解决一些Visual Studio 2015
下旧代码出现的问题,以及一些书本不小心的错误。
程序代码
程序代码包括 5 个源码文件:Student.h
、Student.cpp
、Interface.h
、Interface.cpp
、Main.cpp
。Student 类对应学生类,Interface 对应界面和一些对学生类的操作:浏览、查询、新增等操作,Main 函数启动程序。
代码中的输出操作本人为了保持与项目代码风格一致,刻意使用了 std::cout、std::endl 这种风格,没有使用命名空间。
Student.h
#ifndef STUDENT_H_
#define STUDENT_H_#include <iostream>
#include <fstream>
#include <string.h>class Person
{
protected:char *name;char sex;int age;
public:Person();Person(char *na,char s,int a);~Person();};class Student:virtual public Person
{
protected:char speciality[20];char number[16];
public:Student();Student(char *na,char s,int a,char *spec,char *num);~Student();Student & operator = (Student &);operator char *();operator char();operator int();friend std::ostream & operator <<(std::ostream &, const Student &);friend std::istream & operator >>(std::istream &, Student &);};#endif // !STUDENT_H_
Student.cpp
#include "Student.h"Person::Person()
{name = NULL;
}Person::Person(char * na, char s, int a)
{if (na){name = new char[strlen(na) + 1];strcpy_s(name,strlen(na)+1,na);}sex = s;age = a;
}Person::~Person()
{if (name){delete[]name;}
}Student::Student()
{
}Student::Student(char * na, char s, int a, char * spec, char *num): Person(na,s,a)
{strcpy_s(speciality, strlen(spec) + 1, spec);strcpy_s(number, strlen(num) + 1, num);
}Student::~Student()
{}Student & Student::operator=(Student &st)
{// TODO: ÔÚ´Ë´¦²åÈë return Óï¾äif (name){delete []name;}if (st.name){name = new char[strlen(st.name) + 1];strcpy_s(name, strlen(st.name) + 1, st.name);}else{name = 0;}sex = st.sex;age = st.age;strcpy_s(speciality, strlen(st.speciality) + 1, st.speciality);strcpy_s(number, strlen(st.number) + 1, st.number);return *this;
}Student::operator char * ()
{return name;
}Student::operator char ()
{return sex;
}Student::operator int ()
{return age;
}std::ostream & operator<<(std::ostream & out, const Student & stu)
{// TODO: ÔÚ´Ë´¦²åÈë return Óï¾äout << stu.name << '\t';out << stu.sex << '\t';out << stu.age << '\t';out << stu.speciality << "\t\t";out << stu.number << '\n';return out;
}std::istream & operator >> (std::istream & in, Student & stu)
{// TODO: ÔÚ´Ë´¦²åÈë return Óï¾ächar temp[80];std::cout << "Please enter a student infomation:\n";std::cout << "Name: "; in >> temp;if (temp){stu.name = new char[strlen(temp) + 1];strcpy_s(stu.name, strlen(temp) + 1, temp);}std::cout << "Sex: "; in >> stu.sex;std::cout << "Age: "; in >> stu.age;std::cout << "Speciality: "; in >> stu.speciality;std::cout << "Student Number: "; in >> stu.number;return in;
}
Interface.h
#ifndef INTERFACE_H_
#define INTERFACE_H_#include "Student.h"// student number
const int N = 2;class Interface
{
protected:Student stu[N];int num;
public:Interface();void Browse();void Run();void Input();void Sort();void Statistic();bool Search();
};#endif // !INTERFACE_H_
Interface.cpp
#include "Interface.h"
#include <string.h>
#include <iostream>Interface::Interface()
{num = 0;
}void Display()
{std::cout << std::endl;std::cout << "**********0. EXIT\t**********" << std::endl;std::cout << "**********1. Input info\t**********" << std::endl;std::cout << "**********2. Search\t**********" << std::endl;std::cout << "**********3. Browse\t**********" << std::endl;std::cout << "**********4. Sort by age**********" << std::endl;std::cout << "**********5. Statistic\t**********" << std::endl;
}void Interface::Run()
{unsigned int choice;do{Display();std::cout << "Please choose:";std::cin >> choice;if (choice<0||choice>5){std::cout << "the num you choose must 0~5" << std::endl;return;}switch (choice){case 0:break;case 1:Input();break;case 2:Search();break;case 3:Browse();break;case 4:Sort();break;case 5:Statistic();break;default:break;}} while (choice);
}void Interface::Input()
{if (num == N) {std::cout << "\nInfomation was FULL !\n";return;}int i = num;std::cin >> stu[i];num++;
}void Interface::Browse()
{std::cout << "\n Information about Students that you would like to check£º\n";if (num == 0){std::cout << "\n No information !\n";return;}else{std::cout << "Name" << "\t" << "Sex" << "\t" << "Age" << "\t" << "Speciality" << "\t" << "SNumber" << "\n";for (int i = 0; i < num; i++){std::cout << stu[i];}}
}void Interface::Sort()
{std::cout << "\nSorted by age!\n";int k = 0;for (int i = 0; i < num - 1; i++){k = i;for (int j = i+1; j < num; j++){if ((int)(stu[k]) > (int)(stu[j]))k = j;}if (k!=i){// Student t=stu[k]; doesnot call for operator =Student t;t = stu[k];stu[k] = stu[i];stu[i] = t;}}
}void Interface::Statistic()
{int m = 0;std::cout << "\nStatistic by stu sex!\n";for (int i = 0; i < num; i++){if (char(stu[i]) == 'M' || char(stu[i]) == 'm')m++;}std::cout << "\nResult! Male sut num:"<<m<<"!\n";
}bool Interface::Search()
{char na[20];std::cout << "\nThe stu name you want to locate:\n";std::cin >> na;int i = 0;for ( ; i < num; i++){if (strcmp((char*)(stu[i]), na) == 0)break;}if (i == num){std::cout << "\nNobody!\n";return false;}else{std::cout << stu[i];}return true;
}
Main.cpp
#include "Interface.h"int main()
{Interface interface1;interface1.Run();return 0;
}
这篇关于学生信息管理系统-书本实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!