C語言實現(xiàn)簡單通訊錄系統(tǒng)
更新時間:2021年07月28日 09:27:21 作者:編程乖乖
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)簡單通訊錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言通訊錄系統(tǒng)(增刪改查),供大家參考,具體內(nèi)容如下
全部代碼如下所示:
#include <iostream>
#include <string>
using namespace std;
const int MAX = 1000;
//聯(lián)系人結(jié)構(gòu)體
typedef struct person
{
string m_name; //姓名
int m_sex; //性別 1 男,0 女
int m_age; //年齡
string m_phone; //電話
string m_addr; //住址
}person_t;
//通訊錄結(jié)構(gòu)體
typedef struct addressbooks
{
person_t personArray[MAX]; //通訊錄中保存的聯(lián)系人數(shù)組
int m_size; //通訊錄中人員個數(shù)
}addressbooks_t;
void showMenu()
{
cout << "************************" << endl;
cout << "***** 1.添加聯(lián)系人 *****" << endl;
cout << "***** 2.顯示聯(lián)系人 *****" << endl;
cout << "***** 3.刪除聯(lián)系人 *****" << endl;
cout << "***** 4.查找聯(lián)系人 *****" << endl;
cout << "***** 5.修改聯(lián)系人 *****" << endl;
cout << "***** 6.清空聯(lián)系人 *****" << endl;
cout << "***** 0.退出通訊錄 *****" << endl;
cout << "************************" << endl;
}
void addPerson(addressbooks_t *abs)
{
//判斷通訊錄是否滿了,如果滿了就不再添加
if(abs->m_size == MAX)
{
cout << "通訊錄已滿,無法添加!" << endl;
return ;
}
else
{
//添加具體聯(lián)系人
cout << "請輸入姓名: ";
cin >> abs->personArray[abs->m_size].m_name;
int sex;
while(true)
{
cout << "請輸入性別(1男,0女): ";
cin >> sex;
if(sex == 1 || sex == 0)
{
abs->personArray[abs->m_size].m_sex = sex;
break;
}
else
{
cout << "輸入有誤,請重新輸入!" << endl;
}
}
cout << "請輸入年齡: ";
cin >> abs->personArray[abs->m_size].m_age;
cout << "請輸入電話: ";
cin >> abs->personArray[abs->m_size].m_phone;
cout << "請輸入地址: ";
cin >> abs->personArray[abs->m_size].m_addr;
//更新通訊錄人數(shù)
++ abs->m_size;
cout << "添加成功!" << endl;
system("pause");
}
}
void displayPerson(addressbooks_t *abs)
{
if(abs->m_size == 0)
{
cout << "記錄為空!" << endl;
}
else
{
cout << "姓名\t" << "年齡\t" << "性別\t" << "電話\t\t" << "地址\t\t" << endl;
for(int i = 0; i < abs->m_size; ++i)
{
cout << abs->personArray[i].m_name << "\t";
cout << abs->personArray[i].m_age << "\t";
cout << (abs->personArray[i].m_sex == 1 ? "男" : "女") << "\t";
cout << abs->personArray[i].m_phone << "\t";
cout << abs->personArray[i].m_addr << "\t";
cout << endl;
}
}
system("pause");
}
//按姓名查找用戶,如果查找到返回聯(lián)系人在數(shù)組中的下標(biāo),未查找到返回-1
int findByName(addressbooks_t *abs,string name)
{
for(int i = 0; i < abs->m_size; ++i)
{
if(name == abs->personArray[i].m_name)
{
return i;
}
}
return -1;
}
void deletePerson(addressbooks_t *abs)
{
string name;
cout << "請輸入要刪除聯(lián)的系人姓名:";
cin >> name;
int index = findByName(abs,name);
if(index == -1)
{
cout << "通訊錄中未查到" << name << endl;
}
else
{
for(int i = index; i < abs->m_size; ++i)
{
abs->personArray[i] = abs->personArray[i+1];
}
--abs->m_size;
cout << "刪除成功" << endl;
}
system("pause");
}
void findPerson(addressbooks_t *abs)
{
string name;
cout << "請輸入要查找的聯(lián)系人姓名:";
cin >> name;
int index = findByName(abs,name);
if(index == -1)
{
cout << "通訊錄中未查到" << name << endl;
}
else
{
cout << "姓名\t" << "年齡\t" << "性別\t" << "電話\t\t" << "地址\t\t" << endl;
cout << abs->personArray[index].m_name << "\t";
cout << abs->personArray[index].m_age << "\t";
cout << (abs->personArray[index].m_sex == 1 ? "男" : "女") << "\t";
cout << abs->personArray[index].m_phone << "\t";
cout << abs->personArray[index].m_addr << "\t";
cout << endl;
}
system("pause");
}
void updatePerson(addressbooks_t *abs)
{
string name;
cout << "請輸入要修改的聯(lián)系人姓名:";
cin >> name;
int index = findByName(abs,name);
if(index == -1)
{
cout << "通訊錄中未查到" << name << endl;
}
else
{
//添加具體聯(lián)系人
cout << "請輸入姓名: ";
cin >> abs->personArray[index].m_name;
int sex;
while(true)
{
cout << "請輸入性別(1男,0女): ";
cin >> sex;
if(sex == 1 || sex == 0)
{
abs->personArray[index].m_sex = sex;
break;
}
else
{
cout << "輸入有誤,請重新輸入!" << endl;
}
}
cout << "請輸入年齡: ";
cin >> abs->personArray[index].m_age;
cout << "請輸入電話: ";
cin >> abs->personArray[index].m_phone;
cout << "請輸入地址: ";
cin >> abs->personArray[index].m_addr;
cout << "修改成功!" << endl;
}
system("pause");
}
void clearPerson(addressbooks_t *abs)
{
abs->m_size = 0;
cout << "清空成功!" << endl;
system("pause");
}
int main()
{
//創(chuàng)建一個結(jié)構(gòu)體變量
addressbooks_t abs;
//初始化通訊錄中人員個數(shù)
abs.m_size = 0;
int select = 0; //創(chuàng)建用戶選擇輸入的變量
while(true)
{
system("cls"); //清空屏幕
showMenu(); //菜單調(diào)用
cout << "請輸入您的選擇: ";
cin >> select;
switch (select)
{
case 0: //0.退出通訊錄
cout << "歡迎下次使用" << endl;
return 0;
break;
case 1: //1.添加聯(lián)系人
addPerson(&abs);
break;
case 2: //2.顯示聯(lián)系人
displayPerson(&abs);
break;
case 3: //3.刪除聯(lián)系人
deletePerson(&abs);
break;
case 4: //4.查找聯(lián)系人
findPerson(&abs);
break;
case 5: //5.修改聯(lián)系人
updatePerson(&abs);
break;
case 6: //6.清空聯(lián)系人
clearPerson(&abs);
break;
default:
break;
}
}
return 0;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)之隊列的定義與實現(xiàn)
隊列是一種特殊的線性表,特殊之處在于它只允許在表的前端(head)進(jìn)行刪除操作,而在表的后端(tail)進(jìn)行插入操作。本文將詳細(xì)講講C語言中隊列的定義與實現(xiàn),感興趣的可以了解一下2022-07-07
使用C/C++讀取matlab中.mat格式數(shù)據(jù)的操作
這篇文章給大家介紹了使用C/C++讀取matlab中.mat格式數(shù)據(jù)的操作,文中通過圖文結(jié)合的方式介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12

