c++电话本程序

来源:互联网 发布:单片机步进电机 编辑:程序博客网 时间:2024/06/01 17:10

电话本的功能在menu菜单里完全体现了:

cout <<"这是一个电话本程序,可以执行以下操作:" <<endl;

         cout<< "1->搜索一个电话" <<endl;

         cout<< "2->增加一个电话" <<endl;

         cout<< "3->删除一个电话" <<endl;

         cout<< "4->查看电话本"<< endl;

         cout<< "5->将文件追加到“电话本.txt”后面"<< endl;

         cout<< "6->将文件读到内存并且输出到命令行" <<endl;

         cout<< "7->清屏"<< endl;

         cout<< "0->退出"<< endl;

 

 

 


 

  1. #include"iostream"
  2. #include"cstdlib"
  3. #include"string"
  4. #include"fstream"
  5. #include"utility"
  6. #include"vector"
  7. #include"windows.h"
  8. using namespacestd;
  9.  
  10. class number
  11. {
  12. private:
  13.        string name;
  14.        string num;
  15. public:
  16.        void search(string);
  17.        void add();
  18.        void del(string);
  19.        void show(string);
  20.        void copy();
  21.        friend ostream &operator<<(ostream &output,const number &p)
  22.        {
  23.                output << p.name <<endl;
  24.                output << p.num <<endl;
  25.                return output;
  26.        }
  27.        
  28.        void input()
  29.        {
  30.                 cout << "请输入姓名:" <<endl;
  31.                cin>> name;
  32.                 cout << "请输入电话:" <<endl;
  33.                cin>> num;
  34.        }
  35.        number(string name1=" ",string num1="")
  36.        {
  37.                this->name = name1;
  38.                this->num = num1;
  39.        }
  40.        ~number()
  41.        {}
  42. };
  43.  
  44. voidnumber::search(string s)
  45. {
  46.         ifstreamfin("电话本.txt");
  47.        if(!fin)
  48.                 cout << "文件打开失败,请重新运行" <<endl;
  49.        else
  50.        {
  51.                string s1;
  52.                bool flag = 1;
  53.                while (flag)
  54.                {
  55.                        if (!getline(fin, s1))
  56.                               break;
  57.                        if (s1 == s)
  58.                               flag = 0;
  59.                }
  60.                getline(fin, s1);
  61.                if(!flag)
  62.                         cout << s <<"的电话号码为:" << s1<< endl;
  63.                else
  64.                         cout <<"文件中没有记录" <<endl;
  65.                fin.close();
  66.        }
  67. }
  68.  
  69. voidnumber::add()
  70. {
  71.         ofstreamfout("电话本.txt",ios::app);
  72.        if(!fout)
  73.        {
  74.                 cout << "文件还未被创建,现在创建……" <<endl;
  75.                FILE *f;
  76.                 if ((f = fopen("电话本.txt","a+")) != NULL)
  77.                {
  78.                        fclose(f);
  79.                         ofstreamfout("电话本.txt",ios::in);
  80.                }
  81.                else
  82.                         cout <<"文件打开失败,请重新运行" <<endl;
  83.        }
  84.        fout << *this;
  85.        fout.close();
  86. }
  87.  
  88. voidnumber::copy()
  89. {
  90.        string str;
  91.         cout <<"请输入需要读取的文件名:" <<endl;
  92.        cin>> str;
  93.        vector> tmp;
  94.        string tmpp, tmppp;
  95.        ifstream fin(str.c_str());
  96.        while (fin >> tmpp >>tmppp)
  97.                tmp.push_back(make_pair(tmpp,tmppp));
  98.        fin.close();
  99.         ofstreamfout("电话本.txt",ios::app);
  100.        for(vector >::iterator iter = tmp.begin(); iter != tmp.end();iter++)
  101.                fout << iter->first <<endl << iter->second << endl;
  102.        fout.close();
  103.        
  104. }
  105.  
  106. void number::del(strings)
  107. {
  108.        vector > tmp;
  109.        string tmpp, tmppp;
  110.         ifstreamfin("电话本.txt");
  111.        while (fin >> tmpp >> tmppp){
  112.                if(tmpp == s)
  113.                        continue;
  114.                tmp.push_back(make_pair(tmpp,tmppp));
  115.        }
  116.        fin.close();
  117.         ofstreamfout("电话本.txt");
  118.        for(vector >::iterator iter = tmp.begin(); iter != tmp.end();iter++)
  119.                fout << iter->first <<endl << iter->second << endl;
  120.        fout.close();
  121.        
  122. }
  123.  
  124. void number::show(stringstr)
  125. {
  126.        ifstream fin(str.c_str());
  127.        if(!fin)
  128.                 cout << "文件打开失败,请重新运行" <<endl;
  129.        else
  130.        {
  131.                string s;
  132.                while (getline(fin, s))
  133.                {
  134.                        if (s != " ")
  135.                               cout << s <<endl;
  136.                }
  137.        }
  138.        fin.close();
  139. }
  140.  
  141. char menu()
  142. {
  143.        //int a;
  144.        char a;
  145.         cout <<"这是一个电话本程序,可以执行以下操作:" <<endl;
  146.         cout <<"1->搜索一个电话" <<endl;
  147.         cout <<"2->增加一个电话" <<endl;
  148.         cout <<"3->删除一个电话" <<endl;
  149.         cout <<"4->查看电话本"<< endl;
  150.         cout <<"5->将文件追加到“电话本.txt”后面"<< endl;
  151.         cout <<"6->将文件读到内存并且输出到命令行" <<endl;
  152.         cout <<"7->清屏"<< endl;
  153.         cout <<"0->退出"<< endl;
  154.        cin>> a;
  155.        return a;
  156. }
  157. int main()
  158. {
  159.        number n;
  160.        while (1)
  161.        {
  162.                switch (menu())
  163.                {
  164.                        case '1':{
  165.                                          string s;
  166.                                           cout << "请输入查找的名字:" <<endl;
  167.                                          cin >> s;
  168.                                          n.search(s);
  169.                        }break;
  170.                        case '2':{
  171.                                          number a;
  172.                                          a.input();
  173.                                          a.add();
  174.                        }break;
  175.                        case '3':{
  176.                                          string s;
  177.                                           cout << "请输入删除的名字:" <<endl;
  178.                                          cin >> s;
  179.                                          n.del(s);
  180.                        }break;
  181.                        case '4':
  182.                                n.show("电话本.txt");
  183.                               break;
  184.                        case '5':
  185.                               n.copy();
  186.                               break;
  187.                        case '6':{
  188.                                          string s;
  189.                                           cout << "请输入需要读到内存的文件的文件名(包括扩展名)" <<endl;
  190.                                          cin >> s;
  191.                                          n.show(s);
  192.                        }break;
  193.                        case '7':
  194.                               system("cls");
  195.                               break;
  196.                        case '0':{
  197.                                          system("pause");
  198.                                          return 0;
  199.                        } break;
  200.                        default:{
  201.                                cout<<"输入错误"<<endl;
  202.                        }
  203.                        break;
  204.                }
  205.                system("pause");
  206.                system("cls");
  207.        }
  208. }


0 0