?? phonebook.cpp
字號(hào):
//*******************************************************************
/***************** PhoneBook個(gè)人電話簿管理系統(tǒng) ****************/
/* 作者:郭佩佩 */
/* 學(xué)號(hào):09002138 */
/* 日期:2004/11/26 */
//*******************************************************************
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
#include<stdlib.h>
struct record //結(jié)構(gòu)體類型定義
{
char lastName[20]; //姓氏
char firstName[15]; //名字
char phoneNum[15]; //電話
char mobile[15]; //手機(jī)
record *next;
};
record *head; //全局變量,鏈表頭指針
record *current; //全局變量,鏈表當(dāng)前結(jié)點(diǎn)指針
// 函數(shù)原型聲明
void handleChoice(int choice);
void addRecord();
void insertNode(record *newRecord);
record *posInsertPoint(char lastname[20]);
void makeNodeNewHead(record *newRecord);
void addNodeToEnd(record *newRecord);
void moveCurrentToEnd();
void display();
void update();
void deleteRecord();
void deleteAll();
void deleteHead();
void deleteEnd(record *previous);
void deleteMiddle(record *previous);
int verifyDelete();
void deleteNode(record *previous);
void deleteList();
void searchByLastname();
void wiriteToFile();
void loadFromFile();
void help();
char pause;
// 主函數(shù)
int main()
{
cout << "Welcome...\n";
cout << "*************************************************************\n";
cout << "\n\n\n 歡迎使用PhoneBook個(gè)人電話簿管理系統(tǒng)... \n\n";
cout << " Press enter to continue!\n\n\n";
cout << "\n*************************************************************";
cin.get(pause);
system("cls"); //執(zhí)行系統(tǒng)命令:cls-清屏
int choice;
head = NULL;
loadFromFile();
do
{ //主菜單顯示
cout << "Menu...\n";
cout << "***********************************************************\n";
cout << " PhoneBook個(gè)人電話管理系統(tǒng)主菜單 \n\n";
cout << " 請(qǐng)選擇以下功能: \n";
cout << " 1 - 添加一條記錄\n";
cout << " 2 - 顯示全部記錄\n";
cout << " 3 - 按姓氏查找\n";
cout << " 4 - 更新記錄\n";
cout << " 5 - 刪除一條記錄\n";
cout << " 6 - 清空電話簿\n";
cout << " 7 - 幫助\n";
cout << " 8 - 退出系統(tǒng)\n";
cout << "***********************************************************\n";
cout << "選擇: ";
cin >> choice;
handleChoice(choice);
}
while(choice != 8);
return 0;
}
// 根據(jù)用戶選擇(choice)調(diào)用對(duì)應(yīng)處函數(shù)
void handleChoice(int choice)
{
switch(choice)
{
case 1: //添加一條記錄
addRecord();
break;
case 2: //顯示全部記錄
display();
break;
case 3: //按姓氏查找
searchByLastname();
break;
case 4: //更新記錄
update();
break;
case 5: //刪除一條記錄
deleteRecord();
break;
case 6: //清空電話簿
deleteAll();
break;
case 7: //幫助
help();
break;
case 8: //文件存盤,然后退出系統(tǒng)
wiriteToFile();
if(head != NULL)
{
deleteList();
}
break;
default : //其它輸入則出錯(cuò)
cout <<"輸入有誤!!請(qǐng)您重新輸入選項(xiàng)!!\n";
break;
}
}
// 在鏈表中增加一個(gè)記錄
void addRecord()
{
record *newRecord;
newRecord = new record;
if(newRecord != NULL)
{
system("cls");
cout << "Add a new record...\n";
cout << "*************************************************************\n";
cout << "請(qǐng)您輸入...\n";
cin.ignore(20,'\n');
cout << " 姓氏: ";
cin.get(newRecord->lastName,20);
cin.ignore(20,'\n');
cout << " 名字: ";
cin.get(newRecord->firstName,15);
cin.ignore(20,'\n');
cout << " 電話號(hào)碼: ";
cin.get(newRecord->phoneNum,15);
cin.ignore(20,'\n');
cout << " 手機(jī)號(hào)碼:";
cin.get(newRecord->mobile,15);
cin.ignore(20,'\n');
insertNode(newRecord);
}
else
{
cout << "警告: 存儲(chǔ)器錯(cuò)誤, 新記錄添加失敗!!\n";
}
system("cls");
}
// 將一個(gè)由newRecord 指向的新節(jié)點(diǎn)插入鏈表中
void insertNode(record *newRecord)
{
system("cls");
record *before;
record *after;
if(head == NULL) //鏈表為空,直接插入到頭結(jié)點(diǎn)
{
newRecord->next = NULL;
head = newRecord;
}
else
{
if(strcmp(newRecord->lastName, head->lastName) < 0)
{
makeNodeNewHead(newRecord);
}
else
{
current = posInsertPoint(newRecord->lastName);
before = current;
after = current->next;
if(after == NULL)
{
addNodeToEnd(newRecord);
}
else
{
before->next = newRecord;
newRecord->next = after;
}
}
}
}
// 按姓氏,返回其在鏈表中的正確位置。新節(jié)點(diǎn)即將插入此點(diǎn)。
record *posInsertPoint(char lastname[20])
{
char tempName[20];
record *temp;
int tempint;
if(head->next != NULL)
{
current = head;
temp = current->next;
strcpy(tempName, temp->lastName);
tempint = strcmp(lastname,tempName);
while((tempint > 0) && (current->next !=NULL))
{
current = temp;
if(current->next != NULL)
{
temp = current->next;
strcpy(tempName, temp->lastName);
tempint = strcmp(lastname,tempName);
}
}
}
else
{
current = head;
}
return(current);
}
// 添加到鏈表頭部
void makeNodeNewHead(record *newRecord)
{
record *temp;
temp = head;
newRecord->next = temp;
head = newRecord;
}
// 添加到鏈表末尾
void addNodeToEnd(record *newRecord)
{
newRecord->next = NULL;
moveCurrentToEnd();
current->next = newRecord;
}
// current指針移動(dòng)到鏈表尾
void moveCurrentToEnd()
{
current = head;
while(current->next != NULL)
{
current = current->next;
}
}
// 顯示全部記錄
void display()
{
char fullname[36];
current = head;
if(current != NULL)
{
cout << endl;
cout << " 姓 名 電話號(hào)碼 手機(jī)號(hào)碼\n";
cout << "------------------------------ -------------- ---------------\n";
do
{
strcpy(fullname,"");
strcat(fullname, current->lastName);
strcat(fullname, ", ");
strcat(fullname, current->firstName);
cout.setf(ios::left);
cout << setw(36) << fullname;
cout.unsetf(ios::left);
cout.setf(ios::right);
cout << setw(12) << current->phoneNum;
cout.unsetf(ios::left);
cout.setf(ios::right);
cout << setw(15) << current->mobile << endl;
current = current->next;
cout << endl;
}
while(current != NULL);
cout << "Press Enter to continue \n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
}
else
{
cout << "\n記錄為空!!\n";
cout << "Press Enter to continue \n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
}
}
// 按姓氏查找記錄
void searchByLastname()
{
system("cls");
char searchStr[20];
current = head;
cin.ignore(20,'\n');
cout << "Search...\n";
cout << "*******************************************************************\n";
cout << "請(qǐng)輸入姓氏: ";
cin.get(searchStr,20);
cin.ignore(20,'\n');
while((current != NULL) && (strcmp(current->lastName, searchStr) != 0))
{
current = current->next;
if(current != NULL)
{
cout << "匹配的記錄:\n";
cout << current->firstName << ' ' << current->lastName << endl;
cout << current->phoneNum << ' ' << current->mobile << endl;
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
else
{
cout << "沒有匹配記錄!!\n";
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
}
}
// 更新記錄
void update()
{
system("cls");
char searchStr[20];
current = head;
cin.ignore(20,'\n');
cout << "Update...\n";
cout << "********************************************************************\n";
cout << "請(qǐng)輸入需要更新記錄的姓氏:";
cin.get(searchStr,20);
cin.ignore(20,'\n');
while((current != NULL) && (strcmp(current->lastName, searchStr) != 0))
{
current = current->next;
}
if(current != NULL)
{
cout << "匹配的記錄:\n";
cout << current->lastName << ' ' << current->firstName << ' ';
cout << current->phoneNum << ' ' << current->mobile << endl;
cout << "請(qǐng)您輸入新的記錄信息...\n";
cout << "姓氏: ";
cin.get(current->lastName,20);
cin.ignore(20,'\n');
cout << "名字: ";
cin.get(current->firstName,15);
cin.ignore(20,'\n');
cout << "電話號(hào)碼: ";
cin.get(current->phoneNum,15);
cin.ignore(20,'\n');
cout << "手機(jī)號(hào)碼:";
cin.get(current->mobile,15);
cin.ignore(20,'\n');
cout << "更新成功!!\n";
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
else
{
cout << "沒有指定要更新的記錄!!\n";
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
}
// 刪除一條記錄
void deleteRecord()
{
system("cls");
char searchStr[20];
record *previous;
previous = NULL;
current = head;
cin.ignore(20,'\n');
cout << "請(qǐng)輸入需要?jiǎng)h除的記錄的姓氏: ";
cin.get(searchStr,20);
cin.ignore(20,'\n');
while((current != NULL) && (strcmp(current->lastName, searchStr) != 0))
{
previous = current;
current = current->next;
}
if(current != NULL)
{
cout << "\n匹配記錄\n";
cout << current->firstName << ' ' << current->lastName << endl;
cout << current->phoneNum << ' ' << current->mobile << endl;
if(verifyDelete())
{
deleteNode(previous);
cout << "\n記錄刪除成功!!\n";
}
else
{
cout << "\n記錄刪除失敗!!\n";
}
}
else
{
cout << "\n沒有匹配記錄,刪除失敗!!\n";
}
system("cls");
}
// 清空電話簿
void deleteAll()
{
if(verifyDelete())
{
deleteList();
cout << "\n記錄刪除成功!!\n";
}
else
{
cout << "\n記錄刪除失敗!!\n";
}
system("cls");
}
// 幫助
void help()
{
help:
int question;
system("cls");
cout << "Help...\n";
cout << "**************************************************************\n";
cout << " 請(qǐng)選擇幫助的主題:\n";
cout << " 1: 我的記錄怎么了?\n";
cout << " 2: 我怎么樣清除所有記錄?\n";
cout << " 3: 怎么進(jìn)行操作?\n";
cout << " 4: 怎么聯(lián)系我?\n";
cout << " 5: 退出\n";
cout << "**************************************************************\n";
cout << "選擇: ";
cin >> question;
switch(question)
{
case 1:
cout << "\n建議:\n";
cout << " 檢查電話簿文件路徑是否正確,\n";
cout << " 檢查文件名是否正確.\n";
cout << "Press Enter to continue \n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
case 2:
cout << "\n建議:\n";
cout << " 返回主菜單,選擇清空電話簿,或者直接刪除文件PhoneBook.dat.\n";
cout << "Press Enter to contiune\n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
case 3:
cout << "\n建議:\n";
cout << " 直接選擇主菜單各個(gè)功能號(hào)就OK.\n";
cout << "Press Enter to contiune\n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
case 4:
cout << "\n回答:\n";
cout << " 我的電話:025-85047627\n";
cout << " 我的地址:東大浦口校區(qū)090021信箱\n";
cout << " 我的E-mail: \n";
cout << " straydog@126.com\n";
cout << " guopp126@126.com\n";
cout << "Press Enter to contiune\n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
default:
cout << "Press Enter To exit";
cout << "Press Enter to contiune\n";
break;
}
}
// 用戶操作確認(rèn)
int verifyDelete()
{
char YesNo;
cout << "\nAre you sure (Y/N) ";
cin >> YesNo;
if((YesNo == 'Y') || (YesNo == 'y'))
{
return(1);
}
else
{
return(0);
}
}
// 刪除結(jié)點(diǎn)
void deleteNode(record *previous)
{
if(current == head)
{
deleteHead();
}
else
{
if(current->next == NULL)
{
deleteEnd(previous);
}
else
{
deleteMiddle(previous);
}
}
}
// 刪除頭結(jié)點(diǎn)
void deleteHead()
{
current = head;
if(head->next != NULL)
{
head = current->next;
}
else
{
head = NULL;
}
delete current;
}
// 刪除尾結(jié)點(diǎn)
void deleteEnd(record *previous)
{
delete current;
previous->next = NULL;
current = head;
}
// 刪除鏈表中間結(jié)點(diǎn)
void deleteMiddle(record *previous)
{
previous->next = current->next;
delete current;
current = head;
}
// 刪除鏈表
void deleteList()
{
record *temp;
current = head;
do
{
temp = current->next;
delete current;
current = temp;
}
while(temp != NULL);
head = NULL;
}
// 把鏈表信息寫入文件存盤
void wiriteToFile()
{
ofstream outfile;
outfile.open("PhoneBook.dat",ios::out);
if (outfile)
{
current = head;
if(head != NULL)
{
do
{
outfile << current->lastName << endl;
outfile << current->firstName << endl;
outfile << current->phoneNum << endl;
outfile << current->mobile << endl;
current = current->next;
}
while(current != NULL);
}
outfile << "End" << endl;
outfile.close();
}
else
{
cout << "打開文件錯(cuò)誤!!\n";
}
}
// 從數(shù)據(jù)文件PhoneBook.dat中讀取數(shù)據(jù)重建鏈表處理函數(shù)
void loadFromFile()
{
record *newRecord;
ifstream infile;
int loop = 0;
infile.open("PhoneBook.dat",ios::in);
if (infile)
{
do
{
newRecord = new record;
if(newRecord != NULL)
{
infile.get(newRecord->lastName,20);
infile.ignore(20,'\n');
if((strcmp(newRecord->lastName, "") != 0) && (strcmp(newRecord->lastName, "End") != 0))
{
infile.get(newRecord->firstName, 15);
infile.ignore(20,'\n');
infile.get(newRecord->phoneNum, 15);
infile.ignore(20,'\n');
infile.get(newRecord->mobile, 15);
insertNode(newRecord);
}
else
{
delete newRecord;
loop = 1;
}
}
else
{
cout << "警告: 存儲(chǔ)器錯(cuò)誤, 從磁盤加載文件失敗!!\n";
loop = 1;
}
}
while(loop == 0);
infile.close();
}
else
{
cout << "電話簿為空!!\n";
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -