#include "stdio.h" #include <string> #include <iostream> using namespace std; struct Contacts { string sName; int iTelephone; string sArea; //定义基本结构体 }; struct ContactsList { //定义结构体数组 int iListNumber; Contacts m_Contacts[100]; }; void vShowContacts(ContactsList *m_ContactsList) { //显示联系人 if (m_ContactsList->iListNumber == 0) { cout << "暂时没有联系人" << endl; } else { for (int i = 0; i < m_ContactsList->iListNumber; i++) { cout << "姓名:" << m_ContactsList->m_Contacts[i].sName << " 电话:" << m_ContactsList->m_Contacts[i].iTelephone << " 地区:" << m_ContactsList->m_Contacts[i].sArea << endl; } } } void vAddContacts(Contacts v_Contacts, ContactsList *m_ContactsList) { //添加联系人 cout << "请输入姓名" << endl; cin >> v_Contacts.sName; cout << "请输入电话" << endl; cin >> v_Contacts.iTelephone; cout << "请输入地区" << endl; cin >> v_Contacts.sArea; cout << "添加成功" << endl; m_ContactsList->iListNumber = m_ContactsList->iListNumber + 1; m_ContactsList->m_Contacts[m_ContactsList->iListNumber - 1] = v_Contacts; } bool bDelContacts(ContactsList *m_ContactsList) { //删除联系人 cout << "请输入删除的人的名字" << endl; string sDelName; cin >> sDelName; /* 还可以增加更多的逻辑判断 1.没有联系人 怎么写逻辑 2.只有一个联系人, 怎么去删除 怎么去显示出来 */ for (int i = 0; i < m_ContactsList->iListNumber; i++) { if (m_ContactsList->m_Contacts[i].sName == sDelName ) { //删除指针数组内元素 //1.将后面的元素覆盖此元素,后面依次+1;数组长度-1 //2.将最后一位元素覆盖此元素,数组长度-1 //这里采用第二种方式 m_ContactsList->m_Contacts[i].sName = m_ContactsList->m_Contacts[m_ContactsList->iListNumber - 1].sName; m_ContactsList->m_Contacts[i].iTelephone = m_ContactsList->m_Contacts[m_ContactsList->iListNumber - 1].iTelephone; m_ContactsList->m_Contacts[i].sArea = m_ContactsList->m_Contacts[m_ContactsList->iListNumber - 1].sArea; m_ContactsList->iListNumber -= 1; return true; } } return false; } bool bSearchContacts(ContactsList *m_ContactsList) { cout << "请输入联系人的名字" << endl; string sSearchName; cin >> sSearchName; for (int i = 0; i < m_ContactsList->iListNumber; i++) { if (m_ContactsList->m_Contacts[i].sName == sSearchName) { cout << "检索信息->" << endl; cout << "姓名:" << m_ContactsList->m_Contacts[i].sName << " 电话:" << m_ContactsList->m_Contacts[i].iTelephone << " 地区:" << m_ContactsList->m_Contacts[i].sArea << endl; return true; } } return false; } bool bChangeContacts(ContactsList *m_ContactsList) { cout << "请输入需要修改人的名字" << endl; string sChangeName; cin >> sChangeName; for (int i = 0; i <= m_ContactsList->iListNumber; i++) { if (m_ContactsList->m_Contacts[i].sName == sChangeName) { cout << "名字修改为?" << endl; cin >> m_ContactsList->m_Contacts[i].sName; cout << "电话修改为?" << endl; cin >> m_ContactsList->m_Contacts[i].iTelephone; cout << "地区修改为?" << endl; cin >> m_ContactsList->m_Contacts[i].sArea; cout << "修改完成" << endl; return true; } //TODO } return false; } int main() { ContactsList g_ContactsList; ContactsList Next_ContactsList; g_ContactsList.iListNumber = 0; //联系人置0; Contacts m_Contacts; cout << "******************************" << endl; cout << "*********1.显示联系人*********" << endl; cout << "*********2.添加联系人*********" << endl; cout << "*********3.删除联系人*********" << endl; cout << "*********4.查找联系人*********" << endl; cout << "*********5.修改联系人*********" << endl; cout << "*********6.清空联系人*********" << endl; cout << "************0.退出************" << endl; while (true) { int iInput = 1; cin >> iInput; switch (iInput) { case 1: vShowContacts(&g_ContactsList); break; case 2: vAddContacts(m_Contacts, &g_ContactsList); break; case 3: bool a; a = bDelContacts(&g_ContactsList); if (a == true) { cout << "删除成功" << endl; } else { cout << "删除失败" << endl; } break; case 4: bool b; b = bSearchContacts(&g_ContactsList); if (b == true) { } else { cout << "没有这个联系人" << endl; } break; case 5: bool c; c = bChangeContacts(&g_ContactsList); if (c == true) { //TODO } else { cout << "修改失败" << endl; } break; case 6: g_ContactsList = Next_ContactsList; //可以加一个是否确定的逻辑判断 cout << "成功" << endl; break; case 0: exit(1); break; default : cout << "检索不到命令" << endl; break; } } system("pause"); return 0; }
标签:sName,cout,Contacts,基础,c++,sArea,通讯录,iListNumber,ContactsList From: https://www.cnblogs.com/LightK/p/16667853.html