| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 335 人关注过本帖
标题:通讯录程序问题求助
只看楼主 加入收藏
c5880423
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-6-5
收藏
 问题点数:0 回复次数:0 
通讯录程序问题求助
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char a1[30]="家人",a2[30]="朋友",a3[30]="同学",a4[30]="同事",a5[30]="其他";
typedef struct per
{char name[30];
char tel[30];
char row[30];
struct per *next;
}node;
node *creat()                      //创建通讯信息函数
{node  *head,*p,*q;
int n,i,k,j=0;
printf("你想创建几个?[ ]\b\b");
scanf("%d",&n);
for(i=0;i<n;i++)
    {j=j+1;
    p=(node *)malloc(sizeof(node));
    printf("请输入第%d个人的通讯信息:",i+1);
    printf("\n姓名:");
    scanf("%s",p->name);
    printf("\n电话:");
    scanf("%s",p->tel);
    printf("选择所在分组:\n");
    printf("1.家人\t2.朋友\t3.同学\n4.同事\t5.其他[ ]\b\b");
    scanf("%d",&k);
    switch(k)
    {case 1:strcpy(p->row,a1);;break;
    case 2:strcpy(p->row,a2);;break;
    case 3:strcpy(p->row,a3);;break;
    case 4:strcpy(p->row,a4);;break;
    case 5:strcpy(p->row,a5);;break;
    }
    if(j==1)
    {head=p;q=p;}
    else
    q->next=p;
    q=p;
}
q->next=NULL;
return(head);
}
void look(node *head)
{if(head!=NULL)
{printf("所有人员通讯信息如下:\n");
printf("姓名\t电话\t分组\n");}
else
{printf("没有任何信息,请先创建!\n");
return;}
    while(head!=NULL)
{
printf("%s\t%s\t%s\n",head->name,head->tel,head->row);
head=head->next;
}
}


void find(node *head)
{
    node *p;
    char z[100],x[100];
    p=head;
    system("cls");

    {
        printf("姓名\n");
        scanf("%s",&z);system("cls");
        while(&p->name!=0&&p!=NULL)
        {
            if(strcmp(p->name,z)&&p->next!=NULL)
            {
                p=p->next;
               
            }
            
            else
            {
                if(!strcmp(p->name,z))
                {
                    printf("找到了\n");
                    printf("%s\t%s\t%s\n",p->name,p->tel,p->row);
                    break;
                }
            }
            if(strcmp(p->name,z)&&p->next==NULL)
            {
                printf("找不到\n");break;
            }
        }
    }


            

}
void del(node *head)
{
node *p,*q;
int a,b;
char z[30],x[30];
q=(node *)malloc(sizeof(node));
p=head;
printf("选择你要删除的方式1.姓名 2.关系\n");
scanf("%d",&a);
if(a==1)
{printf("姓名\n");
scanf("%s",&z);
while(&p->name!=0&&p!=NULL)
{   
        if(strcmp(p->name,z))
            {q=p;    p=p->next;}
        else
        {if(!strcmp(p->name,z))
                {printf("找到了\n");q->next=p->next;break;        }
            }
                if(strcmp(p->name,z)&&p->next==NULL)    {        printf("找不到\n");break;    }

}
}
if(a==2)
{
printf("选择关系\n");
        printf("1.家人\t2.朋友\t3.同学\t4.同事\t5.其他[ ]\b\b");
        scanf("%d",&b);
        switch(b)
        {
        case 1:strcpy(x,a1);break;
        case 2:strcpy(x,a2);break;
        case 3:strcpy(x,a3);break;
        case 4:strcpy(x,a4);break;
        case 5:strcpy(x,a5);break;
        }
        while(&p->row!=0&&p!=NULL)
        {if(strcmp(p->row,x))
        {q=p;
         p=p->next;
        }
        else
        {
         if(!strcmp(p->row,x))
         {
                 printf("找到了\n");q->next=p->next;break;   
               
         }
             if(strcmp(p->name,z)&&p->next==NULL);    {        printf("找不到\n");break;    }

        }
        }

}
}
void fix(node *head)
{
    node *p,*q;
    int a,k;
    char z[30];
    q=(node *)malloc(sizeof(node));
    p=head;
    printf("请输入要修改人的姓名\n");
    scanf("%s",&z);
    printf("是否修改记录的1.姓名\t2.电话\t3.分组\t请选择[ ]\b\b\n");
    scanf("%d",&a);system("cls");
    if(a==1)
    {
        while(&p->name!=0&&p!=NULL)
        {
            if(strcmp(p->name,z))
            {
                p=p->next;
            }
            
            else
            {
                if(!strcmp(p->name,z))
                {
                    printf("找到了\n");
                    printf("%s\t%s\t%s\n",p->name,p->tel,p->row);break;
                }
            }
            if(strcmp(p->name,z)&&p->next==NULL)
            {
                printf("找不到\n");break;
            }
        }
        q=(node *)malloc(sizeof(node));
        printf("新的姓名\n");
        scanf("%s",&q->name);
        strcpy(p->name,q->name);
        printf("修改成功\n");
        printf("%s\t%s\t%s\n",p->name,p->tel,p->row);
        free(q);
    }
            
    if(a==2)
    {
        while(&p->name!=0&&p!=NULL)
        {
            if(strcmp(p->name,z))
            {
                p=p->next;
            }
            
            else
            {
                if(!strcmp(p->name,z))
                {
                    printf("找到了\n");
                    printf("%s\t%s\t%s\n",p->name,p->tel,p->row);break;
                }
            }
            if(strcmp(p->name,z)&&p->next==NULL)
            {
                printf("找不到\n");break;
            }
        }
        q=(node *)malloc(sizeof(node));
        printf("新的电话\n");
        scanf("%s",&q->tel);
        strcpy(p->tel,q->tel);
        printf("修改成功\n");
        printf("%s\t%s\t%s\n",p->name,p->tel,p->row);free(q);
    }
    if(a==3)
    {
        while(&p->name!=0&&p!=NULL)
        {
            if(strcmp(p->name,z))
            {
                p=p->next;
            }
            
            else
            {
                if(!strcmp(p->name,z))
                {
                    printf("找到了\n");
                    printf("%s\t%s\t%s\n",p->name,p->tel,p->row);break;
                }
            }
            if(strcmp(p->name,z)&&p->next==NULL)
            {
                printf("找不到\n");break;
            }
        }
        printf("请选择新的分组\n");
        printf("1.家人\t2.朋友\t3.同学\t4.同事\t5.其他[ ]\b\b");
        scanf("%d",&k);
        switch(k)
        {case 1:strcpy(p->row,a1);;break;
        case 2:strcpy(p->row,a2);;break;
        case 3:strcpy(p->row,a3);;break;
        case 4:strcpy(p->row,a4);;break;
        case 5:strcpy(p->row,a5);;break;
        }
        printf("%s\t%s\t%s\n",p->name,p->tel,p->row);
    }
}
void add(node *head)
{
    node *p,*q;
    int k;
    p=head;
    while(p->next!=NULL)   //到链表的最后的节点
    {
        p=p->next;
    }
    q=(node *)malloc(sizeof(node));
    printf("请输入这个人的通讯信息");
    printf("\n姓名:");
    scanf("%s",q->name);
    printf("\n电话:");
    scanf("%s",q->tel);
    printf("选择所在分组:\n");
    printf("1.家人\t2.朋友\t3.同学\n4.同事\t5.其他[ ]\b\b");
    scanf("%d",&k);
    switch(k)
    {case 1:strcpy(q->row,a1);break;
    case 2:strcpy(q->row,a2);break;
    case 3:strcpy(q->row,a3);break;
    case 4:strcpy(q->row,a4);break;
    case 5:strcpy(q->row,a5);break;
    }
    p->next=q;
    q->next=NULL;
}

main()
{int n,j=0;node *p,*q,*head;
FILE *fp;
if(fp=fopen("txl.txt","r")==NULL)
{fp=fopen("txl.txt","w");
fclose(fp);
head=NULL;
}
else
{fp=fopen("txl.txt","r");
        {
        while(!feof(fp))
            {
            j++;
            p=(node *)malloc(sizeof(node));
            fread(p->name,90,1,fp);
                if(j==1)
                    {head=p;q=p;}
                else
                    {q->next=p;q=p;}
            }
        q->next=NULL;
        fclose(fp);
        }
        
    p=q=head;
    while(p->next!=NULL)
        {q=p;
        p=p->next;
        }
    q->next=NULL;
}
printf("\t   welcome to use this system!\n");
while(1)
    {printf("********************************************\n");
    printf("\t\t1.创建\t2.查看\n\t\t3.查找\t4.删除\n\t\t5.修改\t6.增加\n\t\t7.退出\n");
    printf("********************************************\n");
    printf("\t\t请输入你的选择:[ ]\b\b");
    scanf("%d",&n);
    switch(n)
        {case 1:head=creat();break;
        case 2:look(head);break;
        case 3:find(head);break;
        case 4:del(head);break;
        case 5:fix(head);break;
        case 6:add(head);break;
        case 7:;break;
        default :printf("输入有误,请重新输入!\n");
        }
    if(n==7)
            break;
    }
fp=fopen("txl.txt","w");
while(head!=NULL)
    {
    fwrite(head->name,90,1,fp);
    head=head->next;
    }
fclose(fp);
}
运行删除模块时找到了删除的结点停止,如遇到重名问题如何实现继续查找执行删除?
求高手帮忙!!!
搜索更多相关主题的帖子: 通讯录 
2010-06-05 17:31
快速回复:通讯录程序问题求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051490 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved