1.
本篇为上篇的升级版本,用了二叉树来实现phonebook'的功能,不过还是花费了我不少功夫,因为写比较满意,遂把本篇作为自己数据结构期中的实验报告交了上去,感觉老是对我的报告还是很满意的,O(∩_∩)O哈哈~
怎么说呢,这个程序应该是我从一个只会for,while,if以及只会几十行的小白,有了长足进步的开始吧。这里不得不感谢一下我的数据结构老师了,他对我的帮助真的蛮多的。
2.代码的实现:
第一部分为头文件
#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include <iostream>
#include <string>
using namespace std;
const int ESCAPE=27;
const int RETURN=13;
class Phonebook
{
public:
Phonebook( ) :root( NULL )
{
}
virtual ~Phonebook();
Phonebook(const Phonebook& rhs);
Phonebook& operator=(const Phonebook& rhs);
int func_add();
void func_del();
void func_list() const;
void func_sch() const;
void func_upd();
void func_srt();
void func_clear();
private:
struct phonebook_node
{
int id;
string name;
string surname;
string phone;
phonebook_node* left;
phonebook_node* right;
//注意{}后面不要加 ;
phonebook_node(const int &theid,
phonebook_node* lt,
phonebook_node* rt,
const string &thename,
const string &thesurname,
const string &thephone)
:id(theid),left(lt),right(rt),name(thename),surname(thesurname),phone(thephone)
{
}
};
phonebook_node* root;
void insert(const phonebook_node* adding,phonebook_node* &t)const;
void remove(const int &x,phonebook_node* &t) const;
phonebook_node* findMin(phonebook_node* t) const
{
while(t!=NULL)
{
if(t->left==NULL)
{
return t;
}
else
{
t=t->left;
}
}
return NULL;
}
phonebook_node* contains(const int &x,phonebook_node* t) const
{
while(t!=NULL)
{
if(x<t->id) t=t->left;
else if(t->id<x) t=t->right;
else return t;
}
return NULL;
}
void makeEmpty(phonebook_node* &t) const;
void print(phonebook_node* t) const;
int generate_my_id();
bool get(string& s);
};
#endif // PHONEBOOK_H
第二部分为头文件的具体定义
#include "../include/phonebook.h"
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
Phonebook::~Phonebook()
{
Phonebook::makeEmpty(root);
}
Phonebook::Phonebook(const Phonebook& rhs) : root( NULL )
{
*this=rhs;
}
Phonebook& Phonebook::operator=(const Phonebook& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
root=rhs.root;
makeEmpty(root);
return *this;
}
int Phonebook::func_add()
{
phonebook_node* add=new phonebook_node(1,NULL,NULL,"","","");
char yes_no;
system("CLS");//调用系统命令 清除屏幕
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//调整控制台上的文字属性
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t ADD A PERSON"<<endl;
cout<<"******************************************************************"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);//调整文字属性
cout<<"Id..............................: ";
(*add).id=generate_my_id(); // random id
cout<<(*add).id<<endl;
cout<<"Name............................: ";
if(!get((*add).name))
{
return -1;
}
cout<<"Surname.........................: ";
cin>>(*add).surname;
cout<<"Phone...........................: ";
cin>>(*add).phone;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Do you want to save this person ? (Y)es-(N)o : ";
cin>>yes_no;
if(yes_no=='Y' || yes_no=='y') {
//此时需要真正存储到文件中, 而且文件需要一定的结构.
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is saved !!!"<<endl;
insert(add,root);
}
else {
//选择不保存
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is not saved !!!"<<endl; // delete the record
delete add;
}
return 1;
}
int Phonebook::generate_my_id()
{
srand(static_cast<int>(time(0)));
return rand()%2000+1500;
}
void Phonebook::insert(const phonebook_node* adding,phonebook_node* &t)const
{
if(t==NULL)
t=new phonebook_node((*adding).id,NULL,NULL,(*adding).name,(*adding).surname,(*adding).phone);
else if(adding->id<t->id) insert(adding,t->left);
else if(adding->id>t->id) insert(adding,t->right);
else
;
}
void Phonebook::func_del() {
int sch_id;
char yes_no=' ';
system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t DELETE A PERSON"<<endl;
cout<<"******************************************************************"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Please enter a id : ";
cin>>sch_id;
phonebook_node* p=contains(sch_id,root);
if(p!=NULL)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is found !!!"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout<<"Id..............................: "<<(*p).id<<endl;
cout<<"Name............................: "<<(*p).name<<endl;
cout<<"Surname.........................: "<<(*p).surname<<endl;
cout<<"Phone...........................: "<<(*p).phone<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Do you want to delete this person (Y)es-(N)o : ";
cin>>yes_no;
if(yes_no=='Y' || yes_no=='y') {
remove(sch_id,root);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is deleted !!!"<<endl;
}
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is not found !!!"<<endl;
}
}
void Phonebook::print(phonebook_node* t)const
{
if(t==NULL)
;
else
{
print(t->left);//当t指向左右指针是t-> 而当t指向的是成员时用(*t)->
cout<<"Id...:"<<(*t).id<<"\t"<<(*t).name<<"\t\t"<<(*t).surname
<<"\t\t"<<(*t).phone<<endl;
print(t->right);
}
}
void Phonebook::func_list()const
{
system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t LIST ALL PERSON"<<endl;
cout<<"******************************************************************"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
print(root);
if(root==NULL)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"There is no any record!!!"<<endl;
}
}
void Phonebook::func_sch()const
{
int sch_id;
system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t SEARCH A PERSON"<<endl;
cout<<"******************************************************************"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Please enter a id : ";
cin>>sch_id;
phonebook_node* p=contains(sch_id,root);
if(p!=NULL) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is found !!!"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout<<"Id..............................: "<<(*p).id<<endl;;
cout<<"Name............................: "<<(*p).name<<endl;
cout<<"Surname.........................: "<<(*p).surname<<endl;
cout<<"Phone...........................: "<<(*p).phone<<endl;
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is not found !!!"<<endl;
}
}
void Phonebook::remove(const int &x,phonebook_node* &t)const
{
if(t==NULL)
return;
if(x<t->id) remove(x,t->left);
else if(t->id<x) remove(x,t->right);
else if(t->left!=NULL&&t->right!=NULL)
{
t->id=findMin(t->right)->id;
remove(t->id,t->right);
}
else
{
phonebook_node* OldNode=t;
t=(t->left!=NULL)?t->left:t->right;
delete OldNode;
}
}
void Phonebook::func_upd()
{
int sch_id;
char upd_line;
system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t UPDATE A PERSON"<<endl;
cout<<"******************************************************************"<<endl;
print(root);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Please enter a id : ";
cin>>sch_id;
phonebook_node* p=contains(sch_id,root);
if(p!=NULL)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is found !!!"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout<<" Id..............................: "<<(*p).id<<endl; // id
cout<<"1-) Name............................: "<<(*p).name<<endl;
cout<<"2-) Surname.........................: "<<(*p).surname<<endl;
cout<<"3-) Phone...........................: "<<(*p).phone<<endl;
tryagain:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Which line you want to change 1-3 : ";
cin>>upd_line;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
switch(upd_line) {
case '1' : {
cout<<"Please enter new name : ";
cin>>(*p).name;
break;
}
case '2' : {
cout<<"Please enter new surname : ";
cin>>(*p).surname;
break;
}
case '3' : {
cout<<"Please enter new phone number : ";
cin>>(*p).phone;
break;
}
default : {
goto tryagain; // out of range
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is updated !!!"<<endl;
}
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Person is not found !!!"<<endl;
}
}
void Phonebook::func_srt()
{
system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t SORT ALL PERSON"<<endl;
cout<<"******************************************************************"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
print(root);
if(root==NULL) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"There is no any record !!!"<<endl;
}
}
void Phonebook::makeEmpty(phonebook_node* &t)const
{
if(t!=NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
void Phonebook::func_clear()
{
char yes_no;
string sch_name;
system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t CLEAR THE PHONEBOOK"<<endl;
cout<<"******************************************************************"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Are you sure to clear the phonebook?(Y)es-(N)o ";
cin>>yes_no;
if(yes_no=='Y' || yes_no=='y') {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"The phonebook is deleted !!!"<<endl;
makeEmpty(root);
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"Phonebook is not deleted !!!"<<endl;
}
}
bool Phonebook::get(string & s)
{
char c;
s="";
while (true)
{
c=getch();
if (c==ESCAPE)
{
s="(input aborted)";
cout << endl;
return false;
}
if (c==RETURN)
{
cout << endl;
return true;
}
cout.put(c) << flush;
s+=c;
}
}
第三部分为main函数
#include <iostream>
#include <string>
#include <windows.h>
#include <time.h>
#include <fstream>
#include <conio.h>
#include "include/phonebook.h"
using namespace std;
int main() {
Phonebook PhoneBook;
ifstream infile("phonebook.txt");
if( ! infile ) {
cerr << "error: unable to open input file: phone.txt" << endl;
return -1;
}
start:
system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"******************************************************************"<<endl;
cout<<"\t\t\t PHONEBOOK MAIN MENU"<<endl;
cout<<"******************************************************************"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Please Select Action 1 to 8"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout<<"1-) ADD a person to phonebook"<<endl;
cout<<"2-) DELETE a person from phonebook"<<endl;
cout<<"3-) LIST data in phonebook"<<endl;
cout<<"4-) SEARCH person from phonebook"<<endl;
cout<<"5-) UPDATE a person from phonebook"<<endl;
cout<<"6-) SORT date in phonebook"<<endl;
cout<<"7-) CLEAR the phonebook"<<endl;
cout<<"8-) QUIT the program"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout<<"Choice >> ";
char choice;
cin>>choice;
switch(choice) {
case '1' : {
if(PhoneBook.func_add()==-1)
{
goto start;
}else{
break;
}
}
case '2': {
PhoneBook.func_del(); break;
}
case '3' : {
PhoneBook.func_list(); break;
}
case '4' : {
PhoneBook.func_sch(); break;
}
case '5' : {
PhoneBook.func_upd(); break;
}
case '6' : {
PhoneBook.func_srt(); break;
case '7' : {PhoneBook.func_clear(); break;}
}
case '8' : {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout<<"All record will be lost !!! Are you sure want to exit ? (Y)es-(N)o : ";
char yes_no;
cin>>yes_no;
if(yes_no=='Y' || yes_no=='y') {
return 0;
}
else {
goto start;
}
}
default : {
goto start;
}
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
system("PAUSE");
goto start;
} // main fonksiyon