自己看吧
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
char name[20];//名字
char wm[20];//性別
char work[100];//作業單位
char stel[20];//手機
char htel[20];//住宅號碼
char mail[20];//E-Mail
char home[100];//家庭住址
struct student *next;
}stu;
stu *head;//頭指標
void screen()//主選單
{
printf("\n=======================================================\n");
printf(" 歡迎來到通訊錄管理系統\n\n");
printf(" 1.輸入資料 2.顯示資料\n");
printf(" 3.插入資料 4.洗掉資料\n");
printf(" 5.查看資料 6.修改資料\n");
printf(" 7.保存資料 8.回傳主選單\n");
printf("\n~~~~~~輸~~~~~~入~~~~~~9~~~~~~退~~~~~~出~~~~~~程~~~~~~序\n");
}
void input()//輸入資料
{
int ans;//判斷是否繼續輸入
stu *p1,*p2;
p1=(stu *)malloc(sizeof(stu));//申請記憶體來用
if(p1!=NULL)
{
printf("========輸入資料========\n");
head=p1;
while(1)
{
printf("名字:");
scanf("%s",&p1->name);
printf("性別:");
scanf("%s",&p1->wm);
printf("作業單位:");
scanf("%s",&p1->work);
printf("手機:");
scanf("%s",&p1->stel);
printf("住宅號碼:");
scanf("%s",&p1->htel);
printf("E-Mail:");
scanf("%s",&p1->mail);
printf("家庭地址:");
scanf("%s",&p1->home);
printf("===================================\n");
p2=p1;
p1=(stu *)malloc(sizeof(stu));//申請下一個要用的空間
if(p1!=NULL)
p2->next=p1;
printf("請選擇是否繼續輸入:1.繼續 2.退出\n請選擇:");//用戶選擇
scanf("%d",&ans);
if(ans==1)//繼續
continue;
else//退出
{
printf("========輸入完畢========\n");
p2->next=NULL;
free(p1);//將申請的的無用記憶體釋放
break;
}
}
}
}
void look(stu *p1)//顯示資料
{
printf("========顯示資料========\n");
while(p1!=NULL)
{
printf("名字:%s\n",p1->name);
printf("性別:%s\t",p1->wm);
printf("作業單位:%s\t",p1->work);
printf("手機:%s\t",p1->stel);
printf("住宅號碼:%s\t",p1->htel);
printf("E-Mail:%s\t",p1->mail);
printf("家庭住址:%s\n",p1->home);
printf("=====================================\n");
p1=p1->next;
}
printf("========顯示完畢========\n");
}
void insert()//插入資料
{
int ans;//選擇插入位置
char name[20];//插入者的名字
printf("========插入資料========\n");
stu *p1,*p2,*p3;
p1=head;
p3=(stu *)malloc(sizeof(stu));//申請記憶體
p3->next=NULL;
printf("請輸入插入者的資料:\n");
printf("名字:");
scanf("%s",&p3->name);
printf("性別:");
scanf("%s",&p3->wm);
printf("作業單位:");
scanf("%s",&p3->work);
printf("手機:");
scanf("%s",&p3->stel);
printf("住宅號碼:");
scanf("%s",&p3->htel);
printf("E-Mail:");
scanf("%s",&p3->mail);
printf("家庭地址:");
scanf("%s",&p3->home);
printf("請選擇插入位置:1.首位置插入 2.尾部插入 3.插到某人前面\n請選擇:");
scanf("%d",&ans);
switch(ans)
{
case 1://放到頭指標
p3->next=p1;
head=p3;
break;
case 2://放到尾部
while(p1->next!=NULL)
p1=p1->next;
p1->next=p3;
break;
case 3://放到某人前面
printf("請輸入插到誰前面名字:");
scanf("%s",name);
while(strcmp(name,p1->name)!=0)
{
p2=p1;
p1=p1->next;
}
p2->next=p3;
p3->next=p1;
break;
}
printf("========插入成功========\n");
}
void deleted()//洗掉資料
{
stu *p1,*p2;
char name[20];//洗掉者名字
printf("========洗掉資料========\n");
printf("請輸入要洗掉者的名字:");
scanf("%s",name);
p1=head;
if(head==NULL)//通訊錄已經沒資料了
{
printf("通訊錄里什么也沒有了,\n");
printf("========洗掉失敗========\n");
return;
}
if(!strcmp(name,p1->name))//頭指標就是要洗掉的,為什么要單獨拿出來,就是為了可以在洗掉后改變頭指標
{
head=p1->next;
printf("========洗掉成功========\n");
return;
}
while(p1!=NULL&&strcmp(name,p1->name))
{
p2=p1;
p1=p1->next;
}
if(p1==NULL)//查找完了,沒找到
{
printf("查無此人!!!!\n");
printf("========洗掉失敗========\n");
return;
}
if(p1->next!=NULL)//找到了,不是最后一個
{
p1=p1->next;
p2->next=p1;
printf("========洗掉成功========\n");
}
else//是最后一個
{
p2->next=NULL;
printf("========洗掉成功========\n");
}
}
void find(stu *p1)//查找資料
{
char name[20];//查找的名字
printf("========查看資料========\n");
printf("請輸入要查看人的名字:");
scanf("%s",name);
while(p1!=NULL)
{
if(!strcmp(name,p1->name))//找到了輸出
{
printf("你要查找人的資料:\n");
printf("名字:%s\n",p1->name);
printf("性別:%s\t",p1->wm);
printf("作業單位:%s\t",p1->work);
printf("手機:%s\t",p1->stel);
printf("住宅號碼:%s\t",p1->htel);
printf("E-Mail:%s\t",p1->mail);
printf("家庭住址:%s\n",p1->home);
break;
}
p1=p1->next;
}
if(p1==NULL)//找完了,沒找到
printf("您要查找的人不存在\n");
printf("========結束查找========\n");
}
void update(stu *p1)//修改資料
{
int n;//選擇的修改內容
char name[20];//要修改人的名字
printf("========修改資料========\n");
printf("請輸入要修改人的姓名:");
scanf("%s",name);
while(p1!=NULL)
{
if(!strcmp(name,p1->name))//找到了
{
printf("要修改人的資料為:\n");
printf("名字:%s\n",p1->name);
printf("性別:%s\t",p1->wm);
printf("作業單位:%s\t",p1->work);
printf("手機:%s\t",p1->stel);
printf("住宅號碼:%s\t",p1->htel);
printf("E-Mail:%s\t",p1->mail);
printf("家庭住址:%s\n",p1->home);
printf("請選擇要修改的資訊:");
printf("\t1.名字\t2.性別\t3.作業單位\t4.手機\t5.住宅號碼\t6.E-Mail\t7.家庭住址\n\n您的選擇是(1~7):");
scanf("%d",&n);
switch(n)
{
case 1:
printf("名字:");
scanf("%s",&p1->name);
break;
case 2:
printf("性別:");
scanf("%s",&p1->wm);
break;
case 3:
printf("作業單位:");
scanf("%s",&p1->work);
break;
case 4:
printf("手機:");
scanf("%s",&p1->stel);
break;
case 5:
printf("住宅號碼:");
scanf("%s",&p1->htel);
break;
case 6:
printf("E-Mail:");
scanf("%s",&p1->mail);
break;
case 7:
printf("家庭住址:");
scanf("%s",&p1->home);
break;
}
printf("========修改成功========\n");
break;
}
p1=p1->next;
}
if(p1==NULL)//找完了,沒有找到
{
printf("沒有找到該人資訊\n");
printf("========修改失敗========\n");
}
}
void save(stu *p1)//保存資料
{
printf("========保存資料========\n");
FILE *fp;
char file[15];
printf("請輸入檔案名:");
scanf("%s",file);
if((fp=fopen(file,"w"))==NULL)//打開檔案,若沒有此檔案可以新建立
{
printf("cannot open this file\n");//無法建立新檔案
exit(0);//正常運行程式并退出程式
}
fprintf(fp,"名字\t性別\t作業單位\t手機\t住宅號碼\tE-Mail\t家庭住址\n");
while(p1!=NULL)
{
fprintf(fp,"%s\t",p1->name);
fprintf(fp,"%s\t",p1->wm);
fprintf(fp,"%s\t",p1->work);
fprintf(fp,"%s\t",p1->stel);
fprintf(fp,"%s\t",p1->htel);
fprintf(fp,"%s\t",p1->mail);
fprintf(fp,"%s\n",p1->home);
p1=p1->next;
}
printf("========保存成功========\n");
fclose(fp);//關閉檔案,這個要記得
}
int main()
{
int n;
screen();
while(1)
{
printf("請輸入你的選擇(1~9):");
scanf("%d",&n);
if(n<1||n>9)
{
printf("輸入有誤!");
continue;
}
switch(n)
{
case 1:
input();
break;
case 2:
look(head);
break;
case 3:
insert();
break;
case 4:
deleted();
break;
case 5:
find(head);
break;
case 6:
update(head);
break;
case 7:
save(head);
break;
case 8:
screen();
break;
case 9:
printf("===============歡迎你再次使用通訊錄系統===============\n");
exit(1);//退出系統
break;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/396191.html
標籤:其他
上一篇:如何在角度回圈中使用日期管道?
