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

通訊錄c語言

發布時間: 2022-08-10 18:24:41

『壹』 c語言課程通訊錄的設計與實現用到哪些知識點與演算法

通訊錄的數據需要定義一個結構體來保存,比如這樣:
struct TX{
char name[10];
char tel[12];//也可以用長整形 long
...
}
所以會用到結構體數組的輸入、顯示、查詢、修改、插入、刪除、(可能包括排序演算法)、文件保存和文件讀取這些知識點。

『貳』 通訊錄代碼 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語言做通訊錄

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct Telephone
{
char number[200];
char name[20];
char off[20];
char addrass[20];
char mail[20];
char telephone[20];
struct Telephone *next;
};
typedef struct Telephone TEL;
TEL *head=NULL;
void Menu(); /*菜單*/
void Crease(); /*添加條目*/
void print(); /*輸出條目*/
void Search(); /*查找條目( 按姓名 )*/
void Delate(); /*刪除信息*/
void Save(); /*保存到文件*/
void Open(); /*打開文件*/
void Change(); /*修改信息*/
void Arrange(); /*排序*/
void main()
{
char ch;
Open(); /*打開文件*/
while(1)
{
Menu(); /*顯示菜單*/
scanf(" %c",&ch);
switch(ch)
{
case '1':Crease(); /*添加條目*/
break;

case '2':Search(); /*查找條目1.按姓名*/
break;
case '3':Change(); /*修改信息*/
print();
break;
case '4': Delate(); /*刪除信息*/
print(); /*輸出刪除後的結果*/
break;
case '5':print(); /*輸出條目*/
break;
case '0':Save(); /*保存並釋放內存*/
exit(0); /*退出*/
break;
default:
printf("選擇錯誤!");
break;
}
}
}

/*菜單*/
void Menu()
{
printf("\n*****************通訊錄系統*****************\n");
printf("\t1.錄入。\n");
printf("\t2.按姓名查詢\n");
printf("\t3.修改信息\n");
printf("\t4.刪除\n");
printf("\t5.輸出\n");
printf("\t0.保存並退出!\n");
printf("*************************************************\n");
printf("\t請選擇:");
}

/*添加條目*/
void Crease()
{
TEL *p1=NULL,*p2=NULL;
p1=(TEL *)malloc(sizeof(TEL)); /*申請結點*/
printf("輸入編號:");
scanf("%s",p1->number);
printf("輸入姓名:"); /*添加信息*/
scanf("%s",p1->name);
printf("輸入單位:");
scanf("%s",p1->off);
printf("輸入地址:");
scanf("%s",p1->addrass);
printf("輸入郵箱:");
scanf("%s",p1->mail);
printf("輸入電話:");
scanf("%s",p1->telephone);
p1->next=NULL; /*保存到鏈表*/

if(head==NULL)
{
head=(TEL *)malloc(sizeof(TEL)); /*申請空間*/
head->next=p1;
}
else
{
for(p2=head;p2->next!=NULL;p2=p2->next); /*找到結點尾*/
p2->next=p1;
}
printf("此信息已添加!");
}

/*輸出學生信息*/
void print()
{
TEL *p=NULL;
if(head==NULL)
{
printf("此通訊錄中無記錄,請輸入記錄後在使用本功能!\n");
return;
}
printf("**************通訊錄系統*********************\n"); /*輸出信息*/
printf("編號\t姓名\t單位\t地址\t郵箱\t電話\n");
for(p=head->next;p!=NULL;p=p->next)
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);
}

/*查找信息1.按姓名*/
void Search()
{
TEL *p;
char findname[20];
printf("請輸入要查找的姓名:");
scanf("%s",findname);
printf("**************通訊錄系統*********************\n");
printf("編號\t姓名\t單位\t地址\t郵箱\t電話\n");
for(p=head;p!=NULL;p=p->next)
{
if(strcmp(p->name,findname)==0)
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);

}
}

/*刪除信息*/
void Delate()
{
char findname[20]; /*先查找 後刪除*/
TEL *p = head, *pr ;
printf("輸入要刪除的姓名:");
scanf(" %s",findname);
if (head->next == NULL)
{
printf("空鏈表!\n");
return;
}
while ((strcmp(p->name,findname)!=0 )&& p->next != NULL)
{
pr =p;
p =p->next;
}
if (strcmp(findname, p->name)==0) /*輸出刪除信息*/
{
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);
if (p == head->next)
head->next = p->next;
else
pr->next = p->next;
free(p);
printf("此信息已刪除!");
}
else printf("無此信息!\n");

}
void Save() /*保存鏈表信息到文件並釋放內存空間*/
{
TEL *p=NULL;
FILE *fp;
char *Book="books.txt";
if(head==NULL)
{
printf("\n記錄為空!\n");
return;
}
else
p=head->next;
if((fp=fopen(Book,"wb+"))==NULL)
{
printf("\n打不開文件!\n");
return;
}
while(p!=NULL) /*保存信息*/
{
fwrite(p,sizeof(TEL),1,fp);
p=p->next;
}
printf("保存完畢!\n");
fclose(fp);
/*****釋放鏈表空間*****/
for(;head->next!=NULL;)
{
p=head->next;
head->next=head->next->next;
free(p);
}
free(head);
}
/*文件信息輸出到鏈表*/
void Open()
{
FILE *fp;
TEL *p1=NULL,*p2=NULL,*temp=NULL;
if((fp=fopen("books.txt","rb+"))==NULL)
{
printf("\n****************通訊錄******************\n");
return;
}
head=(TEL *)malloc(sizeof(TEL));
head->next=NULL;
temp=p2=head;
while(! feof(fp)) /*循環讀取*/
{
p1=(TEL *)malloc(sizeof(TEL));
temp=p2;
p2->next=p1;
p2=p1;
fread(p1,sizeof(TEL),1,fp);
}
temp->next=NULL;
fclose(fp); /*關閉文件*/
}
void Change()
{
TEL *p;
char name[20];
printf("輸入要修改人的姓名:");
scanf("%s",name);

for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
{printf("**************通訊錄系統*********************\n");
printf("編號\t姓名\t單位\t地址\t郵箱\t電話\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->number,p->name,p->off,p->addrass,p->mail,p->telephone);
printf("輸入要修改的內容:\n");
printf("輸入編號:");
scanf("%s",p->number);
printf("輸入姓名:");
scanf("%s",p->name);
printf("輸入單位:");
scanf("%s",p->off);
printf("輸入地址:");
scanf("%s",p->addrass);
printf("輸入郵箱:");
scanf("%s",p->mail);
printf("輸入電話:");
scanf("%s",p->telephone);
}
else printf("無此信息!\n");
}
}

自己對比一下

『肆』 C語言編寫通訊錄

#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>//包含system函數頭文件
#defineLENsizeof(structstudent)
structstudent
{
charnum[20];//ID號碼
charname[100];//用戶姓名
charphone[20];//電話號碼
charhome[100];//通訊地址
charbirthday[20];//出生日期
structstudent*next;
};
voidface(void)//功能選擇面板
{
printf("********************************************************************");
printf(" ☆★☆★☆★~_~~_~~_~☆★☆★☆★ ");
printf(" ☆★歡迎使用阿冬子通訊錄☆★");
printf(" ☆★選擇你需要操作的功能:☆★(現無記錄,建議先填加記錄)★☆ ");
printf(" ");
printf(" 1.【增加通訊錄信息〗 ");
printf(" 2.〖顯示通訊錄中所有記錄】 ");
printf(" 3.【刪除需要刪除的信息〗 ");
printf(" 4.〖以名字查詢所需的信息】 ");
printf(" 5.【保存通訊錄中的所有記錄到指定文件中〗 ");
printf(" 6.〖退出不保存!!】 ");
printf(" ");
printf(" ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★");
printf(" ******************************************************************** ");
}
voidprint(structstudent*head)
{
structstudent*p;
p=head;
system("CLS");//調用DOS命令CLS能夠清屏
printf("************************************************************* ");
printf("====================→用戶信息記錄表←=================== ");
printf("************************************************************* ");
if(head!=NULL)
do
{
printf("聯系人ID號碼:%s ",p->num);
printf("聯系人姓名:%s ",p->name);
printf("聯系人電話號碼:%s ",p->phone);
printf("學生地址:%s ",p->home);
printf("聯系人出生日期:%s ",p->birthday);
printf("******************************************************** ");
p=p->next;
}while(p!=NULL);
else
{
printf("對不起!!沒有任何聯系人記錄!! ");
printf("============================================================= ");
}
}
//增添電子通訊錄中的內容,即創建連表過程
structstudent*append(structstudent*head)
{
structstudent*p0=NULL,*p1,*p2;//p0為要插入的新節點
p1=head;
p2=head;
system("CLS");
printf(" *********************************************************** ");
printf(" 你能在此目錄下創建並添加聯系人信息");
printf(" *********************************************************** ");
p0=(structstudent*)malloc(LEN);
printf("請輸入聯系人ID號碼:");
gets(p0->num);
printf("請輸入聯系人姓名:");
gets(p0->name);
printf("請輸入聯系人電話號碼:");
gets(p0->phone);
printf("請輸入聯系人地址:");
gets(p0->home);
printf("請輸入聯系人出生日期:");
gets(p0->birthday);
//對插入的節點排序,按姓名的拼音順序
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while((strcmp(p0->name,p1->name)>0)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if((strcmp(p0->name,p1->name))<=0)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}

printf("恭喜你!!成功添加了聯系人信息!!");
printf(" ************************************************************ ");
printf(" ");

}
return(head);

}

//電子通訊錄的維護(刪除),通過輸入聯系人ID號碼刪除聯系人數據
structstudent*del(structstudent*head)
{
structstudent*p1,*p2;
charnum[12];
system("CLS");
printf(" ************************************************************ ");
printf("=================→用戶信息記錄刪除功能←=============== ");
printf("************************************************************ ");
printf("輸入要刪除的聯系人ID號碼:");
gets(num);
p1=head;
if(head==NULL)
{
printf("很抱歉!!沒有任何聯系人紀錄!! ");
printf(" ******************************************************* ");
return(head);
}
while(p1!=NULL)
{
if(strcmp(p1->num,num)==0)
{
if(p1==head)
head=p1->next;
elsep2->next=p1->next;
free(p1);
printf("刪除記錄成功!! ");
return(head);
}
p2=p1;
p1=p1->next;
}
printf("對不起!!沒有要刪除的聯系人紀錄!! ");
return(head);
}

//電子通訊錄的查找,關鍵字為用戶姓名;
voidsearch(structstudent*head)
{
structstudent*p1,*p2;
charname[20];
p1=head;
p2=p1;
system("CLS");
printf(" ************************************************************** ");
printf("================→用戶信息記錄查詢功能←================== ");
printf("************************************************************** ");
printf("輸入要查找聯系人的姓名:");
gets(name);
while(p1!=NULL)
{
if(strcmp(p1->name,name)==0)
{
printf("聯系人ID號碼:");
puts(p1->num);
printf("聯系人姓名:");
puts(p1->name);
printf("聯系人電話號碼:");
puts(p1->phone);
printf("聯系人地址:");
puts(p1->home);
printf("聯系人出生日期:");
puts(p1->birthday);
printf(" ============================================================= ");
break;
}
p2=p1;
p1=p1->next;
}
if(p1==NULL)
printf("對不起!!沒有該聯系人的紀錄!! ");

}
//電子通訊錄的記錄存檔操作,使用文件指針;
voidsave(structstudent*head)
{
FILE*fp,*fp1;
structstudent*p;
p=head;
fp=fopen("record.txt","w");
fp1=fopen("record1.txt","w");
fprintf(fp1,"===============→用戶信息記錄表←================= ");

while(p!=NULL)
{
//首先把數據保存在record.txt中,這是提供給load函數用的數據
//fprintf(fp,"%s%s%s%s%s%s",p->num,p->name,p->phone,p->email,p->home,p->birthday);
//然後把數據保存在record1.txt中,這是能提供直接查詢看的,有比較友好的畫面
fprintf(fp1,"==================================================== ");
fprintf(fp1,"聯系人ID號碼:%s ",p->num);
fprintf(fp1,"聯系人姓名:%s ",p->name);
fprintf(fp1,"聯系人電話:%s ",p->phone);
fprintf(fp1,"聯系人家庭地址:%s ",p->home);
fprintf(fp1,"聯系人出生日期:%s ",p->birthday);
p=p->next;
}
fprintf(fp1,"************************************************************* ");
fclose(fp1);
fclose(fp);
printf(" 恭喜你!!成功儲存,你能在record1.txt找到相應紀錄 ");
printf("************************************************************** ");
printf("PRESSANYKEYTOEXIT. ");
getchar();
exit(1);
}
//電子通訊錄的記錄讀盤操作,使用文件指針;
structstudent*load(void)
{
FILE*fp;
structstudent*head=NULL,*p1=NULL,*p2=NULL;
charc;
inti;
fp=fopen("record.txt","r");
for(i=1;(c=fgetc(fp))!=-1;i++)
{
p1=(structstudent*)malloc(LEN);
//fscanf(fp,"%s%s%s%s%s%s",p1->num,p1->name,p1->phone,p1->email,p1->home,p1->birthday);
if(i==1)
{head=p1;p2=p1;}
else
{p2->next=p1;p2=p1;}
}
if(p1==NULL)
{fclose(fp);return(head);}
p2->next=NULL;
fclose(fp);
return(head);
}

main()
{
FILE*fp1,*fp2;
intc;//功能選擇需要的號碼
system("cls");
system("color2f");
system("cls");
structstudent*head=NULL;
if((fp1=fopen("record.txt","r"))==NULL)
{
fp2=fopen("record.txt","w");//如果不存在record.txt就創建一個
fclose(fp2);
}
head=load();
while(1)
{
face();
printf("選擇你需要操作的功能號碼:");
scanf("%d",&c);
getchar();
switch(c)
{
case1:head=append(head);break;
case2:print(head);break;
case3:head=del(head);break;
case4:search(head);break;
case5:save(head);break;
case6:exit(0);break;
default:printf("Entererror!! ");
}
//printf("***************** ");
printf("◇◆請按ENTER返回功能操作菜單◇◆ ");
//printf("***************** ");
getchar();
system("CLS");

}

}

『伍』 如何用C語言做通訊錄

剽竊代碼即可。。


已經按照你的要求做了一個,VC6上運行確認了:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedefstructPersonalInfo
{
charname[50];
charaddress[30];
chartelno[30];
charpostcode[30];
structPersonalInfo*next;
}INFO;
INFO*head;
voidInitiate();
voidMenu();
voidCreate();//的功能是:創建新的通訊錄。
voidAdd();//在通訊錄的末尾,寫入新的信息,並返回選單
voidFind();//查找記錄
voidAlter();//修改記錄如果未找到要修改的人,則提示通訊錄中沒有此人的信息,並返回選單。
voidDelete();//刪除某人的信息,如果未找到要刪的人,提示通訊錄中沒有此人的信息,並返回選單。
voidList();//的功能是:顯示通訊錄中的所有記錄。
//初始化
voidInitiate()
{
if((head=(INFO*)malloc(sizeof(INFO)))==NULL)exit(1);
head->next=NULL;
}
//顯示菜單
voidMenu()
{
printf("**************歡迎使用通訊錄系統**************");
printf(" ");
printf(" ");
printf("1.創建通訊錄。 ");
printf("2.插入信息。 ");
printf("3.查詢記錄 ");
printf("4.修改記錄 ");
printf("5.刪除記錄! ");
printf("6.顯示所有記錄 ");
printf("0.退出通訊錄 ");
printf("請輸入0~6 ");
}
//創建通訊錄
voidCreate()
{
INFO*p1[100],*p2;
intm,i;
printf("請輸入創建個數:");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
p1[i]=(INFO*)malloc(sizeof(INFO));
printf("請輸入第%d條信息! ",i);
printf("姓名: ");
scanf("%s",&p1[i]->name);
printf("地址: ");
scanf("%s",&p1[i]->address);
printf("電話: ");
scanf("%s",&p1[i]->telno);
printf("郵編: ");
scanf("%s",&p1[i]->postcode);
p1[i]->next=NULL;
if(head->next==NULL)
head->next=p1[i];
else
{
for(p2=head;p2->next!=NULL;p2=p2->next);//找到結點尾
p2->next=p1[i];
}
}
printf("信息已添加! ");
return;//保存到鏈表
}
//添加通訊錄信息
voidAdd()
{
INFO*p,*q;
if((q=(INFO*)malloc(sizeof(INFO)))==NULL)exit(1);
printf("請輸入要添加的信息! ");
printf("姓名: ");//添加信息
scanf("%s",&q->name);
printf("性別: ");
scanf("%s",q->address);
printf("電話: ");
scanf("%s",q->telno);
printf("城市: ");
scanf("%s",q->postcode);
for(p=head;p->next!=NULL;p=p->next);
p->next=q;
q->next=NULL;
printf("此信息已添加!");
return;
}
//查找通訊錄信息
voidFind()
{
INFO*p;
charname[50];
if(head->next==NULL)
{
printf("此通訊錄為空! ");
return;
}
printf("請輸入要查找的姓名: ");
scanf("%s",&name);
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
{
printf("姓名 地址 郵編 電話 ");
printf("%s %s %s %s ",p->name,p->address,p->postcode,p->telno);
}
elseif(p->next==NULL)
printf("無此信息! ");
}
}
//修改通訊錄信息
voidAlter()
{
charname[50];//先查找後刪除
INFO*p,*p1;
if(head->next==NULL)
{
printf("此通訊錄為空! ");
return;
}
printf("請輸入要修改的姓名: ");
scanf("%s",name);
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
break;
elseif(p->next==NULL)
{
printf("無此信息! ");
return;
}
}
p1=(INFO*)malloc(sizeof(INFO));
printf("姓名: ");//添加信息
scanf("%s",p1->name);
strcpy(p->name,p1->name);
printf("性別: ");
scanf("%s",p1->address);
strcpy(p->address,p1->address);
printf("電話: ");
scanf("%s",p1->telno);
strcpy(p->telno,p1->telno);
printf("城市: ");
scanf("%s",p1->postcode);
strcpy(p->postcode,p1->postcode);
printf("此信息已修改! ");
//顯示修改的信息
printf("姓名 地址 郵編 電話 ");
printf("%s %s %s %s ",p->name,p->address,p->postcode,p->telno);
free(p1);
}
//刪除通訊錄信息
voidDelete()
{
charname[50];//先查找後刪除
INFO*p=head->next,*p1=head->next,*p2;
if(head->next==NULL)
{
printf("此通訊錄為空! ");
return;
}
printf("請輸入要刪除的姓名: ");
scanf("%s",name);
while((strcmp(p->name,name)!=0)&&p->next!=NULL)
{
p1=p;
p=p->next;
}
if(strcmp(name,p->name)==0)//輸出刪除信息
{
if(p==head->next&&p->next!=NULL)
head->next=p->next;
elseif(p==head->next&&p->next==NULL)
{
head->next=p->next;
printf("信息已刪除,先此通訊錄為空!! ");
return;
}
else
p1->next=p->next;
}
else
{
printf("此信息不存在!!! ");
return;
}
printf("此信息已刪除!");
printf("姓名 地址 郵編 電話 ");
for(p2=head->next;p2!=NULL;p2=p2->next)
printf("%s %s %s %s ",p2->name,p2->address,p2->postcode,p2->telno);
}
//顯示所有記錄
voidList()
{
INFO*p;
if(head->next==NULL)
{
printf("此通訊錄中無記錄! ");
return;
}
printf("姓名 地址 郵編 電話 ");
for(p=head->next;p!=NULL;p=p->next)
printf("%s %s %s %s ",p->name,p->address,p->postcode,p->telno);
}
voidmain()
{
intchoice;
charyes_no;

system("colora");
Initiate();
do
{
Menu();
printf("請選擇0-6的數字 ");
scanf("%d",&choice);
printf(" ");
switch(choice)
{
case1:Create();
break;
case2:Add();
break;
case3:Find();
break;
case4:Alter();
break;
case5:Delete();
break;
case6:List();
break;
case0:
printf("************感謝您的使用************ ");
exit(0);
break;
default:
printf("輸入有誤!請重新輸入 ");
break;
}
printf("是否繼續YorN? ");
do
{
scanf("%c",&yes_no);
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}

『陸』 c語言通訊錄程序

以前寫了一個簡單的:
#include <stdio.h>
#include <stdlib.h> /*與malloc.h差不多*/
#include <string.h>
#include <iostream>
using namespace std;
#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 Show()
{printf("成功調用該函數\n");}
void Delete()
{printf("成功調用該函數\n");}

void Input() /*向.通訊錄中輸入一個人的信息*/
{ s=(linklist)malloc(sizeof(listnode));
printf("\n\n\t請輸入該用戶的信息:");
printf("姓名:");
scanf("%s",&s->name);
printf("電話號碼:");
scanf("%s",&s->tel_no);
printf("單鍵撥號:");
scanf("%s",&s->sim_no);
printf("E-mail地址:");
scanf("%s",&s->e_addr);
printf("類別:");
scanf("%s",&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)
{
s->next = 0;
head=s;
r = s;
}
else
{
s->next = 0;
r->next = s;
r = s;

}
}
void Alter()
{ printf("success!\n");}
int main()
{
system("color a");
creat();
do
{
printf("\n\n\t\t請選擇操作:");
printf("\n\t\t1.顯示通訊錄");
printf("\n\t\t2.刪除通訊錄");
printf("\n\t\t3.添加通訊錄");
printf("\n\t\t4.編輯通訊錄");
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;
fclose(fp);
exit(0);
break;
default:
printf("\n\t The num should 1-6!!! \n");
break;
}
}
while(1);
}

『柒』 用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語言中做一個通訊錄,能夠添加、刪除、修改、查找

# include<stdio.h>
# include<string.h>
struct tongxun
{char name[20];
char number[20];
struct tongxun *next;
};
int all=0;
struct tongxun* tj() /*創建鏈表並添加成員*//**/
{
struct tongxun *head=NULL;
struct tongxun *p,*q;
system("CLS");

p=q=(struct tongxun *)malloc(sizeof(struct tongxun));

for(;;)
{printf("請輸入姓名:(如果輸入0則退出添加)\n");
scanf("%s",p->name);
if(!(strcmp(p->name,"0"))){ free(p);return head;}
else {printf("請輸入電話號碼:\n");
scanf("%s",p->number);
all++;
if(all==1)
{p->next=head;
q=p;
head=q;}
else
{p->next=NULL;
q->next=p;
q=p;
}
p=(struct tongxun *)malloc(sizeof(struct tongxun));
}
}
}
cz(struct tongxun *head) /*查找函數*/
{char name1[20],*a;
struct tongxun *p;
p=head;a=name1;
system("CLS");
printf("請輸入要查找的姓名:\n");
scanf("%s",a);
while(p!=NULL)
{if((strcmp(p->name,a))==0) {printf("姓名:%s\n電話號碼:%s\n",p->name,p->number);return;}
else p=p->next;
}
printf("沒有此人\n");
return;
}

insert(struct tongxun *head) /*插入新成員*/
{struct tongxun* pnew;
pnew=(struct tongxun *)malloc(sizeof(struct tongxun));
while(1)
{printf("請輸入姓名:(如果輸入0則退出添加)\n");
scanf("%s",pnew->name);
if(!(strcmp(pnew->name,"0"))){ free(pnew);return head;}
else {printf("請輸入電話號碼:\n");
scanf("%s",pnew->number);
all++;
pnew->next=head;
head=pnew;
pnew=(struct tongxun *)malloc(sizeof(struct tongxun));
}
}
}
shuchu(struct tongxun *head) /*輸出成員*/
{struct tongxun *p;
p=head;
printf("這里一共有%d個成員\n",all);
while(p!=NULL)
{printf("姓名:%s\n電話號碼:%s\n",p->name,p->number);
p=p->next;
}
}
xg(struct tongxun *head) /*修改成員*/
{char name1[20],*a;
struct tongxun *p;
p=head;a=name1;
system("CLS");
printf("請輸入要修改的姓名:\n");
scanf("%s",a);
while(p!=NULL)
{if((strcmp(p->name,a))==0) {printf("請重新輸入姓名:\n");
scanf("%s",p->name);
printf("請重新輸入電話號碼:\n");
scanf("%s",p->number);return;}
else p=p->next;
}
printf("沒有此人\n");
return;

}
sc(struct tongxun *head) /*刪除成員*/
{char name1[20],*a;
struct tongxun *p,*q;
p=q=head;a=name1;
system("CLS");
printf("請輸入要刪除的姓名:\n");
scanf("%s",a);
while(p!=NULL)
{
if((strcmp(p->name,a))==0) {all--;q->next=p->next;return;}
else {q=p;p=p->next;}
}
printf("沒有此人\n");
return;

}
void main()
{struct tongxun *head;int i;
while(1)
{printf("請選擇:\n");
printf("1.添加 2.查找 3.修改 4.刪除 5.插入 6.輸出\n");scanf("%d",&i);
switch(i)
{case 1:head=tj();break;
case 2:cz(head);break;
case 3:xg(head);break;
case 4:sc(head);break;
case 5:insert(head);break;
case 6:shuchu(head);break;
default:printf("輸入有誤,請重新輸入:\n");break;
}
}
}

『玖』 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);

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

熱點內容
尋秦記源碼 發布:2024-03-29 13:56:17 瀏覽:495
linux的備份命令 發布:2024-03-29 13:41:22 瀏覽:382
csgo建議什麼配置 發布:2024-03-29 13:31:44 瀏覽:979
電腦ftp服務如何禁用 發布:2024-03-29 13:24:48 瀏覽:332
驅動精靈驅動解壓 發布:2024-03-29 13:07:49 瀏覽:565
學編程好學嗎 發布:2024-03-29 13:07:34 瀏覽:440
python保存mp3文件 發布:2024-03-29 12:47:10 瀏覽:151
win10怎麼配置jdk8 發布:2024-03-29 12:47:09 瀏覽:536
解壓軟體java 發布:2024-03-29 12:40:32 瀏覽:283
長安cs35壓縮比 發布:2024-03-29 12:39:58 瀏覽:177