#include<stdio.h> #include<alloc.h> #include<string.h> #include<stdlib.h> struct pbook { char fname[20],city[20],street[20]; long id; long pnum; long cnum; } struct pbook* add(int c) { struct pbook *x; int i=1; char y,n; do{ x=(struct pbook *)realloc(x,sizeof(struct pbook)*(i+c)); printf("enter first name\n"); gets(x[c].fname); printf("enter phone number\n"); scanf("%ld",&x[c].pnum); printf("cell phone to?\n"); scanf("%c",&n); if(n=='y') { printf("enter cell number\n"); scanf("%ld",&x[c].cnum); } printf("enter city\n"); gets(x[c].city); printf("enter street name\n"); gets(x[c].street); printf("enter id\n"); scanf("%ld",&x[c].id); printf("want to add another record? (yes\no)\n"); scanf("%c",&y); } while(y=='y'); return(x); }
void show(int sum,struct pbook *prm) { int i; for (i=0;i<sum;i++) { puts(prm.fname); printf("%ld",prm.pnum); printf("%ld",prm.cnum); printf("%ld",prm.id); puts(prm.street); puts(prm.city); } } int serch(int sum,struct pbook *pr) { char fname2[20]; int i; printf("enter name to find\n"); gets(fname2); for(i=0;i<sum;i++) if(strcmp(fname2,pr.fname)==0) { puts(pr.fname); printf("%ld",pr.pnum); printf("%ld",pr.cnum); printf("%ld",pr.id); puts(pr.street); puts(pr.city); } else break; return(i); } void main_menu() { printf("1.add a new record\n"); printf("2.serch by first name\n"); printf("3.show the phone book\n"); printf("4.delete all records\n"); printf("5.delete records one by one\n"); printf("press esc to exit\n\n\n\n"); printf("enter you choise\n"); } void del(struct pbook *prb,int c,int sum) {
struct pbook *point; int i; point=(pbook *)realloc(point,sizeof(pbook)*(sum+i)); for(i=0;i<sum&&i!=c;i++) point=prb; } void main() { int num=0; struct pbook *pr; int a=0,b; while(a!=29) { a=getchar(); main_menu(); switch (a) { case '1': { pr=(pbook *)realloc(pr,sizeof(pbook)*(num+1)); pr=add(num); num++; } case '2': serch(num,pr); case '3': show(num,pr); case '4': free(pr); case '5': b=serch(num,pr); del(pr,b,num); }
} }
|