⑴ 用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語言編寫通訊錄,求程序代碼
分析:
跟據樓主的意思!我們可以得出,這個程序只要求我們寫查詢通迅錄的內容
而通迅錄的內容,可以通過初始化得出!
簡而言之:寫一個查詢函數
呵呵···把問題相通了,我們就開始寫演算法吧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] = {
{"lihan", "liaoning", "huluo","han","M",19},
{"zhuqiang", "jiangsu", "changzhu", "han","M",19},
{"wangjiangang", "liaoning", "anshan","han","M",20},
{"zhanghongwei", "shandong", "zhucheng", "han","M",21},
};
(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<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 <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>
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("無此聯系人!");
}
⑺ c語言手機通訊錄 重謝
#include<stdio.h> struct Class { char *cname; //名字 char *cphon; //電話號碼 char *cuphon; //單位電話 char *cmphon; //手機號碼 char *cqq; //qq號碼 char *cbday; //生日日期 struct Class *node; //其餘的自己可以加***** }; typedef struct Class Students; //通訊錄結構 typedef Students *Ps; Ps TcraseClsSt(Ps lina); // 催的操作Ps CraseClsSt(Ps lina,char *cname,char *cphon,char *cuphon,char *cmphon,char *cqq,char *cbday); //存過程void OutSmary(Ps lina); //輸出結果 void main() { Ps lina=NULL; lina=TcraseClsSt(lina); if(lina!=NULL) //如果lina為NULL是錯誤內存非配失敗 { OutSmary(lina); } else printf("Error"); getch(); } Ps TcraseClsSt(Ps lina) { int Cmd=0; char cname[100]; char cphon[100]; char cuphon[100]; char cmphon[100]; char cqq[100]; char cbday[100]; do { printf("Please enter a name:"); scanf("%s",cname); //提示並輸入數據 printf("Enter the phone number:"); scanf("%s",cphon); printf("Enter the unit telephone:"); scanf("%s",cuphon); printf("Enter phone number:"); scanf("%s",cmphon); printf("Enter the number qq:"); scanf("%s",cqq); printf("Enter your date of birth:"); scanf("%s",cbday); lina=CraseClsSt(lina,cname,cphon,cuphon,cmphon,cqq,cbday);//把數據存入鏈表 printf("Enter -1 to exit any exit:"); scanf("%d",&Cmd); } while(Cmd!=-1&&lina!=NULL); return lina; } Ps CraseClsSt(Ps lina,char *cname,char *cphon,char *cuphon,char *cmphon,char *cqq,char *cbday) //鏈表存入函數 { Ps merory; Ps linb; merory=(Ps)malloc(sizeof(Students)); if(!merory) { return NULL; //merory為NULL是返回零 NULL } merory->node=NULL; strcpy(merory->cname,cname); //用字元創拷貝函數 段錯午 在turbo c 編譯器可以通過 gcc可以用別的方法處理 strcpy(merory->cphon,cphon); strcpy(merory->cuphon,cuphon); strcpy(merory->cmphon,cmphon); strcpy(merory->cqq,cqq); strcpy(merory->cbday,cbday); if(lina==NULL) return merory; linb=lina; while(linb->node!=NULL) linb=linb->node; linb->node=merory; return lina; } void OutSmary(Ps lina) //鏈表輸出函數 { while(lina!=NULL) { printf("name:%s,Phone:%s,Unit Number:%s,Phone number:%s,qq No:%s,Date of Birth:%s\n", lina->cname,lina->cphon,lina->cuphon,lina->cmphon,lina->cqq,lina->cbday); lina=lina->node; } }