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

c語言製作通訊錄

發布時間: 2022-09-08 07:46:54

1. c語言中做一個通訊錄,能夠添加、刪除、修改、查找

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

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

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

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

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

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

2. 用C語言編寫建通訊錄

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream.h>
using namespace std;

//struct Student definition
struct Student
{
char name[20];
char tel[20];
char address[100];
struct Student *next;
};

void outputList(struct Student *h)
{
struct Student *temp=h;
while(h!=NULL)
{
printf("%s\t\t%s\t\t%s\n",h->name,h->tel,h->address);
h=h->next;
}
}

struct Student * searchNode(char * num,struct Student *h)
{
struct Student *temp=h;
while(temp!=NULL)
{
if(!strcmp(temp->name,num))
{
return temp;
}
temp=temp->next;
}
return NULL;
}

struct Student * update(char *num,struct Student *h)
{
struct Student *p;
p=searchNode(num,h);
if(p==NULL)
{
printf("can't find the student you want to update\n");
exit(0);
}
else
{
printf("you can update all information except name\n");
printf("please input tel:");
scanf("%s",&p->tel);
printf("please input tel:");
scanf("%s",&p->address);
}
printf("update succeed\n");
return h;
}

//insert student at the begin of linked list
struct Student *insert(struct Student *h)
{
struct Student *temp=new struct Student;
struct Student *stu=new struct Student;
struct Student *p=new struct Student;
printf("please input name:");
scanf("%s",&stu->name);
printf("please input tel:");
scanf("%s",&stu->tel);
printf("please input address:");
scanf("%s",&stu->address);
p=searchNode(stu->name,h);
while(p!=NULL)
{
p=searchNode(stu->name,h);
}
if(p==NULL)
{
temp=stu;
temp->next=h;
return temp;
}
}

struct Student *del(struct Student *h,char *num)
{
struct Student *p1,*p2;
p1=h;
while(p1->next!=NULL && strcmp(p1->name,num)!=0)
{
p2=p1;
p1=p1->next;
}
if (strcmp(p1->name,num)==0)
{
if (p1==h)
{
h=p1->next;
}
else
{
p2->next=p1->next;
}
delete p1;
printf("student %s delete succeed\n",num);
}
else
{
printf("can't find the student you want to delete\n");
}
return h;
}

int main(int argc, char *argv[])
{
int choice=-1;
bool exitFlag=false;
char number[20];
struct Student *head=new struct Student;
head=NULL;
struct Student *p;
while(exitFlag!=true)
{
printf("\n*********please choose operation********\n");
printf("\t1.insert student\n");
printf("\t2.update student\n");
printf("\t3.find student\n");
printf("\t4.delete student\n");
printf("\t5.output student\n");
printf("\t6.exit\n");
printf("****************************************\n");
printf("Now,choose your operation:");
cin>>choice;
switch(choice)
{
case 1:
head=insert(head);
break;
case 2:
printf("please input a student's name you want to update:");
scanf("%s",&number);
p=searchNode(number,head);
if(p==NULL)
{
printf("can't find the student you want to update:");
}
if(p!=NULL)
{
head=update(number,head);
}
break;
case 3:
printf("please input a student's name you want to find:");
scanf("%s",&number);
p=searchNode(number,head);
if(p==NULL)
{
printf("can't find the student you want to find:");
}
if(p!=NULL)
{
printf("name\t\tTel\t\tAddress\n");
printf("%s\t\t%s\t\t%s\n",p->name,p->tel,p->address);
}
break;
case 4:
printf("please input a student's name you want to delete:");
scanf("%s",&number);
head=del(head,number);
break;
case 5:
if(head!=NULL)
{
printf("name\t\tTel\t\tAddress\n");
outputList(head);
}
else
{
printf("list null!\n");
}
break;
case 6:
exitFlag=true;
break;
system("pause");
default :
printf("error choice!please input a right number betwwen 1 and 6!\n");
break;
}
}
system("PAUSE");
return 0;
}

3. 用C語言設計一個通訊錄系統

/*
* main_tongxunlu.c
*
* Created on: 2011-6-21
* Author: zhanglujin
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct record
{
char name[20]; //姓名
char phone[12]; //電話
char adress[50]; //地址
char postcode[8]; //郵政編碼
char e_mail[20]; //電子郵件。
}student[100]; //假設最大數為100.
//定義全局變數num,表示已經輸入的人數 。
int num; //這里使用數組解決通訊錄的問題,實際上使用鏈表更好。
int menu_select()
{
char s[80];
int a;/*定義整形變數*/
system("cls");
printf("\t\t***********歡迎進入通訊管理界面********\n\n");
printf("\t\t\t0. 輸入記錄\n");
printf("\t\t\t1. 顯示記錄\n");
printf("\t\t\t2. 按姓名查找\n");
printf("\t\t\t3. 按電話號碼查找\n");
printf("\t\t\t4. 插入記錄 \n");
printf("\t\t\t5. 按姓名排序\n");
printf("\t\t\t6. 刪除記錄\n");
printf("\t\t\t7. Quit\n");
printf("\t\t***********************************************\n\n");
do{
printf("Enter you choice(0~7):");
scanf("%s",s);
a=atoi(s);
}
while (a<0 || a>7);
return a;
}
int adser()
{
printf("\t\t\t**************** 請輸入用戶信息 ****************\n");
printf("\t\t\t輸入姓名:\n");
scanf("%s",student[num].name);
printf("\t\t\t輸入電話號碼:\n");
scanf("%s",student[num].phone);
printf("\t\t\t輸入地址:\n");
scanf("%s",student[num].adress);
printf("\t\t\t輸入郵編:\n");
scanf("%s",student[num].postcode);
printf("\t\t\t輸入e-mail:\n");
scanf("%s",student[num].e_mail);
num++;
printf("\t\t\t是否繼續添加?(Y/N):\n");
if(getch()=='y' || getch()=='Y')
adser();
return(0);
}
void list()
{
int i;
system("cls");
if(num!=0)
{
printf("\t\t\t*************** 以下為通訊錄所有信息************\n");
for (i=0;i<num;i++)
{
printf("\t\t\t姓名:%s\n",student[i].name);
printf("\t\t\t電話:%s\n",student[i].phone);
printf("\t\t\t地址:%s\n",student[i].adress);
printf("\t\t\t郵編:%s\n",student[i].postcode);
printf("\t\t\te-mail:%s\n",student[i].e_mail);
if(i+1<num)
{
system("pause");
}
}
printf("\t\t\t************************************************\n");
}
else
printf("\t\t\t通訊錄中無任何紀錄\n");
printf("\t\t\t按任意鍵返回主菜單:\n");
getch(); //這里是無回顯的輸入字元,你輸入的字元不會顯示在屏幕上。
return;
}
int searchbyname()
{
int mark=0;
int i;
printf("\t\t\t***************** 按姓名查找 *******************\n");
char name[20];
printf("\t\t\t請輸入姓名:\n");
scanf("%s",name);
for(i=0;i<num;i++)
{
if (strcmp(student[i].name,name)==0)
{
printf("\t\t\t************* 以下是您查找的用戶信息 ***********\n");
printf("\t\t\t姓名: %s",student[i].name);
printf("\t\t\t電話: %s",student[i].phone);
printf("\t\t\t地址: %s",student[i].adress);
printf("\t\t\te-mail:%s",student[i].e_mail);
printf("\t\t\t************************************************\n");
mark++;
if((i+1)<num)
{
printf("\t\t\t是否繼續查找相同名字的用戶信息:(y/n)\n");
if(getch()=='y' || getch()=='Y')
{

4. 用C語言如何做通訊錄

因為不知道你要實現什麼功能,給個程序僅供參考 編譯環境:vc++6.0 #include<iostream> #include<windows.h> using namespace std; typedef struct { char name[25];//姓名 char number[50];//電話號碼 int a;//序號 char cls;//分類 char mail[50];//郵箱 }BOOK; typedef struct node { BOOK data; struct node *next; }Node,*Link; void Menu() { cout<<" |------------------------------------|"<<endl; cout<<" | 1.查看通訊錄; 2.查找聯系人; |"<<endl; cout<<" | 3.刪除聯系人; 4.添加聯系人; |"<<endl; cout<<" | 0.退出系統; |"<<endl; cout<<" |------------------------------------|"<<endl; } void Ordination(Link L)//通訊錄進行排序 { Node *p; int i=1; p=L->next; while(p) { p->data.a=i++; p=p->next; } } int Ordination1(Link L)//通訊錄進行排序 { Node *p; int i=1; p=L->next; while(p) { p->data.a=i++; p=p->next; } if(i==16) cout<<"\t\t\t通訊錄已滿!\n"; return(i-1); } Node *Locate(Link L,char nam[],char num[]) { Node *p; p=L->next; while(p) { if(strcmp(p->data.name,nam)==0||strcmp(p->data.number,num)==0) return p; p=p->next; } return p; } void Fincls(Link L)//按類查找 { Node *p; char str; int count=0; cout<<"請輸入分類(A,B,C):"; cin>>str; p=L->next; if(!p) {cout<<"\t\t\t通訊錄中沒有聯系人.\n";return;} while(p) { if(str==p->data.cls) {cout<<"姓名:"<<p->data.name<<";號碼:"<<p->data.number<<endl; count++;} p=p->next; } if(count==0) cout<<"\t\t\t沒有"<<str<<"類聯系人.\n"; } void Add(Link L)//添加聯系人 { Node *p,*r; char nam[25]; char num[50]; int flag=0; flag=Ordination1(L); if(flag==15) return; cout<<"姓名:"; scanf("%s",nam); cout<<"號碼:"; scanf("%s",num); p=Locate(L,nam,num); if(p!=NULL) {cout<<"\t\t\t該人信息已存在!\n"; return; } r=L; while(r->next!=NULL) r=r->next; p=(Node*)malloc(sizeof(Node)); p->next=NULL; strcpy(p->data.name,nam); strcpy(p->data.number,num); cout<<"分類(選A,B或C):"; p->data.cls=getchar(); p->data.cls=getchar(); cout<<"郵件:"; scanf("%s",p->data.mail); r->next=p; Ordination(L); } void Bohao(Node *p)//自動撥號功能 { int i,j; system("cls"); for(i=0;i<50;i++) { if(p->data.number[i]=='\0') break; } for(j=0;j<i;j++) { printf("\a%c",p->data.number[j]); Sleep(300); } cout<<endl; } void Book(Link L)//通訊錄 { Node *p; char str[25],ch; p=L->next; if(p==NULL) {cout<<"\t\t\t沒有聯系人.\n";return;} cout<<"通訊錄:\n"; while(p!=NULL) { cout<<p->data.a<<".";//顯示序號 cout<<p->data.name<<endl; p=p->next; } cout<<"請輸入姓名查看聯系方式(0退出通訊錄):"; scanf("%s",str); if(strcmp(str,"0")==0) return; p=L->next; while(p) { if(strcmp(p->data.name,str)==0) break; p=p->next; } cout<<"姓名:"<<p->data.name<<";號碼:"<<p->data.number<<endl; cout<<"是否撥號?(y/n):"; ch=getchar(); ch=getchar(); if(ch=='y'||ch=='Y') Bohao(p); } void Delet(Link L)//刪除聯系人 { Node *p,*r; char nam[25]; p=L->next; if(p==NULL) {cout<<"\t\t\t通訊錄為空,無聯系人可刪!\n";} cout<<"需刪除聯系人的姓名:"; scanf("%s",nam); while(p) { if(strcmp(p->data.name,nam)==0) break; p=p->next; } if(p==NULL) {cout<<"\t\t\t沒有此人!\n";return;} r=L; while(r->next) { if(r->next==p) break; r=r->next; } r->next=p->next; Ordination(L); } void main() { Link L; int menu=1; L=(Node*)malloc(sizeof(Node)); L->next=NULL; Menu(); while(menu) { cout<<"請選擇操作(5顯示菜單):"; cin>>menu; switch(menu) { case 1:Book(L);break; case 2:Fincls(L);break; case 3:Delet(L);break; case 4:Add(L);break; case 5:Menu();break; } } }

麻煩採納,謝謝!

5. 用c語言編寫通訊錄程序

這個屬於基本的結構體數組和文件讀寫操作。

6. 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");

}

}

7. 用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);
}

8. 用C語言寫一個通訊錄

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef struct { //通訊錄結點類型
char num[5]; //編號
char name[9]; //姓名
char sex[3]; //性別
char phone[13]; //電話
char addr[31]; //地址
} DataType;
typedef struct node { //結點類型定義
DataType data; //結點數據域
struct node *next; //結點指針域
} ListNode;
typedef ListNode *LinkList;
LinkList head;
ListNode *p;
//函數說明
int menu_select();
LinkList CreateList(void);
void InsertNode(LinkList head,ListNode *p);
ListNode *ListFind(LinkList head);
void DelNode(LinkList head);
void printList(LinkList head);
//主函數
void main()
{
for( ; ; ){
switch(menu_select( ) )
{
case 1:
printf("**********************************\n");
printf("* 通 訊 錄 鏈 表 的 建 立 *\n");
printf("**********************************\n");
head=CreateList( );
break;
case 2:
printf("**********************************\n");
printf("* 通 訊 者 信 息 的 添 加 *\n");
printf("**********************************\n");
printf("編號(4) 姓名(8) 性別(3) 電話(11) 地址(31)\n");
printf("************************************* \n");
p=(ListNode *)malloc(sizeof(ListNode)); //申請新結點
scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,
p->data.phone,p->data.addr);
InsertNode(head,p);
break;
case 3:
printf("***********************************\n");
printf("* 通 訊 錄 信 息 的 查 詢 *\n");
printf("***********************************\n");
p=ListFind(head);
if (p!=NULL) {
printf("編號 姓 名 性別 聯系電話 地址 \n");
printf("--------------------------------------------------\n");
printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,
p->data.sex,p->data.phone,p->data.addr);
printf("---------------------------------------------------\n");
}
else
printf("沒有查到要查詢的通訊者!\n");
break;
case 4:
printf("***********************************\n");
printf("* 通 訊 錄 信 息 的 刪 除 *\n");
printf("***********************************\n");
DelNode(head); //刪除結點
break;
case 5:
printf("************************************\n");
printf("* 通 訊 錄 鏈 表 的 輸 出 *\n");
printf("************************************\n");
printList(head);
break;
case 0:
printf("\t 再 見! \n");
return;
}
}
}
/*******************/
/* 菜單選擇函數程序 */
/***************************/
int menu_select( )
{
int sn;
printf(" 通訊錄管理系統 \n");
printf("===================\n");
printf(" 1.通訊鏈表的建立\n");
printf(" 2.通訊者結點的插入\n");
printf(" 3.通訊者結點的查詢\n");
printf(" 4.通訊者結點的刪除\n");
printf(" 5.通訊錄鏈表的輸出\n");
printf(" 0.退出管理系統\n");
printf("==========================\n");
printf(" 請 選 擇 0-5: ");
for( ; ; )
{
scanf("%d",&sn);
if (sn<0||sn>5)
printf("\n\t輸入錯誤,重選0-5:");
else
break;
}
return sn;
}
/**************************/
/*用尾插法建立通訊錄鏈表函數 */
/**************************/
LinkList CreateList(void)
{//尾插法建立帶頭結點的通訊錄鏈表演算法
LinkList head=(ListNode *)malloc(sizeof(ListNode)); //申請頭結點
ListNode *p,*rear;
int flag=0; //結束標志置0
rear=head; //尾指針初始指向頭結點
while (flag==0)
{ p=(ListNode *)malloc(sizeof(ListNode)); //申新結點
printf("編號(4) 姓名(8) 性別 電話(11) 地址(31)\n");
printf("--------------------------------------------------------------------------------------\n");
scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,p->data.phone,
p->data.addr);
rear->next=p; //新結點連接到尾結點之後
rear=p; //尾指針指向新結點
printf("結束建表嗎?(1/0):");
scanf("%d",&flag);
}
rear->next=NULL; //終端結點指針置空
return head; //返回鏈表頭指針
}
/******************************/
/*在通訊錄鏈表head中插入結點 */
/******************************/
void InsertNode(LinkList head,ListNode *p)
{
ListNode *p1,*p2;
p1=head;
p2=p1->next;
while(p2!=NULL && strcmp(p2->data.num,p->data.num)<0)
{
p1=p2; //p1指向剛訪問過的結點
p2=p2->next; //p2指向表的下一個結點
}
p1->next=p; //插入p所指向的結點
p->next=p2; //連接表中剩餘的結點
}
/******************************************/
/* 有序通訊錄鏈表的查找 */
/******************************************/
ListNode *ListFind(LinkList head)
{// 有序通訊錄鏈表上的查找
ListNode *p;
char num[5];
char name[9];
int xz;
printf("==================\n");
printf(" 1. 按編號查詢 \n");
printf(" 2. 按姓名查詢 \n");
printf("==================\n");
printf(" 請 選 擇: ");
p=head->next; //假定通訊 錄表帶頭結點
scanf("%d",&xz);
if (xz==1) {
printf("請輸入要查找者的編號:");
scanf("%s",num);
while (p&&strcmp(p->data.num,num)<0)
p=p->next;
if ((p==NULL)||strcmp(p->data.num,num))0;
p=NULL; //沒有查到要查找的通訊者
}
else
if (xz==2) {
printf(" 請輸入要查找者的姓名:");
scanf("%s",name);
while(p&&strcmp(p->data.name,name)!=0)
p=p->next;
}
return p;
}
/*******************************/
/* 通訊錄鏈表上的結點刪除 */
/*********************************/
void DelNode(LinkList head)
{
char jx;
ListNode *p,*q;
p=ListFind(head); //調用查找函數
if (p==NULL) {
printf("沒有查到要刪除的通訊者!\n");
return;
}
printf("真的要刪除該結點嗎?(y/n):");
scanf("%c",&jx);
if (jx=='y'||jx=='Y') {
q=head;
while ((q!=NULL) &&(q->next!=p))
q=q->next;
q->next=p->next; //刪除結點
free(p); //釋放被刪結點空間
printf("通訊者已被刪除!\n");
}
}
/**********************************/
/* 通訊錄鏈表的輸出函數 */
/**********************************/
void printList(LinkList head)
{
ListNode *p;
p=head->next;
printf("編號 姓 名 性別 聯系電話 地址 \n");
printf("--------------------------------------------------------------------------------\n");
while (p!=NULL)
{ printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,
p->data.phone,p->data.addr);
printf("---------------------------------------------------------------------------------\n");
p=p->next; //後移一個結點
}
}

9. 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);

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

10. 如何用C語言編寫通訊錄…

;
#include <string>
#include "address.h"
#include "addressList.h"
#include "addressBook.h"
using namespace std;
int main()
{
new AddressBook; //開啟通信錄
return 0;
}
ostream& operator<<(ostream& os, const RecordList& c_rl)
{
RecordList::const_iterator it;
RecordList::const_iterator it_end =c_rl.end();
for (it=c_rl.begin();it!=it_end; it++)
{
os << (*it)->name << '\n' << (*it)->tel << '\n';
}
return os;
}
istream& operator>> (istream& is, RecordList& rl)
{
Record* rec;
string name;
while (true)
{

//注意這里使用的是全局的getline()函數,而不是istream的成員函
//數.全局的getling()函數將從istream中取出最後一個分隔符,
//而istream的成員函數getline則不會
getline(is,name);
if (name =="")
break;
rec = new Record;
rec->name=name;
getline(is,rec->tel );
rl.push_back(rec);
}
return is;
}

#include<stdlib.h>
#include<fstream>
#include "addressBook.h"

using namespace std;

AddressBook::AddressBook()
{
isModified = false;
start();
}

AddressBook::~AddressBook()
{
}

//功能:顯示歡迎畫面,並開始進行用戶交互處理
void AddressBook::start()
{
cout<<"\n"
<<"\t******************************\n"
<<"\t* 這是一個 *\n"
<<"\t* 簡單的通信錄程序 *\n"
<<"\t* 可以對通信錄進行簡單管理 *\n"
<<"\t* 歡迎使用通信錄 *\n"
<<"\t******************************\n"
<<"\n";
handleMenu();
}

//功能:顯示通信錄菜單,返回用戶選擇的選項
//返回:用戶選擇的選項,是1-9之間的一個字元

char AddressBook::menuSelect()
{
cout<<"\n"
<<"1.顯示記錄\n"
<<"2.查詢記錄\n"
<<"3.增加記錄\n"
<<"4.刪除記錄\n"
<<"5.保存記錄\n"
<<"6.讀取記錄\n"
<<"7.新建記錄\n"
<<"8.結束運行\n"
<<"\n左邊數字對應功能選擇,請選1-8:";
char choice[2];
while(true)
{
cin.getline(choice,2);
if(choice[0]<'1' || choice[0]>'8')
cout<<"\n輸入錯誤,重選1-8:";
else
break;
}
return choice[0];
}

//功能:根據用戶選擇的項目調用相應的處理函數
void AddressBook::handleMenu()
{
while(true)
{
switch(menuSelect())
{
case '1':
displayRecords();
break;
case '2':
queryRecord();
break;
.......

//功能:列印用於顯示記錄信息時的表頭
void AddressBook::dispTable()
{

}

//功能:顯示通信錄中的所有記錄
void AddressBook::displayRecords()
{

}

//功能:首先檢查通信錄是否已保存,然後清空當前通信錄中所有記錄
//注意:該函數覆蓋了基類中的函數
void AddressBook::clear()
{

}
//功能:查詢指定記錄
void AddressBook::queryRecord()
{

//功能:向當前通信錄追加新的記錄
//注意:該函數重載了基類中的函數
void AddressBook::addRecord()
{

}

/*

//說明:如果使用string 類,效果更好。下面是實現本功能的程序代碼:
while(true)
{
//輸入新記錄
string name;
cout<<"姓名:";
getline(cin,name);
//如果輸入的姓名為「0」,則結束添加記錄的操作
if(name=="0")
break;
Record* rec=new Record;
rec->name=name;
cout<<"電話:";
getline(cin,rec->tel);
//將新記錄加入鏈表
AddressList::addRecord(rec);
}

//同理,下面的成員函數removeRecord()中的判斷,可以使用如下替代方法:
string str;
getline(cin,str);
while(true)
{
if(str[0]!='1' && str[0]!='2')
{
cout<<"輸入錯誤,重選1-2:";
getline(cin,str);
}
else
break;
}

*/

//功能:從當前通信錄移除指定的記錄
//注意:該函數重載了基類中的函數
void AddressBook::removeRecord()
{
if(empty())
{
cout<<"通信錄已空,退出刪除操作!"<<endl;
return;
}
//選擇獲取需要刪除的記錄的方式
cout<<"可以通過姓名或電話刪除記錄\n"
<<"1.通過姓名\n"
<<"2.通過電話\n";
char choice[2];
cin.getline(choice,2);
while(true)
{
if(choice[0]!='1' && choice[0]!='2')
{
cout<<"輸入錯誤,重選1-2:";
cin.getline(choice,2);
}
else
break;
}
int type=static_cast<int>(choice[0])-48;
//輸入需要匹配的字元串
if(type==1)
cout<<"請輸入姓名:"<<flush;
else
cout<<"請輸入電話:"<<flush;
char pattern[20];
cin.getline(pattern,20);
int deletedCount=0;
//找到匹配的記錄
Iterator it=findRecord(pattern,type,first());
Iterator it_end=pastEnd();
while(it!=it_end)
{
cout<<"找到一條匹配的記錄:\n";
dispTable();
cout<<(*it)->name<<"\t\t"<<(*it)->tel<<"\n";
cout<<"確定要刪除這條記錄嗎?[Y/N]";
cin.getline(choice,2);
//刪除這條記錄
if(choice[0]!='n' && choice[0]!='N')
{
it=AddressList::removeRecord(it);
deletedCount++;
}
//取下一條匹配的記錄
if(it!=it_end)
it=findRecord(pattern,type,++it);
}
cout<<"一共刪除了"<<deletedCount<<"條記錄\n"
<<"現在還有"<<size()<<"條記錄"<<endl;
if(deletedCount!=0)
isModified=false;
}

//功能:將通信錄保存至指定文件
//注意:該函數重載了基類中的函數
void AddressBook::saveRecords()
{
if(empty())
{
cout<<"沒有記錄可存!"<<endl;
return;
}
//因為下面的程序中使用string類的成員函數,所以需要進行一次轉換
string fname;
char name[16];
cout<<"請輸入要存在的文件名(直接回車選擇文件sname):";
cin.getline(name,16);
fname=name;
if(fname.empty())
fname="sname";
ofstream of(fname.c_str(),ios_base::out);
if(!of)
{
cout<<"不能存入文件!"<<endl;
return;
}
AddressList::saveRecords(of);
of.close();
cout<<size()<<"條記錄已經存入文件,請繼續操作。"<<endl;
isModified=false;
}
//功能:從指定文件讀取記錄追加到當前通信錄末尾
//注意:該函數重載了基類中的函數
void AddressBook::loadRecords()
{

}
void AddressBook::quit()
{

}

#ifndef H_ADDRESSBOOK_H_NNN
#define H_ADDRESSBOOK_H_NNN
#include "addressList.h"

//處理通信錄操作的面向用戶使用的類,包含所有功能的用戶界面及用戶交互的實現
class AddressBook :public AddressList
{
protected:
bool isModified; //通信錄是否已被修改還未保存
public:
AddressBook();
virtual ~AddressBook();

//開始通信錄的用戶界面操作
virtual void start();
//菜單處理函數
virtual void handleMenu();
//顯示記錄
virtual void displayRecords();
//查詢記錄
virtual void queryRecord();
//增加記錄
virtual void addRecord();
//刪除記錄
virtual void removeRecord();
//保存記錄
virtual void saveRecords();
//讀取記錄
virtual void loadRecords();
//結束程序
virtual void quit();
//清空當前通信錄
virtual void clear();
protected:
//菜單選擇函數
char menuSelect();
//顯示表頭
void dispTable();
};
#endif //H_ADDRESSBOOK_HZ-NNN

#include "addressList.h"
AddressList::~AddressList()
{
recList.clear();
}
//功能:向通信錄中添加新的記錄
//參數:rec,指向新記錄的指針
void AddressList::addRecord (Record* rec)
{
if (rec !=NULL)
{
recList.push_back(rec);
}
}
//功能:從通訊錄中刪除一個記錄
//參數:it,指向欲刪除記錄的迭代器
//注意:it,應是可提領的迭代器,可以通過findRecord()獲得
AddressList::Iterator AddressList::removeRecord(AddressList::Iterator it)
{
return recList.erase(it);
}
//功能:從通信錄中尋找一個記錄
//參數:pattern,欲尋找的記錄的指定域與pattern相等
// type,為1表示欲尋找記錄的名稱(name)與pattern相等;

// 為2表示欲尋找記錄的電弧(tel)與(pattern)相等。
// from,從from開始尋找匹配的記錄。
//返回:若找到了則返回的迭代器指向第一個匹配記錄的迭代器,
// 若找不到則返回的迭代器等於pastEnd()的返回值。
//注意:from應是可提領的迭代器。
// 第一次調用可以用first(),之後使用上一次findRecord()的返回值增1,
// 直到返回pastEnd(),便可以獲得所有匹配的記錄。

AddressList::Iterator AddressList::findRecord(string pattern,int type,AddressList::Iterator from)
{
Iterator it;
Iterator it_end=recList.end();
for (it=from; it!=it_end; it++)
{
if ((type==1 && (*it)->name==pattern)||(type==2 && (*it)->tel==pattern))
break;
}
return it;
}

//功能:將通信錄保存到輸出流中
//參數:os.指定的輸出流
void AddressList::saveRecords(ostream &os)
{
os << recList;
}
//功能:從輸入流中讀入數據並追加到當前通信錄的末尾
//參數:is,指定的輸入流
//返回:讀入的記錄數
int AddressList::loadRecords(istream &is)
{
int ori =size();
is >> recList;
return size()-ori;
}

#ifndef H_ADDRESSLIST_H_AAA
#define H_ADDRESSLIST_H_AAA

#include <iostream>
#include <string>
#include "address.h"

using namespace std;

//處理通信錄操作的底層類,包括增加記錄、刪
//除記錄、查詢記錄以及保存和讀取記錄的函數,
//該類不涉及任何關於用戶界面的操作
class AddressList
{
protected:
RecordList recList;// 使用對象成員作為數據成員
public:
typedef RecordList::iterator Iterator;
//向通信錄中添加新的記錄
virtual void addRecord(Record* rec);
//從通信錄中刪除一個記錄
virtual Iterator removeRecord(Iterator it);
//從通訊錄中尋找一個記錄
virtual Iterator findRecord(string pattern,int type,Iterator from);
//將通信錄保存到輸出流中
virtual void saveRecords(ostream& os);
//從輸入流中讀入數據並追加到當前通信錄的末尾
virtual int loadRecords(istream& is);
virtual ~AddressList();
//獲得通信錄中存儲的記錄數
const int size() const
//通信錄是否為空
const bool empty() const
//清除通信錄
virtual void clear()
//獲得通信錄的第一條記錄
Iterator first()
//獲得通信錄超過最後一個記錄之後的迭代器
Iterator pastEnd()
};
#endif //H_ADDRESSLIST_H_AAA