include
//创建 联系人信息 结构体 struct Contacts { //ID int id;
//名字string name;//性别string sex;//年龄short age;//联系电话string phone;//家庭住址string addrees;//是否存在数据; 0空 1有int isNull;
};
//创建联系人管理 结构体 struct ContactsMang { Contacts contArray[10];
//当前ID指数int idCount;//当前下标int index;
};
//定义方法 //1. 添加联系人 bool addCont(ContactsMang * contMang);
//2. 显示所有联系人方法 void viewAllCont(ContactsMang * contMang);
//3. 删除联系人 bool removeCont(ContactsMang* contMang);
//4. 查找联系人 struct Contacts getCont(ContactsMang* contMang);
//5. 修改指定联系人 bool updateCont(ContactsMang* contMang);
//6. 清空联系人 bool clearCont(ContactsMang* contMang);
int main() { bool flag = true;
//创建结构体数组ContactsMang cm ;cm.idCount = 1;cm.index = 0;int czs = 0;while (flag) {cout << "----------菜单----------" << endl;cout << "1. 添加联系人" << endl;cout << "2. 显示所有联系人方法" << endl;cout << "3. 删除联系人" << endl;cout << "4. 查找联系人" << endl;cout << "5. 修改指定联系人" << endl;cout << "6. 清空联系人" << endl;cout << "7. 退出( 按\" 7 \"或者不在以上的任意按键 \"退出\" )" << endl;cout << "------------------------" << endl;cin >> czs;switch (czs){case 1:addCont(&cm);system("cls");break;case 2:viewAllCont(&cm);break;case 3:removeCont(&cm);break;case 4:getCont(&cm);break;case 5:updateCont(&cm);break;case 6:clearCont(&cm);break;default:flag = false;cout << "已退出系统" << endl;break;}//system("cls");//清屏}return 0;
}
//1. 添加联系人 bool addCont(ContactsMang * contMang) { struct Contacts cont;
cont.id = contMang->idCount;cont.isNull = 1;cout << "请输入联系人姓名" << endl;cin >> cont.name;cout << "请输入联系人性别(男/女)" << endl;cin >> cont.sex;cout << "请输入联系人年龄" << endl;cin >> cont.age;cout << "请输入联系人电话" << endl;cin >> cont.phone;cout << "请输入联系人家庭住址)" << endl;cin >> cont.addrees;contMang->contArray[contMang->index] = cont;contMang->index++;contMang->idCount++;return true;
}
//2. 显示所有联系人方法 void viewAllCont(ContactsMang * contMang) { cout << “—————所有联系人展示—————-“ << endl; for (int i = 0; i < contMang->index; i++){ if (contMang->contArray[i].isNull == 1) { cout << “ 编号:” << contMang->contArray[i].id; cout << “ 名字:” << contMang->contArray[i].name; cout << “ 性别:” << contMang->contArray[i].sex; cout << “ 年龄:” << contMang->contArray[i].age; cout << “ 电话:” << contMang->contArray[i].phone; cout << “ 家庭住址:” << contMang->contArray[i].addrees << endl; } } cout << “———————展示结束———————“ << endl; }
//3. 删除联系人 bool removeCont(ContactsMang* contMang) { struct Contacts cont; cout << “请输入你需要删除联系人的编号” << endl;
int id = 0;cin >> id;for (int i = 0; i < contMang->index; i++) {if (contMang->contArray[i].id == id) {contMang->contArray[i] = cont;contMang->index--;return true;}}cout << "您需要删除的编号 并不存在" << endl;return false;
}
//4. 查找联系人 struct Contacts getCont(ContactsMang* contMang) { struct Contacts cont; cout << “请输入你需要查找联系人的编号” << endl;
int id = 0;cin >> id;for (int i = 0; i < contMang->index; i++) {if (contMang->contArray[i].id == id) {if (contMang->contArray[i].isNull == 1) {cout << " 编号:" << contMang->contArray[i].id;cout << " 名字:" << contMang->contArray[i].name;cout << " 性别:" << contMang->contArray[i].sex;cout << " 年龄:" << contMang->contArray[i].age;cout << " 电话:" << contMang->contArray[i].phone;cout << " 家庭住址:" << contMang->contArray[i].addrees << endl;cont = contMang->contArray[i];return cont;}}}cout << "您需要查找的编号 并不存在" << endl;return cont;
}
//5. 修改指定联系人 bool updateCont(ContactsMang* contMang) { struct Contacts cont; cout << “请输入你需要修改联系人的编号” << endl;
int id = 0;cin >> id;for (int i = 0; i < contMang->index; i++) {if (contMang->contArray[i].id == id) {if (contMang->contArray[i].isNull == 1) {cont.id = contMang->contArray[i].id;cont.isNull = contMang->contArray[i].isNull;cout << "请输入联系人姓名" << endl;cin >> cont.name;cout << "请输入联系人性别(男/女)" << endl;cin >> cont.sex;cout << "请输入联系人年龄" << endl;cin >> cont.age;cout << "请输入联系人电话" << endl;cin >> cont.phone;cout << "请输入联系人家庭住址)" << endl;cin >> cont.addrees;contMang->contArray[i] = cont;return true;}}}cout << "您需要查找的编号 并不存在" << endl;return false;
};
//6. 清空联系人 bool clearCont(ContactsMang* contMang) { cout << “您确定需要继续操作吗??” << endl; cout << “确定(Y) / 退出(N)”;
string flag;cin >> flag;if (flag == "Y") {contMang->index = 0;cout << "清空完毕!!" << endl;}return true;
};
```
