电话本 编写C++程序完成以下功能:

电话本 编写C++程序完成以下功能:,第1张

//在vc++6.0调试通过

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <map>

#include <utility>

using namespace std

class Telephone

void FileStore(const Telephone &t, const string &filename)

class Telephone

{

public:

Telephone() {}

~Telephone() {}

//通过名字查找

bool Find(const string &name)

//通过首字母查找

bool Find_first(const char ch)

//删除一个元素

void Erase(const string &name)

//增加一个元素

void Add(const string &name, const string &num)

//修改为

void Modify(const string &name, const string &num)

//显示电话簿的内容

void Show(void)

//保存内容到map对象

ifstream &Store(ifstream &infile)

//得到找到的元素

string GetFN(void) const { return m_findnum}

//得到找到的集合

map<string, string>&GetSet(void)

//删除集合的所有元素

void ClearSet()

friend void FileStore(const Telephone &t, const string &filename)

private:

map<string, string>m_tele

map<string, string>m_vp

string m_findnum

}

//------------------------------------------------------------

bool Telephone::Find(const string &name)

{

map<string, string>::iterator it = m_tele.find(name)

if (it != m_tele.end())

{

m_findnum = it->second

return true

}

else

{

return false

}

}

//--------------------------------------------------------------

bool Telephone::Find_first(const char ch)

{

map<string, string>::iterator mit = m_tele.begin()

for (mit != m_tele.end()++mit)

{

string::size_type pos = (mit->first).find(ch)

if (pos == 0)

{

m_vp.insert(make_pair(mit->first, mit->second))

}

}

if (m_vp.empty())

{

return false

}

else

{

return true

}

}

//--------------------------------------------------------------

map<string, string>

&Telephone::GetSet(void)

{

return m_vp

}

//--------------------------------------------------------------

void Telephone::ClearSet(void)

{

m_vp.clear()

}

//--------------------------------------------------------------

void Telephone::Erase(const string &name)

{

m_tele.erase(name)

}

//--------------------------------------------------------------

void Telephone::Add(const string &name, const string &num)

{

m_tele.insert(make_pair(name, num))

}

//--------------------------------------------------------------

void Telephone::Modify(const string &name, const string &num)

{

Erase(name)

Add(name, num)

}

//---------------------------------------------------------------

void Telephone::Show(void)

{

map<string, string>::const_iterator map_it = m_tele.begin()

while (map_it != m_tele.end())

{

cout <<map_it->first <<" "

<<map_it->second <<endl

++map_it

}

}

//------------------------------------------------------------------

ifstream &Telephone::Store(ifstream &infile)

{

string name

string num

while (infile >>name >>num)

{

Add(name, num)

}

return infile

}

//-------------------------------------------------------------------

void FileStore(const Telephone &t, const string &filename)

{

ofstream outfile(filename.c_str())

map<string, string>::const_iterator it = t.m_tele.begin()

while (it != t.m_tele.end())

{

outfile <<it->first <<" " <<it->second <<endl

++it

}

outfile.close()

}

//---------------------------------------------------------------------

ifstream &open_file(ifstream &in, const string &file)

{

in.close()

in.clear()

in.open(file.c_str())

return in

}

//----------------------------------------------------------------------

ofstream &open_file(ofstream &out, const string &file)

{

out.close()

out.clear()

out.open(file.c_str())

return out

}

//----------------------------------------------------------------------

int main(void)

{

Telephone tp

ifstream infile

ofstream outfile

cout <<"输入文件名: "

string filename

cin >>filename

open_file(infile, filename.c_str())

tp.Store(infile)

infile.close()

infile.clear()

while (true)

{

cout <<"选择 *** 作: " <<endl

cout <<"1 --- 查找 "

<<"2 --- 删除 "

<<"3 --- 增加 "

<<"4 --- 修改 "

<<"5 --- 显示 "

<<"6 --- 退出 "

<<endl

char ch

cin >>ch

string name

string num

switch (ch)

{

case '1':

{

cout <<"全名查找 -- a / 首字母查找 -- f" <<endl

char c

cin >>c

if ((c == 'a') || (c == 'A'))

{

cout <<"输入查找的名字: "

cin >>name

if (tp.Find(name))

{

cout <<"找到匹配!" <<endl

cout <<tp.GetFN() <<endl

}

else

{

cout <<"没有找到匹配!" <<endl

}

}

else if ((c == 'f') || (c == 'F'))

{

cout <<"输入首字母: "

char cf

cin >>cf

if (tp.Find_first(cf))

{

cout <<"找到集合!" <<endl

map<string, string>vmp = tp.GetSet()

map<string, string>::iterator vmi = vmp.begin()

for (vmi != vmp.end()++vmi)

{

cout <<vmi->first <<" "

<<vmi->second

<<endl

}

}

else

{

cout <<"没有找到!" <<endl

}

tp.ClearSet()

}

else

{

cout <<"输入错误!" <<endl

}

break

}

case '2':

cout <<"输入要删除的名字: "

cin >>name

tp.Erase(name)

break

case '3':

cout <<"输入要增加的名字和号码:(以exit单词结束)" <<endl

while (true)

{

cin >>name >>num

if ((name == "exit") || (num == "exit"))

break

tp.Add(name, num)

}

break

case '4':

cout <<"输入要修改的名字和号码: "

cin >>name >>num

tp.Modify(name, num)

break

case '5':

cout <<"电话簿内容: " <<endl

tp.Show()

break

default:

break

}

FileStore(tp, filename.c_str())

if ((ch <'1') || (ch >'5'))

break

}

return (0)

}

#include "stdafx.h"

#include<iostream.h>

#include<string.h>

#include<iomanip.h>

class stu

{

char name[20]

double age,homephone,telphone

char sex

public:

stu(){}

stu(char n[20],char se,double ag,double ho,double te)

{

strcpy(name, n)

age=ag

homephone=ho

telphone=te

}

friend void main()

}

void main()

{

cout<<"请选择您需要的 *** 作!"<<endl

cout<<" *** 作:"<<endl

cout<<"(0)通讯录录入"<<endl

cout<<"(1)增加人员"<<endl

cout<<"(2)删除人员"<<endl

cout<<"(3)修改数据"<<endl

cout<<"(4)显示记录"<<endl

cout<<"(5)退出"<<endl

cout<<"选择相关 *** 作请输入相对的括号里的阿拉伯数字!"<<endl

stu *s[50]

int i=0

int j=0

bool flag2=0

char p

do

{

cin>>p

if((p>='0'&&p<='5'))

flag2=1

else

cout<<"指令错误!请重新输入:"<<endl

}while(flag2==0)

switch(p)

{

case '0': //(0)通讯录录入

{

char name[20]

double age,homephone,telphone

char sex,c

do{

cout<<"请输入姓名:"<<endl

cin>>name

cout<<"请输入性别:"<<endl

cin>>sex

cout<<"请输入年龄:"<<endl

cin>>age

cout<<"请输入家里的电话号码:"<<endl

cin>>homephone

cout<<"请输入移动电话号码:"<<endl

cin>>telphone

j++

s[i]=new stu(name, sex, age, homephone , telphone)

i++

cout<<"数据录入成功,想继续录入吗(y/n)"<<endl

cin>>c

flag2=0

do

{

if(c!='y'&&c!='n')

{

cout<<"指令错误!请重新输入!"<<endl

cin>>c

}

else

flag2=1

}while(flag2==0)

}while(c=='y')

break}

////////////////////////////////////////////////////////////////////

case '1': //(1)增加人员(Add)

{

char name[20]

double age,homephone,telphone

char sex,c

do{

cout<<"请输入姓名:"<<endl

cin>>name

cout<<"请输入性别:"<<endl

cin>>sex

cout<<"请输入年龄:"<<endl

cin>>age

cout<<"请输入家里的电话号码:"<<endl

cin>>homephone

cout<<"请输入移动电话号码:"<<endl

cin>>telphone

j++

s[i]=new stu(name, sex, age, homephone , telphone)

i++

cout<<"数据录入成功,想继续录入吗(y/n)"<<endl

cin>>c

flag2=0

do

{

if(c!='y'&&c!='n')

{

cout<<"指令错误!请重新输入!"<<endl

cin>>c

}

else

flag2=1

}while(flag2==0)

}while(c=='y')

break}

case '2': //(2)删除人员(Delete)

{

char name[20]bool flag3=0char c

do{

cout<<"请输入您要删除的学生姓名:"<<endl

cin>>name

for(int h=0h<ih++)

{

if(strcmp(name,s[h]->name)==0)

{

flag3=1

i--

do{

s[h]=s[h+1]

h++

}while(h<=i)

}

}

if(flag3==0)

cout<<"您要求删除的对象本来就不存在!请检查输入的正确性!"

cout<<"要继续删除吗?(y/n)"<<endl

cin>>c

if(c!='y'&&c!='n')

{

cout<<"指令错误!请重新输入!"<<endl

cin>>c

}

}while(c=='y')

break}

case '3': //(3)修改数据(Alter)

{

char name[20],sedouble ag,ho,teflag2=0

char c

do

{

cout<<"请输入您要修改的学生的姓名:"<<endl

cin>>name

for(int h=0h<ih++)

{

if(strcmp(name,s[h]->name)==0)

{

flag2=1

cout<<"请输入性别:"<<endl

cin>>se

cout<<"请输入年龄:"<<endl

cin>>ag

cout<<"请输入家里的电话号码:"<<endl

cin>>ho

cout<<"请输入移动电话号码:"<<endl

cin>>te

s[h]->sex=se

s[h]->age=ag

s[h]->homephone=ho

s[h]->telphone=te

cout<<"数据修改成功!"

}

}

if(flag2==0)

{

cout<<"您要修改的学生本来就不存在!请检查重新输入!"<<endl

}

cout<<"想继续修改吗(y/n)"<<endl

cin>>c

if(c!='y'&&c!='n')

{

cout<<"指令错误!请重新输入!"<<endl

cin>>c

}

}while(c=='y')

break}

case '4': //(4)显示记录(List)

{

cout<<"本系统所有通讯录的数据如下:"<<endl

if(i==0)

cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl

for(int k=0k<ik++)

{

cout<<k+1<<" "<<"姓名:"<<" "<<s[k]->name<<

"性别:"<<" "<<s[k]->sex<<"年龄:"<<" "<<s[k]->age

<<"家里的电话号码:"<<" "<<s[k]->homephone<<"移动电话号码:"

<<" "<<s[k]->telphone<<endl

}

break} }

cout<<"您想继续进行其他 *** 作吗?(y/n)"<<endl

bool flag4=0

do

{

cin>>p

if(p!='y'&&p!='n')

cout<<"指令错误!请重新输入!"<<endl

else

flag4=1

}while(flag4==0)

if(p=='y')

cout<<"请输入 *** 作代码(0 通讯录录入\n1 增加人员(Add)\n2 删除人员(Delete)\n3 修改数据(Alter)\n4 显示记录(List)\n 5 退出(Exit))"<<endl

cin>>p

for(int x=0x<ix++)

{

delete s[x]

cout<<"删除所有成员!"<<endl

}

}

你好, 你是想实现一个具有查询功能的不带数据库的小程序吧?

这样给你写有点不可能:里面难点给你写出来思路

1.这里用text记事本代替数据库实现这个功能(程序打开可能会有2s加载时间)

2.使用open函数 打开并且写入数据;建议人名+电话号;

3.每次打开时候加载text 也是可以用open

4.获取到open的数据 并用split(string,"")分开,再通过 +

分开人名和电话 得到一个2维数组

5.遍历这个数组 和你要查询人名做出匹配

ok

祝楼主好运


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8108069.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存