标题:电话号码查找系统!!这是在网上下载的源代码我只是稍稍改了下,可以执行! ...
取消只看楼主
雨刃巾人
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-3-25
结帖率:50%
已结贴  问题点数:20 回复次数:1 
电话号码查找系统!!这是在网上下载的源代码我只是稍稍改了下,可以执行!但我不清楚这程序里的三个全局变量的作用???还有主函数里面do{}while(1)循环为什
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxsize  20         
#define hashsize 53     
typedef struct dianhua
{                       
        char name[10];
        char tel[20];
        char add[20];
}hashitem;
int n,m,k;
typedef struct
{                                   
        hashitem *elem[hashsize];   
        int count;                  
        int size;                    
}hashtable;                         //初始化哈希表
int eq(char *x,char *y)
{                              
        if(strcmp(x,y)==0)
            return 1;
        else return -1;
}
hashtable *h;
hashitem a[maxsize];
int  number;     
void input(hashitem *a)             //输入用户函数
{   int i;                                 
        printf("输入要添加的个数:");
        scanf("%d",&number);
        printf("\n");
        k=n;
        for(i=n;i<number+m;i++)
        {
            printf("请输入第%d个记录的用户名:",i+1);
            scanf("%s",a[i].name);
            printf("请输入%d个记录的电话号码:",i+1);
            scanf("%s",a[i].tel);
            printf("请输入第%d个记录的地址:",i+1);
            scanf("%s",a[i].add);
            n++;
            printf("\n");
        }
        m=n;
}
long fold(char *s)                    
{                                
        char *p;
        char ss[30];
        long sum=0;
strcpy(ss,s);        
        p=ss;
        while(*p!='\0')
        sum+=*p++;
        return sum;
}
int hash1(char *str)                 //处理姓名函数
{                                       
        long n;
        int m;
        n=fold(str);               
        m=n%hashsize;            
        return m;                           
}
int hash2(char *str)                  //处理电话号码函数
{                                    
        int n;
        int m;
        n=atoi(str);                    
        m=n%hashsize;            
        return m;            
}
void showoutput(hashitem *a)          //显示输出函数        
{    int i=0;
        printf("电话本信息\n");
        printf("            =============================\n");
        printf("            |     姓名    |    电话号码     |        联系地址        |\n");
        for( i=0;i<n;i++)                       
        {   
            printf("            ----------------------------------------------------------\n");
            printf("            |%13s|%17s|%24s|\n",a[i].name,a[i].tel,a[i].add);
        }
            printf("            =============================\n");

}
static int collision(int p,int *c)          //处理冲突函数
{                                
        int i,q;
        i=*c/2+1;
        while(i<hashsize)
        {
            if(*c%2==0)
            {
                ++*c;
                q=(p+i*i)%hashsize;
                if(q>=0) return q;
                else i=*c/2+1;
            }
            else
            {
                q=(p-i*i)%hashsize;
                ++*c;
                if(q>=0) return q;
                else i=*c/2+1;
            }
        }
        return -1;
}
void createhash1(hashtable *h,hashitem *a)        //创建以姓名为关键字的哈希表函数
{   
int i,c;                                       
        int p,pp;                                    
        for(i=k;i<m;i++)
        {   c=0;                 
            p=hash1(a[i].name);
            pp=p;
            while(h->elem[pp]!=NULL)
            {
                pp=collision(p,&c);
                if(pp<0)
                {
                    printf("第%d记录无法解决冲突",i+1);        
                    continue;
                }                                            
            }
            h->elem[pp]=&(a[i]);                           
            h->count++;
            printf("第%d个记录冲突次数为%d。\n",i+1,c);        
        }
        printf("\n建表完成!\n此哈希表容量为%d.\n",hashsize);
}
void createhash2(hashtable *h,hashitem *a)            //创建以电话号码为关键字的哈希表函数
{                                       
        int i,c;
        int p,pp;               
        for(i=k;i<m;i++)
        {    c=0;                 
            p=hash2(a[i].tel);
            pp=p;
            while(h->elem[pp]!=NULL)
            {
                pp=collision(p,&c);
                if(pp<0)
                {
                    printf("第%d记录无法解决冲突",i+1);
                    continue;
                }
            }
            h->elem[pp]=&(a[i]);  
            h->count++;
            printf("第%d个记录冲突次数为%d。\n",i+1,c);
        }
        printf("\n建表完成!\n此哈希表容量为%d.\n",hashsize);
}
void searchhash1(hashtable *h)            //以姓名为关键字查找函数
{   
int c=0;                                
        int p,pp;char str[20];                  
        printf("\n请输入要查找记录的姓名:\n");
        scanf("%s",str);
        p=hash1(str);
        pp=p;
           printf("\n\n以姓名为关键字查找:\n\n");
        while(h->elem[pp]!=NULL)
        {   
            if((h->elem[pp]!=NULL)&&(eq(str,h->elem[pp]->name)==1))
            {
               printf("\n查找成功!以下是您需要要查找的信息:\n");
               printf("姓  名:%s\n电话号码:%s\n联系地址:%s\n",h->elem[pp]->name,h->elem[pp]->tel,h->elem[pp]->add);
               pp=collision(p,&c);
               printf("查找过程冲突次数为%d.\n\n",c-1);
            }
            else   
            {
                printf("\n此人不存在,查找失败!\n\n");
                break;
            }
        }
}
void searchhash2(hashtable *h)              //以电话号码为关键字查找函数
{    int p,pp,c=0;                                
        char tele[20];                                
        printf("\n请输入要查找记录的电话号码:\n");
        scanf("%s",tele);
        p=hash2(tele);
        pp=p;
        printf("\n\n以电话号码为关键字查找:\n\n");
        while(h->elem[pp]!=NULL)
        {   
            if((h->elem[pp]!=NULL)&&(eq(tele,h->elem[pp]->tel)==1))
        {   
            printf("\n查找成功!以下是您需要要查找的信息:\n");
            printf("姓  名:%s\n电话号码:%s\n联系地址:%s\n",h->elem[pp]->name,h->elem[pp]->tel,h->elem[pp]->add);
            pp=collision(p,&c);
            printf("查找过程冲突次数为%d.\n\n",c-1);
        }
            else   
            {
                printf("\n此人不存在,查找失败!\n\n");
                break;
            }
        }
}
void save(hashitem *a)            //保存函数
{
        FILE *fp;
        int i;
            system("cls");
        if((fp=fopen("dianhua.txt","w"))==NULL)
        {
            printf("文件打开失败!");
            exit(1);
        }
        for(i=0;i<m;i++)
            fprintf(fp,"%10s%20s%20s\n",&a[i].name,&a[i].tel,&a[i].add);
        fclose(fp);
        fflush(stdin);
        getchar();
        system("cls");
        printf("文件保存成功!");   
}
int main()
{   int m;
        int i;
        n=0;
        k=0;
        system("color 3f");
        h=(hashtable*)malloc(sizeof(hashtable));
        for(i=0;i<hashsize;i++)
            h->elem[i]=NULL;
        h->size=hashsize;
        h->count=0;
        do
        {system("cls");
         printf("电话号码查找系统\n");
         printf("            =============================\n");
         printf("             $          1-------------添加用户信息                  $\n");
         printf("             $          2-------------显示电话本信息                $\n");
         printf("             $          3-------------以姓名建立哈希表              $\n");
         printf("             $          4-------------以电话号码建立哈希表          $\n");
          printf("             $          5-------------查找并显示给定用户名的记录    $\n");
         printf("             $          6-------------查找并显示给定电话号码的记录  $\n");
         printf("             $          7-------------保存                          $\n");
         printf("             $          8-------------退出程序                      $\n");
          printf("             $------------------------------------------------------$\n");
          printf("             $      注意:执行5、6操作前必须先执行3、4操作!        $\n");
         printf("             $------------------------------------------------------$\n");
         printf("            =============================\n");
         printf("                           请选择(1~8):\n");
          scanf("%d",&m);
          system("cls");
          switch(m)
         {
              case 1:
                 input(a);
                 printf("输入结束!按任意键继续!");
                 fflush(stdin);
                 getchar();
                 break;
            case 2:
                 showoutput(a);
                 printf("                         按任意键继续!");
                 fflush(stdin);
                 getchar();
                 break;
            case 3:
                 createhash1(h,a);
                 printf("按任意键继续!");
                 fflush(stdin);
                 getchar();
                 break;
            case 4:
                createhash2(h,a);
                 printf("按任意键继续!");
                 fflush(stdin);
                 getchar();
                 break;  
            case 5:
                searchhash1(h);
                printf("按任意键继续!");
                fflush(stdin);
                getchar();
                break;
            case 6:
                searchhash2(h);
                printf("按任意键继续!");
                fflush(stdin);
                getchar();
                break;
            case 7:
                save(a);
                printf("按任意键继续!");
                fflush(stdin);
                getchar();
                break;
            case 8:
                printf("谢谢使用,再见!");
                return 0;
            default:
                printf("输入错误,请重新输入!");
                fflush(stdin);
                getchar();
                break;
        }
        }while(1);
}
搜索更多相关主题的帖子: include 源代码 count 电话 
2013-09-09 21:39
雨刃巾人
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-3-25
得分:0 
主函数里面do{}while(1)为什么会用死循环呢??    三个全局变量的作用不明白 ??  真心求大神讲解~~~!!!
2013-09-09 21:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-420354-1-1.html




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

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