文章目錄
- 分析:
- 注意:
- 上代碼:
分析:
通訊錄里邊有聯系人姓名,年齡,手機號,地址等,這些都要用結構體來創建,因此,在頭檔案之后便要創建聯系人結構體,
另外,還需要有一個通訊錄結構體,來記錄聯系人數量以及規定聯系人上限,
簡單的功能:添加聯系人,顯示聯系人,洗掉聯系人,查找聯系人,修改聯系人,清空聯系人,退出系統,(限于筆者水平,系統不夠完整,在這里筆者就拋磚引玉了,)
注意:
本代碼沒有對資料進行限制,比如 電話號碼的位數,
筆者很懶,直接用 string 帶過,
上代碼:
#include<iostream>
#include<string>
using namespace std;
//定義常量:
#define MAX 500
//顯示界面:
void showMeun()
{
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;
}
//創建聯系人結構體:
struct Person
{
string name;//名字
int age;//年齡
string gender;//性別
string tel;//電話
string addr;//地址
};
//創建通訊錄結構體
struct Telep
{
Person personArray[MAX];//給聯系人提供一個上限數量,
int personNum;//記錄通訊錄人數,
};
//添加:
void creat(struct Telep *init)
{
if (init->personNum >= 1000)
{
cout << "滿員了!" << endl;
return;
}
else
{
cout << "請輸入姓名:" << endl;
string name1;
cin >> name1;
int age1;
cout << "請輸入年齡:" << endl;
cin >> age1;
string sex;
cout << "請輸入性別:" << endl;
cout << " 男/女 " << endl;
cin >> sex;
string phone;
cout << "請輸入電話:" << endl;
cin >> phone;
string add1;
cout << "請輸入地址:" << endl;
cin >> add1;
Person exam = { name1,age1,sex,phone,add1 };
init->personArray[init->personNum] = exam;
cout << "添加成功!" << endl;
init->personNum++;
system("pause");//給自己反應時間,
system("cls");//清屏操作,
}
}
//輸出:
void printfP(struct Telep *init)
{
if (init->personNum == 0)
{
cout << "您的通訊錄中未添加任何資料,請添加后再執行本次操作,謝謝您的配合!" << endl;
return;
}
else
{
for (int i = 0; i < init->personNum; i++)
{
cout << "姓名:" << init->personArray[i].name << " 性別:" << init->personArray[i].gender <<
" 年齡:" << init->personArray[i].age << " 手機號碼:" << init->personArray[i].tel <<
" 地址:" << init->personArray[i].addr << endl;
}
}
}
//洗掉:
int found(Telep*init, string name)
{
int i;
for (i = 0; i < init->personNum; i++)
{
if (name == init->personArray[i].name)
{
return i;
}
}
return -1;
}
void deleteArr(Telep* init)
{
string name2;
cout << "請輸入需要洗掉的聯系人的名字:";
cin >> name2;
int ret = found(init, name2);
if(ret==-1)
{
cout << "查無此人!" << endl;
system("pause");
system("cls");
return;
}
else//物理洗掉:
//將某個資料后面的資料前移一位即可,
{
for (int i = ret; i < init->personNum; i++)
{
init->personArray[i] = init->personArray[i + 1];//從被洗掉項開始,后一項覆寫前一項資料,
}
init->personNum--;
cout << "洗掉成功!" << endl;
system("pause");
system("cls");
}
}
void foundPrintf(Telep*init)
{
cout << "請輸入需要查詢的名字:" << endl;
string name3;
cin >> name3;
int ret1 = found(init, name3);
if (ret1 == -1)
{
cout << "查無此人!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "姓名:" << init->personArray[ret1].name << " 性別:" << init->personArray[ret1].gender <<
" 年齡:" << init->personArray[ret1].age << " 手機號碼:" << init->personArray[ret1].tel <<
" 地址:" << init->personArray[ret1].addr << endl;
system("pause");
system("cls");
return;
}
}
void change(Telep* init)
{
string name4;
cout << "請輸入您需要修改資訊的聯系人的名字:" << endl;
cin >> name4;
int ret2 = found(init, name4);
if (ret2 == -1)
{
cout << "查無此人!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "資訊如下:" << endl;
cout << "姓名:" << init->personArray[ret2].name << " 性別:" << init->personArray[ret2].gender <<
" 年齡:" << init->personArray[ret2].age << " 手機號碼:" << init->personArray[ret2].tel <<
" 地址:" << init->personArray[ret2].addr << endl;
cout << "修改:" << endl;
cout << "請輸入修改后姓名:" << endl;
string name5;
cin >> name5;
int age2;
cout << "請輸入修改后年齡:" << endl;
cin >> age2;
string sex;
cout << "請輸入修改后性別:" << endl;
cout << " 男/女 " << endl;
cin >> sex;
string phone;
cout << "請輸入修改后電話:" << endl;
cin >> phone;
string add1;
cout << "請輸入修改后地址:" << endl;
cin >> add1;
Person exam = { name5,age2,sex,phone,add1 };
init->personArray[ret2] = exam;
cout << "修改完畢!" << endl;
}
}
void clear(Telep* init)
{
cout << "確定要清空嗎?千萬別刪庫跑路啊!" << "\t";
cout << "確定請按1,取消按0" << endl;
int b;
cin >> b;
if (b == 1)
{
init->personNum = 0;
cout << "清除完畢!" << endl;
system("pause");
system("cls");
}
else if(b == 0)
{
system("pause");
system("cls");
return;
}
}
int main()
{
Telep init;//創建通訊錄變數
init.personNum = 0;//將通訊錄人數初始化為0
int a;//接受按鍵,執行switch,
while (true)
{
showMeun();
cin >> a;
switch (a)
{
case 1://添加:
creat(&init);
break;
case 2://顯示:
{
printfP(&init);
system("pause");//給自己反應時間,
system("cls");//清屏操作,
}
break;
case 3://洗掉:
deleteArr(&init);
break;
case 4://查找:
foundPrintf(&init);
break;
case 5://修改:
change(&init);
system("pause");
system("cls");
break;
case 6://清空:
clear(&init);
break;
case 0://退出:
cout << "感謝使用通訊錄管理系統,期待下一次再會!" << endl;
system("pause");
return 0;
break;
}
}
}
文章多有不足,歡迎大家批評指標,
程式由學習黑馬程式員視頻寫出,內含個人理解,可能一些細節地方不能照顧到,
越努力越幸運 加油!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/265647.html
標籤:其他
上一篇:東京喰種語錄(節選)
