//项目一:电话簿
//有个父类Book,有成员变量:书本的名字,书本的路径,有4个纯虚函数,分别是增删查改。类PhoneBook继承了类Book,并扩展了电话号码,姓名,扩展了登陆接口,实现了父类的4个纯虚函数。数据信息使用文本的方式存储!
#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <time.h>
class Book
{
private:
string _bookname;//书名
string _bookpath;//当前文件的路径
public:
void setbookname(string name)
{
_bookname = name;
}
string getbookname()
{
return _bookname;
}
void setpath(string path)
{
_bookpath = path;
}
string path()
{
return _bookpath;
}
//定义四个增删查改的纯虚函数
virtual void add() = 0;
virtual void del(string &name) = 0;
virtual void sel(string &name) = 0;
virtual void alt(string &name) = 0;
};
class PhoneBook:public Book
{
private:
string _phoneNumber;//电话号码
string _name;//姓名
public:
PhoneBook(){};
void setphonenumber(string phonenumber)
{
_phoneNumber=phonenumber;
}
string getphonenumber()
{
return _phoneNumber;
}
void setname(string name)
{
_name=name;
}
string getname()
{
return _name;
}
void add(){};
void del(string &name){};
void sel(string &name){};
void alt(string &name){};
};
typedef struct PB
{
string name;
string tel;
string bookname;
}PB;
PB phonebook[20];
int num=0;
class ManagePhoneBook:public PhoneBook
{
public:
int total;//总记录数
vector<PhoneBook> PhoneBook;
//添加
void add()
{
char f='y';
string sTemp;
ofstream onfile;//写入文件类
//onfile.open("phonebook.txt",ios::out|ios::app);
onfile.open("phonebook.txt");
do {
cout<<"输入姓名:";
cin>>sTemp;
phonebook[num].name = sTemp;
onfile<<sTemp<<" ";
cout<<"输入电话号码:";
cin>>sTemp;
phonebook[num].tel = sTemp;
onfile<<sTemp<<" ";
cout<<"输入喜欢的书籍:";
cin>>sTemp;
phonebook[num].bookname = sTemp;
onfile<<sTemp<<" "<<"\n";
num++;
cout<<"还要输出吗?y/n"<<endl;
cin>>f;
} while (f=='y');
onfile.close();
}
//根据姓名删除
void del(string &name,int &num1)
{
ofstream onfile;//写入文件类
onfile.open("phonebook.txt");
int i;
int j=0;
for (i=0; i<num1; i++) {
if (phonebook[i].name == name) {
for (j=i; j<num1-1; j++) {
phonebook[j]=phonebook[j+1];
}
num1--;
i--;
cout<<"删除成功!"<<endl;
}
}
//重写到文件
for (int i=0; i<num1; i++) {
onfile<<phonebook[i].name<<" "<<phonebook[i].tel<<" "<<phonebook[i].bookname<<"\n";
}
onfile.close();
}
//根据姓名查询
void sel(string &name,int &num1)
{
int i;
int j=0;
cout<<"------------------------------------\n";
cout<<" 姓名 电话 喜欢的书籍 \n";
cout<<"------------------------------------\n";
for (i=0; i<num1; i++) {
if (phonebook[i].name == name) {
cout<<phonebook[i].name<<" "<<phonebook[i].tel<<" "<<phonebook[i].bookname<<endl;
}
}
}
//根据姓名修改
void alt(string &name,int &num1)
{
string s;
for (int i=0; i<num1; i++) {
if (phonebook[i].name == name) {
cout<<"输入姓名:";
cin>>s;
phonebook[i].name=s;
cout<<"输入电话:";
cin>>s;
phonebook[i].tel = s;
cout<<"输入书名:";
cin>>s;
phonebook[i].bookname = s;
}
break;
}
ofstream onfile;//写入文件类
onfile.open("phonebook.txt");
//重写到文件
for (int i=0; i<num1; i++) {
onfile<<phonebook[i].name<<" "<<phonebook[i].tel<<" "<<phonebook[i].bookname<<"\n";
}
onfile.close();
}
};
//char password[20];//密码
/*******************函数申明******************************************/
void set_psw();//设置密码
int psw_check();//密码验证
void Show_menu();//显示菜单
int get_menu_choice();//接受菜单选择
void InputIntoFile(vector<PhoneBook> PhoneBook);//将vector写入文件
void PrintTitle();//打印头信息
void menu();//菜单相应
/********************函数定义*****************************************/
//菜单相应
void menu(ManagePhoneBook &mphonebook)
{
while(1)
{
Show_menu();//显示菜单
switch (get_menu_choice()) {
case 1://增加记录
mphonebook.add();
break;
case 2://删除记录
{
string s;
cout<<"输入要删除的学生姓名:";
cin>>s;
mphonebook.del(s,num);
}
break;
case 3://查询记录
{
string ss;
cout<<"输入要查询的学生姓名:";
cin>>ss;
mphonebook.sel(ss,num);
}
break;
case 4://修改记录
{
string sss;
cout<<"输入要修改的学生姓名:";
cin>>sss;
mphonebook.alt(sss,num);
}
break;
case 5://查询所有记录
{
cout<<"------------------------------------\n";
cout<<" 姓名 电话 喜欢的书籍 \n";
cout<<"------------------------------------\n";
for (int i=0; i<num; i++) {
cout<<phonebook[i].name<<" "<<phonebook[i].tel<<" "<<phonebook[i].bookname<<endl;
}
}
break;
default:
printf("*********************\n");
printf(" 欢迎使用本系统 \n");
printf("**********************\n");
break;
}
}
}
//打印头信息
void PrintTitle()
{
cout<<"------------------------------------\n";
cout<<" 姓名 电话 喜欢的书籍 \n";
cout<<"------------------------------------\n";
}
//将结构体写入文件
void InputIntoFile(PB phonebook[20])
{
ofstream onfile;//写入文件类
onfile.open("phonebook.txt");
for (int i=0; i<num; i++) {
onfile<<phonebook[i].name<<" "<<phonebook[i].tel<<" "<<phonebook[i].bookname;
}
}
//设置密码
void set_psw()
{
ofstream onfile;//写入文件类
string psw_set ,psw_check;
do
{
cout<<"You must set password first!\n";
cin>>psw_set;
cout<<"---------------------------\n";
cout<<"conform password:";
cin>>psw_check;
if(psw_check == psw_check)
{
cout<<"set password success!\n";
}
else
{
cout<<"error!\n";
}
}
while(psw_check != psw_set);
onfile.open("password.txt",ios::out);
onfile<<psw_set;
onfile.close();
}
//密码验证
int psw_check()
{
ifstream infile;//读取文件类
ofstream onfile;//文件写入类
unsigned int j=1;
string pword;
string password;
infile.open("password.txt",ios::in);
if (!infile.is_open())//如果打开失败,则重新设置密码
{
set_psw();//重新设置密码
}
//取值
infile>>password;
//打开成功验证密码
do {
cout<<"输入密码,你将有三次机会:"<<j<<"/3次机会"<<endl;
cin>>pword;
j++;
} while (password != pword && j<=3);
if (j<=3) {
cout<<"恭喜登陆成功!"<<endl;
return 1;//打开成功
}
else
{
cout<<"你已经输入了三次了,打开文件失败!"<<endl;
return 0;
}
}
//显示菜单
void Show_menu()
{
cout<<">>>>>>>>>>>>>>>>>>>欢迎使用电话簿<<<<<<<<<<<<<<<<<<<<<<<\n";
cout<<"******************************************************\n";
cout<<" 1.增加记录 | 2.删除记录 \n";
cout<<" 3.查询单个记录 | 4.修改记录 \n";
cout<<" 5.查询所有记录 | 0.退出 \n";
cout<<"******************************************************\n";
}
//接受菜单选择
int get_menu_choice()
{
int menu_ch;//菜单选项
do {
cout<<"请选择菜单:";
cin>>menu_ch;
if (menu_ch<0||menu_ch>5) {
cout<<"error";
}
} while (menu_ch<0||menu_ch>5);
return menu_ch;
}
int main(int argc, const char * argv[])
{
ManagePhoneBook m;
if (psw_check()) {
menu(m);
}
return 0;
}
本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366458,如需转载请自行联系原作者