C++课程实践——身份证管理系统

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<Windows.h>
using namespace std;

bool Check_ID_Card_No(string& a);
class date
{
private:
	int year;
	int month;
	int day;
public:
	date();
	date(int Year, int Month, int Day);
	void print_date();
	date& operator=(const date& a);
	bool operator<=(const date& a)const;
};

date date_input();

bool date::operator<=(const date& a)const
{
	if (year < a.year)
		return true;
	if (year > a.year)
		return false;
	if (month < a.month)
		return true;
	if (month > a.month)
		return false;
	if (day < a.day)
		return true;
	if (day > a.day)
		return false;
	return true;
}

date::date()
{

}

date::date(int Year, int Month, int Day)
{
	year = Year;
	month = Month;
	day = Day;
}

void date::print_date()
{
	cout << year << "年" << month << "月" << day << "日";
}

date& date::operator=(const date& a)
{
	year = a.year;
	month = a.month;
	day = a.day;
	return *this;
}

date date_input()
{
	int Year, Month, Day;
	do
	{
		cout << "年:";
		cin >> Year;
		getchar();
		if (Year < 1900 || Year>2100)
			cout << "输入错误,请重新输入:" << endl;
	} while (Year < 1900 || Year>2100);
	do
	{
		cout << "月:";
		cin >> Month;
		getchar();
		if (Month < 1 || Month>12)
			cout << "输入错误,请重新输入:" << endl;
	} while (Month < 1 || Month>12);
	do
	{
		cout << "日:";
		cin >> Day;
		getchar();
		if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
		{
			if (Day < 1 || Day>31)
				cout << "输入错误,请重新输入:" << endl;
		}
		else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
		{
			if (Day < 1 || Day>30)
				cout << "输入错误,请重新输入:" << endl;
		}
		else if (Month == 2)
		{
			if ((Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0)
			{
				if (Day < 1 || Day>29)
					cout << "输入错误,请重新输入:" << endl;
			}
			else
			{
				if (Day < 1 || Day>28)
					cout << "输入错误,请重新输入:" << endl;
			}
		}
	} while (Day < 1 || Day>31);
	date temp(Year, Month, Day);
	return temp;
}

class ID_Card
{
private:
	string ID_Card_No;
	string name;
	int sex;				//1为男,2为女
	string address;
	date birthday;
	date start_date;
	date end_date;
public:
	ID_Card();
	ID_Card(string Name, string Sex, string Address, string ID_CARD_NO, date Birthday, date Start_date, date End_date);
	string get_ID_Card_No();
	string get_name();
	string get_sex();				//1为男,2为女
	string get_address();
	date get_birthday();
	date get_start_date();
	date get_end_date();
	void print_Validity();
	void print_info();
	void Change_address(string New_Address);
	void Change_Validity(date New_Start_Date, date New_End_Date);
	ID_Card& operator=(const ID_Card& a);
};

ID_Card::ID_Card()
{

}

ID_Card::ID_Card(string Name, string Sex, string Address, string ID_CARD_NO, date Birthday, date Start_date, date End_date)
{
	name = Name;
	if (Sex == "男")
		sex = 1;
	else if (Sex == "女")
		sex = 2;
	address = Address;
	ID_Card_No = ID_CARD_NO;
	birthday = Birthday;
	start_date = Start_date;
	end_date = End_date;
}

string ID_Card::get_ID_Card_No()
{
	return ID_Card_No;
}

string ID_Card::get_name()
{
	return name;
}

string ID_Card::get_sex()
{
	if (sex == 1)
		return "男";
	if (sex == 2)
		return "女";
	return "error";
}

string ID_Card::get_address()
{
	return address;
}

date ID_Card::get_start_date()
{
	return start_date;
}

date ID_Card::get_birthday()
{
	return birthday;
}

date ID_Card::get_end_date()
{
	return end_date;
}

void ID_Card::print_Validity()
{
	start_date.print_date();
	cout << " - ";
	end_date.print_date();
}

void ID_Card::print_info()
{
	cout << "\t\t姓名:" << get_name() << endl;
	cout << "\t\t性别:" << get_sex() << endl;
	cout << "\t\t住址:" << get_address() << endl;
	cout << "\t\t出生日期:";
	birthday.print_date();
	cout << endl << "\t\t身份证号:" << get_ID_Card_No() << endl;
	cout << "\t\t年限:";
	print_Validity();
	cout << endl << endl;
}

ID_Card& ID_Card::operator=(const ID_Card& a)
{
	ID_Card_No = a.ID_Card_No;
	name = a.name;
	sex = a.sex;
	address = a.address;
	birthday = a.birthday;
	start_date = a.start_date;
	end_date = a.end_date;
	return *this;
}

void ID_Card::Change_address(string New_Address)
{
	address = New_Address;
}

void ID_Card::Change_Validity(date New_Start_Date, date New_End_Date)
{
	start_date = New_Start_Date;
	end_date = New_End_Date;
}

ID_Card append()
{
	system("cls");
	string Name, Sex, Address, ID_CARD_NO;
	int sex;
	cout << "请输入姓名:";
	cin >> Name;
	cout << "请输入性别:";
	int t;
	do
	{
		t = 0;
		cin >> Sex;
		if (Sex == "男")
			sex = 1;
		else if (Sex == "女")
			sex = 2;
		else
		{
			cout << "输入错误,请重新输入:" << endl;
			t = 1;
		}
	} while (t);
	cout << "请输入住址:";
	cin >> Address;
	cout << "请输入出生日期:" << endl;
	date Birthday;
	Birthday = date_input();
	cout << "请输入身份证号:";
	do
	{
		t = 0;
		cin >> ID_CARD_NO;
		if (!Check_ID_Card_No(ID_CARD_NO))
			t = 1;
		if (t)
		{
			cout << "身份证号输入格式错误,请重新输入:";
		}
	} while (t);
	date Start_date, End_date;
	int tmp = 0;
	do
	{
		if (tmp)
		{
			cout << "输入的开始日期晚于结束日期,请重新输入:" << endl;
		}
		tmp = 1;
		cout << "请输入身份证有效期开始日期:\n";
		Start_date = date_input();
		cout << "请输入身份证有效期结束日期:\n";
		End_date = date_input();
	} while (!(Start_date <= End_date));
	ID_Card temp(Name, Sex, Address, ID_CARD_NO, Birthday, Start_date, End_date);
	cout << "添加成功。" << endl;
	system("pause");
	return temp;
}

bool Check_ID_Card_No(string& a)
{
	if (a.length() != 18)
		return false;
	if (a[17] == 'x')
		a[17] = 'X';
	for (int i = 0; i < 17; i++)
		if (!(a[i] >= '0' && a[i] <= '9'))
			return false;
	if (!((a[17] >= '0' && a[17] <= '9') || (a[17] == 'X')))
		return false;
	return true;
}

struct node
{
	ID_Card id_card;
	node* next;
} *head = new node, * p = nullptr, * q = head, * t = head;


int main()
{
index:
	system("cls");
	cout << "\n\n\n\t\t\t\t\t" << "欢迎使用身份证管理系统\n" << endl;
	cout << "\t\t\t软件19-8\t徐思崇\t\t1914010828\n";
	cout << "\t\t**************************************************************\n";
	cout << "\t\t" << "请选择要使用的功能\n\t\t\n\t\t\n";
	cout << "\t\t" << "1.浏览全部身份证信息\t\t\t2.查询身份证信息\n\t\t\n";
	cout << "\t\t" << "3.添加身份证信息\t\t\t4.删除身份证信息\n\t\t\n";
	cout << "\t\t" << "5.修改身份证信息\t\t\t0.退出系统\n\n";
	cout << "\t\t**************************************************************\n\t\t";
	string a;
	cin >> a;
	if (a == "0")
	{
		system("cls");
		cout << "\n\n\n\n\t\t谢谢使用\n\n\t\t再见" << endl;
		return 0;
	}
	else if (a == "1")
	{
		int count = 0;
		system("cls");
		t = head->next;
		while (t)
		{
			t->id_card.print_info();
			t = t->next;
			count++;
		}
		if (count)
			cout << "\n\n";
		cout << "\t\t共计" << count << "条信息\n\n\n\n\t\t";
		system("pause");
	}
	else if (a == "2")
	{
		system("cls");
		string input;
		bool find = 0;
		int choise;
	search:
		cout << "请选择查询方式\n" << endl;
		cout << "1.按姓名查询\n";
		cout << "2.按身份证号查询\n";
		cout << "0.返回主页\n";
		while (true)
		{
			cin >> choise;
			if (choise == 1)
			{
				cout << "请输入要查询的姓名:";
				cin >> input;
				t = head;
				while (t)
				{
					if (t->id_card.get_name() == input)
					{
						t->id_card.print_info();
						find = 1;
					}
					t = t->next;
				}
			}
			else if (choise == 2)
			{
				cout << "请输入要查询的身份证号:";
				cin >> input;
				t = head;
				while (t)
				{
					if (t->id_card.get_ID_Card_No() == input)
					{
						t->id_card.print_info();
						find = 1;
					}
					t = t->next;
				}
			}
			else if (choise == 0)
				goto index;
			else
			{
				cout << "输入错误,请重新输入";
				continue;
			}
			if (find)
				break;
			else
			{
				cout << "查无此人,是否要重新查询" << endl;
				cout << "1.是" << endl;
				cout << "2.否" << endl;;
				int temp = 0;
				while (cin >> temp)
					if (temp == 1)
						goto search;
					else if (temp == 2)
						goto index;
					else
						cout << "输入错误,请重新输入";
			}
		}
		system("pause");
	}
	else if (a == "3")
	{
		p = new node;
		p->id_card = append();
		p->next = nullptr;
		if (head->next == nullptr)
			head->next = p;
		else
		{
			q->next = p;
		}
		q = p;
	}
	else if (a == "4")
	{
		system("cls");
		string input;
		bool find = 0;
		string choise;
	search2:
		cout << "请选择删除方式\n";
		cout << "1.按姓名删除\n";
		cout << "2.按身份证号删除\n";
		cout << "0.返回主页\n";
		while (cin >> choise)
		{
			if (choise == "1")
			{
				cout << "请输入要删除的姓名:";
				cin >> input;
				t = head;
				while (t != q)
				{
					node* temp;
					if (t->next->id_card.get_name() == input)
					{
						temp = t->next;
						string tmp;
						cout << "查询到的信息为:\n";
						temp->id_card.print_info();
						cout << "请输入“是”确认删除:\n";
						cin >> tmp;
						if (tmp == "是")
						{
							t->next = temp->next;
							if (temp == q)
								q = t;
							delete temp;
						}
						else
						{
							cout << "已取消" << endl;
							system("pause");
							goto index;
						}
						find = 1;
					}
					t = t->next;
				}
			}
			else if (choise == "2")
			{
				cout << "请输入要删除的身份证号:";
				cin >> input;
				t = head;
				while (t != q)
				{
					node* temp;
					if (t->next->id_card.get_ID_Card_No() == input)
					{
						temp = t->next;
						string tmp;
						cout << "查询到的信息为:\n";
						temp->id_card.print_info();
						cout << "请输入“是”确认删除:\n";
						cin >> tmp;
						if (tmp == "是")
						{
							t->next = temp->next;
							if (temp == q)
							{
								q = t;
							}
							delete temp;
						}
						else
						{
							cout << "已取消" << endl;
							system("pause");
							goto index;
						}
						find = 1;
					}
					t = t->next;
				}
			}
			else if (choise == "0")
			{
				goto index;
			}
			else
			{
				cout << "输入错误,请重新输入";
				continue;
			}
			if (find)
			{
				cout << "删除成功" << endl;
				system("pause");
				break;
			}
			else
			{
				cout << "查无此人,是否要重新查询" << endl;
				cout << "1.是" << endl;
				cout << "2.否" << endl;;
				int temp = 0;
				while (cin >> temp)
					if (temp == 1)
						goto search2;
					else if (temp == 2)
						goto index;
					else
						cout << "输入错误,请重新输入";
			}
		}
	}
	else if (a == "5")
	{
		system("cls");
		string input;
		bool find = 0;
		string choise;
	search3:
		cout << "请选择查询方式\n" << endl;
		cout << "1.按姓名查询\n";
		cout << "2.按身份证号查询\n";
		cout << "0.返回主页\n";
		while (cin >> choise)
		{
			if (choise == "1")
			{
				cout << "请输入要修改的姓名:";
				cin >> input;
				getchar();
				t = head->next;
				while (t)
				{
					if (t->id_card.get_name() == input)
					{
						cout << "当前信息为:\n";
						t->id_card.print_info();
						cout << "请选择要修改的内容:\n";
						cout << "1.住址\n2.身份证有效日期\n";
						string choose;
						cin >> choose;
						if (choose == "1")
						{
							string NewAddress;
							cout << "请输入新的住址:";
							cin >> NewAddress;
							t->id_card.Change_address(NewAddress);

						}
						else if (choose == "2")
						{
							date NewStartDate, NewEndDate;
							do
							{
								cout << "请输入身份证开始日期:\n";
								NewStartDate = date_input();
								cout << "请输入身份证结束日期:\n";
								NewEndDate = date_input();
							} while (!(NewStartDate <= NewEndDate));
							t->id_card.Change_Validity(NewStartDate, NewEndDate);
						}
						cout << "修改成功!" << endl;
						cout << "新的信息为:" << endl;
						t->id_card.print_info();
						system("pause");
						find = 1;
					}
					t = t->next;
				}
			}
			else if (choise == "2")
			{
				cout << "请输入要查询的身份证号:";
				cin >> input;
				getchar();
				t = head->next;
				while (t)
				{
					if (t->id_card.get_ID_Card_No() == input)
					{
						cout << "当前信息为:\n";
						t->id_card.print_info();
						cout << "请选择要修改的内容:\n";
						cout << "1.住址\n2.身份证有效日期\n";
						int choose;
						cin >> choose;
						if (choose == 1)
						{
							string NewAddress;
							cout << "请输入新的住址:";
							cin >> NewAddress;
							t->id_card.Change_address(NewAddress);
						}
						else if (choose == 2)
						{
							date NewStartDate, NewEndDate;
							do
							{
								cout << "请输入身份证开始日期:\n";
								NewStartDate = date_input();
								cout << "请输入身份证结束日期:\n";
								NewEndDate = date_input();
							} while (!(NewStartDate <= NewEndDate));
							t->id_card.Change_Validity(NewStartDate, NewEndDate);
						}
						cout << "修改成功!" << endl;
						cout << "新的信息为:" << endl;
						t->id_card.print_info();
						system("pause");
						find = 1;
					}
					t = t->next;
				}
			}
			else if (choise == "0")
				goto index;
			else
			{
				cout << "输入错误,请重新输入";
				continue;
			}
			if (find)
				break;
			else
			{
				cout << "查无此人,是否要重新查询" << endl;
				cout << "1.是" << endl;
				cout << "2.否" << endl;;
				int temp = 0;
				while (cin >> temp)
					if (temp == 1)
						goto search3;
					else if (temp == 2)
						goto index;
					else
						cout << "输入错误,请重新输入";
			}
		}
	}
	goto index;
}
posted @ 2020-08-21 13:52  Francis-xsc  阅读(6)  评论(0编辑  收藏