STL list之CS學生管理系統
阿新 • • 發佈:2019-01-28
#include "stdafx.h" #include<iostream> #include<string> #include<algorithm> #include <functional> #include<vector> #include<ctime> #include<windows.h> #include<list> #include<fstream> #include<istream> #include<memory> #include<deque> #include <iterator> #include<set> #include<map> #include<unordered_map> #include<iomanip> using namespace std; #define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS class sty { public: int m_age;//成員函式m 開頭 char m_sex[6]; string m_name; }; class Student { public: bool operator ==(const int & d); bool operator ==(const char *d); bool operator ==(const string & d); int m_age;//成員函式m 開頭 char m_sex[6]; string m_name; bool operator ()(const Student&x); friend void operator <<(ostream &out, const Student &i); }; void operator <<(ostream &iout, const Student &i) { iout <<i.m_name ; //return iout; } bool Student::operator ()(const Student&x) { return x.m_name<this->m_name; } bool Student::operator==(const int &d) { if (this->m_age == d) return true; else return false; } bool Student::operator==(const char * d) { for(int i=0;*d!=this->m_sex[i];i++,d++) return false; return true; } bool Student::operator==(const string & d) { if(d!= this->m_name) return false; return true; } typedef list<Student> listStu; // Function class bool com(const Student&x, const Student &d); class func { public: void menu(list<Student> & x);//目錄 void add_number(list<Student> & y); void Delete(list<Student> & y); void Search(list<Student> & y, int age); void Search(list<Student> & y, char sex[]); void Search(list<Student> & y, string name); void Save(list<Student> & y); void Loadtxt(list<Student> & y); void show(list<Student> & y); void Sort(list<Student> & y); void Exit(list<Student> & y); static bool comp_name(const Student&x, const Student & d); static bool comp_age(const Student&x, const Student & d); private: bool save_flag = false; }; void func::Loadtxt(list<Student> & y) { list<Student>::iterator out; out = y.begin(); ifstream file("d:/log.txt"); if (!file) cout << "open fail" << endl; while (!file.eof()) { Student temp; file >> temp.m_name; file >> temp.m_age; file>> temp.m_sex; if (file.fail())//判斷是否從流中獲取有效字 break; y.push_back(temp); } file.close(); } void func::Search(list<Student> & y,int age) { if (find(y.begin(), y.end(), age) != y.end()) { list<Student> ::iterator x; cout << "Search Success!!" << endl; cout << setw(5) << "Name"<< " " << "Age" << " " << "Sex" << endl; for (x = y.begin(); x != y.end(); x++) { if (x->m_age == age) cout << x->m_name << " " << x->m_age << " " << x->m_sex << endl; }// return true; } else cout << "Search fail!!" << endl; } void func::Search(list<Student> & y, char sex[]) { if (find(y.begin(), y.end(), sex) != y.end()) { bool flag = false; cout << "Search Success!!" << endl; cout << setw(5) << "Name" << " " << "Age" << " " << "Sex" << endl; for (list<Student>::iterator x = find(y.begin(), y.end(), sex); x != y.end(); x++) { if(strlen(x->m_sex)== strlen(sex)) cout <<setw(5)<< x->m_name << " " << x->m_age << " " << x->m_sex << endl; }// return true; } else cout << "Search fail!!" << endl; } void func::Search(list<Student> & y, string name) { if (find(y.begin(), y.end(), name) != y.end()) { cout << "Search Success!!" << endl; cout << setw(5) << "Name" << " " << "Age" << " " << "Sex" << endl; for (list<Student> ::iterator x = y.begin(); x != y.end(); x++) { if (x->m_name == name) cout << setw(5) << x->m_name << " " << x->m_age << " " << x->m_sex << endl; }// return true; } else cout << "Search fail!!" << endl; } void func::Delete(list<Student> & y) { cout << "輸入要刪除人的姓名:" << endl; string tempname; cin >> tempname; // list<Student>::iterator temp; while (find(y.begin(), y.end(), tempname) != y.end()) { list<Student>::iterator temp; temp = find(y.begin(), y.end(),tempname); y.erase(temp); } } void func::Save(list<Student> & y) { list<Student>::iterator out; out = y.begin(); if (save_flag == false) save_flag = true; ofstream x("D:/log.txt"); if (!x) cout << "open fail" << endl; else { while (out != y.end()) { x << out->m_name <<' '<< out->m_age<<' ' << out->m_sex<<'\n'; out++; } } x.close(); } void func::show(list<Student> & y) { listStu::iterator temp; temp = y.begin(); cout << "Name" <<" " << "Age" <<" " << "Sex" << endl; while (temp!= y.end()) { cout << setw(5) <<setw(5)<< temp->m_name << " " <<temp->m_age << " " << temp->m_sex << endl; temp++; } } void func::Sort(list<Student> & y) { y.sort( com); /*cout << "1---name sort" << ' ' << "2---age sort" << endl; int i; cin >> i; switch (i) { case 1: { y.sort(comp_name);show(y); }break; case 2: { y.sort(comp_age); show(y); }break; default:cout << "Input fail" << endl; break; }*/ } bool func::comp_name(const Student&x, const Student &d) { return x.m_name < d.m_name; } bool func::comp_age(const Student&x, const Student &d) { return x.m_age < d.m_age; } void func::Exit(list<Student> & y) { if (save_flag == false) { cout << "Is save? Y/N" << endl; char c; cin >> c; if (c == 'Y' || c == 'y') Save(y); } } void func::add_number(list<Student> & y) { Student temp; cout << "age:"; cin >> temp.m_age; cout << '\n'; cout << "sex:"; cin >> temp.m_sex; cout << '\n'; cout << "name:"; cin >> temp.m_name; cout << '\n'; y.push_back(temp); } void func::menu(list<Student> & x) { int i = 0; while (i!=8) { cout << " ==================================" << endl; cout << " = 1.search 5 Loading txt =" << endl; cout << " = 2.delete 6 Show =" << endl; cout << " = 3.add number 7 Sort =" << endl; cout << " = 4.save 8 Exit =" << endl; cout << " ==================================" << endl; cout << "請輸入:"; cin >> i; if (!(i >= 1 && i <= 8)) { //cout << "輸入出錯,請重輸。。" << endl; cin.clear();//clear input flag //cin.ignore(numeric_limits<std::streamsize>::max()); //清除cin裡所有內容 //cin.sync();//clear temp save快取區 cin.ignore(2,'\n'); //它的一個常用功能就是用來清除以回車結束的輸入緩衝 //區的內容,消除上一次輸入對下一次輸入的影響。 //比如可以這麼用:通常把第一個引數設定得足夠大, //這樣實際上總是隻有第二個引數’\n’起作用, //所以這一句就是把回車(包括回車)之前的所以字元從 //輸入緩衝(流)中清除出去。 } switch (i) { case 1: {cout << "1---按姓名查詢" << ' ' << "2---按age查詢" << ' ' << "3---按sex查詢" << endl; int i; cin >> i; switch (i) { case 1: { cout << "輸入姓名:" << endl; string i; cin >> i; Search(x, i); }break; case 2: { cout << "輸入age:" << endl; int i; cin >> i; Search(x, i); }break; case 3: { cout << "輸入sex:" << endl; char i[6]; cin >> i; Search(x, i); }break; default:cout << "Input fail" << endl; break; } }break; case 2:Delete(x); break; case 3: add_number(x); break; case 4:; Save(x); break; case 5:; Loadtxt(x); break; case 6:; show(x); break; case 7: Sort(x); break; case 8: {Exit(x); cout << "quit" << endl; } break; default:cout << "輸入出錯,請重輸。。" << endl; break; } } } bool com(const Student&x, const Student &d) { return x.m_name < d.m_name; } int & get(int *p) { return *p; } // int main() { list<Student> list1; Student stu = { 32,"sad","sss" }; list1.insert(list1.begin(),stu); cout << stu; //func fun; //fun.menu(list1);//載入目錄 system("pause"); return 0; }