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");
}
}
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");
}
}
相关文章推荐
- c++任意数循环赛另一种递归实现方…
- C++中文乱码转为十六进制和转为正常中文
- c++11实现异步定时器
- c++函数模板的使用
- C++ CDialogEx 类的调用流程
- VC++绘图:windows图形环境
- 微软承诺将在今年的 Visual C++ 更新中加入 Clang 编译器
- C语言结构体的初始化
- c++ 时间与字符串转换
- C++primer学习:面向对象程序设计(5):再探文本查询程序
- 0012闰年计算的C++实现
- C/C++中的数学函数double pow( double x, double y )
- C++、C#、java算法学习日记04----二分插入排序
- 头文件互相包含问题
- C++中的定位放置new(placement new)
- C++中的定位放置new(placement new)
- 人事管理系统(C语言实现)
- Java入门-C++基础
- c++ 实现井字棋
- 玩转Visual Studio了解vc++各种类型的工程