還是學校的課設題目,說白了就是借系統的名複習一下數據結構,有用的話請自取。
#include <iostream>
#include <stdio.h>
#include <set>
#include <string>
#include <sstream>
using namespace std;
struct Date{
int year;
int month;
int day;
};
struct Person
{
string name;
string gender;
Date date;
string degree;
string phone;
bool operator < (const Person &rhs) const
{
return rhs.name > name;
}
};
class SortCriterion
{
public:
bool operator() (const Person &a, const Person &b) const
{
if(a.name < b.name)
return true;
else if(a.name == b.name)
{
if(a.phone < b.phone)
return true;
else
return false;
} else
return false;
}
};
set<Person, SortCriterion> P;
set<Person, SortCriterion>::iterator iter;
int add_person()
{
Person new_person;
cout << "請輸入姓名:";
cin >> new_person.name;
cout << "請輸入性別:";
cin >> new_person.gender;
cout << "請輸入生日(XXXX-XX-XX):";
string datetime;
int time;
cin >> datetime;
new_person.date.year = atoi(datetime.substr(0,4).c_str());
new_person.date.month = atoi(datetime.substr(5,7).c_str());
new_person.date.day = atoi(datetime.substr(8,10).c_str());
cout << "請輸入學歷:";
cin >> new_person.degree;
cout << "請輸入電話號碼:";
cin >> new_person.phone;
P.insert(new_person);
return 1;
}
int delete_person()
{
int flag = 0;
Person new_person;
cout << "請輸入姓名:";
cin >> new_person.name;
set<Person, SortCriterion>::iterator iter;
iter = P.begin();
if(iter != P.end())
{
P.erase(iter);
flag = 1;
}
else
{
cout << "Cannot find the element!" << endl;
}
}
int query_person()
{
int flag = 0;
string qname;
cout << "請輸入姓名:";
cin >> qname;
for(iter = P.begin();iter != P.end();iter++)
{
if((*iter).name == qname)
{
cout << (*iter).name << endl;
cout << (*iter).gender << endl;
cout << (*iter).date.year << "-" << (*iter).date.month << "-" << (*iter).date.day << endl;
cout << (*iter).degree << endl;
cout << (*iter).phone << endl;
iter == P.end();
flag = 1;
}
}
}
int change_person()
{
int flag = 0;
string qname;
cout << "請輸入姓名:";
cin >> qname;
for(iter = P.begin();iter != P.end();iter++)
{
if((*iter).name == qname)
{
Person e(*iter);
P.erase(iter++);
cout << "請輸入新的姓名:";
cin >> e.name;
cout << "請輸入新的性別:";
cin >> e.gender;
cout << "請輸入新的生日(XXXX-XX-XX):";
string datetime;
int time;
cin >> datetime;
e.date.year = atoi(datetime.substr(0,4).c_str());
e.date.month = atoi(datetime.substr(5,7).c_str());
e.date.day = atoi(datetime.substr(8,10).c_str());
cout << "請輸入新的學歷:";
cin >> e.degree;
cout << "請輸入新的電話號碼:";
cin >> e.phone;
P.insert(iter, e);
flag = 1;
}
}
return flag;
}
int main()
{
int order;
cout << "1.新增" << endl;
cout << "2.刪除" << endl;
cout << "3.查詢" << endl;
cout << "4.修改" << endl;
while(true)
{
cout << "請選擇選項:";
cin >> order;
switch(order)
{
case 1:
if(add_person())
cout << "添加成功" << endl;
break;
case 2:
if(delete_person())
cout << "刪除成功" << endl;
break;
case 3:
if(query_person())
cout << "查詢成功" << endl;
break;
case 4:
if(change_person())
cout << "修改成功" << endl;
break;
}
}
return 0;
}