當前位置:首頁 » 編程語言 » c語言通訊錄

c語言通訊錄

發布時間: 2022-01-11 23:06:23

c語言編寫通信錄

/*編程建立一通訊簿,存放有姓名、電話號碼、住址,然後對通信簿進行查找、添加、修改及刪除。*/
#include<stdio.h>
struct person
{
char name[8];
char tel[15];
char addr[50];
};
char filename[20];
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void delete();
main()
{
int m;

creat();
while(1)
{
printf("\n\n添加,請按1");
printf("\n查找,請按2");
printf("\n修改,請按3");
printf("\n刪除,請按4");
printf("\n輸出,請按5");
printf("\n退出,請按0\n");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: delete();
break;
case 5: output();
break;
case 0: exit();
}
printf("\n\n操作完畢,請再次選擇!");
}
else
printf("\n\n選擇錯誤,請再次選擇!");
}
}
void creat()
{
struct person one;
long s1;
printf("\n請輸入通訊簿名:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\n不能建立通訊簿!");
exit();
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","電話號碼","住址");
printf("\n請輸入姓名、電話號碼及住址(以0結束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
printf("\n\n%20s\n","通 訊 簿");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
printf("\n請輸入添加的姓名、電話號碼及住址\n");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
fclose(fp);
}
void search()
{
int k=0;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}
void modify()
{
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n請輸入新姓名、電話號碼及住址:");
scanf("%s%s%s",one.name,one.tel,one.addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
}
else
printf("\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}
void delete()
{
int m,k=0,flag;
long offset1,offset2;
char namekey[8], valid[4];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊簿!");
exit();
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,記錄為");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n確實要刪除,按1;不刪除,按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s\n","","","");
}
}
else
printf("\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}

Ⅱ c語言通訊錄代碼

#include
#include
#include
#include
#define maxlen 15
struct persons
{
int num;
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
}persons[maxlen];

typedef struct lnode{
int num;
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
struct lnode *next;
}listnode,*linklist;

linklist head=NULL,r=NULL;
listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
int i;
char name1[10],ch;char tel_no1[15];char arch1;char sim_no1;char e_addr1[20];
char s1[20];
FILE *fp;

void creat()
{
int j;
long k;
fp=fopen("數據文件.txt","r t");
if(fp!=NULL)
{for(i=0;i<=maxlen;i++ )
{ j=fgetc(fp);
if(j==EOF)
return;
k=i;
fseek(fp,k*sizeof(struct persons),0);
fread(&persons[i],sizeof(struct persons),1,fp);

s=(linklist)malloc(sizeof(listnode));
s->num=persons[i].num;
strcpy(s->name,persons[i].name);
strcpy(s->e_addr,persons[i].e_addr);
strcpy(s->tel_no,persons[i].tel_no);
s->sim_no=persons[i].sim_no;
s->arch=persons[i].arch;
if(head==NULL)
{head=s;r=head;head->next=NULL;}
else
{r->next=s;
r=s;r->next=NULL;
}}fclose(fp);}
else
{ fp=fopen("數據文件.txt","w");
i=1;
}
}
void ShowA()
{p1=head;

while(p1!=NULL)
{if(p1->arch=='A')
{
cout<<endl;
cout<name<<endl;
cout<tel_no<<endl;
cout<sim_no<<endl;}
p1=p1->next;continue;}
}
void ShowB()
{p1=head;

while(p1!=NULL)
{if(p1->arch=='B')
{
cout<<endl;
cout<name<<endl;
cout<tel_no<<endl;
cout<sim_no<<endl;}
p1=p1->next;continue;}
}
void ShowC()
{p1=head;

while(p1!=NULL)
{if(p1->arch=='C')
{
cout<<endl;
cout<name<<endl;
cout<tel_no<<endl;
cout<sim_no<<endl;}
p1=p1->next;continue;}
}
void Show()
{char ch1;
printf(" Which arch do you want to show?\n");
cout<<" A:同學同事"<<"B:家人"<<"C:其他"<<endl;
cout<<"請選擇:"<<endl;
cin>>ch1;
switch(ch1)
{case'A':ShowA();break;
case'B':ShowB();break;
case'C':ShowC();break;
default:cout<<"error"<<endl;
}
}

void Delete()
{ printf("\n\n\t 請輸入要刪除用戶的姓名:");

cin>>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 Input()
{ s=(linklist)malloc(sizeof(listnode));

printf("\n\n\t 請輸入該用戶的信息:");

cout<<"name:";
cin>>s->name;
cout<<"tel_no:";
cin>>s->tel_no;
cout<<"sim_no:";
cin>>s->sim_no;
cout<<"e_addr:";
cin>>s->e_addr;
cout<<"arch:";
cin>>s->arch;

if(head==NULL)printf("\n\n");
else
{p8=head;
while(p8!=NULL&&strcmp(s->name,p8->name)!=0&&strcmp(s->tel_no,p8->tel_no)!=0)
p8=p8->next;
if(p8!=NULL)
{printf(" 您添加的用戶已存在!");
free(s);}}

if(head==NULL)
{head=s;head->next=NULL;}
else
{r->next=s;
r=s;r->next=NULL;}
}

void Alter()
{

printf("\n\n\t 請輸入姓名:");

cin>>name1;
p3=head;
while(p3!=NULL&&strcmp(name1,p3->name)!=0)
p3=p3->next;
if(p3==NULL)
printf("\n\n\t 您選擇的用戶不存在!");
else
{ printf("\n\n\t 請輸入該用戶的新信息!");

cout<<"name:";
cin>>name1;
cout<<"tel_no:";
cin>>tel_no1;
cout<<"e_addr:";
cin>>e_addr1;
cout<<"sim_no:";
cin>>sim_no1;
cout<<"arch:";
cin>>arch1;
while(p9!=NULL&&strcmp(name1,p9->name)!=0&&strcmp(tel_no1,p9->tel_no)!=0)
p9=p9->next;
if(p9==NULL)
{strcpy(p3->name,name1);
strcpy(p3->tel_no,tel_no1);
strcpy(p3->e_addr,e_addr1);
p3->sim_no=sim_no1;
p3->arch=arch1;
}
else
printf(" 您添加的用戶已存在!");

}
}
void Dial()
{int a;char b;
cout<<"1.撥叫用戶:"<<endl;
cout<<"2.使用單鍵撥號:"<<endl;
cin>>a;
p0=head;
if(a==1)
{cout<<" 請輸入撥叫用戶的姓名:";
cin>>name1;
while(strcmp(name1,p0->name)!=0&&p0!=NULL)
p0=p0->next;
if(p0==NULL)
cout<<" 您選擇的用戶不存在!";
else
/*for(c=0;c<15;c++)
{cout<<endl;
cout<<(*p0).tel_no[c];}*/
{cout<<endl;
cout<tel_no;
cout<<endl;}
}
else
{cout<<"請輸入單鍵號碼:";
cin>>b;
while(p0!=NULL&&b!=p0->sim_no)
p0=p0->next;
if(p0==NULL)
cout<<" 您輸入的單鍵號碼不存在!";
else
{cout<<endl;
cout<name<<endl;
cout<tel_no;
cout<<endl;}
}
}

void Save()
{ int j;
fp=fopen("數據文件.txt","w");
for(p2=head,j=0;p2!=NULL;j++ ,p2=p2->next)
{
strcpy(persons[j].name,p2->name);
strcpy(persons[j].tel_no,p2->tel_no);
persons[j].sim_no=p2->sim_no;
strcpy(persons[j].e_addr,p2->e_addr);
persons[j].arch=p2->arch;
fwrite(&persons[j],sizeof(struct persons),1,fp);
}
}

void main()
{ creat();
do
{
printf(" **********************************************************");
printf("\n\n\n\n\t 歡迎來到通訊錄!");
printf("\n\n\t\t 請選擇操作:");
printf("\n\t\t1. 顯示通訊錄");
printf("\n\t\t2. 刪除通訊錄");

printf("\n\t\t3. 添加通訊錄");
printf("\n\t\t4. 編輯通訊錄");
printf("\n\t\t5. 撥號功能");
printf("\n\t\t6. 保存剛才的操作並退出\n");
printf(" **********************************************************");
printf("\n\n\n");
printf("\t 選擇您想要的操作:");
cin>>ch;
switch(ch)
{ case '1': Show();
break;
case '2': Delete();
break;
case '3': Input();
break;
case '4': Alter();
break;
case '5': Dial();
break;
case '6': Save();
fclose(fp);
exit(0);
break;
default:
printf("\n\t*********************************\n");
printf("\n\t The num should 1-6!!! \n");
printf("\n\t**********************************");
break;
}
}while(1);
}

轉的.......woyuyuchao

Ⅲ c語言通訊錄

關於這道題的基本思路,我可以告訴你:
通訊錄一般由如下幾個信息組成:姓名、性別、通訊地址、電話號碼、郵編等組成。
如果想編寫一個20個人的通訊錄程序,那麼就可以定義一個大小為 20 的結構數組。C 語言詳細代碼如下:
#include <stdio.h>
#define ADDRESS_LEN 100 /* 通訊地址長度宏定義,可以根據需要進行修改 */
#define PHONENUM_LEN 20 /* 電話號碼長度宏定義,可以自行修改 */
#define NUMBER 20 /* 20 個人的通訊錄,可以自行修改 */
struct address /* 定義一個通訊錄的結構數組 */

{
char name[20] ; /* 姓名 */

char sex[5] ; /* 性別 */

char address[ADDRESS_LEN] ; /* 通訊地址 */

char telepone_num[PHONENUM_LEN] ; /* 電話號碼 */

char zip[10 ] ; /* 郵政編碼 */

} ;
void main( )
{
int i = 0 ;

struct address my_address[NUMBER] ;

for( i = 0 ; i < NUMBER ; i ++ )
{

gets(my_address[i].name) ;

gets(my_address[i].sex) ;

gets(my_address[i].address);

gets(my_address[i].telephone_num);

gets(my_address[i].zip);

}

for( i = 0 ; i < NUMBER ; i ++ )
printf("%s\t%s\t%s\t%s\t%s\n", my_address[i].name,my_address[i].sex,my_address[i].address,my_address[i].telephone_num,my_address[i].zip);

}
你可以將該程序輸入到電腦中,上機編譯、鏈接、並運行試一試。

Ⅳ 用C語言寫一個通訊錄

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef struct { //通訊錄結點類型
char num[5]; //編號
char name[9]; //姓名
char sex[3]; //性別
char phone[13]; //電話
char addr[31]; //地址
} DataType;
typedef struct node { //結點類型定義
DataType data; //結點數據域
struct node *next; //結點指針域
} ListNode;
typedef ListNode *LinkList;
LinkList head;
ListNode *p;
//函數說明
int menu_select();
LinkList CreateList(void);
void InsertNode(LinkList head,ListNode *p);
ListNode *ListFind(LinkList head);
void DelNode(LinkList head);
void printList(LinkList head);
//主函數
void main()
{
for( ; ; ){
switch(menu_select( ) )
{
case 1:
printf("**********************************\n");
printf("* 通 訊 錄 鏈 表 的 建 立 *\n");
printf("**********************************\n");
head=CreateList( );
break;
case 2:
printf("**********************************\n");
printf("* 通 訊 者 信 息 的 添 加 *\n");
printf("**********************************\n");
printf("編號(4) 姓名(8) 性別(3) 電話(11) 地址(31)\n");
printf("************************************* \n");
p=(ListNode *)malloc(sizeof(ListNode)); //申請新結點
scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,
p->data.phone,p->data.addr);
InsertNode(head,p);
break;
case 3:
printf("***********************************\n");
printf("* 通 訊 錄 信 息 的 查 詢 *\n");
printf("***********************************\n");
p=ListFind(head);
if (p!=NULL) {
printf("編號 姓 名 性別 聯系電話 地址 \n");
printf("--------------------------------------------------\n");
printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,
p->data.sex,p->data.phone,p->data.addr);
printf("---------------------------------------------------\n");
}
else
printf("沒有查到要查詢的通訊者!\n");
break;
case 4:
printf("***********************************\n");
printf("* 通 訊 錄 信 息 的 刪 除 *\n");
printf("***********************************\n");
DelNode(head); //刪除結點
break;
case 5:
printf("************************************\n");
printf("* 通 訊 錄 鏈 表 的 輸 出 *\n");
printf("************************************\n");
printList(head);
break;
case 0:
printf("\t 再 見! \n");
return;
}
}
}
/*******************/
/* 菜單選擇函數程序 */
/***************************/
int menu_select( )
{
int sn;
printf(" 通訊錄管理系統 \n");
printf("===================\n");
printf(" 1.通訊鏈表的建立\n");
printf(" 2.通訊者結點的插入\n");
printf(" 3.通訊者結點的查詢\n");
printf(" 4.通訊者結點的刪除\n");
printf(" 5.通訊錄鏈表的輸出\n");
printf(" 0.退出管理系統\n");
printf("==========================\n");
printf(" 請 選 擇 0-5: ");
for( ; ; )
{
scanf("%d",&sn);
if (sn<0||sn>5)
printf("\n\t輸入錯誤,重選0-5:");
else
break;
}
return sn;
}
/**************************/
/*用尾插法建立通訊錄鏈表函數 */
/**************************/
LinkList CreateList(void)
{//尾插法建立帶頭結點的通訊錄鏈表演算法
LinkList head=(ListNode *)malloc(sizeof(ListNode)); //申請頭結點
ListNode *p,*rear;
int flag=0; //結束標志置0
rear=head; //尾指針初始指向頭結點
while (flag==0)
{ p=(ListNode *)malloc(sizeof(ListNode)); //申新結點
printf("編號(4) 姓名(8) 性別 電話(11) 地址(31)\n");
printf("--------------------------------------------------------------------------------------\n");
scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,p->data.phone,
p->data.addr);
rear->next=p; //新結點連接到尾結點之後
rear=p; //尾指針指向新結點
printf("結束建表嗎?(1/0):");
scanf("%d",&flag);
}
rear->next=NULL; //終端結點指針置空
return head; //返回鏈表頭指針
}
/******************************/
/*在通訊錄鏈表head中插入結點 */
/******************************/
void InsertNode(LinkList head,ListNode *p)
{
ListNode *p1,*p2;
p1=head;
p2=p1->next;
while(p2!=NULL && strcmp(p2->data.num,p->data.num)<0)
{
p1=p2; //p1指向剛訪問過的結點
p2=p2->next; //p2指向表的下一個結點
}
p1->next=p; //插入p所指向的結點
p->next=p2; //連接表中剩餘的結點
}
/******************************************/
/* 有序通訊錄鏈表的查找 */
/******************************************/
ListNode *ListFind(LinkList head)
{// 有序通訊錄鏈表上的查找
ListNode *p;
char num[5];
char name[9];
int xz;
printf("==================\n");
printf(" 1. 按編號查詢 \n");
printf(" 2. 按姓名查詢 \n");
printf("==================\n");
printf(" 請 選 擇: ");
p=head->next; //假定通訊 錄表帶頭結點
scanf("%d",&xz);
if (xz==1) {
printf("請輸入要查找者的編號:");
scanf("%s",num);
while (p&&strcmp(p->data.num,num)<0)
p=p->next;
if ((p==NULL)||strcmp(p->data.num,num))0;
p=NULL; //沒有查到要查找的通訊者
}
else
if (xz==2) {
printf(" 請輸入要查找者的姓名:");
scanf("%s",name);
while(p&&strcmp(p->data.name,name)!=0)
p=p->next;
}
return p;
}
/*******************************/
/* 通訊錄鏈表上的結點刪除 */
/*********************************/
void DelNode(LinkList head)
{
char jx;
ListNode *p,*q;
p=ListFind(head); //調用查找函數
if (p==NULL) {
printf("沒有查到要刪除的通訊者!\n");
return;
}
printf("真的要刪除該結點嗎?(y/n):");
scanf("%c",&jx);
if (jx=='y'||jx=='Y') {
q=head;
while ((q!=NULL) &&(q->next!=p))
q=q->next;
q->next=p->next; //刪除結點
free(p); //釋放被刪結點空間
printf("通訊者已被刪除!\n");
}
}
/**********************************/
/* 通訊錄鏈表的輸出函數 */
/**********************************/
void printList(LinkList head)
{
ListNode *p;
p=head->next;
printf("編號 姓 名 性別 聯系電話 地址 \n");
printf("--------------------------------------------------------------------------------\n");
while (p!=NULL)
{ printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,
p->data.phone,p->data.addr);
printf("---------------------------------------------------------------------------------\n");
p=p->next; //後移一個結點
}
}

Ⅳ c語言通訊錄編寫

靠山山倒,靠樹樹倒,靠自己最好!
自己寫吧,一個
結構體{
姓名;
性別;
年齡;
手機;
}
如果不會用鏈表,那你就用數組!
錄入:就是往數組里寫入數據
刪除:就清為0就可以了唄
查詢:就是從數組的第一項到保存數查找一遍嘍。
大哥
,你是在做課程設計吧?計科的?
傷不起!!!!!!

Ⅵ 通訊錄代碼 C語言 跪求

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define N 100 void input();//添加新用戶函數
void amend();//修改用戶信息函數
void delete_client();//刪除用戶信息函數
void demand_client();//用戶信息查詢函數
void collect_telephone();//用戶信息匯總函數
void save_client(struct telephone message);//保存函數
void demand_name();//按用戶名查詢
void demand_telephone();//按電話號碼查詢

struct telephone
{
char client_name[20];
char client_address[30];
char client_telephone[15];
}; //添加新用戶函數
void input()
{
struct telephone message;
char reply='y';
char save='y';
while (reply=='y')
{ printf("用戶姓名:");
scanf("%s",message.client_name);

printf("電話號碼:");
scanf("%s",message.client_telephone); save_client(message);

printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //保存函數
void save_client(struct telephone message)
{
FILE *fp;
fp=fopen("message.dat","a+");
if (fp!=NULL)
{
fwrite(&message,sizeof(struct telephone),1,fp);
}
else
{
printf("\n打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
} //修改用戶信息函數
void amend()
{
struct telephone message;
FILE *fp;
char amend_name[20];
char reply='y';
char found='y';
char save='y';
int size=sizeof(struct telephone);
while (reply=='y')
{
found='n';
fp=fopen("message.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("\n請輸入要修改的姓名:");
scanf("%s",amend_name);
while ((fread(&message,size,1,fp))==1)
{
if ((strcmp(amend_name,message.client_name))==0)
{
found='y';
break;
}
}
if (found=='y')
{ printf("==========================================\n");
printf("\n用戶姓名:%s\n",message.client_name);

printf("\n電話號碼:%s\n",message.client_telephone);
printf("==========================================\n");
printf("修改用戶信息:\n");
printf("\n用戶姓名:");
scanf("%s",message.client_name); printf("\n電話號碼:");
scanf("%s",message.client_telephone);
printf("\n要保存嗎?(y/n):");
scanf(" %c",&save);
if (save=='y')
{
fseek(fp,-size,1);
fwrite(&message,sizeof(struct telephone),1,fp);
}
}
else
{
printf("無此人信息!\n");
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //刪除用戶信息函數
void delete_client()
{
struct telephone message[N];
struct telephone temp_str;
struct telephone delete_str;
int i=0,j=0;
char reply='y';
char found='y';
char confirm='y';
char delete_name[20];
FILE *fp;
while (reply=='y')
{
system("cls");
fp=fopen("message.dat","r");
if (fp!=NULL)
{
i=0;
found='n';
printf("\n請輸入姓名:");
scanf("%s",delete_name);
while ((fread(&temp_str,sizeof(struct telephone),1,fp))==1)
{
if ((strcmp(delete_name,temp_str.client_name))==0)
{
found='y';
delete_str=temp_str;
}//查找要刪除的記錄
else
{
message[i]=temp_str;
i++;
}//將其它無關記錄保存起來
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
if (found=='y')
{
printf("==========================================\n");
printf("用戶姓名:%s\n",delete_str.client_name);

printf("電話號碼:%s\n",delete_str.client_telephone);
printf("==========================================\n");
}
else
{
printf("無此人信息,按任意鍵返回……\n");
getchar();
break;
}
printf("確定要刪除嗎?(y/n):");
scanf(" %c",&confirm);
if (confirm=='y')
{
fp=fopen("message.dat","w");
if (fp!=NULL)
{
for(j=0;j<i;j++)
{
fwrite(&message[j],sizeof(struct telephone),1,fp);
}
printf("記錄已刪除!!!\n");
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
}
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
}
//用戶信息查詢函數
void demand_client()
{
int choice=1;
while (choice!=3)
{
system("cls");
printf("電話查詢菜單\n");
printf(" 1 按聯系人姓名查詢\n");
printf(" 2 按聯系人電話號碼查詢\n");
printf(" 3 返回主菜單\n");
printf("請選擇(1-3):");
scanf("%d%*c",&choice);
if (choice>3)
{
printf("請輸入1-6之間的整數\n");
printf("按任意鍵返回菜單……\n");
getchar();
continue;
}
if (choice==1)
{
demand_name();
}
else if (choice==2)
{
demand_telephone();
}
}
} //按用戶名查詢
void demand_name()
{
struct telephone message;
FILE *fp;
char amend_name[20];
char reply='y';
char found='y';
while (reply=='y')
{
found='n';
fp=fopen("message.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("\n請輸入姓名:");
scanf("%s",amend_name);
while ((fread(&message,sizeof(struct telephone),1,fp))==1)
{
if ((strcmp(amend_name,message.client_name))==0)
{
found='y';
break;
}
}
if (found=='y')
{ printf("==========================================\n");
printf("用戶姓名:%s\n",message.client_name); printf("電話號碼:%s\n",message.client_telephone);
printf("==========================================\n");
}
else
{
printf("無此人信息!\n");
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //按電話號碼查詢
void demand_telephone()
{
struct telephone message;
FILE *fp;
char telephone[20];
char reply='y';
char found='y';
while (reply=='y')
{
found='n';
fp=fopen("message.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("\n請輸入電話號碼:");
scanf("%s",telephone);
while ((fread(&message,sizeof(struct telephone),1,fp))==1)
{
if ((strcmp(telephone,message.client_telephone))==0)
{
found='y';
break;
}
}
if (found=='y')
{ printf("==========================================\n");
printf("用戶姓名:%s\n",message.client_name); printf("電話號碼:%s\n",message.client_telephone);
printf("==========================================\n");
}
else
{
printf("無此電話號碼的有關信息!\n");
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("要繼續嗎?(y/n):");
scanf(" %c",&reply);
}
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
} //用戶信息匯總函數
void collect_telephone()
{
struct telephone message;
FILE *fp;
fp=fopen("message.dat","r");
if (fp!=NULL)
{
system("cls");
printf("\n用戶姓名\t\t電話號碼\n");
while ((fread(&message,sizeof(struct telephone),1,fp))==1)
{
printf("\n%-24s",message.client_name); printf("%-12s\n",message.client_telephone);
}
}
else
{
printf("打開文件時出現錯誤,按任意鍵返回……\n");
getchar();
return;
}
fclose(fp);
printf("按任意鍵返回主菜單……\n");
getch();
} void main()
{
char choice[10]="";
int len=0;
while (choice[0]!='7')
{ printf("\t==========電話本號碼查詢系統=============\n"); printf("\t\t 1、添加新聯系人\n");
printf("\t\t 2、修改聯系人信息\n");
printf("\t\t 3、刪除聯系人信息\n");
printf("\t\t 4、聯系人信息查詢\n");
printf("\t\t 5、聯系人信息匯總\n");

printf("\t\t 7、退出\n");
printf("\t=========================================\n");
printf("請選擇(1-7):");
scanf("%s",choice);
len=strlen(choice);
if (len>1)
{
printf("請輸入1-6之間的整數\n");
printf("按任意鍵返回主菜單……\n");
getchar();
getchar();
continue;
} switch (choice[0]) {
case '1':
input();
break;
case '2':
amend();
break;
case '3':
delete_client();
break;
case '4':
demand_client();
break;
case '5':
collect_telephone();
break; default:
break;

}
}
}

Ⅶ C語言製作簡單的通訊錄

主要是鏈表的操作,直接定義個結構體和指針的運用。

熱點內容
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:98
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:757
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354
入門編程游戲的書 發布:2024-09-20 03:31:26 瀏覽:236
e盒的演算法 發布:2024-09-20 03:30:52 瀏覽:144
win10登錄密碼如何修改登錄密碼 發布:2024-09-20 03:09:43 瀏覽:71