本文主要是介绍c语言字母转电话号码,C语言电话本程序(只是简单的姓名和电话号码增删改查),在线等...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
满意答案
a897567003
2014.11.10
采纳率:49% 等级:12
已帮助:8297人
#include
#include
#include
#defineOK1
#defineERROR0
#defineListNodeLensizeof(ListNode)
structlistNode{/*self-referentialstructure*/
charname[20];
charteleNo[15];
charEmail[35];
structlistNode*nextPtr;
};
typedefstructlistNodeListNode;
typedefListNode*ListNodePtr;
voidinsert(ListNodePtr*,char*,char*,char*);
intdel(ListNodePtr*,char*);
ListNodePtrsearch(ListNodePtr*,char*);
intisEmpty(ListNodePtr);
voidprintList(ListNodePtr);
voidinstructions(void);
ListNodePtrload(void);
voidsave(ListNodePtr);
voidcreatfile(void);
intmain()
{
ListNodePtrstartPtr=NULL,elemPtr;
charjudge,choice;
charitem1[20],item2[15],item3[35];
if((startPtr=load())==NULL)creatfile();/*文件不存在则创建*/
instructions();/*显示菜单*/
printf("\nCommand>");/*命令提示符*/
scanf("%c",&choice);
if(choice>='A'&&choice<='Z')choice=choice+32;/*大小写转换*/
while(choice!='q'){
switch(choice){
case'a':
printf("请输入要添加的姓名、电话号码和Email:(eg.NameTelNo.Email)\n");
scanf("\n%s",item1);
scanf("\n%s",item2);
scanf("\n%s",item3);
insert(&startPtr,item1,item2,item3);
break;
case'd':
if(!isEmpty(startPtr)){
printf("请输入要删除的姓名:");
scanf("\n%s",item1);
if(del(&startPtr,item1)){
printf("%s已经被删除.\n",item1);
}
else
printf("%s未找到\n\n",item1);
}
else
printf("电话本是空的!\n\n");
break;
case's':
if(!isEmpty(startPtr)){
printf("请输入要查找的姓名:");
scanf("\n%s",item1);
if((elemPtr=search(&startPtr,item1))!=NULL){
printf("\n%-20s%-15s%-35s\n","姓名:","电话号码:","Email:");
printf("%-20s%-15s%-35s\n",elemPtr->name,elemPtr->teleNo,elemPtr->Email);
}
else
printf("%s未找到!\n\n",item1);
}
else
printf("电话本是空的!\n\n");
break;
case'p':
printList(startPtr);
break;
default:
printf("无效的选择!\n\n");
instructions();
break;
}
printf("\nCommand>");
scanf("\n%c",&choice);
if(choice>='A'&&choice<='Z')choice=choice+32;/*大小写转换*/
}
printf("是否保存(Y/N)?");
scanf("\n%c",&judge);
if(judge=='Y'||judge=='y')
save(startPtr);
printf("结束!\n");
returnOK;
}
/*Printtheinstructions*/
voidinstructions(void)
{
printf("q.Quit(退出)"
"\ta.Add(添加)"
"\td.Delete(删除)"
"\ts.Search(查找)"
"\tp.Print(打印)\n"
"请选择命令:(Q/q、A/a、D/d、S/s、P/p)\n");
}
/*Insertanewvalueintothelistinsortedorder*/
voidinsert(ListNodePtr*sPtr,charvalue1[],charvalue2[],charvalue3[])
{
ListNodePtrnewPtr,previousPtr,currentPtr;
newPtr=(ListNodePtr)malloc(ListNodeLen);
if(newPtr!=NULL){/*isspaceavailable*/
strcpy(newPtr->name,value1);
strcpy(newPtr->teleNo,value2);
strcpy(newPtr->Email,value3);
newPtr->nextPtr=NULL;
previousPtr=NULL;
currentPtr=*sPtr;
while(currentPtr!=NULL&&strcmp(value1,currentPtr->name)==1){
previousPtr=currentPtr;/*walkto...*/
currentPtr=currentPtr->nextPtr;/*...nextnode*/
}
if(previousPtr==NULL){
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}
}
else
printf("%s无法添加,没有可用内存!\n",value1);
}
/*Deletealistelement*/
intdel(ListNodePtr*sPtr,charvalue[])
{
ListNodePtrpreviousPtr,currentPtr,tempPtr;
if(!strcmp(value,(*sPtr)->name)){
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr;/*删除头结点*/
free(tempPtr);/*释放头结点*/
returnOK;
}
else{
previousPtr=*sPtr;
currentPtr=(*sPtr)->nextPtr;
while(currentPtr!=NULL&&strcmp(currentPtr->name,value)){
previousPtr=currentPtr;/*移动...*/
currentPtr=currentPtr->nextPtr;/*...下一个结点*/
}
if(currentPtr!=NULL){
tempPtr=currentPtr;
previousPtr->nextPtr=currentPtr->nextPtr;
free(tempPtr);
returnOK;
}
}
returnERROR;
}
/*Searchelementinthelist*/
ListNodePtrsearch(ListNodePtr*sPtr,charvalue[])
{
ListNodePtrcurrentPtr;
if(!strcmp(value,(*sPtr)->name))
return*sPtr;
else{
currentPtr=(*sPtr)->nextPtr;
while(currentPtr!=NULL&&strcmp(currentPtr->name,value)){
currentPtr=currentPtr->nextPtr;/*...下一个结点*/
}
if(currentPtr!=NULL){
returncurrentPtr;
}
}
returnNULL;
}
/*Return1ifthelistisempty,0otherwise*/
intisEmpty(ListNodePtrsPtr)
{
returnsPtr==NULL;
}
/*Printthelist*/
voidprintList(ListNodePtrheadPtr)
{
ListNodePtrcurrentPtr=headPtr;
inti=0;
if(currentPtr==NULL)
printf("电话本是空的.\n\n");
else{
printf("\n%31s\n","********");
printf("%30s\n","电话本");
printf("%31s\n","********");
printf("%-20s%-15s%-35s\n","姓名:","电话号码:","Email:");
printf("------------------------------------------------------------\n");
while(currentPtr!=NULL){
printf("%-20s%-15s%-35s\n",currentPtr->name,currentPtr->teleNo,currentPtr->Email);
currentPtr=currentPtr->nextPtr;
i++;
}
printf("------------------------------------------------------------\n");
printf("总计%d个.\n",i);
}
}
/*Savefile*/
voidsave(ListNodePtrheadPtr)
{
FILE*fp;
ListNodePtrcurrentPtr=headPtr;
if((fp=fopen("tele.dat","wb"))==NULL){
printf("无法打开文件.\n");
return;
}
while(currentPtr!=NULL){
if(fwrite(currentPtr,ListNodeLen,1,fp)!=1){
printf("文件写入出错.\n");
break;
}
else
currentPtr=currentPtr->nextPtr;
}
fclose(fp);
}
/*Loadfile*/
ListNodePtrload()
{
FILE*fp;
ListNodePtrheadPtr,currentPtr;
headPtr=currentPtr=NULL;
if((fp=fopen("tele.dat","rb"))==NULL)
returnNULL;
while(!feof(fp)){
currentPtr=(ListNodePtr)malloc(ListNodeLen);
if(fread(currentPtr,ListNodeLen,1,fp)!=1){
printf("文件读取失败.\n");
free(currentPtr);
break;
}
else
insert(&headPtr,currentPtr->name,currentPtr->teleNo,currentPtr->Email);
}
fclose(fp);
returnheadPtr;
}
/*creatfile*/
voidcreatfile(){
FILE*fp;
if((fp=fopen("tele.dat","wb"))==NULL){
printf("文件创建失败.\n");
return;
}
fclose(fp);
}
00分享举报
这篇关于c语言字母转电话号码,C语言电话本程序(只是简单的姓名和电话号码增删改查),在线等...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!