buraka的gravatar头像
buraka 2018-01-03 17:11:55

为什么c程序一输入用户就停止工作?

001#include <stdio.h>
002#include <stdlib.h>
003#include <string.h>
004 
005int key1;
006int key2;
007int *p;
008struct node
009{
010    char name[10];
011    char address[20];
012    char num[11];
013    node *next;
014};
015 
016typedef node *pnode;
017typedef node *xingming;
018node **phone;
019node **nam;
020 
021void hash1(char num[11])
022{
023    int i=3;
024 
025    key1=(int)num[2];
026 
027    while(num[i]!=NULL)
028    {
029        key1+=(int)num[i];
030        i++;
031    }
032    key1=key1%20;
033}
034void hash2(char name[10])
035{
036    int i=1;
037    key2=(int)name[0];
038    while(name[i]!=NULL)
039    {
040        key2+=(int)name[i];
041        i++;
042    }
043    key2=key2%20;
044 
045}
046 
047node *input()
048{
049    node *temp;
050    temp= new node;
051    temp->next=NULL;
052    printf("请输入姓名:\n");
053    scanf("%s",temp->name);
054    printf("输入地址:\n");
055    scanf("%s",temp->address);
056    printf("输入电话:\n");
057    scanf("%s",temp->num);
058 
059    return temp;
060}
061int add()
062{
063    node *newphone;
064    node *newname;
065    newphone=input();
066    newname=newphone;
067    hash1(newphone->num);
068    hash2(newname->name);
069    newphone->next =phone[key1]->next;
070    phone[key1]->next=newphone;
071    newname->next=nam[key2]->next;
072    nam[key2]->next=newname;
073    return 0;
074}
075void create1()
076{
077    int i;
078    phone=new pnode[20];
079    for(i=0; i<20; i++)
080    {
081        phone[i]=new node;
082        phone[i]->next=NULL;
083    }
084}
085void create2()
086{
087    int i;
088    nam=new xingming[20];
089    for(i=0; i<20; i++)
090    {
091        nam[i]=new node;
092        nam[i]->next=NULL;
093    }
094}
095void display()
096{
097    int i;
098    node *p=NULL;
099    for(i=0; i<20; i++)
100    {
101        p=nam[i]->next;
102        while(p)
103        {
104            printf("%s\n%s\n%s\n\n",p->name,p->address,p->num);
105            p=p->next;
106        }
107    }
108}
109void serch_num(char num[])
110{
111    hash1(num);
112    node *q=phone[key1]->next;
113    while(q!=NULL)
114    {
115        if(strcmp(num,q->num)==0)
116            break;
117        q=q->next;
118    }
119    if(q)
120        printf("%s\n%s\n%s\n",q->name,q->address,q->num);
121    else printf("无此记录\n");
122}
123void serch_name(char name[10])
124{
125    hash2(name);
126    node *q=nam[key2]->next;
127    while(q!=NULL)
128    {
129        if(strcmp(name,q->name)==0)
130            break;
131        q=q->next;
132    }
133    if(q)
134        printf("%s\n%s\n%s\n",q->name,q->address,q->num);
135    else printf("无此记录\n");
136}
137void menu()
138{
139    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
140    printf("@_____________请输入你的选择_______________@\n");
141    printf("@_______0.添加记录   1.查找记录____________@\n");
142    printf("@_______2.显示记录   3.清空记录____________@\n");
143    printf("@________________4.退出系统________________@\n");
144    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
145}
146int main()
147{
148    char num[11];
149    char name[10];
150 
151    create1();
152    create2();
153 
154    int select;
155    while(1)
156    {
157        menu();
158        scanf("%d",&select);
159        switch(select)
160        {
161        case 1:
162        {
163            system("cls");
164            printf("6.号码查询,7.姓名查询\n");
165            int a;
166            scanf("%d",&a);
167            if(a=6)
168            {
169                printf("请输入电话号码:\n");
170                scanf("%s",&num);
171                printf("输出查找的信息:\n");
172                serch_num(num);
173            }
174            else
175            {
176                printf("请输入姓名\n");
177                scanf("%s",name);
178                printf("输出查找的信息\n");
179                serch_name(name);
180            }
181        }
182        break;
183        case 2:
184        {
185            system("cls");
186            printf("显示结果\n");
187            display();
188        }
189        break;
190        case 0:
191        {
192            system("cls");
193            printf("请输入要添加的内容\n");
194            add();
195        }
196        break;
197        case 3:
198        {<a href="http://static.zuidaima.com/images/237173/201801/20180103171135562_w.png" target="_blank"><img alt="为什么c程序一输入用户就停止工作?" data-rawwidth="981" data-rawheight="512" src="https://static.zuidaima.com/resource/img/gray_loading.png" class="lazy img-rounded" data-original="http://static.zuidaima.com/images/237173/201801/20180103171135562_w_650x339.png" style="width:100%;height:339px"></a>
199            system("cls");
200            printf("列表已清空\n");
201            create1();
202            create2();
203        }
204        break;
205        default:
206            return 0;
207        }
208    }
209    return 0;
210}

 

 

所有回答列表(1)
无聊居士的gravatar头像
无聊居士  LV5 2018年1月6日
程序停止响应,是因为你的内存出现非法读写操作
请检查你的代码所有关于内存读写的地方
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友