C++实现简单电话通讯录
生活随笔
收集整理的這篇文章主要介紹了
C++实现简单电话通讯录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 課程設計
- 新增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_HPerson.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_HPersonBook.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;elsecout<<" \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;elsecout<<" 程序運行錯誤"<<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;elsecout<<" 程序運行錯誤"<<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 }總結
以上是生活随笔為你收集整理的C++实现简单电话通讯录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于android小区智能管理,基于An
- 下一篇: 瀚高数据库块恢复示例