| 導購 | 订阅 | 在线投稿
分享
 
 
 

通訊錄的源代碼(用鏈表實現)

2008-06-01 02:10:50  編輯來源:互聯網  简体版  手機版  移動版  評論  字體: ||

#include <stdio.h>

#include <stdlib.h> /*與malloc.h差不多*/

#include <string.h>

#define maxlen 100

strUCt persons

{ char name[10]; /*定義結構體數組用于緩存數據*/

char addr[20];

char phnum[10];

}persons[maxlen];

;

;

;

typedef struct lnode{ /*通訊錄結構中結點的定義*/

char name[10]; /*姓名*/

char addr[20]; /*地址*/

char phnum[10]; /*電話*/

struct lnode *next;

}listnode,*linklist;

;

;

;

linklist head=NULL,r=NULL; /*定義頭指針和尾指針*/

listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7;

int i;

char name1[10],ch;

char str1[20];

FILE *fp; /*定義文件指針*/

;

;

void creat() /*將文件的信息讀入結構體數組在轉存入鏈表中*/

{ int j;

long k;

fp=fopen("people.txt","r+t"); /*打開文件*/

if(fp!=NULL)

{for(i=1;i<maxlen;i++)

{ j=fgetc(fp);

if(j==EOF)

return;

k=i-1;

fseek(fp,k*sizeof(struct persons),0); /*讀取一個人的信息*/

fread(&persons[i],sizeof(struct persons),1,fp);

s=(linklist)malloc(sizeof(listnode)); /*裝存入鏈表中*/

strcpy(s->name,persons[i].name);

strcpy(s->addr,persons[i].addr);

strcpy(s->phnum,persons[i].phnum);

if(head==NULL) /*用尾插法將其插入鏈表中*/

head=s;

else

r->next=s;

r=s;}

}

else

{ fp=fopen("people.txt","w"); /*不能打開另開辟一個文件*/

i=1;

}

}

;

;

;

void Show() /*顯示所有的信息*/

{ p1=head;

while(p1!=NULL)

{ printf(" name:%s ",p1->name);

printf(" addr:%s",p1->addr);

printf(" phnum:%s",p1->phnum);

p1=p1->next;

}

}

;

;

;

void Delete() /*定義一個刪除的函數*/

{ printf(" please input the name:");

gets(name1); /*輸入要刪除人的姓名*/

p4=head;

if(strcmp(p4->name,name1)==0)

{ p4=p4->next; /*根據各種情況判定可能性*/

head=p4;

}

else

{ while(strcmp(p4->next->name,name1)!=0)

p4=p4->next;

p5=p4->next;

p4->next=p5->next;

free(p5);

}

}

;

;

;

void Find() /*查找的函數定義*/

{ printf(" please input the name:");

p0=head;

gets(name1); /*查找人的姓名輸入*/

while(strcmp(name1,p0->name)!=0&&p0!=NULL)

p0=p0->next;

if(p0==NULL)

printf(" It is not exit in the addr-book!");

else

{ printf(" name:%s ",p0->name);

printf(" addr:%s",p0->addr);

printf(" phnum:%s",p0->phnum);

}

}

;

;

;

void Input() /*向通訊錄中輸入一個人的信息*/

{ s=(linklist)malloc(sizeof(listnode));

printf(" please input the sb's meg:");

printf(" name:");

scanf("%s",s->name);

printf(" Addr:");

scanf("%s",s->addr);

printf(" phnum:");

scanf("%s",s->phnum);

if(head==NULL)

head=s;

else

r->next=s;

r=s;

}

;

;

;

void Alter() /*改變一個人的信息*/

{

int j;

printf(" Please input the name:");

gets(name1); /*輸入要人的姓名*/

p3=head;

while(strcmp(name1,p3->name)!=0&&p3!=NULL)

p3=p3->next;

if(p3==NULL)

printf(" It is not exit in the addr-book!");

else

{ printf(" please input the new meg!"); /*輸入要改人的新信息*/

printf(" name:");

scanf("%s",name1);

strcpy(p3->name,name1);

printf(" Addr:");

scanf("%s",name1);

strcpy(p3->addr,name1);

printf(" phnum:");

scanf("%s",name1);

strcpy(p3->phnum,name1);

}

}

;

;

;

void Save() /*保存信息*/

{ int j;

fp=fopen("people.txt","w");

for(p2=head,j=0;p2!=NULL;j++,p2=p2->next)/*將信息裝出入結構體數組在出入鏈表中*/

{ /*避免地址的出入,造成亂碼文件*/

strcpy(persons[j].name,p2->name);

strcpy(persons[j].addr,p2->addr);

strcpy(persons[j].phnum,p2->phnum);

fwrite(&persons[j],sizeof(struct persons),1,fp);

}

}

;

;

;

void main()

{ creat();

do

{ printf(" WELCOME TO USE Pan Weifeng's Address book");/*顯示提示的信息*/

printf(" Please make a choice below:");

printf(" 1.Show all the meg");

printf(" 2.Delete a piece of meg");

printf(" 3.Find a piece of meg");

printf(" 4.Insert a piece of meg");

printf(" 5.Alter a piece of meg");

printf(" 6.Save and Exit");

printf(" ");

printf(" Input Your Choice:");

ch=getche();

switch(ch)

{ case '1': Show(); /*用單條件多選擇語句實現調用與循環*/

break;

case '2': Delete();

break;

case '3': Find();

break;

case '4': Input();

break;

case '5': Alter();

break;

case '6': Save();

fclose(fp);

exit(0);

break;

default:

printf(" ********************************* ");

printf(" The num should 1-4!!! ");

printf(" **********************************");

break;

}

}while(1);

}

#include <stdio.h> #include <stdlib.h> /*與malloc.h差不多*/ #include <string.h> #define maxlen 100 strUCt persons { char name[10]; /*定義結構體數組用于緩存數據*/ char addr[20]; char phnum[10]; }persons[maxlen]; ; ; ; typedef struct lnode{ /*通訊錄結構中結點的定義*/ char name[10]; /*姓名*/ char addr[20]; /*地址*/ char phnum[10]; /*電話*/ struct lnode *next; }listnode,*linklist; ; ; ; linklist head=NULL,r=NULL; /*定義頭指針和尾指針*/ listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7; int i; char name1[10],ch; char str1[20]; FILE *fp; /*定義文件指針*/ ; ; void creat() /*將文件的信息讀入結構體數組在轉存入鏈表中*/ { int j; long k; fp=fopen("people.txt","r+t"); /*打開文件*/ if(fp!=NULL) {for(i=1;i<maxlen;i++) { j=fgetc(fp); if(j==EOF) return; k=i-1; fseek(fp,k*sizeof(struct persons),0); /*讀取一個人的信息*/ fread(&persons[i],sizeof(struct persons),1,fp); s=(linklist)malloc(sizeof(listnode)); /*裝存入鏈表中*/ strcpy(s->name,persons[i].name); strcpy(s->addr,persons[i].addr); strcpy(s->phnum,persons[i].phnum); if(head==NULL) /*用尾插法將其插入鏈表中*/ head=s; else r->next=s; r=s;} } else { fp=fopen("people.txt","w"); /*不能打開另開辟一個文件*/ i=1; } } ; ; ; void Show() /*顯示所有的信息*/ { p1=head; while(p1!=NULL) { printf(" name:%s ",p1->name); printf(" addr:%s",p1->addr); printf(" phnum:%s",p1->phnum); p1=p1->next; } } ; ; ; void Delete() /*定義一個刪除的函數*/ { printf(" please input the name:"); gets(name1); /*輸入要刪除人的姓名*/ p4=head; if(strcmp(p4->name,name1)==0) { p4=p4->next; /*根據各種情況判定可能性*/ head=p4; } else { while(strcmp(p4->next->name,name1)!=0) p4=p4->next; p5=p4->next; p4->next=p5->next; free(p5); } } ; ; ; void Find() /*查找的函數定義*/ { printf(" please input the name:"); p0=head; gets(name1); /*查找人的姓名輸入*/ while(strcmp(name1,p0->name)!=0&&p0!=NULL) p0=p0->next; if(p0==NULL) printf(" It is not exit in the addr-book!"); else { printf(" name:%s ",p0->name); printf(" addr:%s",p0->addr); printf(" phnum:%s",p0->phnum); } } ; ; ; void Input() /*向通訊錄中輸入一個人的信息*/ { s=(linklist)malloc(sizeof(listnode)); printf(" please input the sb's meg:"); printf(" name:"); scanf("%s",s->name); printf(" Addr:"); scanf("%s",s->addr); printf(" phnum:"); scanf("%s",s->phnum); if(head==NULL) head=s; else r->next=s; r=s; } ; ; ; void Alter() /*改變一個人的信息*/ { int j; printf(" Please input the name:"); gets(name1); /*輸入要人的姓名*/ p3=head; while(strcmp(name1,p3->name)!=0&&p3!=NULL) p3=p3->next; if(p3==NULL) printf(" It is not exit in the addr-book!"); else { printf(" please input the new meg!"); /*輸入要改人的新信息*/ printf(" name:"); scanf("%s",name1); strcpy(p3->name,name1); printf(" Addr:"); scanf("%s",name1); strcpy(p3->addr,name1); printf(" phnum:"); scanf("%s",name1); strcpy(p3->phnum,name1); } } ; ; ; void Save() /*保存信息*/ { int j; fp=fopen("people.txt","w"); for(p2=head,j=0;p2!=NULL;j++,p2=p2->next)/*將信息裝出入結構體數組在出入鏈表中*/ { /*避免地址的出入,造成亂碼文件*/ strcpy(persons[j].name,p2->name); strcpy(persons[j].addr,p2->addr); strcpy(persons[j].phnum,p2->phnum); fwrite(&persons[j],sizeof(struct persons),1,fp); } } ; ; ; void main() { creat(); do { printf(" WELCOME TO USE Pan Weifeng's Address book");/*顯示提示的信息*/ printf(" Please make a choice below:"); printf(" 1.Show all the meg"); printf(" 2.Delete a piece of meg"); printf(" 3.Find a piece of meg"); printf(" 4.Insert a piece of meg"); printf(" 5.Alter a piece of meg"); printf(" 6.Save and Exit"); printf(" "); printf(" Input Your Choice:"); ch=getche(); switch(ch) { case '1': Show(); /*用單條件多選擇語句實現調用與循環*/ break; case '2': Delete(); break; case '3': Find(); break; case '4': Input(); break; case '5': Alter(); break; case '6': Save(); fclose(fp); exit(0); break; default: printf(" ********************************* "); printf(" The num should 1-4!!! "); printf(" **********************************"); break; } }while(1); }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有