代码
#include <iostream>;
using namespace std;
#define MAX 50 //设置通讯录最大存储容量
//封装显示主界面的函数;如:showMenu();
void showMenu() {
system("cls"); //清屏操作;
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;
cout << "请输入你想要的操作:" ;
}
//设计联系人的结构体;结构体大括号后要加分号;
struct Contact {
string mName;
string mNumber;
string mSex; //中文的单个汉字不可以用 char 来修饰;
int mAge;
string mAddress;
};
//设计通讯录的结构体
struct AddressBook {
struct Contact contactArr[MAX];
int size;
};
//核查输入的性别,只能是 男 或 女;否则重新输入;
string checkSex(string sex){
while (true) {
cout << "\n性别:";
cin >> sex;
if (sex == "男" || sex == "女") {
return sex;
} else {
cout << "请输入男或女" << endl;
}
}
}
//判断输入类型是否和定义的一致;cin.fail();避免 a 等 char 类型数据;
//这里出现奇葩问题;当输入的类型不符时;输入内容占几个字节,就输出多少行“提示”;
int checkAge(int age){
while (cin.fail() || age > 120 || age < 0) {
cout << "请输入正常年龄(0 - 120)"<<endl;
cout << "\n年龄:";
cin.clear(); //清除错误状态
cin.ignore(); //跳过无效数据
cin >> age;
if (cin.good() && age <= 120 && age >= 0) {
break;
}
}
return age;
}
//核查是否修改数据,当输入的是 0 时,不进行修改;
bool checkIsUpdate(string input) {
if (input == "0") {
return false;
}
return true;
}
//添加联系人;
void addContact(AddressBook * addressBook) {
if (addressBook->size == MAX) {
cout << "您的通讯录已满,请删除不必要的联系方式后重试!" << endl;
return;
}else {
string name ;
string number;
string sex ;
int age;
string address;
cout << "请输入联系人以下信息:\n\n姓名:";
cin >> name;
cout << "\n号码:";
cin >> number;
//这里核查后要有返回值重新赋值;否则最后的赋值结果还是错误的信息;
sex = checkSex(sex);
cout << "\n年龄:";
cin >> age;
age = checkAge(age);
cout << "\n住址:";
cin >> address;
Contact contact;
contact.mNumber = number;
contact.mName = name;
contact.mAge = age;
contact.mSex = sex;
contact.mAddress = address;
int size = addressBook->size;
addressBook->contactArr[size] = contact;
addressBook->size++;
cout << "添加成功" << endl;
system("pause"); //按任意键继续;而后清屏显示菜单;
showMenu();
}
}
void showContact(AddressBook addressBook) {
if (addressBook.size != 0) {
cout << "姓名\t" << "性别\t" << "电话号码\t" << "年龄\t" << "家庭住址\t" << endl;
for (int i = 0; i < addressBook.size; i++) {
cout << " " << addressBook.contactArr[i].mName << "\t"
<< " " << addressBook.contactArr[i].mSex << "\t"
<< " " << addressBook.contactArr[i].mNumber << "\t\t"
<< " " << addressBook.contactArr[i].mAge << "\t"
<< " " << addressBook.contactArr[i].mAddress << "\t" << endl;
}
}else {
cout << "通讯录暂无数据,请先进行添加!" << endl;
}
system("pause");
showMenu();
}
void deleteContact(AddressBook * addressBook) {
if (addressBook->size != 0) {
cout << "请输入你要删除的联系人姓名:" << endl;
string deleteName;
cin >> deleteName;
cout << "即将删除:" << deleteName << " 的联系人信息" << endl;
while (true) {
bool isExit = false;
for (int i = 0; i < addressBook->size; i++) {
if (addressBook->contactArr[i].mName._Equal(deleteName)) {
isExit = true;
if (i < addressBook->size -1) {
//这里注意:如果删除的数据是最后一条,那么直接 size-- 即可;
for (; i < addressBook->size - 1; i++) {
//删除选择中的联系人信息;
addressBook->contactArr[i] = addressBook->contactArr[i + 1];
}
}
addressBook->size--;
cout << "联系人:" << deleteName << " 删除成功!" << endl;
break;
}
}
if (!isExit) {
cout << "查无此人" << endl;
}
isExit = false;
break;
}
}else {
cout << "通讯录暂无数据,请先进行添加!" << endl;
}
system("pause");
showMenu();
}
void queryContact(AddressBook addressBook) {
if (addressBook.size != 0) {
string queryName;
cout << "请输入待查询的联系人姓名:";
cin >> queryName;
while (true) {
bool isExit = false;
for (int i = 0; i < addressBook.size; i++) {
if (addressBook.contactArr[i].mName._Equal(queryName)) {
isExit = true;
cout << "姓名\t" << "性别\t" << "电话号码\t" << "年龄\t" << "家庭住址\t" << endl;
cout << " " << addressBook.contactArr[i].mName << "\t"
<< " " << addressBook.contactArr[i].mSex << "\t"
<< " " << addressBook.contactArr[i].mNumber << "\t\t"
<< " " << addressBook.contactArr[i].mAge << "\t"
<< " " << addressBook.contactArr[i].mAddress << "\t" << endl;
break;
}
}
if (!isExit) {
cout << "查无此人" << endl;
}
isExit = false;
break;
}
}else {
cout << "通讯录暂无数据,请先进行添加!" << endl;
}
system("pause");
showMenu();
}
void updateContact(AddressBook * addressBook) {
if (addressBook->size != 0) {
cout << "请输入待修改的联系人姓名:" << endl;
string upDateName;
cin >> upDateName;
cout << "即将修改:" << upDateName << " 的联系人信息" << endl;
while (true) {
bool isExit = false;
for (int i = 0; i < addressBook->size; i++) {
if (addressBook->contactArr[i].mName._Equal(upDateName)) {
isExit = true;
isExit = true;
cout << "\n姓名\t" << "性别\t" << "电话号码\t" << "年龄\t" << "家庭住址\t" << endl;
cout << " " << addressBook->contactArr[i].mName << "\t"
<< " " << addressBook->contactArr[i].mSex << "\t"
<< " " << addressBook->contactArr[i].mNumber << "\t\t"
<< " " << addressBook->contactArr[i].mAge << "\t"
<< " " << addressBook->contactArr[i].mAddress << "\t" << endl;
cout << "请输入需要修改的信息;如不需修改按 0 跳过" << endl;
string newName;
string newNumber;
string newSex;
int newAge;
string newAddress;
cout << "姓名" << endl;
//更新数据操作使用的三目运算符,多少有些冗余;
cin >> newName;
checkIsUpdate(newName) ? addressBook->contactArr[i].mName = newName : addressBook->contactArr[i].mName;
cout << "\n性别:";
cin >> newSex;
checkIsUpdate(newSex) ? addressBook->contactArr[i].mSex = newSex : addressBook->contactArr[i].mSex;
cout << "\n号码:";
cin >> newNumber;
checkIsUpdate(newNumber) ? addressBook->contactArr[i].mNumber = newNumber: addressBook->contactArr[i].mNumber;
cout << "\n年龄:";
cin >> newAge;
checkIsUpdate(newAge+"") ? addressBook->contactArr[i].mAge = newAge : addressBook->contactArr[i].mAge;
cout << "\n住址:";
cin >> newAddress;
checkIsUpdate(newAddress) ? addressBook->contactArr[i].mAddress = newAddress : addressBook->contactArr[i].mAddress;
cout << "修改成功" << endl;
cout << "\n姓名\t" << "性别\t" << "电话号码\t" << "年龄\t" << "家庭住址\t" << endl;
cout << " " << addressBook->contactArr[i].mName << "\t"
<< " " << addressBook->contactArr[i].mSex << "\t"
<< " " << addressBook->contactArr[i].mNumber << "\t\t"
<< " " << addressBook->contactArr[i].mAge << "\t"
<< " " << addressBook->contactArr[i].mAddress << "\t" << endl;
system("pause"); //按任意键继续;而后清屏显示菜单;
showMenu();
break;
}
}
if (!isExit) {
cout << "查无此人" << endl;
}
isExit = false;
break;
}
}else {
cout << "通讯录暂无数据,请先进行添加!" << endl;
}
system("pause");
showMenu();
}
//刷新三观的“重置数组”……
void clearContact(AddressBook * addressBook) {
if (addressBook->size != 0) {
cout << "清空联系人将不可还原,是否继续?输入确定:将清空联系人" << endl;
string isClear;
cin >> isClear;
if (isClear._Equal("确定")) {
//清空数组
addressBook->size = 0;
cout << "联系人已全部清空!" << endl;
}else {
cout << "与取消清空联系人!" << endl;
}
}else {
cout << "通讯录暂无数据,请先进行添加!" << endl;
}
system("pause");
showMenu();
}
int main() {
//创建通讯录变量
AddressBook addressBook;
//初始化通讯录中的联系人个数;
addressBook.size = 0;
showMenu();//调用菜单,显示主界面
//根据用户输入的数字进行相关操作;
int selected;
cin >> selected;
while (selected || cin.fail()) {
if (cin.fail() || selected > 6 || selected < 0) {
cin.clear();
cin.ignore();
cout << "请输入 0 - 6 以内的操作:" << endl;
cin >> selected;
continue; //如果输出值非法,那么之后的操作就不必执行了!continue 很靓!
}
switch (selected) {
case 1: // 1、添加联系人
addContact(&addressBook);//通讯录利用指针传递,这样才可以真正修改通讯录实参;
break;
case 2: // 2、显示联系人
showContact(addressBook);//查询利用值传递即可;
break;
case 3: // 3、删除联系人
deleteContact(&addressBook);
break;
case 4: // 4、查找联系人
queryContact(addressBook);//查询利用值传递即可;
break;
case 5: // 5、修改联系人
updateContact(&addressBook);
break;
case 6: // 6、清空联系人
clearContact(&addressBook);
break;
default:
break;
}
cin >> selected;
}
//按 0 退出界面;其他操作都提示重新按键;
if (cin.good() && selected == 0) {
cout << "欢迎下次使用!" << endl;
system("pause");
}
return 0;
}
亮点:
1.项目的框架:注意两个结构体的创建!
2.当用户输入进行操作时:
如果输入类型与定义类型不符时!很可能会造成 之后的代码不正常执行;解决办法:在再次输入之前添加如下语句:
cin.clear(); //清除错误状态
cin.ignore(); //跳过无效数据
3.注意:中文的单个汉字不可以用 char 来修饰;因为 char 是一个字节,而一个汉字是4个字节;
4.判断输入类型是否和定义的一致;cin.fail();这里遇见奇葩问题;当输入的类型不符时;输入内容占几个字节,就输出多少行“提示”;
5.一些 API
system("cls"); //清屏操作;
system("pause"); //按任意键继续;
6.对数组元素进行删除时要注意:
删除数组元素时;一次将后面的数覆盖前一个数;最后 size-- ;
要注意阐述元素时,不要角标越界;
对于最后一个元素,不必覆盖处理;直接进行 size-- 即可;
7.刷新三观的“重置数组”……设置 size == 0;
8.定义方法对数组进行操作时;如果需要修改数组的值时,传入其指针进行修改;如果仅是查询等操作则可以直接穿数组;