c++電話本程序

電話本的功能在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 namespace std;
  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. void number::search(string s)
  45. {
  46.          ifstream fin("電話本.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. void number::add()
  70. {
  71.          ofstream fout("電話本.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.                             ofstream fout("電話本.txt", ios::in);
  80.                    }
  81.                    else
  82.                             cout << "文件打開失敗,請重新運行" << endl;
  83.          }
  84.          fout << *this;
  85.          fout.close();
  86. }
  87.  
  88. void number::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.          ofstream fout("電話本.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(string s)
  107. {
  108.          vector > tmp;
  109.          string tmpp, tmppp;
  110.          ifstream fin("電話本.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.          ofstream fout("電話本.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(string str)
  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. }


發佈了36 篇原創文章 · 獲贊 65 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章