求助用C++6.0设计一个通讯录

(1).设一个通讯录由以下几项数据信息构成:数据项类型姓名字符串地址字符串邮政编码字符串电话号码字符串(2).试为通信录数据定义类型和定义通信录变量,实现功能(存取,显示... (1).设一个通讯录由以下几项数据信息构成:
数据项 类型
姓名 字符串
地址 字符串
邮政编码 字符串
电话号码 字符串
(2).试为通信录数据定义类型和定义通信录变量,实现功能(存取,显示,删除记录,查询记录)
展开
 我来答
pubnish
2008-12-22
知道答主
回答量:13
采纳率:0%
帮助的人:25.6万
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
struct AddressList
{

char name[20];
char address[30];
char tel[20];
char youbian[50];
}st[20];

int i=0;

void shuru()
{
FILE *fp;
char a;
fp=fopen("information.txt","a+");
do
{

printf("\n请输入姓名:");
scanf("%d",&st[i].name);
gets(st[i].name);
printf("\n");
printf("\n请输入地址:");
gets(st[i].address);
fflush(stdin);
printf("\n请输入电话:");
gets(st[i].tel);
fflush(stdin);
printf("\n请输入邮编:");
gets(st[i].youbian);
fflush(stdin);
fwrite(&st[i],sizeof(struct AddressList),1,fp);
printf("\n是否继续输入另外一个人的信息?(y/n)");
fflush(stdin);
a=getchar();
i++;
}
while(a=='y'&&i<=20);
fclose(fp);
}

int xianshi()
{
int j;FILE *fp;
if((fp=fopen("information.txt","r"))==NULL)
{
printf("通讯录还没记录");
return 0;
}
printf("\t姓名\t电话\t地址\t邮编\n");
for(j=0;fread(&st[j],sizeof(struct AddressList),1,fp)!=0;j++)
printf("\t%s\t%s\t%s\t%s\n",st[j].name,st[j].tel,st[j].address,st[j].youbian);
return 1;

}

int chazhao()
{
int m;FILE *fp;
char name[20],b;
if((fp=fopen("information.txt","r"))==NULL)
{
printf("还没有通讯信息");
return 0;
}
do
{
printf("\n请输入你需要查找人的姓名:");
fflush(stdin);
gets(name);
for(m=0;fread(&st[m],sizeof(struct AddressList),1,fp)!=0;m++)
{
if(strcmp(name,st[m].name)==0)
{
printf("\n\t你查找的联系人在第%d个位置找到\n",m+1);
break;
}
}
if(m>=20)
printf("\n\t没有找到这个人\n");
else
{
printf("\t姓名\t电话\t地址\t邮编\n");
printf("\t%s\t%s\t%s\t%s\n",st[m].name,st[m].address,st[m].tel,st[m].youbian);
}
printf("\n是否查找另一个人的信息?(y/n)");
fflush(stdin);
b=getchar();
}
while (b=='y');
return 1;
}

int shanchu()
{
char name[20],c;FILE *fp,*fp1;
int a,flag=0;
if((fp=fopen("information.txt","r"))==NULL)
{
printf("还没有学生信息");
return 0;
}
fp1=fopen("informationl.txt","w");
do
{
printf("\n请输入要删除人的姓名:\n");
fflush(stdin);
gets(name);
for(a=0;fread(&st[a],sizeof(struct AddressList),1,fp)!=0;a++)
{
if(strcmp(name,st[a].name)!=0)
fwrite(&st[a],sizeof(struct AddressList),1,fp1);
else
flag=1;
}
fclose(fp);
fclose(fp1);
fp=fopen("information.txt","w");
fp1=fopen("informationl.txt","r");
for(a=0;fread(&st[a],sizeof(struct AddressList),1,fp1)!=0;a++)
{
fwrite(&st[a],sizeof(struct AddressList),1,fp);
}
fclose(fp);
fclose(fp1);
if(flag==0)
printf("\t没有找到这个人\n");
else
xianshi();
printf("\n是否继续删除另一个联系人的信息?(y/n)");
fflush(stdin);
c=getchar();
}
while(c=='y');
return 1;
}

void charu()
{
xianshi();
shuru();

}

void jieshu()
{
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\t\t欢迎使用次通讯录\t\n");
printf("\n");
printf("\n");
printf("\n");
printf("\t\t\t07电专班:田依轩\t\t\n");
}

void main()
{
int change;
do
{
system("cls");
printf("================================欢迎来到通讯录=================================\n");
printf("\t\t1: 输入联系人的信息\n");
printf("\t\t2: 显示联系人信息\n");
printf("\t\t3: 查找联系人信息\n");
printf("\t\t4: 删除联系人信息\n");
printf("\t\t5: 插入联系人信息\n");
printf("\t\t6: 退出程序\n");
fflush(stdin);
printf("\t\t请在以下功能选项中选择:");
scanf("%d",&change);
switch(change)
{
case 1:
shuru();break;
case 2:
xianshi();break;
case 3:
chazhao();break;
case 4:
shanchu();break;
case 5:
charu();break;
case 6:
jieshu();break;
}
getch();
}
while(change!=6);
}
匿名用户
2008-12-09
展开全部
这是我大一的时候编的课程设计
可能和你的要求有点不一样
你可以自己改改
#include<conio.h>
#include<windows.h>
#include<fstream>
#include<iostream>
#include<string>
#include<string.h>
#include<iomanip>
using namespace std;
struct NUM//电话本结构体
{
string hom;
string off;
string tel;
};
struct BOOK//定义结构体类型BOOK
{
string name;
NUM nums[10];
string addr;
}book[10];

void chose();
void Alter();
void DEL();
void Quit();
static i=0;

void menu()//菜单函数
{
system("CLS");
cout <<endl;
cout<<"\t\t★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★" <<endl
<<"\t\t☆ ☆" <<endl
<<"\t\t★ Record openly and fast ★" <<endl
<<"\t\t☆ ☆" <<endl
<<"\t\t★ 1. Add(添加). 4. Modification(修改). ★" <<endl
<<"\t\t☆ ☆" <<endl
<<"\t\t★ 2. Deletion(删除). 5. Inquiry (查询). ★" <<endl
<<"\t\t☆ ☆" <<endl
<<"\t\t★ 3. Reveal(输出) . 6. Withdraw (退出). ★" <<endl
<<"\t\t☆ ☆" <<endl
<<"\t\t★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★" <<endl <<endl
<<"\t\t\tchose :" ;
chose();
}

void save()//保存数据
{
ofstream outfile("f.dat");
if(! outfile)
{
cerr<<"It is unable to open !!"<<endl;//提示无法打开
exit(1);
}
int h=0;
do
{
outfile<<book[h].name<<endl
<<" "<<book[h].nums[h].hom<<endl
<<" "<<book[h].nums[h].off<<endl
<<" "<<book[h].nums[h].tel<<endl
<<" "<<book[h].addr<<endl;
h++;
}while(h<i);
outfile.close();
}

void creat()//添加联系人
{
system("CLS");//清屏
if(i>9)
{
cout<<"The memory has been already full!"<<endl;//如果输入的数据大于10,则提示内存满
cout<<"Main menu / Quit?(M/Q)";//主菜单/退出?(M/Q)
char l;
cin>>l;
switch(l)
{
case 'M':
case 'm':system("CLS");menu();break;
case 'Q':
case 'q':system("CLS");Quit();break;
default:system("CLS");//清屏
cout<<"Input the mistake!!"<<endl;
cout << "Press any key to continue!" << endl;//按任意键继续
getch();menu();break;//输入错误
}
menu();
}
else
{
cout<<"Input name :";
cin>>book[i].name;
cout<<"Input home telephone:";
cin>>book[i].nums[i].hom;
cout<<"Input office number:";
cin>>book[i].nums[i].off;
cout<<"Input mobile phone :";
cin>>book[i].nums[i].tel;
cerr<<"Input home address :";
cin>>book[i].addr;
i++;
save();
system("CLS");//清屏
cout<<"The contact has already kept !"<<endl//联系人已保存
<<"Go on adding ?(Y/N)";//是否继续添加
char c;
cin>>c;
switch(c)
{
case 'N':
case 'n':system("CLS");menu();break;
case 'Y':
case 'y':creat();break;
default:cout<<"Input the mistake!!"<<endl;
cout << "Press any key to continue." << endl;
getch();menu();break;
}
}
}

void List()//显示所有联系人
{
system("CLS");//清屏
ifstream infile("f.dat");
if(! infile)
{
cerr<<"It is unable to open !!!"<<endl;
exit(1);
}
cout.setf(ios::left);//输出数据在本域宽范围内向左对齐
int n=0;
for(int m=0;m<i;m++)
{
infile>>book[m].name>>book[m].nums[m].hom
>>book[m].nums[m].off>>book[m].nums[m].tel>>book[m].addr;
cout<<"Name :"<<book[m].name<<endl//姓名
<<" Home telephone:"<<book[n].nums[n].hom<<endl//住宅电话
<<" Office number:"<<book[n].nums[n].off<<endl//办公号码
<<" Mobile phone :"<<book[n].nums[n].tel<<endl//手机号码
<<" Home address :"<<book[n].addr<<endl<<endl;//住宅地址
n++;
}
cout<<"1:Main menu(返回菜单)"<<endl
<<"2:Quit(退出)"<<endl;
char l;
cin>>l;
switch(l)
{
case '1':system("CLS");menu();break;
case '2':system("CLS");Quit();break;
default:system("CLS");//清屏
cout<<"Input the mistake!!"<<endl;
cout << "Press any key to continue." << endl;
getch();menu();break;
}
}

void Find()//查询联系人
{
system("CLS");//清屏
string NM;
cout<<"Please input the name that will be inquired about the contact :"<<endl;
//提示输入要查询联系人的姓名
cin>>NM;
system("CLS");//清屏
int m=0;
bool fabl=1;
for(int j=0;j<i;j++)
{
m=0;
if(NM==book[j].name)
{fabl=0;
m=j;
cout<<"Name :"<<book[m].name<<endl//姓名
<<" Home telephone:"<<book[m].nums[m].hom<<endl//住宅电话
<<" Office number:"<<book[m].nums[m].off<<endl//办公号码
<<" Mobile phone :"<<book[m].nums[m].tel<<endl//手机号码
<<" Home address :"<<book[m].addr<<endl<<endl;//住宅地址
continue;
}
}
if(fabl==1) cout<<"This contact has not existed or already deleted !!"<<endl;
//提示"此联系人不存在或已删除"
else if(fabl==0)
{
cout<<"Finish inquiring !!"<<endl;//查询完毕
}
cout<<"1:Main menu"<<endl
<<"2:Go on"<<endl;
char l;
cin>>l;
switch(l)
{
case '1':system("CLS");menu();break;
case '2':system("CLS");Find();break;
default:system("CLS");cout<<"Input the mistake!!"<<endl;
cout << "Press any key to continue." << endl;
getch();menu();break;
}
}

void Alter()//修改信息
{
system("CLS");
cout<<"Please input the name of the contact that will revise :"<<endl;
//请输入要修改的联系人的姓名
string Ch;
cin>>Ch;
int a=0;
bool fabl2=0;
for(int b=0;b<i;b++)
{
a=0;
if(Ch==book[b].name)
{fabl2=1;a=b;continue;}
else fabl2=0;
}
if(fabl2==0)cout<<"This contact has not existed or already deleted!!"<<endl;
//此联系人不存在或已删除
else if(fabl2==1)
{
system("CLS");//清屏
cout.setf(ios::left);//输出数据在本域宽范围内向左对齐
cout<<"The primitive information of the contact whom you want to revise is :"<<endl;
//您要修改的联系人的原始信息为
cout<<"Name :"<<book[a].name<<endl//姓名
<<" Home telephone:"<<book[a].nums[a].hom<<endl//住宅电话
<<" Office number:"<<book[a].nums[a].off<<endl//办公号码
<<" Mobile phone :"<<book[a].nums[a].tel<<endl//手机号码
<<" Home address :"<<book[a].addr<<endl<<endl//住宅地址
<<"Please input new information:"<<endl//请输入新的信息
<<" Name:";
cin>>book[a].name;
cout<<" Home telephone:";
cin>>book[a].nums[a].hom;
cout<<" Office number:";
cin>>book[a].nums[a].off;
cout<<" Mobile phone :";
cin>>book[a].nums[a].tel;
cout<<" Home address :";
cin>>book[a].addr;
cout<<"Information is revised successfully !"<<endl;//修改成功
save();
}
cout<<"1:Main menu"<<endl
<<"2:Go on alter"<<endl;
char l;
cin>>l;
switch(l)
{
case '1':system("CLS");menu();break;
case '2':system("CLS");Alter();break;
default:system("CLS");cout<<"Input the mistake !!"<<endl;
cout << "Press any key to continue." << endl;
getch();menu();break;
}
}

void DEL()//删除联系人
{
system("CLS");
ofstream outfile("f.dat");
if(! outfile)
{
cerr<<"It is unable to open !!!"<<endl;
exit(1);
}
cout<<"Please input the name of contact that you want to delete:"<<endl;
//请输入您要删除的联系人的姓名
string Ch;
cin>>Ch;
int a=0;
bool fabl3=0;
for(int b=0;b<i;b++)
{
a=0;
if(Ch==book[b].name)
{fabl3=1;a=b;break;}
else fabl3=0;
}

if(fabl3==0) cout<<"This contact has not existed or already deleted!!"<<endl;
//此联系人不存在或已删除
else if(fabl3=1)
{
for(a=b;a<i;a++)
{
book[a].name=book[a+1].name;
book[a].nums[a].hom=book[a+1].nums[a+1].hom;
book[a].nums[a].off=book[a+1].nums[a+1].off;
book[a].nums[a].tel=book[a+1].nums[a+1].tel;
book[a].addr=book[a+1].addr;
i=i-1;
}
cout<<"The contact deletes successfully!!"<<endl;//联系人删除成功
{
int n=0;
do
{
outfile<<book[n].name<<endl
<<" "<<book[n].nums[n].hom<<endl
<<" "<<book[n].nums[n].off<<endl
<<" "<<book[n].nums[n].tel<<endl
<<" "<<book[n].addr<<endl;
n++;
}
while(n<i);
}
}
cout<<"1:Main menu"<<endl
<<"2:Go on delete"<<endl;
char l;
cin>>l;
switch(l)
{
case '1':system("CLS");menu();break;
case '2':system("CLS");DEL();break;
default:system("CLS");cout<<"Input the mistake!!"<<endl;
cout << "Press any key to continue." << endl;
getch();menu();break;
}
}

void Quit()//退出
{
system("CLS");
cout<<"Do you sure to quit?(Y/N):"<<endl;
char p;
cin>>p;
switch (p)
{
case 'Y':
case 'y':
system("CLS");
cout<<"Thanks and use !!"<<endl;
exit(0);
case 'N':
case 'n':menu();
}
}

void chose()//选择
{
char Q;
cin>>Q;
switch(Q)
{
case '1':creat();break;
case '2':DEL();menu();break;
case '3':List();menu();break;
case '4':Alter();menu();break;
case '5':Find();menu();break;
case '6':Quit();break;
default:cout<<"Input the mistake!!!"<<endl;
cout << "Press any key to continue." << endl;//按任意键继续
getch();menu();
}
}

void main()//主函数
{
menu();
chose();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式