首页 > 代码库 > C++学习之电话薄功能(类的简单应用)

C++学习之电话薄功能(类的简单应用)

本篇我们讨论一个小程序:实现电话薄简单功能。(一条语句最好一个作用)

问题描述 :

a) : 实现电话添加记录功能;(电话重复的视为同一人,不予增添);即电话号码唯一。

b) : 删除记录功能;(实现提醒功能:确认是否要真的删除)

c) : 查询记录功能;(按手机号码查询);

待优化功能:

1) :每次启动程序都为空。故可以先从文件读取若干项至vector中;readfile功能。

2) :没有提供修改记录的功能;比如某人的电话号码或者住址改了。(先按照号码查询,然后按要求修改之即可。);

3) :每次需将改动的记录写回文件。writetofile();

4) : 每次add、search、delete都必须遍历容器vector。是否有方法改进?

 

头文件:

 1 //一条语句最多一个作用 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <fstream> 9 #include <sstream>10 #include <stdexcept>11 using namespace std;

类:

 1 class Item 2 { 3     public: 4         Item() 5             :name_(""), num_(0), address_("") 6         {} 7         Item(string name, int num,  string address) 8             :name_(name), num_(num), address_(address) 9         {}10 11         void get(string name, int num, string address)12         {13             name_ = name ;14             num_ = num ;15             address_ = address ;16         }17     18         string getName()const19         {20             return name_ ;21         }22 23         int getNum()const24         {25             return num_;26         }27 28         string getAddress()const29         {30             return address_ ;31         }32         33         void showitem()34         {35             cout<< "姓名: " << name_ << endl;36             cout<< "电话号码: "<<num_ << endl ;37             cout <<"住址: "<< address_ << endl ;38         }39             40     private:41         string name_ ;42         int num_ ;43         string address_ ;44 };

3个全局 函数声明 及其 实现功能:

1 void addItem( vector<Item> &vec);//实现连续添加(如果已存在,则略过)。2 void searchItem( vector<Item> &vec);//实现按号码查询。3 void delItem( vector<Item> &vec);//实现删除。


main函数:

功能:提供一个简单界面。不同数字实现不同功能。

 1 int main(int argc, const char *argv[]) 2 { 3     vector<Item> vec ; 4     int tmp ; 5     int cnt = 0 ;//标记输入错误总次数 6     while(1) 7     { 8         system("clear"); 9         cout<< "1、添加 2、查询 3、删除 0、退出" << endl ;10         cin >> tmp ;11         switch(tmp)12         {13             case 1: 14                 addItem(vec); 15                 break ;16             case 2: 17                 searchItem(vec);18                 break ;19             case 3:20                 delItem(vec);21                 break ;22             case 0: 23                 exit(0); 24             default:25                 if(cnt < 5)//输入错误五次,则退出程序26                 {27                     cout << "the number of your input is illegal, please input once more:"<< endl; 28                     cnt++ ;29                     cin >> tmp ;30                 }else31                     exit(0);32                 break; 33         }34     }35     return 0;36 }


additem:

 1 void addItem( vector<Item> &vec) 2 { 3     char flag = y; 4     string name, address ; 5     int num ; 6     Item person ;  7     while(flag == y) 8     { 9        // system("clear");10         cout << "please input name:" << endl ;11         cin >> name ;12         cout << "please input telephone number:" << endl ;13         cin >> num ;14 15         cout <<"please input address:" << endl ;16         cin >> address ;17         18         int tmp = 0 ;//标记查找成功没有19         for(vector<Item>::iterator it = vec.begin();20             it != vec.end();21             ++it)22         {23             if(num == it->getNum())//已经存在该item了。24                 {25                     cout << "the num is already exist!" << endl ;26                     tmp = 1 ;27                     break ;28                 }29         }30         if(tmp == 1)//如果已经存在,则不添加。31             goto done;32         person.get(name, num, address);33         vec.push_back(person);34                 35         person.showitem();36         cout << "add success!" << endl ;37 38         done:39         cout << "continue to add item or not?(y/n)";40         41         cin >> flag ; // 只需要输入即可,while自己会判断条件。42     }43 }


searchitem:

 1 void searchItem( vector<Item> &vec) 2 { 3     char flag = y; 4     int num ; 5     while(flag == y) 6     { 7         cout << "please input search number:" << endl ; 8         cin  >> num ; 9         10         vector<Item>::iterator it = vec.begin();11         while(it != vec.end())12         {13             if(num == it->getNum()) 14             {15                 cout << "search success!" << endl ;16                 it->showitem();17                 break ;18             }19             it++ ;20         }21         if(it == vec.end())22             cout << "search failure!" << endl ;23         cout << "continue to search?(y/n)";24         cin >> flag ;25     }26 }


deleteitem:

 1 void delItem( vector<Item> &vec) 2 { 3     int num ; 4     char flag =y; 5     while(flag == y) 6     { 7         cout << "input the number that you want to delete:" << endl ; 8         cin >> num ; 9         10         vector<Item>::iterator it = vec.begin();11         while(it != vec.end())12         {13             if(num == it->getNum()) 14             {15                 it->showitem();16                 string tmp  ;17                 cout << "Do you really want to delete this item?(yes/no)" << endl ;18                 19                 cin >> tmp ;20                 if(tmp == "yes")//是否确定要删除该item21                 {   22                     it = vec.erase(it);23                     cout << "delete success!" << endl ;24                 }25                 break ;26             }27             it ++ ;28         }29         if(it == vec.end())30         {31             cout << "The number is not exist!" << endl ;      32         }33         cout<< "continue to delete?(y/n)" << endl ;34         35         cin >> flag ;36     }37 }


 

C++学习之电话薄功能(类的简单应用)