#include<iostream>
#include<sstream>
#include<fstream>
#include<numeric>
#include<algorithm>
#include<string>
#include<unordered_map>
#include<unordered_set>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const string file = "D:\\mytest.txt";
unordered_map<string , int> sheng , shi;
class person
{
public:
person() = default;
person(string a , string b , string c , string d , string e) : name(a) , age(b) , sheng(c) , shi(d) , id(e) {}
string name; //名字
string age; //性别
string sheng; //省份
string shi; //市区
string id; //身份证号码
person* next;
person* prew;
};
void insert_person(person* x , person*& root)
{
sheng[x -> sheng]++;
shi[x -> shi]++;
x -> next = root;
if(root)
root -> prew = x;
root = x;
x -> prew = nullptr;
}
void read_file(person*& root)
{
ifstream fi(file);
while(fi)
{
string line;
getline(fi , line);
stringstream ss(line);
string a , b , c , d , e;
ss >> a >> b >> c >> d >> e;
person* per = new person(a , b , c , d, e);
sheng[per -> sheng]++;
shi[per -> shi] ++;
insert_person(per, root);
}
fi.close();
}
void delete_person(person* x , person*& root)
{
sheng[x -> sheng]--;
shi[x -> shi]--;
if(x -> prew)
x -> prew -> next = x -> next;
else
root = x -> next;
if(x -> next)
x -> next -> prew = x -> prew;
}
person* search_person(string id , person* root)
{
while(root)
{
if(root -> id == id)
break;
root = root -> next;
}
return root;
}
void write_file(person* root)
{
ofstream of(file);
while(root)
{
of << root -> name << " " << root -> age << " " << root -> sheng << " " << root -> shi << " " << root -> id;
root = root -> next;
if(root)
of << "\n";
}
of.close();
}
void xiugai(person* p)
{
string a ,b , c , d , e;
cout << "请重新输入该改身份证信息:\t";
--sheng[p -> sheng];
--shi[p -> shi];
cin >> a >> b >> c >> d >> e;
p -> name = a;
p -> age = b;
p -> sheng = c;
p -> shi = d;
p -> id = e;
++sheng[p -> sheng];
++shi[p -> shi];
}
void view(person*& root)
{
string s;
while(true)
{
cout<<"\n\t\t\t 欢 迎 使 用 身 份 管 理 系 统 !"<<endl;
cout << "1---------------------给定身份证号码显示其身份信息" << endl;
cout << "2---------------------给定一个省份,显示该省份的人员的数量" << endl;
cout << "3---------------------给定一个地区,显示该地区的人员的数量" << endl;
cout << "4---------------------添加一个身份信息" << endl;
cout << "5---------------------给定一个身份证号码,修改该身份信息" << endl;
cout << "6---------------------给定一个身份证号码,删除该身份信息" << endl;
cout << "0---------------------退出系统" << endl;
cout << "请输入一个想要使用的功能:\t";
cin >> s;
if(s == "0")
break;
if(s == "1")
{
string id;
cout << "请输入一个身份证id :\t";
cin >> id;
cout << endl;
auto q = search_person(id, root);
if(q)
{
cout << "所要查找的信息如下:" << endl;
cout << q -> name << " " << q -> age << " " << q -> sheng << " " << q -> shi << " " << q -> id << endl;
}
else
cout << "所要查找的身份证信息不存在!" << endl;
}
else if(s == "2")
{
string now;
cout << "请输入一个想要查找的省份:\t";
cin >> now;
cout << endl;
cout << "该省份的人数为:\t";
cout << sheng[now];
cout << endl;
}
else if(s == "3")
{
string now;
cout << "请输入一个想要查找的市区:\t";
cin >> now;
cout << endl;
cout << "该市区的人数为:\t";
cout << shi[now];
cout << endl;
}
else if(s == "4")
{
cout << "请分别输入想要添加的人员的相关信息:\t";
string a , b ,c , d , e;
cin >> a >> b >> c >> d >> e;
cout << endl;
person* p = new person(a , b , c ,d , e);
insert_person(p, root);
}
else if(s == "5")
{
cout << "请输入一个想要修改的身份证号码:\t";
string now;
cin >> now;
cout << endl;
auto q = search_person(now, root);
if(q)
{
xiugai(q);
}
else
cout << "所输入的身份证id不存在!" << endl;
}
else if(s == "6")
{
cout << "请输入一个想要删除的身份证号码:\t";
string now;
cin >> now;
cout << endl;
auto q = search_person(now, root);
if(q)
{
delete_person(q , root);
}
else
cout << "所想要删除的身份证id不存在!" << endl;
}
}
}
int main()
{
person* root;
read_file(root);
view(root);
write_file(root);
return 0;
}