当前位置:编程学习 > C/C++ >>

电话本用C语言怎么写?

要用指针来搞定?
答案:#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define OK 1
#define ERROR 0
#define ListNodeLen sizeof(ListNode)

struct listNode { /* self-referential structure */
char name[20];
char teleNo[15];
char Email[35];
struct listNode *nextPtr;
};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

void insert( ListNodePtr *, char *, char *, char * );
int del( ListNodePtr *, char * );
ListNodePtr search( ListNodePtr *, char *);
int isEmpty( ListNodePtr );
void printList( ListNodePtr );
void instructions( void );
ListNodePtr load( void );
void save( ListNodePtr );
void creatfile( void );

int main()
{
ListNodePtr startPtr=NULL, elemPtr;
char judge, choice;
char item1[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. Name TelNo. 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" );
return OK;
}

/* Print the instructions */
void instructions( void )
{
printf("q. Quit(退出)"
"\ta. Add(添加)"
"\td. Delete(删除)"
"\ts. Search(查找)"
"\tp. Print(打印)\n"
"请选择命令:(Q/q、A/a、D/d、S/s、P/p)\n");
}

/* Insert a new value into the list in sorted order */
void insert( ListNodePtr *sPtr, char value1[], char value2[], char value3[] )
{
ListNodePtr newPtr, previousPtr, currentPtr;

newPtr=(ListNodePtr)malloc( ListNodeLen );

if( newPtr!=NULL ) { /* is space available */
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; /* walk to ... */
currentPtr=currentPtr->nextPtr; /* ... next node */
}

if( previousPtr==NULL ) {
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else {
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}
}
else
printf( "%s 无法添加,没有可用内存!\n", value1 );
}

/* Delete a list element */
int del( ListNodePtr *sPtr, char value[] )
{
ListNodePtr previousPtr, currentPtr, tempPtr;

if( !strcmp(value,(*sPtr)->name) ) {
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr; /* 删除头结点 */
free( tempPtr ); /* 释放头结点 */
return OK;
}
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 );
return OK;
}
}

return ERROR;
}

/* Search element in the list */
ListNodePtr search( ListNodePtr *sPtr, char value[] )
{
ListNodePtr currentPtr;

if( !strcmp(value,(*sPtr)->name) )
return *sPtr;
else {
currentPtr=(*sPtr)->nextPtr;

while( currentPtr!=NULL&&strcmp(currentPtr->name,value) ) {
currentPtr=currentPtr->nextPtr; /* ... 下一个结点 */
}

if( currentPtr!=NULL ) {
return currentPtr;
}
}
return NULL;
}

/* Return 1 if the list is empty, 0 otherwise */
int isEmpty( ListNodePtr sPtr )
{
return sPtr==NULL;
}

/* Print the list */
void printList( ListNodePtr headPtr )
{
ListNodePtr currentPtr=headPtr;
int i=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 );
}
}

/* Save file */
void save( ListNodePtr headPtr )
{
FILE *fp;
ListNodePtr currentPtr=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 );
}

/* Load file */
ListNodePtr load()
{
FILE *fp;
ListNodePtr headPtr, currentPtr;
headPtr=currentPtr=NULL;
if( ( fp=fopen( "tele.dat", "rb" ) )==NULL )
return NULL;
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 );
return headPtr;
}

/* creat file */
void creatfile() {
FILE *fp;
if( (fp=fopen( "tele.dat", "wb"))==NULL) {
printf( "文件创建失败.\n" );
return;
}
fclose( fp );
}

用一个类或结构链表,记录姓名和电话。 
将输入的名字在所有的类中查找即可。
至于可视化方面的内容,很简单,你自己做吧。

上一个:C语言和Pascal哪个更实用
下一个:求c语言编程文件操作实例!

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,