c語言通訊錄代碼
『壹』 用c語言編寫一個通訊錄,並對他進行增加,查詢,刪除,修改,顯示記錄等操作,要寫出源代碼並畫出流程圖
這個是我編的,用數組和鏈表兩種功能實現的通訊錄
基本能滿足你的要求!!
代碼如下:
#include "stdlib.h"
#define NEW (struct node *)malloc(sizeof(struct node))
struct student
{ char name[10],tel[11];
}a[20];
struct node
{ char name[20],tel[11];
struct node *next;
};
main()
{ struct student *jianli(),*delete(struct student *);
struct student *charu(struct student *);
void xianshi(struct student *);
struct node *create(),*delnode(struct node*,char *);
struct node *insert(struct node *,struct node *,char *);
void prlist(struct node *);
struct student *p;
struct node *head=NULL,*stu;
char s[80],name[20],q[80];
int c,w;
a:;
system("cls");
printf("\nEnter your choice\n");
printf("1.SHUZU\n2.LIANBIAO\n0.Quit\n");
gets(q);
w=atoi(q);
switch(w)
{ case 1:
do
{ do
{ printf("----------------------------------------------\n");
printf("******************Phone book******************\n");
printf("----------------------------------------------\n");
printf(" | | <1> Add a note | |\n");
printf(" | | <2> Show the list | |\n");
printf(" | | <3> Delete a note | |\n");
printf(" | | <4> Insert a note | |\n");
printf(" | | <0> Quit | |\n");
printf("----------------------------------------------\n");
printf(" Enter your choice(0-4):");
gets(s);
c=atoi(s);
}while(c<0||c>4);
system("cls");
switch(c)
{ case 1: p=jianli();break;
case 2: xianshi(p);break;
case 3: printf("\nPlease input the name to deleted\n");
p=delete(p);break;
case 4: printf("\nPlease input the new name\n");
p=charu(p);break;
}
}while(c);goto a;
case 2:
do
{ do
{printf("----------------------------------------------\n");
printf("******************Phone book******************\n");
printf("----------------------------------------------\n");
printf(" | | <1> Add a note | |\n");
printf(" | | <2> Show the list | |\n");
printf(" | | <3> Delete a note | |\n");
printf(" | | <4> Insert a note | |\n");
printf(" | | <0> Quit | |\n");
printf("----------------------------------------------\n");
printf(" Enter your choice(0-4):");
gets(s);
c=atoi(s);
}while(c<0||c>4);
system("cls");
switch(c)
{ case 1: head=create();break;
case 2: prlist(head);break;
case 3: printf("\nInput the name to deleted\n");
gets(name);
head=delnode(head,name);break;
case 4: stu=NEW;
printf("\nInput the new node\n");
printf("name:");
gets(stu->name);
printf("tel:");
gets(stu->tel);
stu->next=NULL;
printf("\nInsert position\n");
printf("name:");
gets(name);
head=insert(head,stu,name);
}
}while(c);goto a;
}
}
#include "string.h"
struct student *jianli()
{ char c1[10],c2[11];
int i=0;
printf("name:");
gets(c1);
while(strlen(c1)!=0)
{ strcpy(a[i].name,c1);
printf("tel:");
gets(c2);
strcpy(a[i].tel,c2);
i++;
printf("name:");
gets(c1);
}
return a;
}
#include "string.h"
struct student *delete(struct student *p)
{ char c1[10];
int i=0,j,l=0;
while(strlen(p[i].name)!=0)
i++;
printf("name:");
gets(c1);
for(j=0;j<=i+1;j++)
if(strcmp(c1,p[j].name)==0)
{p[j]=p[j+1];
l=j;}
while(l<i+1)
{p[l]=p[l+1];
l++;}
return p;
}
#include "string.h"
struct student *charu(struct student *p)
{ char c1[10],c2[11];
int i=0;
while(strlen(p[i].name)!=0)
i++;
printf("name:");
gets(c1);
strcpy(p[i].name,c1);
printf("tel:");
gets(c2);
strcpy(p[i].tel,c2);
return p;
}
#include "string.h"
void xianshi(struct student *p)
{ int i=0;
printf("name\ttel\n\n");
while(strlen(p[i].name)!=0)
{ printf("%s\t%s\n",p[i].name,p[i].tel);
i++;}
}
#include "stdlib.h"
#include "string.h"
#define NEW (struct node *)malloc(sizeof(struct node))
struct node *create()
{ struct node *h;
struct node *p,*q;
char name[20];
h=q=NULL;
printf("name:");
gets(name);
while(strlen(name)!=0)
{ p=NEW;
if(p==NULL)
{ printf("Allocation failure\n");
exit(0);
}
strcpy(p->name,name);
printf("tel:");
gets(p->tel);
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
printf("name:");
gets(name);
}
return h;
}
struct node *insert(struct node *head,struct node *p0,char *x)
{ struct node *p,*q;
if(head==NULL)
{ head=p0;
p0->next=NULL;
}
else
{ p=head;
while(strcmp(x,p->name)!=0&&p->next!=NULL)
{ q=p;p=q->next;}
if(strcmp(x,p->name)==0)
{ if(p==head)
head=p0;
else
q->next=p0;
p0->next=p;
}
else
{ p->next=p0;
p0->next=NULL;
}
}
return head;
}
void prlist(struct node *head)
{ struct node *p;
p=head;
printf("name\ttel\n\n");
while(p!=NULL)
{ printf("%s\t%s\n",p->name,p->tel);
p=p->next;
}
}
struct node *delnode(struct node *head,char *x)
{ struct node *p,*q;
if(head==NULL)
{ printf("this is a empty list.");
return head;
}
p=head;
while(strcmp(x,p->name)!=0&&p->next!=NULL)
{ q=p;p=p->next;}
if(strcmp(x,p->name)==0)
{ if(p==head)
head=p->next;
else
q->next=p->next;
free(p);
}
else
printf("Not found.");
return head;
}
『貳』 求用C語言編寫一個學生通訊錄管理系統
/* HELLO.C -- Hello, world */
#include<stdio.h>
struct person
{
char name[8];
char tel[15];
char addr[50];
};
char filename[20]="E:\\txl.txt";
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void delete();
main( )
{
int m;
creat();
while(1)
{
printf("\n\t\t*********歡迎使用通訊錄信息管理系統*********\n\n");
printf("\n\t\t添加,請按1");
printf("\n\t\t查找,請按2");
printf("\n\t\t修改,請按3");
printf("\n\t\t刪除,請按4");
printf("\n\t\t輸出,請按5");
printf("\n\t\t退出,請按0\n");
printf("\n\t\t********************************************\n\n");
printf("Please select(0--5):");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: delete();
break;
case 5: output();
break;
case 0: exit();
}
printf("\n\n操作完畢,請再次選擇!");
}
else
printf("\n\n選擇錯誤,請再次選擇!");
}
}
/*輸入模塊creat( ): 創建通訊錄的子函數。*/
void creat()
{
struct person one;
long s1;
/*printf("\n請輸入通訊錄文件名:");
scanf("%s",filename); */
if((fp=fopen(filename,"at+"))==NULL)
{
printf("\n不能建立通訊錄!");
exit();
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","電話號碼","住址");
printf("\n請輸入姓名、電話號碼及住址(以0結束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
/*輸出模塊output( ):輸出通訊錄中聯系人通訊信息的子函數*/
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
printf("\n\n%20s\n","通 訊 錄");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
/*添加模塊append( ):向通訊錄中添加某人通訊信息的子函數*/
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
printf("\n請輸入添加的姓名、電話號碼及住址\n");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
fclose(fp);
}
/*查找模塊search( ):在通訊錄中查找某人通訊信息的子函數*/
void search()
{
int k=0;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}
/*修改模塊modify( ):在通訊錄中修改某人通訊信息的子函數*/
void modify()
{
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n請輸入新姓名、電話號碼及住址:");
scanf("%s%s%s",one.name,one.tel,one.addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
}
else
printf("\n對不起,通訊錄中沒有此人的記錄。");
fclose(fp);
}
/*刪除模塊delete( ):在通訊錄中刪除某人通訊信息的子函數*/
void delete()
{
int m,k=0,flag;
long offset1,offset2;
char namekey[8], valid[4];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
if(k)
{
printf("\n已查到,記錄為");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n確實要刪除,按1;不刪除,按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s\n","","","");
}
else
printf("\n對不起,通訊錄中沒有此人的記錄。");
fclose(fp);
}
}
}
}
<PIXTEL_MMI_EBOOK_2005>1###########################################################</PIXTEL_MMI_EBOOK_2005>
『叄』 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"
#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語言編程製作一個班級通訊錄,用結構定義通訊錄,包括姓名,學號,手機號,email地址,QQ。
參考一下吧
/*#include "stdio.h"
int i;
void prt()
{
for(i=5;i<8;i++) printf("%c",'*');
printf("\t");
}
main()
{
for(i=5;i<=8;i++) prt();
}
*/
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#define MAX 50
void input();
void sort();
void display();
void insert();
void del();
void average();
int now_no=0;
struct student
{
int no;
char name[20];
char sex[4];
float score1;
float score2;
float score3;
float sort;
float ave;
float sum;
};
struct student stu[MAX],*p;
main()
{
char as;
start: printf("\t\t\t歡迎使用學生成績管理系統\n\n");
do{
printf("\n\t\t\t\t1.錄入學員信息\n\t\t\t\t2.顯示學員信息\n\t\t\t\t3.成績排序信息\n\t\t\t\t4.添加學員信息\n\t\t\t\t5.刪除學員信息\n\t\t\t\t6.退出\n");
printf("\t\t\t\t選擇功能選項:");
as=getch();
switch(as)
{
case '1':
system("cls");
input();
break;
case '2':
system("cls");
display();
break;
case '3':
system("cls");
sort();
break;
case '4':
system("cls");
insert();
break;
case '5':
system("cls");
del();
break;
case '6':
system("exit");
exit(0);
default:
system("cls");
goto start;
}
}while(1);
}
void input()
{
int i=0;
char ch;
do
{
printf("\t\t\t\t1.錄入學員信息\n輸入第%d個學員的信息\n",i+1);
printf("\n輸入學生編號:");
scanf("%d",&stu[i].no);
fflush(stdin);
printf("\n輸入學員姓名:");
fflush(stdin);
gets(stu[i].name);
printf("\n輸入學員性別:");
fflush(stdin);
gets(stu[i].sex);
printf("\n輸入學員成績:");
fflush(stdin);
scanf("%f",&stu[i].score1);
printf("\n輸入學員成績:");
fflush(stdin);
scanf("%f",&stu[i].score2);
printf("\n輸入學員成績:");
fflush(stdin);
scanf("%f",&stu[i].score3);
printf("\n\n");
i++;
now_no=i;
printf("%d",now_no);
printf("是否繼續輸入?(Y/N)");
ch=getch();
system("cls");
}
while(ch!='n');
system("cls");
}
void sort()
{
struct student temp;
int i,j;
for(i=1;i<now_no;i++)
{
for(j=1;j<=now_no-i;j++)
{
if(stu[j-1].ave<stu[j].ave)
{temp=stu[j];<br/>stu[j]=stu[j-1];<br/>stu[j-1]=temp;<br/>}}}
}
void display()
{
int i;
char as;
float ave;
do
{
printf("\t\t班級學員信息列表\n");
printf("\t編號\t姓名\t性別\t成績\t成績\t成績\t平均值\n");
for(i=0;i<now_no;i++)
{
stu[i].sum=stu[i].score1+stu[i].score2+stu[i].score3;
stu[i].ave=stu[i].sum/3;
printf("\t%d\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].ave);
}
printf("\t\t按任意鍵返回主菜單.");
as=getch();
goto exit;
}
while(as!='n');
exit: system("cls");
}
void insert()
{
char ch;
do
{
printf("\n\t\t輸入新插入學員隊信息\n");
printf("\n輸入學生編號:");
scanf("%d",&stu[now_no].no);
fflush(stdin);
printf("\n輸入學員姓名:");
fflush(stdin);
gets(stu[now_no].name);
printf("\n輸入學員性別:");
fflush(stdin);
gets(stu[now_no].sex);
printf("\n輸入學員成績:");
fflush(stdin);
scanf("%f",&stu[now_no].score1);
printf("\n輸入學員成績:");
fflush(stdin);
scanf("%f",&stu[now_no].score2);
printf("\n輸入學員成績:");
fflush(stdin);
scanf("%f",&stu[now_no].score3);
printf("\n\n");
now_no=now_no+1;
sort();
printf("是否繼續輸入?(Y/N)");
ch=getch();
system("cls");
}
while(ch!='n');
}
void del()
{
int inum,i,j;
printf("輸入要刪除學員的編號:");
fflush(stdin);
scanf("%d",&inum);
for(i=0;i<now_no;i++)
{
if(stu[i].no==inum)
{
for(j=i;j<now_no;j++)
{
stu[i]=stu[i+1];
}
now_no=now_no-1;
}
break;
}
system("cls");
}
『陸』 求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("無此聯系人!");
}