#include <iostream>
#include <string>
using namespace std; // 为什么要禁用C++的名字空间?
// C++为什么要引入名字空间?
// 是为了解决什么问题?
#define MAX 1000
struct person {
string name;
int sex; // 1男 2女
int age;
string phone;
string add;
};
struct addbook {
struct person personArray[MAX]; // 为什么不用vector ?
int size;
};
void showMenu() {
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(struct addbook *book) {
if(book->size == MAX) {
cout << "联系人已满" << endl;
return;
}
string name;
cout << "请输入联系人姓名" << endl;
cin >> name;
book->personArray[book->size].name = name;
int sex;
cout << "请输入联系人性别 1、男 2、女" << endl;
while(true) {
cin >> sex;
if(sex == 1 || sex == 2) {
book->personArray[book->size].sex = sex;
break;
}
cout << "输入有误请重新输入" << endl;
}
int age;
cout << "请输入联系人年龄" << endl;
cin >> age;
book->personArray[book->size].age = age;
string phone;
cout << "请输入联系人电话" << endl;
cin >> phone;
book->personArray[book->size].phone = phone;
string add;
cout << "请输入联系人住址" << endl;
cin >> add;
book->personArray[book->size].add = add;
book->size++;
cout << "添加成功" << endl;
//system("pause");
//system("cls");
}
//void showPerson(struct addbook book) {
void showPerson(const struct addbook *book) {
if(book->size == 0) {
cout << " 当前记录为空" << endl;
}
for(int i = 0; i < book->size; i++) {
cout << "姓名:" << book->personArray[i].name << "\t";
cout << "性别:" << (book->personArray[i].sex == 1 ? "男" : "女")
<< "\t";
cout << "年龄:" << book->personArray[i].age << "\t";
cout << "电话:" << book->personArray[i].phone << "\t";
cout << "住址:" << book->personArray[i].add << endl;
};
/*system("pause");
system("cls");*/
}
//int isExist(struct addbook *book, string name) {
int isExist(const struct addbook *book, string name) {
for(int i = 0; i < book->size; i++) {
if(book->personArray[i].name == name) {
return i;
}
//return -1; // ???
}
return -1;
}
void deletePerson(struct addbook *book) {
cout << "请输入需要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(book, name);
if(ret != -1) {
//for(int i = ret; i < book->size; i++) {
for(int i = ret; i < book->size - 1; i++) {
book->personArray[ret] = book->personArray[ret + 1];
}
book->size--;
cout << "删除成功" << endl;
} else {
cout << "查无此人" << endl;
}
}
//void findPerson(struct addbook *book) {
void findPerson(const struct addbook *book) {
cout << "请输入查找联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(book, name);
if(ret != -1) {
cout << "姓名:" << book->personArray[ret].name << "\t";
cout << "性别:" << (book->personArray[ret].sex == 1 ? "男" : "女")
<< "\t";
cout << "年龄:" << book->personArray[ret].age << "\t";
cout << "电话:" << book->personArray[ret].phone << "\t";
cout << "住址:" << book->personArray[ret].add << endl;
} else {
cout << "查无此人" << endl;
}
}
int main() {
struct addbook book;
book.size = 0;
int select = 0;
while(1) {
showMenu();
cin >> select;
switch(select) {
case 1:
addPerson(&book); //利用地址传递修改
break;
case 2:
//showPerson(book); // 知道这么写意味着什么吗?这会把book整个复制一份
// // 把复制的这个book传递给showPerson
showPerson(&book);
break;
case 3:
deletePerson(&book);
break;
case 4:
findPerson(&book);
break;
case 5:
break;
case 6:
break;
case 0:
cout << "欢迎下次使用" << endl;
//system("pause");
return 0;
break;
default:
break;
}
}
//system("pause");
return 0;
}
|