您的位置:首页 > 编程语言 > C语言/C++

c++电话本程序

2015-10-30 18:57 405 查看
电话本的功能在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");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: