當前位置:首頁 » 編程語言 » c語言哈希表通訊錄代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言哈希表通訊錄代碼

發布時間: 2022-06-13 02:47:36

① 數據結構 哈希表,c語言解答

#include <stdio.h>
#include<malloc.h>
#include<string.h>
//#include
#define HASH_LEN 50 //哈希表的長度
#define M 47
#define NAME_NO 30 //人名的個數
typedef struct NAME
{
char *py; //名字的拼音
int k; //拼音所對應的整數
}NAME;
NAME NameList[HASH_LEN];

typedef struct hterm //哈希表
{
char *py; //名字的拼音
int k; //拼音所對應的整數
int si; //查找長度
}HASH;
HASH HashList[HASH_LEN];
/*-----------------------姓名(結構體數組)初始化---------------------------------*/
void InitNameList()
{ int i;
char *f;
int r,s0;
NameList[0].py="chenghongxiu";
NameList[1].py="yuanhao";
NameList[2].py="yangyang";
NameList[3].py="zhanghen";
NameList[4].py="chenghongxiu";
NameList[5].py="xiaokai";
NameList[6].py="liupeng";
NameList[7].py="shenyonghai";
NameList[8].py="chengquan";
NameList[9].py="luqing";
NameList[10].py="gongyunxiang";
NameList[11].py="sunzhenxing";
NameList[12].py="sunrongfei";
NameList[13].py="sunminglong";
NameList[14].py="zhanghao";
NameList[15].py="tianmiao";
NameList[16].py="yaojianzhong";
NameList[17].py="yaojianqing";
NameList[18].py="yaojianhua";
NameList[19].py="yaohaifeng";
NameList[20].py="chengyanhao";
NameList[21].py="yaoqiufeng";
NameList[22].py="qianpengcheng";
NameList[23].py="yaohaifeng";
NameList[24].py="bianyan";
NameList[25].py="linglei";
NameList[26].py="fuzhonghui";
NameList[27].py="huanhaiyan";
NameList[28].py="liudianqin";
NameList[29].py="wangbinnian";

for (i=0;i<NAME_NO;i++)// *求出各個姓名的拼音所對應的整數
{
s0=0;
f=NameList[i].py;

for (r=0;*(f+r) != '\0';r++) //方法:將字元串的各個字元所對應的ASCII碼相加,所得的整數做為哈希表的關鍵字
s0=*(f+r)+s0;

NameList[i].k=s0;
}
}
/*-----------------------建立哈希表---------------------------------*/
void CreateHashList()
{int i;
for ( i=0; i<HASH_LEN;i++)//哈希表的初始化
{
HashList[i].py="";
HashList[i].k=0;
HashList[i].si=0;
}

for (i=0; i<NAME_NO;)
{
int sum=0;
int adr=(NameList[i].k) % M; //哈希函數
int d=adr;
if(HashList[adr].si==0) //如果不沖突
{
HashList[adr].k=NameList[i].k;
HashList[adr].py=NameList[i].py;
HashList[adr].si=1;
}
else //沖突
{
do
{
d=(d+((NameList[i].k))%10+1)%M; //偽散列
sum=sum+1; //查找次數加1
}while (HashList[d].k!=0);

HashList[d].k=NameList[i].k;
HashList[d].py=NameList[i].py;
HashList[d].si=sum+1;
}i++;
}
}

/*-------------------------------------查找------------------------------------*/
void FindList()
{ int r;
char name[20]={0};
int s0=0;
int sum=1;
int adr;
int d;
printf("\n\n請輸入姓名的拼音: "); //輸入姓名
scanf("%s",name);

for ( r=0;r<20;r++) //求出姓名的拼音所對應的整數(關鍵字)
s0+=name[r];

adr=s0 % M; //使用哈希函數
d=adr;

if(HashList[adr].k==s0) //分3種情況進行判斷
printf("\n姓名:%s 關鍵字:%d 查找長度為: 1",HashList[d].py,s0);
else if (HashList[adr].k==0)
printf("無該記錄!");
else
{
int g=0;
do
{
d=(d+s0%10+1)%M; //偽散列
sum=sum+1;
if (HashList[d].k==0)
{
printf("無記錄! ");
g=1;
}
if (HashList[d].k==s0)
{
printf("\n姓名:%s 關鍵字:%d 查找長度為:%d",HashList[d].py,s0,sum);
g=1;
}
}while(g==0);
}
}

/*--------------------------------顯示哈希表----------------------------*/
void Display()
{int i;
float average=0;
printf("\n\n地址\t關鍵字\t\t搜索長度\tH(key)\t\t拼音 \n"); //顯示的格式
for( i=0; i<15; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
// printf("按任意鍵繼續顯示...\n"); //由於數據比較多,所以分屏顯示(以便在Win9x/DOS下能看到所有的數據)
// getch();
for( i=15; i<30; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
// printf("按任意鍵繼續顯示...\n");
// getch();
for( i=30; i<40; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
//printf("按任意鍵繼續顯示...\n");
//getch();
for( i=40; i<50; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}

for (i=0;i<HASH_LEN;i++)
{average+=HashList[i].si;
average/=NAME_NO;
printf("\n\n平均查找長度:ASL(%d)=%f \n\n",NAME_NO,average);
}
}
/*--------------------------------主函數----------------------------*/
void main()
{
/* ::SetConsoleTitle("哈希表操作"); //Windows API函數,設置控制台窗口的標題
HANDLE hCon = ::GetStdHandle(STD_OUTPUT_HANDLE); //獲得標准輸出設備的句柄
::SetConsoleTextAttribute(hCon, 10|0); //設置文本顏色
*/
printf("\n------------------------哈希表的建立和查找----------------------");
InitNameList();
CreateHashList ();

while(1)
{ char ch1;
printf("\n\n");
printf(" 1. 顯示哈希表\n");
printf(" 2. 查找\n");
printf(" 3. 退出\n");

err:

scanf("%c",&ch1);
if (ch1=='1')
Display();
else if (ch1=='2')
FindList();
else if (ch1=='3')
return;
else
{
printf("\n請輸入正確的選擇!");
goto err;
}
}
}

② 用C語言做通訊錄

這看你是怎樣定義的了。如果你50個同學是用結構體來定義的話就比較容易實現。用二維數組的話,就比較麻煩。
查找名字可以用strcmp(輸入的名字,編好的名字)==0如果相等,就等於查找到你輸入的名字了。記錄它的下標,再輸出它的聯系方式。
下面是一個比較完整的通訊錄代碼,你參考一下啦。(看到專家兩個字,我真沒資格去回答你了,汗)

/*10.3.2源程序*/
/******頭文件(.h)***********/
#include "stdio.h" /*I/O函數*/
#include "stdlib.h" /*標准庫函數*/
#include "string.h"/*字元串函數*/
#include "ctype.h" /*字元操作函數*/
#define M 50 /*定義常數表示記錄數*/
typedef struct /*定義數據結構*/
{
char name[20]; /*姓名*/
char units[30]; /*單位*/
char tele[10]; /*電話*/
}ADDRESS;
/******以下是函數原型*******/
int enter(ADDRESS t[]); /*輸入記錄*/
void list(ADDRESS t[],int n); /*顯示記錄*/
void search(ADDRESS t[],int n); /*按姓名查找顯示記錄*/
int delete(ADDRESS t[],int n); /*刪除記錄*/
int add(ADDRESS t[],int n); /*插入記錄*/
void save(ADDRESS t[],int n); /*記錄保存為文件*/
int load(ADDRESS t[]); /*從文件中讀記錄*/
void display(ADDRESS t[]); /*按序號查找顯示記錄*/
void sort(ADDRESS t[],int n); /*按姓名排序*/
void qseek(ADDRESS t[],int n); /*快速查找記錄*/
void (); /*文件復制*/
void print(ADDRESS temp); /*顯示單條記錄*/
int find(ADDRESS t[],int n,char *s) ; /*查找函數*/
int menu_select(); /*主菜單函數*/
/******主函數開始*******/
main()
{
int i;
ADDRESS adr[M]; /*定義結構體數組*/
int length; /*保存記錄長度*/
clrscr(); /*清屏*/
for(;;)/*無限循環*/
{
switch(menu_select()) /*調用主菜單函數,返回值整數作開關語句的條件*/
{
case 0:length=enter(adr);break;/*輸入記錄*/
case 1:list(adr,length);break; /*顯示全部記錄*/
case 2:search(adr,length);break; /*查找記錄*/
case 3:length=delete(adr,length);break; /*刪除記錄*/
case 4:length=add(adr,length); break; /*插入記錄*/
case 5:save(adr,length);break; /*保存文件*/
case 6:length=load(adr); break; /*讀文件*/
case 7:display(adr);break; /*按序號顯示記錄*/
case 8:sort(adr,length);break; /*按姓名排序*/
case 9:qseek(adr,length);break; /*快速查找記錄*/
case 10:();break; /*復制文件*/
case 11:exit(0); /*如返回值為11則程序結束*/
}
}
}
/*菜單函數,函數返回值為整數,代表所選的菜單項*/
menu_select()
{
char s[80];
int c;
gotoxy(1,25);/*將游標定為在第25行,第1列*/
printf("press any key enter menu......\n");/*提示壓任意鍵繼續*/
getch(); /*讀入任意字元*/
clrscr(); /*清屏*/
gotoxy(1,1);
printf("********************MENU*********************\n\n");
printf(" 0. Enter record\n");
printf(" 1. List the file\n");
printf(" 2. Search record on name\n");
printf(" 3. Delete a record\n");
printf(" 4. add record \n");
printf(" 5. Save the file\n");
printf(" 6. Load the file\n");
printf(" 7. display record on order\n");
printf(" 8. sort to make new file\n");
printf(" 9. Quick seek record\n");
printf(" 10. the file to new file\n");
printf(" 11. Quit\n");
printf("***********************************************\n");
do{
printf("\n Enter you choice(0~11):"); /*提示輸入選項*/
scanf("%s",s); /*輸入選擇項*/
c=atoi(s); /*將輸入的字元串轉化為整型數*/
}while(c11); /*選擇項不在0~11之間重輸*/
return c; /*返回選擇項,主程序根據該數調用相應的函數*/
}
/***輸入記錄,形參為結構體數組,函數值返回類型為整型表示記錄長度*/
int enter(ADDRESS t[])
{
int i,n;
char *s;
clrscr(); /*清屏*/
printf("\nplease input num \n"); /*提示信息*/
scanf("%d",&n); /*輸入記錄數*/
printf("please input record \n"); /*提示輸入記錄*/
printf("name unit telephone\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
{
scanf("%s%s%s",t[i].name,t[i].units,t[i].tele); /*輸入記錄*/
printf("----------------------------------------------\n");
}
return n; /*返回記錄條數*/
}
/*顯示記錄,參數為記錄數組和記錄條數*/
void list(ADDRESS t[],int n)
{
int i;
clrscr();
printf("\n\n*******************ADDRESS******************\n");
printf("name unit telephone\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
printf("%-20s%-30s%-10s\n",t[i].name,t[i].units,t[i].tele);
if((i+1)%10==0) /*判斷輸出是否達到10條記錄*/
{
printf("Press any key continue...\n"); /*提示信息*/
getch(); /*壓任意鍵繼續*/
}
printf("************************end*******************\n");
}
/*查找記錄*/
void search(ADDRESS t[],int n)
{
char s[20]; /*保存待查找姓名字元串*/
int i; /*保存查找到結點的序號*/
clrscr(); /*清屏*/
printf("please search name\n");
scanf("%s",s); /*輸入待查找姓名*/
i=find(t,n,s); /*調用find函數,得到一個整數*/
if(i>n-1) /*如果整數i值大於n-1,說明沒找到*/
printf("not found\n");
else
print(t[i]); /*找到,調用顯示函數顯示記錄*/
}
/*顯示指定的一條記錄*/
void print(ADDRESS temp)
{
clrscr();
printf("\n\n********************************************\n");
printf("name unit telephone\n");
printf("------------------------------------------------\n");
printf("%-20s%-30s%-10s\n",temp.name,temp.units,temp.tele);
printf("**********************end***********************\n");
}
/*查找函數,參數為記錄數組和記錄條數以及姓名s */
int find(ADDRESS t[],int n,char *s)
{
int i;
for(i=0;i<n;i++)/*從第一條記錄開始,直到最後一條*/
{
if(strcmp(s,t[i].name)==0) /*記錄中的姓名和待比較的姓名是否相等*/
return i; /*相等,則返回該記錄的下標號,程序提前結結束*/
}
return i; /*返回i值*/
}
/*刪除函數,參數為記錄數組和記錄條數*/
int delete(ADDRESS t[],int n)
{
char s[20]; /*要刪除記錄的姓名*/
int ch=0;
int i,j;
printf("please deleted name\n"); /*提示信息*/
scanf("%s",s);/*輸入姓名*/
i=find(t,n,s); /*調用find函數*/
if(i>n-1) /*如果i>n-1超過了數組的長度*/
printf("no found not deleted\n"); /*顯示沒找到要刪除的記錄*/
else
{
print(t[i]); /*調用輸出函數顯示該條記錄信息*/
printf("Are you sure delete it(1/0)\n"); /*確認是否要刪除*/
scanf("%d",&ch); /*輸入一個整數0或1*/
if(ch==1) /*如果確認刪除整數為1*/
{
for(j=i+1;j<n;j++) /*刪除該記錄,實際後續記錄前移*/
{
strcpy(t[j-1].name,t[j].name); /*將後一條記錄的姓名拷貝到前一條*/
strcpy(t[j-1].units,t[j].units); /*將後一條記錄的單位拷貝到前一條*/
strcpy(t[j-1].tele,t[j].tele); /*將後一條記錄的電話拷貝到前一條*/
}
n--; /*記錄數減1*/
}
}
return n; /*返回記錄數*/
}
/*插入記錄函數,參數為結構體數組和記錄數*/
int add(ADDRESS t[],int n)/*插入函數,參數為結構體數組和記錄數*/
{
ADDRESS temp; /*新插入記錄信息*/
int i,j;
char s[20]; /*確定插入在哪個記錄之前*/
printf("please input record\n");
printf("************************************************\n");
printf("name unit telephone\n");
printf("--------------------------------------------------\n");
scanf("%s%s%s",temp.name,temp.units,temp.tele); /*輸入插入信息*/
printf("------------------------------------------------\n");
printf("please input locate name \n");
scanf("%s",s); /*輸入插入位置的姓名*/
i=find(t,n,s); /*調用find,確定插入位置*/
for(j=n-1;j>=i;j--) /*從最後一個結點開始向後移動一條*/
{
strcpy(t[j+1].name,t[j].name); /*當前記錄的姓名拷貝到後一條*/
strcpy(t[j+1].units,t[j].units); /*當前記錄的單位拷貝到後一條*/
strcpy(t[j+1].tele,t[j].tele); /*當前記錄的電話拷貝到後一條*/
}
strcpy(t[i].name,temp.name); /*將新插入記錄的姓名拷貝到第i個位置*/
strcpy(t[i].units,temp.units); /*將新插入記錄的單位拷貝到第i個位置*/
strcpy(t[i].tele,temp.tele); /*將新插入記錄的電話拷貝到第i個位置*/
n++; /*記錄數加1*/
return n; /*返回記錄數*/
}
/*保存函數,參數為結構體數組和記錄數*/
void save(ADDRESS t[],int n)
{
int i;
FILE *fp; /*指向文件的指針*/
if((fp=fopen("record.txt","wb"))==NULL) /*打開文件,並判斷打開是否正常*/
{
printf("can not open file\n");/*沒打開*/
exit(1); /*退出*/
}
printf("\nSaving file\n"); /*輸出提示信息*/
fprintf(fp,"%d",n); /*將記錄數寫入文件*/
fprintf(fp,"\r\n"); /*將換行符號寫入文件*/
for(i=0;i<n;i++)
{
fprintf(fp,"%-20s%-30s%-10s",t[i].name,t[i].units,t[i].tele);/*格式寫入記錄*/
fprintf(fp,"\r\n"); /*將換行符號寫入文件*/
}
fclose(fp);/*關閉文件*/
printf("****save success***\n"); /*顯示保存成功*/
}
/*讀入函數,參數為結構體數組*/
int load(ADDRESS t[])
{
int i,n;
FILE *fp; /*指向文件的指針*/
if((fp=fopen("record.txt","rb"))==NULL)/*打開文件*/
{
printf("can not open file\n"); /*不能打開*/
exit(1); /*退出*/
}
fscanf(fp,"%d",&n); /*讀入記錄數*/
for(i=0;i<n;i++)
fscanf(fp,"%20s%30s%10s",t[i].name,t[i].units,t[i].tele); /*按格式讀入記錄*/
fclose(fp); /*關閉文件*/
printf("You have success read data from file!!!\n"); /*顯示保存成功*/
return n; /*返回記錄數*/
}
/*按序號顯示記錄函數*/
void display(ADDRESS t[])
{
int id,n;
FILE *fp; /*指向文件的指針*/
if((fp=fopen("record.txt","rb"))==NULL) /*打開文件*/
{
printf("can not open file\n"); /*不能打開文件*/
exit(1); /*退出*/
}
printf("Enter order number...\n"); /*顯示信息*/
scanf("%d",&id); /*輸入序號*/
fscanf(fp,"%d",&n); /*從文件讀入記錄數*/
if(id>=0&&id<n) /*判斷序號是否在記錄范圍內*/
{
fseek(fp,(id-1)*sizeof(ADDRESS),1); /*移動文件指針到該記錄位置*/
print(t[id]); /*調用輸出函數顯示該記錄*/
printf("\r\n");
}
else
printf("no %d number record!!!\n ",id); /*如果序號不合理顯示信息*/
fclose(fp); /*關閉文件*/
}
/*排序函數,參數為結構體數組和記錄數*/
void sort(ADDRESS t[],int n)
{
int i,j,flag;
ADDRESS temp; /*臨時變數做交換數據用*/
for(i=0;i<n;i++)
{
flag=0; /*設標志判斷是否發生過交換*/
for(j=0;j<n-1;j++)
if((strcmp(t[j].name,t[j+1].name))>0) /*比較大小*/
{
flag=1;
strcpy(temp.name,t[j].name); /*交換記錄*/
strcpy(temp.units,t[j].units);
strcpy(temp.tele,t[j].tele);
strcpy(t[j].name,t[j+1].name);
strcpy(t[j].units,t[j+1].units);
strcpy(t[j].tele,t[j+1].tele);
strcpy(t[j+1].name,temp.name);
strcpy(t[j+1].units,temp.units);
strcpy(t[j+1].tele,temp.tele);
}
if(flag==0)break; /*如果標志為0,說明沒有發生過交換循環結束*/
}
printf("sort sucess!!!\n"); /*顯示排序成功*/
}
/*快速查找,參數為結構體數組和記錄數*/
void qseek(ADDRESS t[],int n)
{
char s[20];
int l,r,m;
printf("\nPlease sort before qseek!\n"); /*提示確認在查找之前,記錄是否已排序*/
printf("please enter name for qseek\n"); /*提示輸入*/
scanf("%s",s); /*輸入待查找的姓名*/
l=0;r=n-1; /*設置左邊界與右邊界的初值*/
while(l<=r) /*當左邊界<=右邊界時*/
{
m=(l+r)/2; /*計算中間位置*/
if(strcmp(t[m].name,s)==0) /*與中間結點姓名欄位做比較判是否相等*/
{
print(t[m]); /*如果相等,則調用print函數顯示記錄信息*/
return ; /*返回*/
}
if(strcmp(t[m].name,s)<0) /*如果中間結點小*/
l=m+1; /*修改左邊界*/
else
r=m-1; /*否則,中間結點大,修改右邊界*/
}
if(l>r) /*如果左邊界大於右邊界時*/
printf("not found\n"); /*顯示沒找到*/
}
/*復制文件*/
void ()
{
char outfile[20]; /*目標文件名*/
int i,n;
ADDRESS temp[M]; /*定義臨時變數*/
FILE *sfp,*tfp; /*定義指向文件的指針*/
clrscr();/*清屏*/
if((sfp=fopen("record.txt","rb"))==NULL) /*打開記錄文件*/
{
printf("can not open file\n"); /*顯示不能打開文件信息*/
exit(1); /*退出*/
}
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示信息*/
scanf("%s",outfile); /*輸入目標文件名*/
if((tfp=fopen(outfile,"wb"))==NULL) /*打開目標文件*/
{
printf("can not open file\n"); /*顯示不能打開文件信息*/
exit(1); /*退出*/
}
fscanf(sfp,"%d",&n); /*讀出文件記錄數*/
fprintf(tfp,"%d",n);/*寫入目標文件數*/
fprintf(tfp,"\r\n"); /*寫入換行符*/
for(i=0;i<n;i++)
{
fscanf(sfp,"%20s%30s%10s\n",temp[i].name,temp[i].units,
temp[i].tele); /*讀入記錄*/
fprintf(tfp,"%-20s%-30s%-10s\n",temp[i].name,
temp[i].units,temp[i].tele); /*寫入記錄*/
fprintf(tfp,"\r\n"); /*寫入換行符*/
}
fclose(sfp); /*關閉源文件*/
fclose(tfp); /*關閉目標文件*/
printf("you have success file!!!\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");

}

}

④ 用哈希表,針對同班同學信息設計一個通訊錄,學生信息有姓名,學號,電話號碼等。

如果要判斷添加的對象是否相等應重寫equals方法,像集合框架SetMap等使用equals方法判斷對象是否相等,而hashCode方法只是為了提高系統查詢效率,當然你要保證兩個equal的對象返回的hashCode也相同

⑤ 用C語言編寫一個通訊錄管理系統

C語言編寫一個通訊錄管理系統的源代碼如下:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

/*定義保存通迅錄的信息*/

structfriends

{

charname[20];/*名字*/

charprovince[20];/*省份*/

charcity[20];/*所在城市*/

charnation[20];/*民族*/

charsex[2];/*性別M/F*/

intage;/*年齡*/

}

(5)c語言哈希表通訊錄代碼擴展閱讀

1、在C++中應該使用inline內連函數替代宏調用,這樣既可達到宏調用的目的,又避免了宏調用的弊端。

2、在C語言兩個函數的名稱不能相同,否則會導致編譯錯誤。在C++中,函數名相同而參數不同的兩個函數被解釋為重載。

3、在大型程序中,使函數名易於管理和使用,不必絞盡腦汁地去處理函數名。

⑥ 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語言實現哈希表的相關運算演算法 編寫程序實現哈希表的構造過程。

#define MaxSize 100 //定義最大哈希表長度
#define NULLKEY -1 //定義空關鍵字值
#define DELKEY -2 //定義被刪關鍵字值
typedef int KeyType; //關鍵字類型
typedef char * InfoType; //其他數據類型
typedef struct
{
KeyType key; //關鍵字域
InfoType data; //其他數據域
int count; //探查次數域
} HashData;

typedef HashData HashTable[MaxSize]; //哈希表類型

void InsertHT(HashTable ha,int &n,KeyType k,int p) //將關鍵字k插入到哈希表中
{
int i,adr;
adr=k % p;
if (ha[adr].key==NULLKEY || ha[adr].key==DELKEY) //x[j]可以直接放在哈希表中
{
ha[adr].key=k;
ha[adr].count=1;
}
else //發生沖突時採用線性探查法解決沖突
{
i=1; //i記錄x[j]發生沖突的次數
do
{
adr=(adr+1) % p;
i++;
}
while (ha[adr].key!=NULLKEY && ha[adr].key!=DELKEY);
ha[adr].key=k;
ha[adr].count=i;
}
n++;
}
void CreateHT(HashTable ha,KeyType x[],int n,

⑧ 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 "iostream.h"
#include <iostream>
#include "string.h"
#include "fstream"
#define NULL 0
unsigned int key;
unsigned int key2;
int *p;
struct node //建節點
{
char name[8],address[20];
char num[11];
node * next;
};

typedef node* pnode;
typedef node* mingzi;
node **phone;
node **nam;
node *a;

using namespace std; //使用名稱空間

void hash(char num[11]) //哈希函數
{
int i = 3;
key=(int)num[2];

while(num[i]!=NULL)
{
key+=(int)num[i];
i++;
}
key=key%20;
}

void hash2(char name[8]) //哈希函數
{
int i = 1;
key2=(int)name[0];
while(name[i]!=NULL)
{
key2+=(int)name[i];
i++;
}
key2=key2%20;
}

node* input() //輸入節點
{
node *temp;
temp = new node;
temp->next=NULL;
cout<<"輸入姓名:"<<endl;
cin>>temp->name;
cout<<"輸入地址:"<<endl;
cin>>temp->address;
cout<<"輸入電話:"<<endl;
cin>>temp->num;
return temp;
}

int apend() //添加節點
{
node *newphone;
node *newname;
newphone=input();
newname=newphone;
newphone->next=NULL;
newname->next=NULL;
hash(newphone->num);
hash2(newname->name);
newphone->next = phone[key]->next;
phone[key]->next=newphone;
newname->next = nam[key2]->next;
nam[key2]->next=newname;
return 0;
}

void create() //新建節點
{
int i;
phone=new pnode[20];
for(i=0;i<20;i++)
{
phone[i]=new node;
phone[i]->next=NULL;
}
}
void create2() //新建節點
{
int i;
nam=new mingzi[20];
for(i=0;i<20;i++)
{
nam[i]=new node;
nam[i]->next=NULL;
}
}
void list() //顯示列表
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}
void list2() //顯示列表
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=nam[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}

void find(char num[11]) //查找用戶信息
{
hash(num);
node *q=phone[key]->next;
while(q!= NULL)
{
if(strcmp(num,q->num)==0)
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"無此記錄"<<endl;
}
void find2(char name[8]) //查找用戶信息
{
hash2(name);
node *q=nam[key2]->next;
while(q!= NULL)
{
if(strcmp(name,q->name)==0)
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"無此記錄"<<endl;
}

void save() //保存用戶信息
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
fstream iiout("out.txt", ios::out);
iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl;
p=p->next;
}
}
}

void menu() //菜單
{

cout<<"0.添加記錄"<<endl;
cout<<"3.查找記錄"<<endl;
cout<<"2.姓名散列"<<endl;
cout<<"4.號碼散列"<<endl;
cout<<"5.清空記錄"<<endl;
cout<<"6.保存記錄"<<endl;
cout<<"7.退出系統"<<endl;
}

int main()
{
char num[11];
char name[8];

create();
create2() ;

int sel;
while(1)
{
menu();
cin>>sel;
if(sel==3)
{ cout<<"9號碼查詢,8姓名查詢"<<endl;
int b;
cin>>b;
if(b==9)
{ cout<<"請輸入電話號碼:"<<endl;
cin >>num;
cout<<"輸出查找的信息:"<<endl;
find(num);
}
else
{ cout<<"請輸入姓名:"<<endl;
cin >>name;
cout<<"輸出查找的信息:"<<endl;
find2(name);}
}
if(sel==2)
{ cout<<"姓名散列結果:"<<endl;
list2();
}
if(sel==0)
{ cout<<"請輸入要添加的內容:"<<endl;
apend();
}
if(sel==4)
{ cout<<"號碼散列結果:"<<endl;
list();
}
if(sel==5)
{ cout<<"列表已清空:"<<endl;
create();
create2();
}
if(sel==6)
{ cout<<"通信錄已保存:"<<endl;
save();
}
if(sel==7) return 0;
}
return 0;

}