我的C++之旅 — 01
(原创)
以下是我编写的通讯录管理系统的源代码。
功能:
1.联系人添加、修改、查询、删除。
2.通讯录清空功能。
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
struct person {
string name;
string tele;
string adress;
int age;
int sex;
};
struct telemobile {
int num=0;
person per[500];
};
void menu() {
cout << "**************************" << endl;
cout << "***** 1. 添加联系人 ******" << endl;
cout << "***** 2. 显示联系人 ******" << endl;
cout << "***** 3. 删除联系人 ******" << endl;
cout << "***** 4. 查找联系人 ******" << endl;
cout << "***** 5. 修改联系人 ******" << endl;
cout << "***** 6. 清空联系人 ******" << endl;
cout << "***** 0. 退出通讯录 ******" << endl;
cout << "**************************" << endl;
}
void addPerson(telemobile* t) {
cout << "请输入姓名:" << endl;
string name;
string telee;
string addre;
cin>>name;
t->per[t->num].name = name;
cout << "请输入性别(1--男、2--女):" << endl;
while (1) {
int agee;
cin >> agee;
if (agee == 1 || agee == 2) {
t->per[t->num].sex = agee;
break;
}
}
cout << "请输入年龄:" << endl;
cin >> t->per[t->num].age;
cout << "请输入电话:" << endl;
cin >> telee;
t->per[t->num].tele = telee;
cout << "请输入地址:" << endl;
cin >> addre;
t->per[t->num].adress = addre;
t->num++;
cout << "保存成功!" << endl;
system("pause");
system("cls");
}
void displayPerson(telemobile* t) {
if (t->num == 0) {
cout << "暂无信息!" << endl;
}
else {
int i;
for (i = 0;i < t->num;i++) {
cout<<"\t姓名:"<<t->per[i].name<<"性别"<< t->per[i].sex<<"年龄"<< t->per[i] .age<<"电话:"<< t->per[i].tele<<"地址:"<< t->per[i].adress;
}
system("pause");
system("cls");
}
}
int isExist(telemobile* t) {
int i;
string name;
cout << "请输入删除人姓名:" << endl;
cin >> name;
for (i = 0;i < t->num;i++) {
if (t->per[i].name == name) {
return i;
}
}
return 0;
}
void delePerson(telemobile* t,int j) {
int i;
for (i = j;i < t->num;i++) {
t->per[i]=t->per[i+1];
}
t->num--;
cout << "删除成功!" << endl;
system("pause");
system("cls");
}
void findPerson(telemobile* t,int i) {
cout << "\t姓名:" << t->per[i].name << "性别" << t->per[i].sex << "年龄" << t->per[i].age << "电话:" << t->per[i].tele << "地址:" << t->per[i].adress;
system("pause");
system("cls");
}
void datePerson(telemobile* t, int i) {
cout << "\t姓名:" << t->per[i].name << "性别" << t->per[i].sex << "年龄" << t->per[i].age << "电话:" << t->per[i].tele << "地址:" << t->per[i].adress;
cout << "请更改信息:" << endl;
cout << "姓名:" << endl;
string namee;
cin >> namee;
t->per[i].name = namee;
cout << "性别(1--男,2--女):" << endl;
while (1) {
int agee;
cin >> agee;
if (agee == 1 || agee == 2) {
t->per[t->num].sex = agee;
break;
}
else {
cout << "请按照格式输入:" << endl;
}
}
cout << "年龄:" << endl;
cin >> t->per[t->num].age;
cout << "请电话:" << endl;
string telee,addre;
cin >> telee;
t->per[t->num].tele = telee;
cout << "请地址:" << endl;
cin >> addre;
t->per[t->num].adress = addre;
cout << "保存成功!" << endl;
system("pause");
system("cls");
}
void main() {
int i, j;
int select = 0;
telemobile abs;
cout << "**************************" << endl;
cout << "**************************" << endl;
cout << "**************************" << endl;
cout << "******** 欢迎使用 ********" << endl;
cout << "**************************" << endl;
cout << "******通讯录管理系统******" << endl;
cout << "**************************" << endl;
cout << "**************************" << endl;
cout << "**************************" << endl;
system("pause");
system("cls");
while (1) {
menu();
cout << "请选择功能:" << endl;
cin >> select;
switch (select) {
case 1 :
addPerson(&abs);
break;
case 2 :
displayPerson(&abs);
break;
case 3 :
while (1) {
j = isExist(&abs);
if (j!=0) {
delePerson(&abs,j);
}
else {
cout << "查无此人!是否重新删除(是--1,否--0)?" << endl;
while (1) {
cin >> i;
if (!(i == 0 || i == 1)) {
cout << "请输入正确格式" << endl;
}
else {
break;
}
}
if (i == 0) {
system("pause");
system("cls");
break;
}
}
}
break;
case 4 :
while (1) {
j = isExist(&abs);
if (j != 0) {
findPerson(&abs, j);
}
else {
cout << "查无此人!是否重新查找(是--1,否--0)?" << endl;
while (1) {
cin >> i;
if (!(i == 0 || i == 1)) {
cout << "请输入正确格式" << endl;
}
else {
break;
}
}
if (i == 0) {
system("pause");
system("cls");
break;
}
}
}
break;
case 5 :
while (1) {
j = isExist(&abs);
if (j != 0) {
datePerson(&abs, j);
}
else {
cout << "查无此人!是否重新查找(是--1,否--0)?" << endl;
while (1) {
cin >> i;
if (!(i == 0 || i == 1)) {
cout << "请输入正确格式" << endl;
}
else {
break;
}
}
if (i == 0) {
system("pause");
system("cls");
break;
}
}
}
break;
case 6 :
abs.num = 0;
cout << "通讯录清空完毕!" << endl;
break;
case 0 :
cout << "欢迎下次使用!" << endl;
system("pause");
return ;
break;
default:
break;
}
}
system("pause");
}