c語言實現通訊錄管理系統(用鏈表實現)


題目:

       通訊錄(通過鏈表實現)

       設計並實現一個簡易的通訊錄軟件,管理個人通訊記錄。一條通訊記錄可包括:姓名、工作單位、手機、住宅電話、E-Mail、家庭住址等(可自行增刪,但不可過少)。該系統應實現以下基本功能:

     (1)增加新的通訊記錄。

    (2)刪除已有的通訊記錄。

    (3)修改已有的通訊記錄。

    (4)瀏覽全部或指定(如指定姓名、工作單位等)的通訊記錄。

    (5)合理組織排列各項功能,界面可使用鍵盤操作。

    (6)以文件的形式存儲數據。

說明:

      大一時的c語言課設,用鏈表實現一個通訊錄管理系統,為了美觀好看,花了很多時間調整齊度,記錄一下大一時的作業。其主要功能是對通訊錄可輸入,顯示,插入,刪除,最難是可保存,這個學文件的時候不怎么會。內容我自己弄了7個,名字,性別,工作單位,手機,住宅電話,E-Mail,家庭住址(其他太多其實都是一樣的,就懶得加了)。主要運用到對指針中的鏈表的功能和使用要比較扎實,分部列寫就可以了。

實現圖片:

附上代碼:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 typedef struct student
  5 {
  6     char name[20];//名字
  7     char wm[20];//性別
  8     char work[100];//工作單位
  9     char stel[20];//手機
 10     char htel[20];//住宅號碼
 11     char mail[20];//E-Mail
 12     char home[100];//家庭住址
 13     struct student *next;
 14 }stu;
 15 stu *head;//頭指針
 16 void screen()//主菜單
 17 {
 18     printf("\n=======================================================\n");
 19     printf("              歡迎來到通訊錄管理系統\n\n");
 20     printf("            1.輸入數據         2.顯示數據\n");
 21     printf("            3.插入數據         4.刪除數據\n");
 22     printf("            5.查看數據         6.修改數據\n");
 23     printf("            7.保存數據         8.返回主菜單\n");
 24     printf("\n~~~~~~輸~~~~~~入~~~~~~9~~~~~~退~~~~~~出~~~~~~程~~~~~~序\n");
 25 }
 26 void input()//輸入數據
 27 {
 28     int ans;//判斷是否繼續輸入
 29     stu *p1,*p2;
 30     p1=(stu *)malloc(sizeof(stu));//申請內存來用
 31     if(p1!=NULL)
 32     {
 33         printf("========輸入數據========\n");
 34         head=p1;
 35         while(1)
 36         {
 37             printf("名字:");
 38             scanf("%s",&p1->name);
 39             printf("性別:");
 40             scanf("%s",&p1->wm);
 41             printf("工作單位:");
 42             scanf("%s",&p1->work);
 43             printf("手機:");
 44             scanf("%s",&p1->stel);
 45             printf("住宅號碼:");
 46             scanf("%s",&p1->htel);
 47             printf("E-Mail:");
 48             scanf("%s",&p1->mail);
 49             printf("家庭地址:");
 50             scanf("%s",&p1->home);
 51             printf("===================================\n");
 52             p2=p1;
 53             p1=(stu *)malloc(sizeof(stu));//申請下一個要用的空間
 54             if(p1!=NULL)
 55                 p2->next=p1;
 56             printf("請選擇是否繼續輸入:1.繼續  2.退出\n請選擇:");//用戶選擇
 57             scanf("%d",&ans);
 58             if(ans==1)//繼續
 59                 continue;
 60             else//退出
 61             {
 62                 printf("========輸入完畢========\n");
 63                 p2->next=NULL;
 64                 free(p1);//將申請的的無用內存釋放
 65                 break;
 66             }
 67         }
 68     }
 69 }
 70 void look(stu *p1)//顯示數據
 71 {
 72     printf("========顯示數據========\n");
 73     while(p1!=NULL)
 74     {
 75         printf("名字:%s\n",p1->name);
 76         printf("性別:%s\t",p1->wm);
 77         printf("工作單位:%s\t",p1->work);
 78         printf("手機:%s\t",p1->stel);
 79         printf("住宅號碼:%s\t",p1->htel);
 80         printf("E-Mail:%s\t",p1->mail);
 81         printf("家庭住址:%s\n",p1->home);
 82         printf("=====================================\n");
 83         p1=p1->next;
 84     }
 85     printf("========顯示完畢========\n");
 86 }
 87 void insert()//插入數據
 88 {
 89     int ans;//選擇插入位置
 90     char name[20];//插入者的名字
 91     printf("========插入數據========\n");
 92     stu *p1,*p2,*p3;
 93     p1=head;
 94     p3=(stu *)malloc(sizeof(stu));//申請內存
 95     p3->next=NULL;
 96     printf("請輸入插入者的數據:\n");
 97     printf("名字:");
 98     scanf("%s",&p3->name);
 99     printf("性別:");
100     scanf("%s",&p3->wm);
101     printf("工作單位:");
102     scanf("%s",&p3->work);
103     printf("手機:");
104     scanf("%s",&p3->stel);
105     printf("住宅號碼:");
106     scanf("%s",&p3->htel);
107     printf("E-Mail:");
108     scanf("%s",&p3->mail);
109     printf("家庭地址:");
110     scanf("%s",&p3->home);
111     printf("請選擇插入位置:1.首位置插入  2.尾部插入  3.插到某人前面\n請選擇:");
112     scanf("%d",&ans);
113     switch(ans)
114     {
115     case 1://放到頭指針
116         p3->next=p1;
117         head=p3;
118         break;
119     case 2://放到尾部
120         while(p1->next!=NULL)
121             p1=p1->next;
122         p1->next=p3;
123         break;
124     case 3://放到某人前面
125         printf("請輸入插到誰前面名字:");
126         scanf("%s",name);
127         while(strcmp(name,p1->name)!=0)
128         {
129             p2=p1;
130             p1=p1->next;
131         }
132         p2->next=p3;
133         p3->next=p1;
134         break;
135     }
136     printf("========插入成功========\n");
137 }
138 void deleted()//刪除數據
139 {
140     stu *p1,*p2;
141     char name[20];//刪除者名字
142     printf("========刪除數據========\n");
143     printf("請輸入要刪除者的名字:");
144     scanf("%s",name);
145     p1=head;
146     if(head==NULL)//通訊錄已經沒數據了
147     {
148         printf("通訊錄里什么也沒有了。\n");
149         printf("========刪除失敗========\n");
150         return;
151     }
152     if(!strcmp(name,p1->name))//頭指針就是要刪除的,為什么要單獨拿出來,就是為了可以在刪除后改變頭指針
153     {
154         head=p1->next;
155         printf("========刪除成功========\n");
156         return;
157     }
158     while(p1!=NULL&&strcmp(name,p1->name))
159     {
160         p2=p1;
161         p1=p1->next;
162     }
163     if(p1==NULL)//查找完了,沒找到
164     {
165         printf("查無此人!!!!\n");
166         printf("========刪除失敗========\n");
167         return;
168     }
169     if(p1->next!=NULL)//找到了,不是最后一個
170     {
171         p1=p1->next;
172         p2->next=p1;
173         printf("========刪除成功========\n");
174     }
175     else//是最后一個
176     {
177         p2->next=NULL;
178         printf("========刪除成功========\n");
179     }
180 }
181 void find(stu *p1)//查找數據
182 {
183     char name[20];//查找的名字
184     printf("========查看數據========\n");
185     printf("請輸入要查看人的名字:");
186     scanf("%s",name);
187     while(p1!=NULL)
188     {
189         if(!strcmp(name,p1->name))//找到了輸出
190         {
191             printf("你要查找人的數據:\n");
192             printf("名字:%s\n",p1->name);
193             printf("性別:%s\t",p1->wm);
194             printf("工作單位:%s\t",p1->work);
195             printf("手機:%s\t",p1->stel);
196             printf("住宅號碼:%s\t",p1->htel);
197             printf("E-Mail:%s\t",p1->mail);
198             printf("家庭住址:%s\n",p1->home);
199             break;
200         }
201         p1=p1->next;
202     }
203     if(p1==NULL)//找完了,沒找到
204         printf("您要查找的人不存在\n");
205     printf("========結束查找========\n");
206 }
207 void update(stu *p1)//修改數據
208 {
209     int n;//選擇的修改內容
210     char name[20];//要修改人的名字
211     printf("========修改數據========\n");
212     printf("請輸入要修改人的姓名:");
213     scanf("%s",name);
214     while(p1!=NULL)
215     {
216         if(!strcmp(name,p1->name))//找到了
217         {
218             printf("要修改人的數據為:\n");
219             printf("名字:%s\n",p1->name);
220             printf("性別:%s\t",p1->wm);
221             printf("工作單位:%s\t",p1->work);
222             printf("手機:%s\t",p1->stel);
223             printf("住宅號碼:%s\t",p1->htel);
224             printf("E-Mail:%s\t",p1->mail);
225             printf("家庭住址:%s\n",p1->home);
226             printf("請選擇要修改的信息:");
227             printf("\t1.名字\t2.性別\t3.工作單位\t4.手機\t5.住宅號碼\t6.E-Mail\t7.家庭住址\n\n您的選擇是(1~7):");
228             scanf("%d",&n);
229             switch(n)
230             {
231                 case 1:
232                     printf("名字:");
233                     scanf("%s",&p1->name);
234                     break;
235                 case 2:
236                     printf("性別:");
237                     scanf("%s",&p1->wm);
238                     break;
239                 case 3:
240                     printf("工作單位:");
241                     scanf("%s",&p1->work);
242                     break;
243                 case 4:
244                     printf("手機:");
245                     scanf("%s",&p1->stel);
246                     break;
247                 case 5:
248                     printf("住宅號碼:");
249                     scanf("%s",&p1->htel);
250                     break;
251                 case 6:
252                     printf("E-Mail:");
253                     scanf("%s",&p1->mail);
254                     break;
255                 case 7:
256                     printf("家庭住址:");
257                     scanf("%s",&p1->home);
258                     break;
259             }
260             printf("========修改成功========\n");
261             break;
262         }
263         p1=p1->next;
264     }
265     if(p1==NULL)//找完了,沒有找到
266     {
267         printf("沒有找到該人信息\n");
268         printf("========修改失敗========\n");
269     }
270 }
271 void save(stu *p1)//保存數據
272 {
273     printf("========保存數據========\n");
274     FILE *fp;
275     char file[15];
276     printf("請輸入文件名:");
277     scanf("%s",file);
278     if((fp=fopen(file,"w"))==NULL)//打開文件,若沒有此文件可以新建立
279     {
280         printf("cannot open this file\n");//無法建立新文件
281         exit(0);//正常運行程序並退出程序
282     }
283     fprintf(fp,"名字\t性別\t工作單位\t手機\t住宅號碼\tE-Mail\t家庭住址\n");
284     while(p1!=NULL)
285     {
286         fprintf(fp,"%s\t",p1->name);
287         fprintf(fp,"%s\t",p1->wm);
288         fprintf(fp,"%s\t",p1->work);
289         fprintf(fp,"%s\t",p1->stel);
290         fprintf(fp,"%s\t",p1->htel);
291         fprintf(fp,"%s\t",p1->mail);
292         fprintf(fp,"%s\n",p1->home);
293         p1=p1->next;
294     }
295     printf("========保存成功========\n");
296     fclose(fp);//關閉文件,這個要記得
297 }
298 int main(void)
299 {
300     int n;
301     screen();
302     while(1)
303     {
304         printf("請輸入你的選擇(1~9):");
305         scanf("%d",&n);
306         if(n<1||n>9)
307         {
308             printf("輸入有誤!");
309             continue;
310         }
311         switch(n)
312         {
313         case 1:
314             input();
315             break;
316         case 2:
317             look(head);
318             break;
319         case 3:
320             insert();
321             break;
322         case 4:
323             deleted();
324             break;
325         case 5:
326             find(head);
327             break;
328         case 6:
329             update(head);
330             break;
331         case 7:
332             save(head);
333             break;
334         case 8:
335             screen();
336             break;
337         case 9:
338             printf("===============歡迎你再次使用通訊錄系統===============\n");
339             exit(1);//退出系統
340             break;
341         }
342     }
343 }

 

 

———————————————————————————————————————————————————————————————————————

付出

凌晨四點的太陽:

Kobe:Do you know what Los Angeles looks like at four in the morning?

“I don't know. What do you think of Los Angeles at four every morning.”

Kobe:Full of stars, few lights, few pedestrians.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM