C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理_第1頁(yè)
C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理_第2頁(yè)
C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理_第3頁(yè)
C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理_第4頁(yè)
C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理本文實(shí)例為大家分享了C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理的具體代碼,供大家參考,具體內(nèi)容如下

這是第二周老師給的一個(gè)小項(xiàng)目要求實(shí)現(xiàn)基本通訊錄功能,有數(shù)據(jù)的增刪改查,包含插入時(shí)間的能力。

頭文件

#includeiostream

#includestring

#includemalloc.h//system功能調(diào)用

#includewindows.h//使用本地系統(tǒng)API獲取插入時(shí)間

#includesstream

基本存儲(chǔ)結(jié)構(gòu)體

typedefstructinfo{

stringnumber;

stringdate;

stringname;

stringadress;

stringbirthday;

typedefstructLNode{

Adata;

structLNode*next;

}LNode,*LinkList;

鏈表數(shù)據(jù)初始化

用前插法插入數(shù)據(jù)

intInitList(LinkListL){//初始化鏈表

L=newLNode;

L-next=NULL;

returnOK;

intListInsert(LinkListL,stringname,stringadress,stringbirthday,stringdate,stringnumber){//考慮使用數(shù)組來(lái)賦值

LinkListp;p=newLNode;

p-=name;

p-data.adress=adress;

p-data.date=date;

p-data.birthday=birthday;

p-data.number=number;

p-next=L-next;

L-next=p;

returnOK;

}

本地WindowsAPI調(diào)用插入時(shí)間

SYSTEMTIMEsys;

GetLocalTime(sys);

stringy=doubleToString(sys.wYear);

stringm=doubleToString(sys.wMonth);

stringd=doubleToString(sys.wDay);

stringymd=y+"-"+m+"-"+d;

因?yàn)楂@取的是一個(gè)double值,您得對(duì)其時(shí)間強(qiáng)制類(lèi)型轉(zhuǎn)換

stringdoubleToString(doublenum)

{//強(qiáng)制類(lèi)型轉(zhuǎn)換double強(qiáng)制轉(zhuǎn)換為string類(lèi)型

stringstreamss;

stringstr;

ssnum;

ssstr;

returnstr;

}

數(shù)據(jù)查詢(xún)功能

LinkListSearchElemChar(LinkListL,inti,stringe){//思路,傳遞參數(shù)1,2,3,4,eg1代表name,再分不同的方法在鏈表內(nèi)循環(huán)查找操作

if(i==1){//查名字

while(L!=NULL){

if(L-==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

}elseif(i==2){//查生日

while(L!=NULL){

if(L-data.birthday==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

elseif(i==3){//查地址

while(L!=NULL){

if(L-data.adress==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

}elseif(i==4){//查時(shí)間

while(L!=NULL){

if(L-data.date==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

elseif(i==5){//查電話(huà)

while(L!=NULL){

if(L-data.number==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

}

完整案例

//樂(lè)公第二周項(xiàng)目實(shí)現(xiàn)基本通訊錄存儲(chǔ)結(jié)構(gòu)

//基礎(chǔ)鏈表存儲(chǔ)數(shù)據(jù)

#includeiostream

#includestring

#includemalloc.h//system功能調(diào)用

#includewindows.h//使用本地系統(tǒng)API獲取插入時(shí)間

#includesstream

#defineERROR0

#defineOK1

usingnamespacestd;

typedef

intElemType;/*定義表元素的類(lèi)型*/

//結(jié)構(gòu)體文件

typedefstructinfo{

stringnumber;

stringdate;

stringname;

stringadress;

stringbirthday;

typedefstructLNode{

Adata;

structLNode*next;

}LNode,*LinkList;

intInitList(LinkListL){//初始化鏈表

L=newLNode;

L-next=NULL;

returnOK;

intListInsert(LinkListL,stringname,stringadress,stringbirthday,stringdate,stringnumber){//考慮使用數(shù)組來(lái)賦值

LinkListp;p=newLNode;

p-=name;

p-data.adress=adress;

p-data.date=date;

p-data.birthday=birthday;

p-data.number=number;

p-next=L-next;

L-next=p;

returnOK;

//查找元素(難題需要解決)

LinkListSearchElemChar(LinkListL,inti,stringe){//思路,傳遞參數(shù)1,2,3,4,eg1代表name,再分不同的方法在鏈表內(nèi)循環(huán)查找操作

if(i==1){//查名字

while(L!=NULL){

if(L-==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

}elseif(i==2){//查生日

while(L!=NULL){

if(L-data.birthday==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

elseif(i==3){//查地址

while(L!=NULL){

if(L-data.adress==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

}elseif(i==4){//查時(shí)間

while(L!=NULL){

if(L-data.date==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

elseif(i==5){//查電話(huà)

while(L!=NULL){

if(L-data.number==e){

returnL;

}else{

L=L-next;

}

}

if(L=NULL){

cout"未查到數(shù)據(jù)!"endl;

}

LinkListSearchElemBefore(LinkListL,stringe){//刪除結(jié)點(diǎn)必須返回前個(gè)結(jié)點(diǎn)的地址,這里查找前個(gè)結(jié)點(diǎn)

LinkListP;

while(L!=NULL){

if(L-==e){

returnP;

}else{

P=L;

L=L-next;

}

}

if(L=NULL){

returnL;

}

intShowMenu(){//主菜單

inta;

cout"------歡迎您使用樂(lè)公通訊錄系統(tǒng)!------"endl;

cout"-----請(qǐng)根據(jù)功能輸入對(duì)應(yīng)的序號(hào)--------"endl;

cout"-----------1.新建聯(lián)系人--------------"endl;

cout"--------2.查看所有聯(lián)系人-------------"endl;

cout"--------3.修改選定聯(lián)系人-------------"endl;

cout"--------4.查詢(xún)選定聯(lián)系人-------------"endl;

cout"--------5.刪除選定聯(lián)系人-------------"endl;

cout"------------6.退出系統(tǒng)---------------"endl;

cout"請(qǐng)您輸入序號(hào)并按回車(chē)進(jìn)入:";

cina;

returna;

stringdoubleToString(doublenum)

{//強(qiáng)制類(lèi)型轉(zhuǎn)換double強(qiáng)制轉(zhuǎn)換為string類(lèi)型

stringstreamss;

stringstr;

ssnum;

ssstr;

returnstr;

voidforeachelem(LinkListL){

if(L-next==NULL){

cout"通訊錄里還沒(méi)有聯(lián)系人,快去新建一下吧~"endl;

}else{

while(L-next!=NULL){

L=L-next;

cout"聯(lián)系人姓名:"L-endl;

cout"聯(lián)系人電話(huà):"L-data.numberendl;

cout"聯(lián)系人地址:"L-data.adressendl;

cout"聯(lián)系人生日:"L-data.birthdayendl;

cout"錄入時(shí)間:"L-data.dateendl;

}

cout"\n";

//system("pause");

intserachsamename(LinkListL,stringname){//查找是否存在姓名相同的聯(lián)系人

while(L-next!=NULL){

L=L-next;

if(L-==name)return0;

}

return1;

intmain(){

inti;

LinkListL;

InitList(L);

while(i!=6){

i=ShowMenu();

if(i==1){

cout"您選擇了:新建聯(lián)系人"endl;

stringnumber;

stringdate;

stringtime;

stringname;

stringadress;

stringbirthday;

cout"請(qǐng)輸入聯(lián)系人姓名:";

cinname;

SYSTEMTIMEsys;

GetLocalTime(sys);

stringy=doubleToString(sys.wYear);

stringm=doubleToString(sys.wMonth);

stringd=doubleToString(sys.wDay);

stringymd=y+"-"+m+"-"+d;

intrepeat=serachsamename(L,name);

if(repeat==0){

cout"聯(lián)系人姓名重復(fù),請(qǐng)刪除舊聯(lián)系人或更改姓名!"endl;

}else{

cout"請(qǐng)輸入聯(lián)系人電話(huà):";

cinnumber;

if(number.size()!=11){

cout"手機(jī)號(hào)輸入有誤,請(qǐng)大于11位!"endl;

}else{

cout"請(qǐng)輸入聯(lián)系人生日:";

cinbirthday;

cout"請(qǐng)輸入聯(lián)系人地址:";

cinadress;

cout"聯(lián)系人于"ymd;

intok;

ok=

ListInsert(L,name,adress,birthday,ymd,number);

if(ok==1){

cout"日新建成功!"endl;

}else{

cout"新建失敗!"endl;

}

}

}

system("pause");

system("cls");

}elseif(i==2){

cout"您選擇了:遍歷聯(lián)系人"endl;

foreachelem(L);

system("pause");

system("cls");

}elseif(i==3){

cout"您選擇了:修改選定聯(lián)系人"endl;

cout"請(qǐng)輸入要修改的聯(lián)系人姓名:";

stringname;

cinname;

LinkListB;

B=SearchElemChar(L,1,name);

if(B){

system("cls");

cout"聯(lián)系人查找成功!姓名:"B-endl;

intselect;

cout"---------修改姓名請(qǐng)輸入1---------"endl;

cout"---------修改電話(huà)請(qǐng)輸入2---------"endl;

cout"---------修改生日請(qǐng)輸入3---------"endl;

cout"---------修改地址請(qǐng)輸入4---------"endl;

cout"請(qǐng)根據(jù)序號(hào)輸入對(duì)象的選項(xiàng)修改:";

cinselect;

switch(select){

case1:

{

stringname;

cout"請(qǐng)輸入新姓名:";

cinname;

B-=name;

cout"修改完成!"endl;

break;

}

case2:{

stringnumber;

cout"請(qǐng)輸入新電話(huà):";

cinnumber;

if(number.size()!=11){

cout"手機(jī)號(hào)輸入有誤,請(qǐng)大于11位!"endl;

}else{

B-data.number=number;

cout"修改完成!"endl;

}

break;

}

case3:{

stringbirthday;

cout"請(qǐng)輸入新生日:";

cinbirthday;

B-data.birthday=birthday;

cout"修改完成!"endl;

break;

}

case4:{

stringadress;

cout"請(qǐng)輸入新地址:";

cinadress;

B-data.adress=adress;

cout"修改完成!"endl;

break;

}

default:cout"序號(hào)輸入錯(cuò)誤,請(qǐng)重新輸入!"endl;

}

}else{

cout"未查找到聯(lián)系人!請(qǐng)重新輸入!"endl;

}

system("pause");

system("cls");

}elseif(i==4){

system("cls");

cout"您選擇了:查詢(xún)選定聯(lián)系人"endl;

cout"---------根據(jù)姓名查詢(xún)請(qǐng)輸入1---------"endl;

cout"---------根據(jù)電話(huà)查詢(xún)請(qǐng)輸入2---------"endl;

cout"---------根據(jù)生日查詢(xún)請(qǐng)輸入3---------"endl;

cout"---------根據(jù)地址查詢(xún)請(qǐng)輸入4---------"endl;

intselect;

cout"請(qǐng)根據(jù)序號(hào)輸入對(duì)象的進(jìn)行查詢(xún):";

cinselect;

switch(select){

case1:{

cout"請(qǐng)輸入要查詢(xún)的聯(lián)系人姓名:";

stringname;

cinname;

LinkListB;

B=SearchElemChar(L,1,name);

if(B){

cout"查詢(xún)成功!"endl;

cout"聯(lián)系人姓名:"B-endl;

cout"聯(lián)系人電話(huà):"B-data.numberendl;

cout"聯(lián)系人生日:"B-data.birthdayendl;

cout"聯(lián)系人地址:"B-data.adressendl;

cout"插入日期:"B-data.dateendl;

}else{

cout"查詢(xún)失敗!請(qǐng)重新輸入!"endl;

}

break;

}

case2:

{

cout"請(qǐng)輸入要查詢(xún)的聯(lián)系人電話(huà):";

stringnumber;

cinnumber;

LinkListB;

B=SearchElemChar(L,5,number);

if(B){

cout"查詢(xún)成功!"endl;

cout"聯(lián)系人姓名:"B-endl;

cout"聯(lián)系人電話(huà):"B-data.numberendl;

cout"聯(lián)系人生日:"B-data.birthdayendl;

cout"聯(lián)系人地址:"B-data.adressendl;

cout"插入日期:"B-data.dateendl;

}else{

cout"查詢(xún)失??!請(qǐng)重新輸入!"endl;

}

break;

}

case3:{

cout"請(qǐng)輸入要查詢(xún)的聯(lián)系人生日:";

stringbd;

cinbd;

LinkListB;

B=SearchElemChar(L,2,bd);

if(B){

cout"查詢(xún)成功!"endl;

cout"聯(lián)系人姓名:"B-endl;

cout"聯(lián)系人電話(huà):"B-data.numberendl;

cout"聯(lián)系人生日:"B-data.birthdayendl;

cout"聯(lián)系人地址:"B-data.adressendl;

cout"插入日期:"B-data.dateendl;

}else{

cout"查詢(xún)失敗!請(qǐng)重新輸入!"endl;

}

break;

}

case4:{

cout"請(qǐng)輸入要查詢(xún)的聯(lián)系人地址:";

stringad;

cinad;

LinkListB;

B=SearchElemChar(L,3,ad);

if(

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論