- 课程设计
- 新增Win32GUI设计版本
主函数
#include <iostream>
#include "PersonBook.h"
using namespace std;
int menu_select()
{
//显示主菜单
int c;
cout << "*******************************\n";
cout << "* 模拟电话簿 v1.0*\n";
cout << "* *\n";
cout << "* 1. 添加通信录*\n";
cout << "* 2. 删除通信录*\n";
cout << "* 3. 修改通信录*\n";
cout << "* 4. 显示通信录*\n";
cout << "* 5. 电话簿存盘*\n";
cout << "* 6. 读出电话簿*\n";
cout << "* 7. 退出*\n";
cout << "*******************************\n";
cout << "\n 请输入(1~7): ";
do //键盘输入循环
{
cin>>c;
}
while(c<1 || c>7);
return c;
}
void menu()
{
char choice;
PersonBook maillist;
for(;;) //循环,直到键盘输入结束代码
{
try
{
choice = menu_select();
switch(choice) //根据键盘输入,调用相应的功能
{
case 1:
maillist.Enter();
break;
case 2:
maillist.Erase();
break;
case 3:
maillist.Modify();
break;
case 4:
maillist.List();
break;
case 5:
maillist.Save();
break;
case 6:
maillist.Load();
break;
case 7:
throw 7;
}
}
catch(int n)
{
if(n==7)
break;
}
}
}
int main()
{
menu();
return 0;
}
Person.h文件
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class Person
{
private:
string name,gender;
int age;
string phone,address;
public:
Person();
void setName(string name);
void setGender(string gender);
void setAge(int age);
void setPhone(string phone);
void setAddress(string address);
string getName();
string getGender();
int getAge();
string getPhone();
string getAddress();
void show();
virtual ~Person();
};
#endif // PERSON_H
Person.cpp文件
#include <iostream>
#include "Person.h"
using namespace std;
Person::Person()
{
name=gender=phone=address="NULL";
age=-1;
}
void Person::setName(string name)
{
this->name=name;
}
void Person::setGender(string gender)
{
this->gender=gender;
}
void Person::setAge(int age)
{
this->age=age;
}
void Person::setPhone(string phone)
{
this->phone=phone;
}
void Person::setAddress(string address)
{
this->address=address;
}
string Person::getName()
{
return name;
}
string Person::getGender()
{
return gender;
}
int Person::getAge()
{
return age;
}
string Person::getPhone()
{
return phone;
}
string Person::getAddress()
{
return address;
}
void Person::show()
{
cout<<name<<"\t"<<gender<<"\t"<<age<<"\t"<<phone<<"\t"<<address<<endl;
}
Person::~Person()
{
//dtor
}
PersonBook.h文件
#ifndef PERSONBOOK_H
#define PERSONBOOK_H
#include<vector>
#include"Person.h"
const int itemNum = 100;
class PersonBook
{
public:
PersonBook();
void Enter();
void Erase();
void Modify();
void Load();
void Save();
void List();
virtual ~PersonBook();
protected:
private:
Person item[itemNum];
int num;
};
#endif // PERSONBOOK_H
PersonBook.cpp文件
#include "PersonBook.h"
#include<iostream>
#include<vector>
#include<cstring>
#include"Person.h"
#include <iomanip>
#include <fstream>
using namespace std;
PersonBook::PersonBook()
{
num=0;
}
void PersonBook::Enter()
{
string name,gender,phone,address;
int age;
try
{
cout<<" 添加联系人"<<endl;
cout<<" \t请输入姓名:";
cin>>name;
if(name.length()<=0||name.length()>10)
throw 1;
cout<<" \t请输入性别:";
cin>>gender;
/*if(gender!="男"||gender!="女")
throw 2;*/
cout<<" \t请输入年龄:";
cin>>age;
if(age<0||age>150)
throw 3;
cout<<" \t请输入手机号:";
cin>>phone;
if(phone.length()!=11)
throw 4;
cout<<" \t请输入地址:";
cin>>address;
if(address.length()<=0||address.length()>30)
throw 5;
Person p;
p.setName(name);
p.setGender(gender);
p.setAge(age);
p.setPhone(phone);
p.setAddress(address);
item[num]=p;
num++;
cout<<endl;
}
catch(int n)
{
if(n==1)
cout<<" \t姓名长度在1 到 10 之间."<<endl;
else if(n==2)
cout<<" \t性别应输入”男“或”女“."<<endl;
else if(n==3)
cout<<" \t年龄大小在0 到150 之间."<<endl;
else if(n==4)
cout<<" \t手机号为11 位."<<endl;
else if(n==5)
cout<<" \t地址长度在1 到 30 之间."<<endl;
else
cout<<" \t程序运行错误"<<endl;
cout<<"\n \t请再一次输入."<<endl;
Enter();
}
}
void PersonBook::Erase()
{
string name;
try
{
if(num==0)
throw 0;
cout<<" 请输入删除联系人的姓名:";
cin>>name;
if(name.length()<=0||name.length()>10)
throw 1;
else
{
for(int i=0; i<num; i++)
{
if(item[i].getName()==name)
{
item[i].setName("NULL");
item[i].setGender("NULL");
item[i].setPhone("NULL");
item[i].setAddress("NULL");
item[i].setAge(-1);
num--;
cout<<" 删除成功!"<<endl;
cout<<endl;
}
else
{
cout<<" 未找到该联系人!"<<endl;
}
}
}
}
catch(int n)
{
if(n==1)
cout<<" 姓名长度在1 到 10 之间."<<endl;
else if(n==0)
cout<<" 通讯录为空!"<<endl;
else
cout<<" 程序运行错误"<<endl;
cout<<" 请再一次输入."<<endl;
Erase();
}
}
void PersonBook::Modify()
{
string name,gender;
int age;
string phone,address;
try
{
if(num==0)
throw 0;
cout<<" 请输入修改联系人的姓名:";
cin>>name;
cout<<" 请输入修改联系人的性别:";
cin>>gender;
cout<<" 请输入修改联系人的年龄:";
cin>>age;
cout<<" 请输入修改联系人的手机号:";
cin>>phone;
cout<<" 请输入修改联系人的地址:";
cin>>address;
if(name.length()<=0||name.length()>10)
throw 1;
else
{
for(int i=0; i<num; i++)
{
if(item[i].getName()==name)
{
item[i].setName(name);
item[i].setGender(gender);
item[i].setPhone(phone);
item[i].setAddress(address);
item[i].setAge(age);
cout<<" 修改成功!"<<endl;
cout<<endl;
}
else
{
cout<<" 未找到该联系人!"<<endl;
}
}
}
}
catch(int n)
{
if(n==1)
{
cout<<" 姓名长度在1 到 10 之间."<<endl;
cout<<" 请再一次输入."<<endl;
Modify();
}
else if(n==0)
cout<<" 通讯录为空!"<<endl;
else
cout<<" 程序运行错误"<<endl;
}
}
void PersonBook::Load()
{
ifstream file;
int i;
char fname[]="F:\\Files\\codeblockFile\\exp\\book.txt";
/*cout << " 输入读取的文件名: ";
cin >> fname;*/
file.open(fname,ios::in);
if(!file)
{
cout << " 打开文件失败!\n";
cin.get();
}
else
{
num = 0;
for(i=0; i<itemNum; i++)
{
if(file&&!file.eof())
{
file.read(reinterpret_cast<char*>(&item[i]), sizeof(Person) );
}
if(item[i].getName()!="NULL")
{
num++;
}
}
cout<<" 导入成功!\n"<<endl;
file.close();
}
}
void PersonBook::Save()
{
ofstream file;
int i;
char fname[]="F:\\Files\\codeblockFile\\exp\\book.txt";
/*cout << " 输入保存的文件名: ";
cin >> fname;*/
file.open(fname,ios::out);
if(!file)
{
cout << " 打开文件失败!\n";
cin.get();
}
else
{
for(i=0; i<itemNum; i++)
{
if(file&&!file.eof())
file.write(reinterpret_cast<const char*>(&item[i]), sizeof(Person) );
}
file.close();
cout<<" 保存成功!\n"<<endl;
}
}
void PersonBook::List()
{
cout<<" 通讯录"<<endl;
cout<<" 序号\t姓名\t性别\t年龄\t手机号\t\t地址"<<endl;
for(int i=0; i<num; i++)
{
cout<<" "<<i+1<<"\t";
cout<<item[i].getName()<<"\t";
cout<<item[i].getGender()<<"\t";
cout<<item[i].getAge()<<"\t";
cout<<item[i].getPhone()<<"\t";
cout<<item[i].getAddress()<<endl;
}
cout<<"\n";
}
PersonBook::~PersonBook()
{
//dtor
}