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

c語言通訊錄源代碼

發布時間: 2022-12-30 04:26:27

㈠ 通訊錄代碼 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語言通訊錄

關於這道題的基本思路,我可以告訴你:
通訊錄一般由如下幾個信息組成:姓名、性別、通訊地址、電話號碼、郵編等組成。
如果想編寫一個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
#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語言代碼

分析:
跟據樓主的意思!我們可以得出,這個程序只要求我們寫查詢通迅錄的內容
而通迅錄的內容,可以通過初始化得出!
簡而言之:寫一個查詢函數
呵呵···把問題相通了,我們就開始寫演算法吧let's go!!!
----------------------------------------------------------------
演算法:
1.獲得用戶的輸入 (就是要查詢的對象的名字)
2.查詢 (在這我用窮舉通迅錄的方式查詢了,^_^&&^_^)
3.輸出查詢結果

演算法就這樣被我們征服了!!!呵呵~~好有成就感哇!!
但我們現在還不能開始編碼,我得們先想好怎麼編,要做到胸有成竹!!!
那我現在來想一下該怎麼編碼吧!let's go!!!
----------------------------------------------------------------
要保存通迅的信息,那麼我們得用一個結構體吧:
struct friends
{
char name[20]; /* 名字不能寫得太長哦 */
char province[20]; /* 省份 */
char city[20]; /* 所在城市 */
char nation[20]; /* 民族 */
char sex[2]; /* 性別 M/F */
int age; /* 年齡 */
};

要獲得用戶輸入,要用個 char search_name[20];
查詢結果反回一個數,記錄對象在通迅錄中的位置:int index;
查詢中有用要循環,用一個記數器: int i;
----------------------------------------------------------------
OK,該用的變數我們都想好了!演算法我們也想好了。還等什麼呢,開始編碼吧
呵呵~~是不是等這個時候都等得急了~~~~~
-------------------------------------------------------------------
*******************************************************************
******* 程序實現:
*******************************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* 定義保存通迅錄的信息 */
struct friends
{
char name[20]; /* 名字 */
char province[20]; /* 省份 */
char city[20]; /* 所在城市 */
char nation[20]; /* 民族 */
char sex[2]; /* 性別 M/F */
int age; /* 年齡 */
};

void getname (char search_name[]);
int search (struct friends friend_list[], char search_name[]);
void print_result(struct friends friend_list[], int index);

int main (void)
{
int index;
char search_name[20];
struct friends friend_list[4] = {
,
,
,
,
};

(void) getname (search_name); /* 獲得用戶輸入 */
index = search (friend_list, search_name); /* 查詢 */
(void) print_result (friend_list,index); /* 列印結果 */

return 0;
}

/****************************************
*** 函數名:getname
*** 功能:獲得用戶要查詢的對象的名字
****************************************/
void getname (char search_name[])
{
printf ("Pleace enter the name of your friends you want to search>>");
scanf ("%s", search_name);
}

/****************************************
*** 函數名:search
*** 功能:查詢對象
****************************************/
int search (struct friends friend_list[], char search_name[])
{
int i;

/* 窮舉通迅錄 */
for (i = 0; i < 4; ++i)
{
if (strcmp(friend_list[i].name, search_name) == 0)
{
return (i);
}
}

if (i == 4)
{
printf ("I am sorry! there is nobody by the name you enter!\n");
fflush(stdin);
getchar();
exit (0);
}
}

/****************************************
*** 函數名:print_result
*** 功能:列印結果
****************************************/
void print_result(struct friends friend_list[], int index)
{
printf ("the imformation of %s:\n", friend_list[index].name);
printf ("------------------------------------------------\n");
printf (" NAME: %-s\n", friend_list[index].name);
printf ("PROVINCE: %-s\n", friend_list[index].province);
printf (" CITY: %-s\n", friend_list[index].city);
printf (" NATION: %-s\n", friend_list[index].nation);
printf (" SEX: %-s\n", friend_list[index].sex);
printf (" AGE: %-d\n", friend_list[index].age);
printf ("-------------------------------------------------\n");

fflush(stdin);
getchar();

}

*****************************************************************************
*****************************************************************************
呵呵~~一口氣把它寫出來了!!!前期寫演算法是很重要的!!
現在還沒結束!!我們要先來測試一下!!
--------------------------------------
Pleace enter the name of your friends you want to search>>lihan
the imformation of lihan:
------------------------------------------------
NAME: lihan
PROVINCE: liaoning
CITY: huluo
NATION: han
SEX: M
AGE: 19
-------------------------------------------------
--------------------------------------
Pleace enter the name of your friends you want to search>>lbmzwyy
I am sorry! there is nobody by the name you enter!

說明成功了~~~呵呵~~太高興了~~
--------------------------------------
請記注一點:剋制編碼的誘惑
無論多麼小的問題都要先分析好問題,想好演算法,才能開始編碼!!!我相信這樣做一定對你有好處的!

如果對您有幫助,請記得採納為滿意答案,謝謝!祝您生活愉快!

㈤ 用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 <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) /*用尾插法將其插入鏈表中*/

else
{r->next=s;
r=s;r->next=NULL;
}
}fclose(fp);
}
else
{ fp=fopen("數據文件.txt","w"); /*不能打開另開辟一個文件*/
i=1;
}
}

void Show()

void Delete()

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()

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語言通訊錄代碼 謝謝 QAQ代碼太長 急需 詳細一點 感謝

發我幫你注釋下

㈧ C語言的通訊錄代碼是什麼

#include x0dx0a#include x0dx0a#include x0dx0a#include x0dx0a#define N 100 void input();//添加新用戶函數 x0dx0avoid amend();//修改用戶信息函數 x0dx0avoid delete_client();//刪除用戶信息函數 x0dx0avoid demand_client();//用戶信息查詢函數 x0dx0avoid collect_telephone();//用戶信息匯總函數 x0dx0avoid save_client(struct telephone message);//保存函數 x0dx0avoid demand_name();//按用戶名查詢 x0dx0avoid demand_telephone();//按電話號碼查詢 x0dx0a x0dx0astruct telephone x0dx0a{ x0dx0achar client_name[20]; x0dx0achar client_address[30]; x0dx0achar client_telephone[15]; x0dx0a}; //添加新用戶函數 x0dx0avoid input() x0dx0a{ x0dx0astruct telephone message; x0dx0achar reply='y' x0dx0achar save='y' x0dx0awhile (reply=='y') x0dx0a{ printf("用戶姓名:"); x0dx0ascanf("%s",message.client_name); x0dx0a x0dx0aprintf("電話號碼:"); x0dx0ascanf("%s",message.client_telephone); save_client(message); x0dx0a x0dx0aprintf("要繼續嗎?(y/n):"); x0dx0ascanf(" %c",&reply); x0dx0a} x0dx0aprintf("按任意鍵返回主菜單??\n"); x0dx0agetchar(); x0dx0agetchar(); x0dx0a} //保存函數 x0dx0avoid save_client(struct telephone message) x0dx0a{ x0dx0aFILE *fp; x0dx0afp=fopen("message.dat","a+"); x0dx0aif (fp!=NULL) x0dx0a{ x0dx0afwrite(&message,sizeof(struct telephone),1,fp); x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("\n打開文件時出現錯誤,按任意鍵返回??\n"); x0dx0agetchar(); x0dx0areturn; x0dx0a} x0dx0afclose(fp); x0dx0a} //修改用戶信息函數 x0dx0avoid amend() x0dx0a{ x0dx0astruct telephone message; x0dx0aFILE *fp; x0dx0achar amend_name[20]; x0dx0achar reply='y' x0dx0achar found='y' x0dx0achar save='y' x0dx0aint size=sizeof(struct telephone); x0dx0awhile (reply=='y') x0dx0a{ x0dx0afound='n' x0dx0afp=fopen("message.dat","r+w"); x0dx0aif (fp!=NULL) x0dx0a{ x0dx0asystem("cls"); x0dx0aprintf("\n請輸入要修改的姓名:"); x0dx0ascanf("%s",amend_name); x0dx0awhile ((fread(&message,size,1,fp))==1) x0dx0a{ x0dx0aif ((strcmp(amend_name,message.client_name))==0) x0dx0a{ x0dx0afound='y' x0dx0abreak; x0dx0a} x0dx0a} x0dx0aif (found=='y') x0dx0a{ printf("==========================================\n"); x0dx0aprintf("\n用戶姓名:%s\n",message.client_name); x0dx0a x0dx0aprintf("\n電話號碼:%s\n",message.client_telephone); x0dx0aprintf("==========================================\n"); x0dx0aprintf("修改用戶信息:\n"); x0dx0aprintf("\n用戶姓名:"); x0dx0ascanf("%s",message.client_name); printf("\n電話號碼:"); x0dx0ascanf("%s",message.client_telephone); x0dx0aprintf("\n要保存嗎?(y/n):"); x0dx0ascanf(" %c",&save); x0dx0aif (save=='y') x0dx0a{ x0dx0afseek(fp,-size,1); x0dx0afwrite(&message,sizeof(struct telephone),1,fp); x0dx0a} x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("無此人信息!\n"); x0dx0a} x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("打開文件時出現錯誤,按任意鍵返回??\n"); x0dx0agetchar(); x0dx0areturn; x0dx0a} x0dx0afclose(fp); x0dx0aprintf("要繼續嗎?(y/n):"); x0dx0ascanf(" %c",&reply); x0dx0a} x0dx0aprintf("按任意鍵返回主菜單??\n"); x0dx0agetchar(); x0dx0agetchar(); x0dx0a} //刪除用戶信息函數 x0dx0avoid delete_client() x0dx0a{ x0dx0astruct telephone message[N]; x0dx0astruct telephone temp_str; x0dx0astruct telephone delete_str; x0dx0aint i=0,j=0; x0dx0achar reply='y' x0dx0achar found='y' x0dx0achar confirm='y' x0dx0achar delete_name[20]; x0dx0aFILE *fp; x0dx0awhile (reply=='y') x0dx0a{ x0dx0asystem("cls"); x0dx0afp=fopen("message.dat","r"); x0dx0aif (fp!=NULL) x0dx0a{ x0dx0ai=0; x0dx0afound='n' x0dx0aprintf("\n請輸入姓名:"); x0dx0ascanf("%s",delete_name); x0dx0awhile ((fread(&temp_str,sizeof(struct telephone),1,fp))==1) x0dx0a{ x0dx0aif ((strcmp(delete_name,temp_str.client_name))==0) x0dx0a{ x0dx0afound='y' x0dx0adelete_str=temp_str; x0dx0a}//查找要刪除的記錄 x0dx0aelse x0dx0a{ x0dx0amessage[i]=temp_str; x0dx0ai++; x0dx0a}//將其它無關記錄保存起來 x0dx0a} x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("打開文件時出現錯誤,按任意鍵返回??\n"); x0dx0agetchar(); x0dx0areturn; x0dx0a} x0dx0afclose(fp); x0dx0aif (found=='y') x0dx0a{ x0dx0aprintf("==========================================\n"); x0dx0aprintf("用戶姓名:%s\n",delete_str.client_name); x0dx0a x0dx0aprintf("電話號碼:%s\n",delete_str.client_telephone); x0dx0aprintf("==========================================\n"); x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("無此人信息,按任意鍵返回??\n"); x0dx0agetchar(); x0dx0abreak; x0dx0a} x0dx0aprintf("確定要刪除嗎?(y/n):"); x0dx0ascanf(" %c",&confirm); x0dx0aif (confirm=='y') x0dx0a{ x0dx0afp=fopen("message.dat","w"); x0dx0aif (fp!=NULL) x0dx0a{ x0dx0afor(j=0;j3) x0dx0a{ x0dx0aprintf("請輸入1-6之間的整數\n"); x0dx0aprintf("按任意鍵返回菜單??\n"); x0dx0a getchar(); x0dx0acontinue; x0dx0a} x0dx0aif (choice==1) x0dx0a{ x0dx0ademand_name(); x0dx0a} x0dx0aelse if (choice==2) x0dx0a{ x0dx0ademand_telephone(); x0dx0a} x0dx0a} x0dx0a} //按用戶名查詢 x0dx0avoid demand_name() x0dx0a{ x0dx0astruct telephone message; x0dx0aFILE *fp; x0dx0achar amend_name[20]; x0dx0achar reply='y' x0dx0achar found='y' x0dx0awhile (reply=='y') x0dx0a{ x0dx0afound='n' x0dx0afp=fopen("message.dat","r+w"); x0dx0aif (fp!=NULL) x0dx0a{ x0dx0asystem("cls"); x0dx0aprintf("\n請輸入姓名:"); x0dx0ascanf("%s",amend_name); x0dx0awhile ((fread(&message,sizeof(struct telephone),1,fp))==1) x0dx0a{ x0dx0aif ((strcmp(amend_name,message.client_name))==0) x0dx0a{ x0dx0afound='y' x0dx0abreak; x0dx0a} x0dx0a} x0dx0aif (found=='y') x0dx0a{ printf("==========================================\n"); x0dx0aprintf("用戶姓名:%s\n",message.client_name); printf("電話號碼:%s\n",message.client_telephone); x0dx0aprintf("==========================================\n"); x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("無此人信息!\n"); x0dx0a} x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("打開文件時出現錯誤,按任意鍵返回??\n"); x0dx0agetchar(); x0dx0areturn; x0dx0a} x0dx0afclose(fp); x0dx0aprintf("要繼續嗎?(y/n):"); x0dx0ascanf(" %c",&reply); x0dx0a} x0dx0aprintf("按任意鍵返回主菜單??\n"); x0dx0agetchar(); x0dx0agetchar(); x0dx0a} //按電話號碼查詢 x0dx0avoid demand_telephone() x0dx0a{ x0dx0astruct telephone message; x0dx0aFILE *fp; x0dx0achar telephone[20]; x0dx0achar reply='y' x0dx0achar found='y' x0dx0awhile (reply=='y') x0dx0a{ x0dx0afound='n' x0dx0afp=fopen("message.dat","r+w"); x0dx0aif (fp!=NULL) x0dx0a{ x0dx0asystem("cls"); x0dx0aprintf("\n請輸入電話號碼:"); x0dx0ascanf("%s",telephone); x0dx0awhile ((fread(&message,sizeof(struct telephone),1,fp))==1) x0dx0a{ x0dx0aif ((strcmp(telephone,message.client_telephone))==0) x0dx0a{ x0dx0afound='y' x0dx0abreak; x0dx0a} x0dx0a} x0dx0aif (found=='y') x0dx0a{ printf("==========================================\n"); x0dx0aprintf("用戶姓名:%s\n",message.client_name); printf("電話號碼:%s\n",message.client_telephone); x0dx0aprintf("==========================================\n"); x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("無此電話號碼的有關信息!\n"); x0dx0a} x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("打開文件時出現錯誤,按任意鍵返回??\n"); x0dx0agetchar(); x0dx0areturn; x0dx0a} x0dx0afclose(fp); x0dx0aprintf("要繼續嗎?(y/n):"); x0dx0ascanf(" %c",&reply); x0dx0a} x0dx0aprintf("按任意鍵返回主菜單??\n"); x0dx0agetchar(); x0dx0agetchar(); x0dx0a} //用戶信息匯總函數 x0dx0avoid collect_telephone() x0dx0a{ x0dx0astruct telephone message; x0dx0aFILE *fp; x0dx0afp=fopen("message.dat","r"); x0dx0aif (fp!=NULL) x0dx0a{ x0dx0asystem("cls"); x0dx0aprintf("\n用戶姓名\t\t電話號碼\n"); x0dx0awhile ((fread(&message,sizeof(struct telephone),1,fp))==1) x0dx0a{ x0dx0aprintf("\n%-24s",message.client_name); printf("%-12s\n",message.client_telephone); x0dx0a} x0dx0a} x0dx0aelse x0dx0a{ x0dx0aprintf("打開文件時出現錯誤,按任意鍵返回??\n"); x0dx0agetchar(); x0dx0areturn; x0dx0a} x0dx0afclose(fp); x0dx0aprintf("按任意鍵返回主菜單??\n"); x0dx0agetch(); x0dx0a} void main() x0dx0a{ x0dx0achar choice[10]=""; x0dx0aint len=0; x0dx0awhile (choice[0]!=ƍ') x0dx0a{ printf("\t==========電話本號碼查詢系統=============\n"); printf("\t\t 1、添加新聯系人\n"); x0dx0aprintf("\t\t 2、修改聯系人信息\n"); x0dx0aprintf("\t\t 3、刪除聯系人信息\n"); x0dx0aprintf("\t\t 4、聯系人信息查詢\n"); x0dx0aprintf("\t\t 5、聯系人信息匯總\n"); x0dx0a x0dx0aprintf("\t\t 7、退出\n"); x0dx0aprintf("\t=========================================\n"); x0dx0aprintf("請選擇(1-7):"); x0dx0ascanf("%s",choice); x0dx0alen=strlen(choice); x0dx0aif (len>1) x0dx0a{ x0dx0aprintf("請輸入1-6之間的整數\n"); x0dx0aprintf("按任意鍵返回主菜單??\n"); x0dx0agetchar(); x0dx0agetchar(); x0dx0acontinue; x0dx0a} switch (choice[0]) { x0dx0acase Ƈ': x0dx0ainput(); x0dx0abreak; x0dx0acase ƈ': x0dx0aamend(); x0dx0abreak; x0dx0acase Ɖ': x0dx0adelete_client(); x0dx0abreak; x0d

㈨ C語言 通訊錄源代碼 無鏈表 無指針

#include<stdio.h> /*引用庫函數*/
#include<stdlib.h>
#include<ctype.h>
#include<string.h>typedef struct /*定義結構體數組*/
{
char num[10]; /*學號*/
char name[20]; /*姓名*/
char sushe[20];/*宿舍*/
char tel[20];/*電話*/
char qq[20];/*QQ號碼*/
char email[20];/*電子郵箱*/
}Student;
Student stu[80]; /*結構體數組變數*/ int menu_select() /*菜單函數*/
{
char c;
do{
system("cls"); /*運行前清屏*/
printf("\t\t |☆☆☆☆☆☆通訊錄信息管理系統☆☆☆☆☆\n"); /*菜單選擇*/

printf("\t\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 |******** 6. 按姓名檢索信息 *************|\n");
printf("\t\t |******** 7. 讀取文件 *******************|\n");
printf("\t\t |******** 8. 保存文件 *******************|\n");
printf("\t\t |******** 0. 退出系統 *******************|\n");

printf("\t\t |****************************************|\n");

printf("\t\t |***製作人:QIANG *****|\n");

printf("\t\t |****************************************|\n");

printf("\t\t (*^__^*) 您好,請選擇(*^__^*)→→→(0-8):");
c=getchar(); /*讀入選擇*/
}while(c<'0'||c>'9');
return(c-'0'); /*返回選擇*/
}
int Input(Student stud[],int n) /*輸入若干條記錄*/
{
int i=0,p=0;
char sign,f,x[10]; /*x[10]為清除多餘的數據所用*/
while(sign!='n'&&sign!='N') /*判斷*/
{
printf("\t\t\t請輸入學號:"); /*交互輸入*/
scanf("\t\t\t%s",stud[n+i].num);
for(p=0;p=n;p++)
{
if(strcmp(stud[p].num,stud[n+i].num)==0)
{
printf("\t\t\t學號已經存在!\n");
system("pause");
return(n);
}
}
printf("\t\t\t請輸入姓名:");
scanf("\t\t\t%s",stud[n+i].name);
printf("\t\t\t請輸入住址:");
scanf("\t\t\t%s",stud[n+i].sushe);
printf("\t\t\t請輸入電話號碼:");
scanf("\t\t\t%s",stud[n+i].tel);
printf("\t\t\t請輸入QQ:");
scanf("\t\t\t%s",stud[n+i].qq);
printf("\t\t\t請輸入電子郵箱:");
scanf("\t\t\t%s",&stud[n+i].email);
gets(x); /*清除多餘的輸入*/
printf("\t\t\tany more records?(Y/N)");
scanf("\t\t\t%c",&sign); /*輸入判斷*/
i++;
}
return(n+i);
} void Display(Student stud[],int n) /*顯示所有記錄*/
{
int i;
printf("--------------------------------------------------------------------------------\n"); /*格式頭*/
printf("學號 名字 住址 電話號碼 QQ號碼 電子郵箱\n");
printf("--------------------------------------------------------------------------------\n");

for(i=1;i<n+1;i++) /*循環輸入*/
{
printf("%-16s%-15s%-15s%-15s%-15s%-15s\n",stud[i-1].num,stud[i-1].name,stud[i-1].sushe,stud[i-1].tel,stud[i-1].qq,stud[i-

1].email);
if(i>1&&i%10==0) /*每十個暫停*/
{printf("--------------------------------------------------------------------------------\n"); /*格式*/ <br> printf("\t\t\t"); <br> system("pause"); <br> printf("--------------------------------------------------------------------------------\n"); <br> }
}
printf("\t\t\t");
system("pause");
} void Sort_by_num(Student stud[],int n) /*按學號排序*/
{ int i,j;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
{
strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
strcpy(t,stud[j+1].sushe);
strcpy(stud[j+1].sushe,stud[j].sushe);
strcpy(stud[j].sushe,t);
strcpy(t,stud[j+1].tel);
strcpy(stud[j+1].tel,stud[j].tel);
strcpy(stud[j].tel,t);
strcpy(t,stud[j+1].qq);
strcpy(stud[j+1].qq,stud[j].qq);
strcpy(stud[j].qq,t);
strcpy(t,stud[j+1].email);
strcpy(stud[j+1].email,stud[j].email);
strcpy(stud[j].email,t);
}
} void Query_a_record(Student stud[],int n) /*查找並顯示一個記錄*/
{
char s[20];
int i=0;
printf("\t\t\t請輸入要查找的人的名字:"); /*互動式輸入*/
scanf("\t\t\t%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("\t\t\t對不起,查無此人!\n"); /*輸入失敗信息*/
return;

}
printf("\t\t\t學號:%s\n",stud[i].num); /*輸出該學生信息*/
printf("\t\t\t住址:%s\n",stud[i].sushe);
printf("\t\t\t電話號碼:%s\n",stud[i].tel);
printf("\t\t\tQQ號碼:%s\n",stud[i].qq);
printf("\t\t\t電子郵箱:%s\n",stud[i].email);
}
int Delete_a_record(Student stud[],int n) /*按姓名查找,刪除一條記錄*/
{
char s[20];
int i=0,j;
printf("\t\t\t請輸入要刪除的人的名字:"); /*互動式問尋*/
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("\t\t\t對不起,查無此人!\n"); /*返回失敗信息*/
return(n);
}
for(j=i;j<n-1;j++) /*刪除操作*/
{
strcpy(stud[j].num,stud[j+1].num);
strcpy(stud[j].sushe,stud[j+1].sushe);
strcpy(stud[j].tel,stud[j+1].tel);
strcpy(stud[j].qq,stud[j+1].qq);
strcpy(stud[j].name,stud[j+1].name);
strcpy(stud[j].email,stud[j+1].email);

}
printf("\t\t\t刪除成功!\n"); /*返回成功信息*/
return(n-1);
}
int AddfromText(Student stud[],int n) /*從文件中讀入數據*/
{
int i=0,num;
FILE *fp; /*定義文件指針*/
char filename[20]; /*定義文件名*/
printf("\t\t\t請您輸入要讀取的文件名:");
scanf("\t\t\t%s",filename); /*輸入文件名*/
if((fp=fopen(filename,"rb"))==NULL) /*打開文件*/
{
printf("\t\t\t打開文件失敗!\n"); /*打開失敗信息*/
printf("\t\t\t");

system("pause");
return(n);
}
fscanf(fp,"%d",&num); /*讀入總記錄量*/
while(i<num) /*循環讀入數據*/
{
fscanf(fp,"%s%s%s%s%s%s",stud[n+i].num,stud[n+i].name,stud[n+i].sushe,stud[n+i].tel,stud[n+i].qq,stud[n+i].email);
i++;
}
n+=num;
fclose(fp); /*關閉文件*/
printf("\t\t\t讀取成功!\n");
printf("\t\t\t");
system("pause");
return(n);
} void WritetoText(Student stud[],int n) /*將所有記錄寫入文件*/
{
int i=0;
FILE *fp; /*定義文件指針*/
char filename[20]; /*定義文件名*/
printf("\t\t\t保存文件\n"); /*輸入文件名*/
printf("\t\t\t請您為文件取個名字:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"w"))==NULL) /*打開文件*/
{
printf("\t\t\t成功!\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n); /*循環寫入數據*/
while(i<n)
{
fprintf(fp,"%s %s %s %s %s %s \n",stud[i].num,stud[i].name,stud[i].sushe,stud[i].tel,stud[i].qq,stud[i].email);
i++;
}
fclose(fp); /*關閉文件*/
printf("成功!\n"); /*返回成功信息*/
} int Modify_a_record(Student stud[],int n) /*按姓名查找,修改一條記錄*/
{
char s[20];
char newname[20];
char newnum[20];
char newsushe[20];
char newtel[20];
char newqq[20];
char newemail[20];
int i=0;
printf("\t\t\t輸入要修改人的名字:"); /*互動式問尋*/
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{
printf("\t\t\t找不到這個信息!\n"); /*返回失敗信息*/
return(n+1);
}
printf("請輸入新姓名:");
scanf("%s",newname);
strncpy(stud[i].name,newname,sizeof(stud[i].name));
stud[i].name[sizeof(stud[i].name)-1]='\0'; printf("請輸入新學號:");
scanf("%s",newnum);
strncpy(stud[i].num,newnum,sizeof(stud[i].num));
stud[i].num[sizeof(stud[i].num)-1]='\0'; printf("請輸入新地址:");
scanf("%s",newsushe);
strncpy(stud[i].sushe,newsushe,sizeof(stud[i].sushe));
stud[i].sushe[sizeof(stud[i].sushe)-1]='\0'; printf("請輸入新電話號碼:");
scanf("%s",newtel);
strncpy(stud[i].tel,newtel,sizeof(stud[i].tel));
stud[i].tel[sizeof(stud[i].tel)-1]='\0'; printf("請輸入新qq號碼:");
scanf("%s",newname);
strncpy(stud[i].qq,newqq,sizeof(stud[i].qq));
stud[i].qq[sizeof(stud[i].qq)-1]='\0'; printf("請輸入新電子郵箱:");
scanf("%s",newemail);
strncpy(stud[i].email,newemail,sizeof(stud[i].email));
stud[i].email[sizeof(stud[i].email)-1]='\0'; printf("\t\t\t修改信息成功咯\n"); /*返回成功信息*/
return (n);
}
void main() /*主函數*/
{
int n=0;
system("color 1A"); //調用dos改背景與前景色
for(;;)
{
switch(menu_select()) /*選擇判斷*/
{
case 1:
printf("\t\t\t輸入信息\n"); /*輸入若干條記錄*/
n=Input(stu,n);
break;
case 2:
printf("\t\t\t顯示信息\n"); /*顯示所有記錄*/
Display(stu,n);
break;
case 3:
printf("\t\t\t按學號排序\n");
Sort_by_num(stu,n); /*按學號排序*/
printf("\t\t\t排列成功咯!\n");
printf("\t\t\t");
system("pause");
break;
case 4:
printf("\t\t\t修改學生信息\n");
n=Modify_a_record(stu,n); /*按姓名查找,修改一條記錄*/
printf("\t\t\t");
system("pause");
break;
case 5:
printf("\t\t\t刪除學生信息\n");
n=Delete_a_record(stu,n); /*按姓名查找,刪除一條記錄*/
printf("\t\t\t");
system("pause");
break;
case 6:
printf("\t\t\t查找一個信息\n");
Query_a_record(stu,n); /*查找並顯示一個記錄*/
printf("\t\t\t");
system("pause");
break;
case 7:
printf("\t\t\t打開保存過的文件\n");
n=AddfromText(stu,n); /*新增功能,輸出統計信息*/
break;
case 8:
printf("\t\t\t保存數據到文件\n");
WritetoText(stu,n); /*循環寫入數據*/
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\t感謝使用,祝你好運哦!\n"); /*結束程序*/
printf("\t\t\t");
system("pause");
exit(0);
}
}
}

㈩ 求C語言程序設計手機通訊錄代碼,急,

//這個是我教材上的,不知道你要的是不這個
#include<stdio.h>
#include<string.h>
struct friends_list
{
char name[10];
int age;
char telephone[13];
};

int Count=0;
void new_friend(struct friends_list friends[]);
void search_friend(struct friends_list friends[],char *name);

int main(void)
{
int choice;
char name[10];
struct friends_list friends[50];

do
{
printf("通訊錄功能選項:1:新建2:查詢0:退出\n");
printf("請選擇功能:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:new_friend(friends);
break;
case 2:printf("請輸入要查找的聯系人姓名:");
scanf("%s",name);
search_friend(friends,name);
break;
case 0:break;
}
}
while(choice!=0);
printf("謝謝使用通訊錄功能!\n");

return 0;
}
void new_friend(struct friends_list friends[])
{
struct friends_list f;
if(Count==50)
{
printf("通訊錄已滿!\n");
return;
}
printf("請輸入新聯系人的姓名:");
scanf("%s",f.name);
printf("請輸入新聯系人的年齡:");
scanf("%d",&f.age);
printf("請輸入新聯系人的聯系電話:");
scanf("%s",f.telephone);
friends[Count]=f;
Count++;
}
void search_friend(struct friends_list friends[],char *name)
{
int i,flag=0;
if(Count==0)
{
printf("通訊錄是空的!\n");
return;
}
for(i=0;i<Count;i++)
if(strcmp(name,friends[i].name)==0)
{
flag=1;
break;
}
if(flag)
{
printf("姓名:%s\t",friends[i].name);
printf("年齡:%d\t",friends[i].age);
printf("電話:%s\n",friends[i].telephone);
}
else
printf("無此聯系人!");
}

熱點內容
redhat存儲 發布:2025-07-02 11:12:50 瀏覽:585
優酷不能用流量緩存 發布:2025-07-02 11:10:46 瀏覽:940
彩虹島小草怎麼設置腳本 發布:2025-07-02 11:10:33 瀏覽:921
越壓縮越封閉 發布:2025-07-02 11:09:10 瀏覽:90
jre下載linux 發布:2025-07-02 11:07:43 瀏覽:219
安卓手機換行如何操作 發布:2025-07-02 11:03:12 瀏覽:547
玩客雲伺服器搭建 發布:2025-07-02 10:59:58 瀏覽:357
假笑資料庫 發布:2025-07-02 10:59:09 瀏覽:850
手機怎麼製作腳本 發布:2025-07-02 10:59:05 瀏覽:366
mybatis的動態sql語句 發布:2025-07-02 10:56:51 瀏覽:958