電話本的功能在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;
- #include"iostream"
- #include"cstdlib"
- #include"string"
- #include"fstream"
- #include"utility"
- #include"vector"
- #include"windows.h"
- using namespace std;
- class number
- {
- private:
- string name;
- string num;
- public:
- void search(string);
- void add();
- void del(string);
- void show(string);
- void copy();
- friend ostream &operator <<(ostream &output,const number &p)
- {
- output << p.name << endl;
- output << p.num << endl;
- return output;
- }
- void input()
- {
- cout << "請輸入姓名:" << endl;
- cin >> name;
- cout << "請輸入電話:" << endl;
- cin >> num;
- }
- number(string name1=" ",string num1=" ")
- {
- this->name = name1;
- this->num = num1;
- }
- ~number()
- {}
- };
- void number::search(string s)
- {
- ifstream fin("電話本.txt");
- if (!fin)
- cout << "文件打開失敗,請重新運行" << endl;
- else
- {
- string s1;
- bool flag = 1;
- while (flag)
- {
- if (!getline(fin, s1))
- break;
- if (s1 == s)
- flag = 0;
- }
- getline(fin, s1);
- if (!flag)
- cout << s << "的電話號碼爲:" << s1 << endl;
- else
- cout << "文件中沒有記錄" << endl;
- fin.close();
- }
- }
- void number::add()
- {
- ofstream fout("電話本.txt", ios::app);
- if (!fout)
- {
- cout << "文件還未被創建,現在創建……" << endl;
- FILE *f;
- if ((f = fopen("電話本.txt", "a+")) != NULL)
- {
- fclose(f);
- ofstream fout("電話本.txt", ios::in);
- }
- else
- cout << "文件打開失敗,請重新運行" << endl;
- }
- fout << *this;
- fout.close();
- }
- void number::copy()
- {
- string str;
- cout << "請輸入需要讀取的文件名:" << endl;
- cin >> str;
- vector> tmp;
- string tmpp, tmppp;
- ifstream fin(str.c_str());
- while (fin >> tmpp >> tmppp)
- tmp.push_back(make_pair(tmpp, tmppp));
- fin.close();
- ofstream fout("電話本.txt",ios::app);
- for (vector >::iterator iter = tmp.begin(); iter != tmp.end(); iter++)
- fout << iter->first << endl << iter->second << endl;
- fout.close();
- }
- void number::del(string s)
- {
- vector > tmp;
- string tmpp, tmppp;
- ifstream fin("電話本.txt");
- while (fin >> tmpp >> tmppp) {
- if (tmpp == s)
- continue;
- tmp.push_back(make_pair(tmpp, tmppp));
- }
- fin.close();
- ofstream fout("電話本.txt");
- for (vector >::iterator iter = tmp.begin(); iter != tmp.end(); iter++)
- fout << iter->first << endl << iter->second << endl;
- fout.close();
- }
- void number::show(string str)
- {
- ifstream fin(str.c_str());
- if (!fin)
- cout << "文件打開失敗,請重新運行" << endl;
- else
- {
- string s;
- while (getline(fin, s))
- {
- if (s != " ")
- cout << s << endl;
- }
- }
- fin.close();
- }
- char menu()
- {
- //int a;
- char a;
- 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;
- cin >> a;
- return a;
- }
- int main()
- {
- number n;
- while (1)
- {
- switch (menu())
- {
- case '1':{
- string s;
- cout << "請輸入查找的名字:" << endl;
- cin >> s;
- n.search(s);
- }break;
- case '2':{
- number a;
- a.input();
- a.add();
- }break;
- case '3':{
- string s;
- cout << "請輸入刪除的名字:" << endl;
- cin >> s;
- n.del(s);
- }break;
- case '4':
- n.show("電話本.txt");
- break;
- case '5':
- n.copy();
- break;
- case '6':{
- string s;
- cout << "請輸入需要讀到內存的文件的文件名(包括擴展名)" << endl;
- cin >> s;
- n.show(s);
- }break;
- case '7':
- system("cls");
- break;
- case '0':{
- system("pause");
- return 0;
- } break;
- default:{
- cout<<"輸入錯誤"<<endl;
- }
- break;
- }
- system("pause");
- system("cls");
- }
- }