// mytest.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include #include class Student{public: Student(std::string name, unsigned long id, std::string sex, std::string political, std::string address) : m_name(name), m_id(id), m_sex(sex), m_political(political), m_address(address) { }public: unsigned long m_id; std::string m_name; std::string m_sex; std::string m_political; std::string m_address;};typedef std::map StudentCont;class StudentMgr{public: StudentMgr() : m_num(0) { } void add() { std::cin.clear(); std::cin.sync(); std::string name(""); std::cout << "please input student's name: "; std::getline(std::cin, name); std::string sex(""); std::cout << "please input student's sex: "; std::getline(std::cin,sex); std::string political(""); std::cout << "please input student's political apperance: "; std::getline(std::cin,political); std::string address(""); std::cout << "please input student's address: "; std::getline(std::cin,address); m_num++; Student student(name, m_num, sex, political, address); m_students.insert(std::make_pair(m_num, student)); std::cout << "Student " << m_num << " " << name << " " << sex << " " << political << " " << address << " add successfully!" << std::endl; } void del() { unsigned long id; std::cout << "Please input the student's id:"; std::cin >> id; StudentCont::iterator iter = m_students.find(id); if(iter != m_students.end()) { m_students.erase(id); std::cout << "Id number: " << id << " was deleted successfully!" << std::endl; } else { std::cout << "Id number: " << id << " doesn't exist in the system!" << std::endl; } } void mod() { unsigned long id; std::cout << "Please input the student's id:"; std::cin >> id; StudentCont::iterator iter = m_students.find(id); if(iter != m_students.end()) { std::cout << "Id number: " << id << " is found, please input the information you want to change!" << std::endl; std::cout << "please input your choice number:" << std::endl; std::cout << "--------------------------------" << std::endl; std::cout << "-[1] name. -" << std::endl; std::cout << "-[2] sex. -" << std::endl; std::cout << "-[3] political apperance. -" << std::endl; std::cout << "-[4] address. -" << std::endl; std::cout << "-[5] Quit! -" << std::endl; std::cout << "--------------------------------" << std::endl; unsigned long choiceNum; std::cin >> choiceNum; switch(choiceNum) { case 1: { std::string name; std::cin.clear(); std::cin.sync(); std::cout << "please input new student's name: "<< std::endl; std::getline(std::cin,name); (*iter).second.m_name = name; break; } case 2: { std::string sex; std::cin.clear(); std::cin.sync(); std::cout << "please input correct student's sex: "<< std::endl; std::getline(std::cin,sex); (*iter).second.m_sex = sex; break; } case 3: { std::string political; std::cin.clear(); std::cin.sync(); std::cout << "please input student's new political apperance: "<< std::endl; std::getline(std::cin,political); (*iter).second.m_political = political; break; } case 4: { std::string address; std::cin.clear(); std::cin.sync(); std::cout << "please input student's new address: "<< std::endl; std::getline(std::cin,address); (*iter).second.m_address = address; break; } case 5: break; default: { std::cerr << "Input choice number:" << choiceNum << " is wrong!" << std::endl; break; } } } else { std::cout << "Id number: " << id << " doesn't exist in the system, return to the menu!" << std::endl; } std::cout << "modify sucessfull!" << std::endl; } void query() { unsigned long id; std::cout << "Please input the student's id:"; std::cin >> id; StudentCont::iterator iter = m_students.find(id); if(iter != m_students.end()) { std::cout << "The student's information is: " << (*iter).second.m_id << " " << (*iter).second.m_name << " " << (*iter).second.m_address << " " << (*iter).second.m_political << " " << (*iter).second.m_sex << std::endl; } else { std::cout << "Id number: " << id << " doesn't exist in the system, return to the menu!" << std::endl; } }private: unsigned long m_num; StudentCont m_students;};void printMenu(){ std::cout << "please input your choice number:" << std::endl; std::cout << "--------------------------------" << std::endl; std::cout << "-[1] Add a student. -" << std::endl; std::cout << "-[2] Delete a student. -" << std::endl; std::cout << "-[3] Modify a student. -" << std::endl; std::cout << "-[4] Query students. -" << std::endl; std::cout << "-[5] Quit! -" << std::endl; std::cout << "--------------------------------" << std::endl;}void startstudentManagement(){ std::cout << "Thank you for using this simple student management system, Welcome!" << std::endl; printMenu(); std::auto_ptr studentMgr(new StudentMgr); bool stop = false; do { unsigned long choiceNum = 0; std::cout << "Your choice number is: "; std::cin.clear(); std::cin.sync(); std::cin >> choiceNum; switch(choiceNum) { case 1: studentMgr->add(); break; case 2: studentMgr->del(); break; case 3: studentMgr->mod(); break; case 4: studentMgr->query(); break; case 5: stop = true; break; default: std::cerr << "The input number is none of the choice, please try again." << std::endl; stop = true; break; } } while(!stop);}void stopstudentManagement(){ std::cout << "Thank you for using this simple student management system, Bye!" << std::endl;}int main(void){ startstudentManagement(); stopstudentManagement(); return 0;}