日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

编写一个通信录

發布時間:2024/3/24 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写一个通信录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目要求:
編寫一個關于通訊錄的程序
1. 可輸入姓名、電話(可多個)、通訊地址、Email等必要的信息;
2. 可根據姓名查詢電話等信息(最好還能根據電話查詢姓名),并顯示在屏幕上;
3. 要求通訊錄保存成文件,如張三.txt,用文件流的方式實現;
4. 最好用面向對象方法編寫。

開發環境:
QT Creator
但是建立的是純C++工程

//person.h 人 類 #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 // PERSON_H //person.cpp #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; } //direct.h 通信錄類頭文件 #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 //direct.cpp #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++)//此處size-1是由于getline()會多讀一行空行,使容器內多壓入一行空行{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++)//此處size-1是由于getline()會多讀一行空行,使容器內多壓入一行空行{str = str_people3.at(i);outfile<<str<<endl;}str_people3.clear();outfile.close();}else{cout<<"輸入有誤......"<<endl;}ofile.close();} } //main.cpp #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; }

總結

以上是生活随笔為你收集整理的编写一个通信录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。