#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;
}
STL list之CS学生管理系统
最新推荐文章于 2022-05-10 10:48:15 发布