求一电话号码存储有关的程序
我刚学C语言,,我希望你们帮我写个稍微精炼点的,,不要用指针,,我们学校说由于课时有限,,说指针我们自己下去研究。。希望你们不应指针,,其他内容C语言都行,,还有链表也没学。。谢谢。。马上期末了
程序代码:
/* Note:Your choice is C IDE */ #include <stdio.h> #include <stdlib.h> #include <string.h> struct addr { char name[30]; char street[40]; char city[20]; char state[3]; char tel[11]; struct addr *next; /* pointer to next entry */ }record; struct addr *head,*start; struct addr *last; int menu_select(); void enter(); void deleted(); void search(); void main() { char s[80],choice; struct addr *info; start=last=NULL; switch(menu_select()) { case 1:enter(); break; case 2:deleted(); break; case 3:search(); break; case 4:exit(0); } } int menu_select() { char s[80]; int c; printf("1__Enter a name\n"); printf("2__Delete a record\n"); printf("3__Search\n"); printf("4__Quit\n"); do { printf("\n Enter your choice:"); gets(s); c=atoi(s); }while(c<1||c>4); return(c); } struct addr* des_store(struct addr *i,struct addr *top) { if(!last) { last=i; return(i); } else { top->next=i; i->next=NULL; last=i; return(i); } } void enter() { struct addr *info; void inputs(char *,char *,int ); int n; for(n=0;n<2;n++) { info=(struct addr *)malloc(sizeof(record)); if(info==NULL) { printf("\n out of memory"); return; } inputs("enter name:",info->name,30); if(info->name[0]=='0') break; else { inputs("enter street:",info->street,40); inputs("enter city:",info->city,20); inputs("enter state:",info->state,3); inputs("enter tel:",info->tel ,11); start=des_store(info,start); if(n==0) head=start; } } } void inputs(char *p1,char* s,int count) { char p[40]; do { printf("%s",p1); gets(p); if (strlen(p)>count) printf("\n too long\n"); }while(strlen(p)>count); strcpy(s,p); } void display(struct addr *info) { printf("%s\n",info->name); printf("%s\n",info->street); printf("%s\n",info->city); printf("%s\n",info->tel); printf("\n\n"); } struct addr *find(char *name) { struct addr *info; info=head; while(info) { if(!strcmp(name,info->name)) return(info); else info=info->next; } return(info); } void search() { char name[40]; struct addr *info; printf("enter name to find:"); gets(name); if((info=find(name))==NULL) printf("not found\n"); else display(info); } void deleted() { char s[80]; struct addr*p1,*p2,*info; printf("enter name:"); gets(s); info=find(s); if(info!=NULL) { if (head==info) { head=info->next; printf("deleted:%s\n",info->name); free(info); } else { p1=head->next; while(info!=p1) { p2=p1; p1=p1->next; } p2->next=p1->next; printf("deleted:%s\n",info->name); free(info); } } else printf("%s not find!\n",info->name); }