通讯录的制作.docx
《通讯录的制作.docx》由会员分享,可在线阅读,更多相关《通讯录的制作.docx(14页珍藏版)》请在冰豆网上搜索。
![通讯录的制作.docx](https://file1.bdocx.com/fileroot1/2022-12/31/7a5b3ba8-c244-4c07-b267-0662754e87f3/7a5b3ba8-c244-4c07-b267-0662754e87f31.gif)
通讯录的制作
1、通讯录的制作
#include
#include
#include
#include
#include
structAddress//说明结构
{
charname[20];
charTel[20];
charEmail[20];
charRelation[20];
Address*next;
};
Addressallone[3];
voidCreatelist(Address*&head)//建立链表函数
{Address*s,*p;
s=newAddress;
inta=1;
while(a==1)
{
cout<<"请输入姓名:
";
cin>>s->name;
cout<<"请输入电话号:
";
cin>>s->Tel;
cout<<"请输入Email:
";
cin>>s->Email;
cout<<"请输入与您关系:
";
cin>>s->Relation;
{
if(head==NULL)head=s;
elsep->next=s;
}
p=s;
s=newAddress;
cout<<"是否继续输入,输入按1不输入按0:
";//判断是否继续输入
cin>>a;
}
p->next=NULL;
deletes;
return;
}
voidshowlist(Address*head)//显示链表
{cout<<"您的通信录为:
"<<'\n';
while(head)
{
cout<name<<'\t';
cout<Tel<<'\t';
cout<Email<<'\t';
cout<Relation<<'\n';
head=head->next;
}
cout<}
voidChayuelist(Address*head)//查阅链表
{
charchayue[20];
cout<<"请输入你所要查阅的姓名:
";
cin>>chayue;
while(head)
{
if(strcmp(head->name,chayue)==0)
{
cout<name<<'\t';
cout<Tel<<'\t';
cout<Email<<'\t';
cout<Relation<<'\n';
}
head=head->next;
}
cout<}
voidbaocun(Address*head)//保存
{
ofstreamoutstuf;
outstuf.open("c:
\\save.txt",ios:
:
out);
if(!
outstuf)
{
cerr<<"Filecouldnotbeopen."<abort();
}
while(head)
{
outstuf<name<<''<Tel<<''<Email<<''<Relation<<'\n';文档收集自网络,仅用于个人学习
cout<<"?
";
head=head->next;
}
}
voidtianjia(Address*&head)//添加
{
Address*s;
s=newAddress;//生成新结点
cout<<"\n\t\t请输入姓名:
";
cin>>s->name;
cout<<"\n\t\t请输入电话号:
";
cin>>s->Tel;
cout<<"\n\t\t请输入Email:
";
cin>>s->Email;
cout<<"\n\t\t请输入与您关系:
";
cin>>s->Relation;
cout<<"\n\t\t你已经添加成功";
s->next=head;
head=s;
return;
}
voidmain()//主函数
{system("cls");
intgongnenghao;
Address*head=NULL;
cout<<"*************************本程序为通讯录管理系统***********************"<while(gongnenghao<=11)
{system("cls");
cout<<"请选择你所要实现的功能的编号"<cout<<"\n\t\t1.写记录"<cout<<"\n\t\t2.显示记录"<cout<<"\n\t\t3.查阅记录"<cout<<"\n\t\t5.删除记录"<cout<<"\n\t\t8.保存记录"<cout<<"\n\t\t9.添加记录"<cout<<"\n\t\t111.退出"<cin>>gongnenghao;
if(gongnenghao==1)
{
Createlist(head);
}
if(gongnenghao==2)
{
showlist(head);
}
if(gongnenghao==3)
{
Chayuelist(head);
}
if(gongnenghao==8)
{
baocun(head);
}
if(gongnenghao>9)
{
cout<<"退出";
}
if(gongnenghao==9)
{
tianjia(head);
}
}
}
2、求回文串
#include
usingnamespacestd;
//定义栈
typedefstructstack{
charbata;
structstack*next;
}Stack;
//定义队列
typedefstructQNode{
chardata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct{
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
intmain()
{
Stack*top=newStack;
if(!
top)exit
(1);
top=NULL;//初始化栈
LinkQueue*x=newLinkQueue;
if(!
x)exit
(1);
x->front=x->rear=NULL;//初始化队列
cout<<"请输入要检测的字符串,可以包含空格,以@结束"<charsr[100];
sr[0]='';
inti=-1;
do{
i++;
cin>>sr[i];
stack*s1=newstack;
if(!
s1)exit
(1);
if(i==0)
{
s1->next=NULL;//输入栈第一个数据
s1->bata=sr[i];
top=s1;
}
s1->bata=sr[i];
s1->next=top;
top=s1;
QNode*x1=newQNode;
if(!
x1)exit
(1);
if(i==0)//输入队列第一个数据
{
x1->data=sr[i];
x->front=x1;
x->rear=x1;
x->front->next=NULL;
}
x1->data=sr[i];
x->rear->next=x1;
x->rear=x1;
}while(sr[i]!
='@');
cout<<"检测结果"<if(top==NULL&&x->front==NULL)
cout<<"未输入!
!
!
"<top=top->next;//将'@'字符删除
while(top!
=NULL&&x->front!
=NULL)
{
if(top->bata!
=x->front->data)
{
cout<<"此字符串不是回文!
"<return0;
}
else
{
cout<<"此字符串是回文。
"<return0;
}
}
}
2、模拟停车厂管理的问题
#include
usingnamespacestd;
#defineOVERFLOW-2
#defineOK1
#defineERROR0
#defineSTACK_INIT_SIZE100
#defineSTACKINCRENT10
#defineNULL0
structSNode{
charCState;
intCNumber;
intCTime;
};//定义一个结构体结点
typedefstruct{
SNode*base;
SNode*top;
intstacksize;
}SqStack;//分配栈的存储空间
typedefstructQNode{
SNodedate;
structQNode*next;
}QNode,*QueuePtr;//队列链表结点
typedefstruct{
QueuePtrfront;
QueuePtrrear;
}LinkQueue;//分配队列的存储空间
//////////////////////////////////////////////////
//函数的建立
intInitStack(SqStack&S){
//构造一个栈
S.base=newSNode[STACK_INIT_SIZE];
if(!
S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
returnOK;
}
intPush(SqStack&S,SNodee){
//入栈
*S.top++=e;
returnOK;
}
intPop(SqStack&S,SNode&e){
//出栈
if(S.top==S.base)returnERROR;
e=*--S.top;
returnOK;
}
intInitQueue(LinkQueue&Q){
//建一个空队列
Q.front=Q.rear=newQNode;
Q.front->next=NULL;
returnOK;
}
intEnQueue(LinkQueue&Q,SNodee){
//入队列
QNode*p;
p=newQNode;
if(!
p)exit(OVERFLOW);
p->date=e;
Q.rear->next=p;
Q.rear=p;
returnOK;
}
intDeQueue(LinkQueue&Q,SNode&e){
//出队列
if(Q.front==Q.rear)returnERROR;
QNode*p;
p=newQNode;
p=Q.front->next;
e=p->date;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
delete(p);
returnOK;
}
////////////////////////////////////////////
intmain(){
SqStackS1,S2;
LinkQueueQ;
InitStack(S1);
InitStack(S2);
InitQueue(Q);
/*构造两个空栈S1,S2和一个队列Q*/
intn,price,i=1;
cout<<"停车场有多少个位置?
"<cin>>n;
cout<<"每分钟的价格是多少?
"<cin>>price;
SNodek;
do{
cout<<"输入信息"<cin>>k.CState>>k.CNumber>>k.CTime;
if(k.CState!
='A'&&k.CState!
='D'&&k.CState!
='E'){
cout<<"输入错误,请重新输入"<continue;
}
if(k.CState=='A'){
if(S1.top-S1.base>=n){
EnQueue(Q,k);
cout<<"停车场已满,车子放在便道上的"<
}//如果栈满,入队列并输出在便道上几号位
else{
Push(S1,k);//K入栈
cout<<"车子放在停车场的"<}
}//到达状态的处理
elseif(k.CState=='D'){
SNodee,a,g;
while
(1){
Pop(S1,e);//删除栈顶元素,返回一个e
if(e.CNumber==k.CNumber){//如果返回的车牌和输入的车牌一样就输出停留时间和所需费用文档收集自网络,仅用于个人学习
inttime,money;
time=k.CTime-e.CTime;
money=time*price;
cout<cout<<"应缴纳的费用为"<while(S2.top-S2.base!
=0){//S2不空就把S2的元素给S1
Pop(S2,a);
Push(S1,a);
}//while
while(S1.top-S1.base=Q.rear){//栈S1不空,就从队列中取出元素放入S1中,直到栈s1满或队列中没有元素了文档收集自网络,仅用于个人学习
DeQueue(Q,g);
i--;
Push(S1,g);
}//while
break;//当弹出的e和我们输入的k车牌号一样时跳出循环体
}//if
elsePush(S2,e);//当弹出的e和我们输入的k车牌号不一样时,e入栈s2且继续循环体
}//while
}//离去状态的处理
}while(k.CState!
='E');//当状态为结束时结束输入
return0;
}//main