生活随笔
收集整理的這篇文章主要介紹了
编写一个通信录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目要求:
編寫一個關于通訊錄的程序
1. 可輸入姓名、電話(可多個)、通訊地址、Email等必要的信息;
2. 可根據姓名查詢電話等信息(最好還能根據電話查詢姓名),并顯示在屏幕上;
3. 要求通訊錄保存成文件,如張三.txt,用文件流的方式實現;
4. 最好用面向對象方法編寫。
開發環境:
QT Creator
但是建立的是純C++工程
#ifndef PERSON_H
#define PERSON_H
#include <vector>
#include <string>
#include <iostream>
#include <map>using namespace std;
class Person
{
public:
void setPerson(
string name,
vector<string> phone,
string address,
string email);
string getName();
vector<string> getPhone();
string getAddress();
string getEmail();
private:
string m_name;
vector<string> m_phone;
string m_address;
string m_email;
};
#endif
#include "person.h"void Person::setPerson(
string name,
vector<string> phone,
string address,
string email)
{m_name = name;m_phone = phone;m_address = address;m_email = email;
}
string Person::getName()
{
return m_name;
}
vector<string> Person::getPhone()
{
return m_phone;
}
string Person::getAddress()
{
return m_address;
}
string Person::getEmail()
{
return m_email;
}
#ifndef DIRECT_H
#define DIRECT_H#include "person.h"class Direct
{
public:
int menu();
void addperson(Person* person);
void browse();
void findperson();
void deleteperson();
};
#endif // DIRECT_H
#include "direct.h"
#include <stdlib.h>
#include <fstream>
#include <iostream>using namespace std;
int Direct::menu()
{
cout<<
"***************************************************************"<<endl;
cout<<
"*********************** 功能菜單:*****************************"<<endl;
cout<<
"********************* 1.新建聯系人 ****************************"<<endl;
cout<<
"********************* 2.刪除聯系人 ****************************"<<endl;
cout<<
"********************* 3.查找聯系人 ****************************"<<endl;
cout<<
"********************* 4.瀏覽通信錄 ****************************"<<endl;
cout<<
"********************* 5.退出通信錄 ****************************"<<endl;
cout<<
"***************************************************************"<<endl;
cout<<
"******************* 請選擇您要進行的操作:*********************"<<endl;
int order =
0;
cin>>order;
return order;
}
void Direct::addperson(Person* person)
{person =
new Person();
string name,number,address,email;
vector<string> phone;
cout<<
"請輸入姓名:";
cin>>name;
cout<<
"您要存幾條電話號碼: ";
int count;
cin>>count;
for(
int i=
1;i<=count;i++){
cout<<
"請輸入電話號碼"<<i<<
": ";
cin>>number;phone.push_back(number);}
cout<<
"請輸入地址:";
cin>>address;
cout<<
"請輸入郵箱:";
cin>>email;person->setPerson(name,phone,address,email);ofstream outfile;outfile.open(
"通信錄.txt",ios::app);
if(!outfile){
cout<<
"通信錄.txt can't open. "<<endl;
abort();}
else{outfile<<
"姓名: "<<person->getName()<<
" ";
for(
int i=
0;i<person->getPhone().size();i++){outfile<<
"電話號碼"<<i+
1<<
": "<<person->getPhone().at(i)<<
" ";}outfile<<
"地址: "<<person->getAddress()<<
" ";outfile<<
"Email: "<<person->getEmail()<<endl;outfile.close();}
}
void Direct::browse()
{ifstream infile;infile.open(
"通信錄.txt");
if(!infile){
cout<<
"通信錄.txt can't open. "<<endl;
abort();}
else{
char people[
100];
while (!infile.eof()){infile.getline(people,
100);
cout<<people<<endl;}}infile.close();
}
void Direct::findperson()
{ifstream infile;infile.open(
"通信錄.txt");
if(!infile){
cout<<
"通信錄.txt can't open. "<<endl;
abort();}
else{
cout<<
"請輸入要查找人的信息(姓名、電話、住址或者郵箱): "<<endl;
string str_find;
cin>>str_find;
char people[
100];
int i=
0;
while(!infile.eof()){infile.getline(people,
100);
string str_people = people;size_t n = str_people.find(str_find,
0);
int N = (
int)n;
if(N>=
0){
cout<<str_people<<endl;++i;}}
if(
0==i){
cout<<
"查無此人......"<<endl;}}
}
void Direct::deleteperson()
{ifstream infile;infile.open(
"通信錄.txt");
if(!infile){
cout<<
"通信錄.txt can't open. "<<endl;
abort();}
else{
cout<<
"請輸入要刪除人的信息(姓名、電話、住址或者郵箱): "<<endl;
string str_find;
cin>>str_find;
char people[
100];
int i=
0;
vector<string> str_people2;
vector<string> str_people3;
string s =
"N";
while(!infile.eof()){infile.getline(people,
100);str_people3.push_back(people);
string str_people1 = people;size_t n = str_people1.find(str_find,
0);
int N = (
int)n;
if(N>=
0){
cout<<
"是否要刪除此人信息?Y/N"<<endl;
cout<<str_people1<<endl;
cin>>s;++i;}
else if(N==-
1&&people!=
"\n"){
if(people!=
"0"){str_people2.push_back(people);}}}ofstream ofile(
"通信錄.txt",ios::trunc);
string str;
if(s ==
"Y"){ofstream outfile;outfile.open(
"通信錄.txt",ios::app);
for(
int i=
0;i<str_people2.size()-
1;i++){str = str_people2.at(i);outfile<<str<<endl;}str_people2.clear();outfile.close();}
else if(s ==
"N"){ofstream outfile;outfile.open(
"通信錄.txt",ios::app);
for(
int i=
0;i<str_people3.size()-
1;i++){str = str_people3.at(i);outfile<<str<<endl;}str_people3.clear();outfile.close();}
else{
cout<<
"輸入有誤......"<<endl;}ofile.close();}
}
#include "direct.h"int main()
{Direct* direct =
new Direct();Person* person;
int userOrder =
0;
while(userOrder!=
5){userOrder = direct->menu();
switch(userOrder){
case 1:
cout<<
"********************** 新建聯系人 *****************************"<<endl;direct->addperson(person);
break;
case 2:
cout<<
"********************** 刪除聯系人 *****************************"<<endl;direct->deleteperson();
break;
case 3:
cout<<
"********************** 查找聯系人 *****************************"<<endl;direct->findperson();
break;
case 4:
cout<<
"********************** 瀏覽通信錄 *****************************"<<endl;direct->browse();
break;
case 5:
cout<<
"********************** 退出通信錄 *****************************"<<endl;
break;
default:
break;}}
return 0;
}
總結
以上是生活随笔為你收集整理的编写一个通信录的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。