当前位置:首页 » 编程语言 » 电话簿管理系统c语言

电话簿管理系统c语言

发布时间: 2024-07-06 09:13:05

1. 用c语言或c++做一个通讯录管理系统

#include<iostream.h>
#include<iomanip.h>
#include<string.h>
class pbook
{
int cellphone,phone;
char name[20],first,sex[10],workplace[30];
char date[15];
public:
void addin();
char* gname(){return name;}
char getf(){ return first;}
void print()
{
cout<<setw(12)<<name<<setw(12)<<sex;
cout<<setw(12)<<workplace<<setw(12)<<cellphone<<setw(12)<<phone<<setw(12)<<date<<endl;}
static int dele(pbook *book,int n);
static void edit(pbook *book,int n,char *name);
static void findn(pbook *book,int n,char *name);
static void findf(pbook *book,int n,char f);
static void rang(pbook *book,int n);
};
void pbook::addin()
{
cout<<"姓名:";
cin>>name;
cout<<endl<<"性别(男|女): ";
cin>>sex;
cout<<endl<<"工作单位:";
cin>>workplace;
cout<<endl<<"手机号码:";
cin>>cellphone;
cout<<endl<<"固定电话:";
cin>>phone;
cout<<endl<<"储存日期:";
cin>>date;
cout<<endl<<"姓名首字母(大写):";
cin>>first;
cout<<endl<<"电话簿添加成功!"<<endl;
}
int pbook::dele(pbook *book,int n){
int p=-2,c=n;
char name[20];cout<<"请输入姓名"<<endl;
cin>>name;
for(int i=0;i<n;i++)
{
if(strcmp(book[i].gname(),name)==0)
{
p=i;break;
}
}
if(p!=-2)
{
for(i=p;i<n;i++)
book[i]=book[i+1];
return --c;
}
else
return c;
}
void pbook::edit(pbook *book,int n,char *name)
{ int p;
for(int i=0;i<n;i++)
{if(strcmp(book[i].gname(),name)==0)
p=i;break;
}
book[p].addin();
cout<<"修改成功"<<endl;
}
void pbook::findn(pbook *book,int n,char *sname)
{
int i;
for(i=0;i<n;i++)
if(strcmp(book[i].gname(),sname)==0)
{
book[i].print();break;
}
if(i==n)
{
cout<<"查无此人"<<endl;
}
}
void pbook::findf(pbook *book,int n,char f)
{
int p=-1;
cout<<setw(15)<<"姓名"<<setw(15)<<"性别"<<setw(15)<<"工作单位"<<setw(15)<<"手机号码";
cout<<setw(15)<<"固定电话"<<setw(15)<<"存储日期"<<endl;
for(int i=0;i<n;i++)
{
if(book[i].getf()==f)
{
p=i;
book[p].print();
}
}
if(p==-1)cout<<"查无此人"<<endl;
}
void pbook::rang(pbook *book,int n)
{
pbook m;int i,j;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
if(book[i].getf()>book[i+1].getf())
{
m=book[j];
book[j]=book[j+1];
book[j+1]=m;
}
}
cout<<setw(12)<<"姓名"<<setw(12)<<"性别"<<setw(12)<<"工作单位"<<setw(12)<<"手机号码";
cout<<setw(12)<<"固定电话"<<setw(12)<<"存储日期"<<endl;
for(i=0;i<n;i++)
book[i].print();
}
void display()
{

cout <<endl;
cout<<"********************电话簿********************"<<endl;
cout<<" ------- 1.添加联系人 ------- "<<endl;
cout<<" ------- 2.删除联系人 ------- "<<endl;
cout<<" ------- 3.显示所有联系人 ------- "<<endl;
cout<<" ------- 4.修改联系人信息 ------- "<<endl;
cout<<" ------- 5.按姓名查询 ------- "<<endl;
cout<<" ------- 6.按首字母查询 ------- "<<endl;
cout<<" ------- 7.退出系统请按 ------- "<<endl;
cout<<"**********************************************"<<endl;
}
void main()
{
pbook book[10];int n=0;char name[20];
int i;char j=1;char g;char a;
while(j!=0)
{
display();
cout<<"请选择菜单号(1---7):";
cin>>i;
while(!(i==1||i==2||i==3||i==4||i==5||i==6||i==7))
{
cout<<"\a\a选择错误! 请重新选择! 请选择菜单号(0---7):";
cin>>i;
}
while(i==1||i==2||i==3||i==4||i==5||i==6||i==7)
{
if(i==1)
{ n++;book[n-1].addin();}
else if(i==7)
{
cout<<"谢谢使用本系统!"<<endl;
j=0;
}
else
{
if(i==2)
n=pbook::dele(book,n);
else if(i==3)
pbook::rang(book,n);
else if(i==4)
{ cout<<"请输入姓名"<<endl;
cin>>name;
pbook::edit(book,n,name);
}
else if(i==5)
{
cout<<"请输入姓名"<<endl;
cin>>name;
cout<<setw(12)<<"姓名"<<setw(12)<<"性别"<<setw(12)<<"工作单位"<<setw(12)<<"手机号码";
cout<<setw(12)<<"固定电话"<<setw(12)<<"存储日期"<<endl;
pbook::findn(book,n,name);
}
else
{
cout<<"请输入字符:";
cin>>g;
pbook::findf(book,n,g);
}
}
cout<<"继续请按y,否则请按n."<<endl;
cin>>a;
if(a=='y'){};
if(a=='n')break;
}
cout<<"继续菜单请按y,否则请按n."<<endl;
cin>>j;
}
}

2. 用C语言或C++做个通讯录管理系统

//简单的学生通讯录管理系统
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
structrecord
{
charname[20];//姓名
charphone[12];//电话
charadress[50];//地址
charpostcode[8];//邮政编码
chare_mail[20];//电子邮件。
}student[100];//假设最大数为100.
//定义全局变量num,表示已经输入的人数。
intnum;//这里使用数组解决通讯录的问题,实际上使用链表更好。
intmenu_select()
{
chars[80];
inta;/*定义整形变量*/
system("cls");
printf(" ***********欢迎进入通讯管理界面******** ");
printf(" 0.输入记录 ");
printf(" 1.显示记录 ");
printf(" 2.按姓名查找 ");
printf(" 3.按电话号码查找 ");
printf(" 4.插入记录 ");
printf(" 5.按姓名排序 ");
printf(" 6.删除记录 ");
printf(" 7.Quit ");
printf(" *********************************************** ");
do{
printf("Enteryouchoice(0~7):");
scanf("%s",s);
a=atoi(s);
}
while(a<0||a>7);
returna;
}
intadser()
{
printf(" ****************请输入用户信息**************** ");
printf(" 输入姓名: ");
scanf("%s",student[num].name);
printf(" 输入电话号码: ");
scanf("%s",student[num].phone);
printf(" 输入地址: ");
scanf("%s",student[num].adress);
printf(" 输入邮编: ");
scanf("%s",student[num].postcode);
printf(" 输入e-mail: ");
scanf("%s",student[num].e_mail);
num++;
printf(" 是否继续添加?(Y/N): ");
if(getch()=='y'||getch()=='Y')
adser();
return(0);
}
voidlist()
{
inti;
system("cls");
if(num!=0)
{
printf(" ***************以下为通讯录所有信息************ ");
for(i=0;i<num;i++)
{
printf(" 姓名:%s ",student[i].name);
printf(" 电话:%s ",student[i].phone);
printf(" 地址:%s ",student[i].adress);
printf(" 邮编:%s ",student[i].postcode);
printf(" e-mail:%s ",student[i].e_mail);
if(i+1<num)
{
system("pause");
}
}
printf(" ************************************************ ");
}
else
printf(" 通讯录中无任何纪录 ");
printf(" 按任意键返回主菜单: ");
getch();//这里是无回显的输入字符,你输入的字符不会显示在屏幕上。
return;
}
intsearchbyname()
{
intmark=0;
inti;
printf(" *****************按姓名查找******************* ");
charname[20];
printf(" 请输入姓名: ");
scanf("%s",name);
for(i=0;i<num;i++)
{
if(strcmp(student[i].name,name)==0)
{
printf(" *************以下是您查找的用户信息*********** ");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ************************************************ ");
mark++;
if((i+1)<num)
{
printf(" 是否继续查找相同名字的用户信息:(y/n) ");
if(getch()=='y'||getch()=='Y')
{
continue;
}
else
return(0);
}
else
{
printf(" 按任意键返回主菜单");
getch();
return(0);
}
}
}
if(mark==0)
{
printf(" 没有相同姓名的用户纪录 ");
printf(" 按任意键返回主菜单 ");
getch();
return(0);
}
return0;
}
intsearchbyphone()
{
intmark=0;
inti;
printf(" ******************按电话查找****************** ");
charphone[10];
printf(" 请输入电话号码: ");
scanf("%s",phone);
for(i=0;i<num;i++)
{
if(strcmp(student[i].phone,phone)==0)
{
printf(" **************以下是您查找的用户信息********** ");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ************************************************ ");
printf(" 按任意键返回主菜单 ");
mark++;
getch();
return(0);
}
}
if(mark==0)
{
printf(" 没有改用户的信息 ");
printf(" 按任意键返回主菜单 ");
getch();
return(0);
}
return(0);
}
voiddeletebyphone()
{
inti,j;
intdeletemark=0;
charphone[20];
printf(" 请输入要删除用户电话号码: ");
scanf("%s",phone);
if(num==0)
{
printf(" 对不起,文件中无任何纪录 ");
printf(" 按任意键返回主菜单 ");
getch();
return;
}
for(i=0;i<num;i++)
{
if(strcmp(student[i].phone,phone)==0)
{
printf(" 以下是您要删除的用户纪录: ");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" 是否删除?(y/n)");
if(getch()=='y'||getch()=='Y')
{
for(j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf(" 删除成功");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
else
return;
}
continue;
}
if(deletemark==0)
{
printf(" 没有该用户的纪录");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
return;
}
voiddeletebyname()
{
inta=0;
intfindmark=0;
intj;
intdeletemark=0;
inti;
charname[20];
printf(" 请输入要删除用户姓名: ");
scanf("%s",name);
for(i=a;i<num;i++)
{
if(strcmp(student[i].name,name)==0)
{
printf(" 以下是您要删除的用户纪录:");
findmark++;
printf(" ________________________________");
printf(" 姓名:%s",student[i].name);
printf(" 电话:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ________________________________");
printf(" 是否删除?(y/n)");
if(getch()=='y'||getch()=='Y')
{
for(j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf(" 删除成功");
if((i+1)<num)
{
printf(" 是否继续删除相同姓名的用户信息?(y/n)");
if(getch()=='y')
{
a=i;
continue;
}
}
printf(" 是否继续删除?(y/n)");
if(getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf(" 是否继续删除相同姓名的用户信息?(y/n)");
if(getch()=='y'||getch()=='Y')
{
a=i;
continue;
}
}
}
else
continue;
}
if((deletemark==0)&&(findmark==0))
{
printf(" 没有该用户的纪录");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
elseif(findmark!=0)
{
printf(" 没有重名信息");
printf(" 没有该用户的纪录");
printf(" 是否继续删除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
}
intdele()
{
charchoic;
printf(" 1-按电话号码删除2-按姓名删除");
printf(" 请选择:");
choic=getch();
switch(choic)
{
case'1':deletebyphone();break;
case'2':deletebyname();break;
}
return(0);
}
intsortbyname()//按姓名进行排序
{
inti,j;
structrecordtmp;
for(i=1;i<num;i++)
{
if(strcmp(student[i].name,student[i-1].name)<0)
{
tmp=student[i];
j=i-1;
do
{
student[j+1]=student[j];
j--;
}while((strcmp(tmp.name,student[j].name)<0&&j>=0));
student[j+1]=tmp;
}
}
printf(" 排序成功,是否显示?(y/n)");
if(getch()=='y')
list();
return(0);
}
intmain()
{
printf(" ************************************************ ");
printf(" ********welcometoTONGXUNLU******************* ");
printf(" ###########codebyXXXXX################### ");
printf(" ************************************************* ");
printf("按任意键进入主菜单 ");
getch();
intselectnum;
while(1)
{
selectnum=menu_select();
switch(selectnum)
{
case0:
{
adser();
break;
}
case1:
{
list();
break;
}
case2:
{
searchbyname();
break;
}
case3:
{
searchbyphone();
break;
}
case4:
{
adser();//这里插入,应该能指定位置,不过意义不大,所以和添加记录一样了。
break;
}
case5:
{
sortbyname();
break;
}
case6:
{
dele();
break;
}
case7:
{
printf("BYEBYE! ");
system("pause");
getchar();
exit(0);
}
}
}
getchar();
return0;
}

3. 求用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>

4. C语言程序设计,通讯录管理系统

/*【用C语言编写具有数据插入、修改、删除、显示和查询功能的电话薄管理程序】
1、数据包括:人名、工作单位、电话号码和E-mail地址。
2、可对记录中的姓名和电话号码进行修改。
3、可增加或删除记录。
4、可显示所有保存的记录。
5、可按人名或电话号码进行查询。

另:采用结构体数组和文件系统实现。结构体包括人名、工作单位、电话号码和E-mail
绝对原创,2010年7月12日晚刚写完测试通过的,纯C语言! */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define szSTR 64
#define fileDB "c:\\contact.txt"

struct CONTACT {
char name[szSTR];/*姓名*/
char unit[szSTR];/*工作单位*/
char tele[szSTR];/*电话*/
char mail[szSTR];/*mail*/
};

typedef struct CONTACT CON;

#define BNAME 1
#define BUNIT 2
#define BTELE 4
#define BMAIL 8
#define BFULL 0xFF
int OPER[5] = {BFULL, BNAME, BUNIT, BTELE, BMAIL};

void input(CON * s, int mask)
{
if(mask & BNAME) { printf("请输入学生姓名:"); scanf("%s", s->name); }
if(mask & BUNIT) { printf("请输入工作单位:"); scanf("%s", s->unit); }
if(mask & BTELE) { printf("请输入联系电话:"); scanf("%s", s->tele); }
if(mask & BMAIL) { printf("请输入电子邮件:"); scanf("%s", s->mail); }
}/*end input*/

void show (int id, CON * s)
{
if(!s) return ;
if(id)
printf("%d\t%s\t%s\t%s\t%s\n",
id, s->name , s->unit , s->tele , s->mail );
else
printf("【姓名】%s【工作单位】%s【电话】%s【电邮】%s\n",
s->name , s->unit , s->tele , s->mail );
/*end if*/
}/*end show*/

void regist(void)
{
CON s; FILE * f = 0;
input(&s, BFULL);
f = fopen(fileDB, "ab");
fwrite(&s,sizeof(s),1,f);
fclose(f);
printf("资料已保存!\n");
}/*end regist*/

int browse(char * key, fpos_t * p, CON * s)
{
FILE * f =0; int i = 0, r = 0; CON t;
char * a = 0, * b = 0; fpos_t q;
if(!s) return 0;
f = fopen(fileDB, "rb");
if(!f) {
printf("数据库暂无记录\n");
return 0;
}/*end if*/
printf("编号\t姓名\t单位\t电话\t电邮\n");
printf("------------------------------------------------\n");
while(!feof(f)) {
fgetpos(f, &q);
r = fread(&t, sizeof(t), 1, f);
if(r<1)break;
if(t.name[0]==0) continue;
if(key) {
a = strstr(t.name , key);
b = strstr(t.tele , key);
if(a == 0 && b == 0) continue;
}/*end if*/
if(p) *p = q;
if(s) memcpy(s, &t, sizeof(t));
i++;
show(i, &t);
}/*end while*/
fclose(f);
printf("------------------------------------------------\n");
return i;
}/*end browse*/

fpos_t query(CON * s)
{
char key[szSTR]=""; fpos_t p = 0; int r = 0;
printf("请输入姓名或者电话号码:");
scanf("%s", key);
r = browse(key, &p, s);
return r?p:-1;
}/*end query*/

void clear(void)
{
FILE * f = 0; CON s;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
memset(&s, 0, sizeof(s));
fwrite(&s, sizeof(s),1, f);
fclose(f);
printf("记录已删除!\n");
}/*end clear*/

void modify(void)
{
FILE * f = 0; CON s; int cmd = 0;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
for(;;) {
show(0, &s);
printf("请选择:1-姓名;2-工作单位;3-电话;4-电邮;5-保存并退出;6-放弃修改并退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 1:
case 2:
case 3:
case 4:
input(&s, OPER[cmd]);
break;
case 5:
fsetpos(f, &p);
fwrite(&s, sizeof(s), 1, f);
case 6:
fclose(f);
return;
default:
printf("错误的操作,请重新输入。\n");
}/*end case*/
}/*mext*/
}/*end clear*/

int main(void)
{
int cmd = 0; CON s;
for(;;) {
printf("欢迎使用通信簿!请选择:1-添加;2-查询;3-删除;4-修改;5-全部输出;0-退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 0: return 0;
case 1: regist(); break;
case 2: query(&s); break;
case 3: clear(); break;
case 4: modify(); break;
case 5: browse(0, 0, &s); break;
}//end case
}
return 0;
}

5. 求用C语言编写一个 通讯录管理系统 。设计要求如下

这个主要用到链表来做就可以的啦,链表的基本操作(创建、插入、删除等)要掌握好。之前我自己写有一个,和你的功能差不多,但是变量有所不同,给你参考参考吧,可以仿照。不懂的再问吧。代码如下:

#include"stdafx.h"
using namespace std;
//结构体
struct contaxt
{
char name[10];
char sex[5];
char Adrre[50];
char Class[20];
char Mobi[20];
char qq[10];
struct contaxt *next;
};
//函数声明
typedef struct contaxt CONT;
CONT *InsertCon(CONT *head);
CONT *AddContaxt(int n);
CONT *FindCon(CONT *head);
CONT *AmendMessage(CONT *head);
CONT *DeleteMessage(CONT *head);
void showmessage(CONT *head);
int menu(CONT *head);
CONT *saveFileCONT(CONT *head );
CONT *readFileCONT(CONT *head);
int choose;
//入口
void main()
{
//system("color F4");
CONT *head;
head = NULL;

menu(head);

}

//系统主菜单
int menu(CONT *head)
{
cout << "请选择操作菜单!!!" << endl ;
do
{
cout << "---------------------------欢迎进入通讯录管理系统-------------------------------"
<< "---------------------------------操作菜单---------------------------------------";
cout << "--------------------------------------------------------------------------------"
<< " 1 添加联系人信息 "
<< " 2 插入联系人信息 " << endl
<< " 3 查找联系人信息 "
<< " 4 修改联系人信息 " << endl
<< " 5 删除联系人信息 "
<< " 6 显示联系人信息 " << endl
<< " 7 退出本系统 " << endl
<< " 8 保存文件 " << endl
<< " 9 读取文件 " << endl
<< "--------------------------------------------------------------------------------" << endl;
cout << "请选择操作菜单!!!" << endl ;
int choose;
//choose = getchar();
fflush(stdin);
//return choose;
cin >> choose;
if(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7&&choose != 8)
{
cout << "输入错误:"<<endl;
}
else
{

switch(choose)
{
case 1:
int n;
do
{
fflush(stdin);
cout << "请确定要添加几个联系人:";
cin >> n;
}while(n <= 0);
head = AddContaxt(n);
break;
case 2:
system("CLS");
head = InsertCon(head);
break;
case 3:
head = FindCon(head);
break;
case 6:
system("CLS");
showmessage(head);
break;
case 7:
exit(0);
break;
case 4:
system("CLS");
AmendMessage(head);
break;
case 5:
system("CLS");
DeleteMessage(head);
break;
case 8:
system("CLS");
head = saveFileCONT(head );
break;
case 9:
// head = readFileCONT(head);

default:
system("CLS");
cout << "你输入错误,请重新选择!!!" << endl;
break;
}
}
}while(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7);

return choose;

}
//添加联系人
CONT *AddContaxt(int n)
{
CONT *p,*q,*head;
int i;
system("CLS");
p = (CONT *)malloc(sizeof(CONT));
cout << "开始添加联系人信息:"<< endl;
cout << "输入姓名:";
cin >> p->name;
cout << "输入姓别:";
cin >> p->sex ;
cout << "输入班级:";
cin >> p->Class ;
cout << "输入住址:";
cin >> p->Adrre ;
cout << "输入手机号:";
cin >> p->Mobi ;
cout << "输入qq号:";
cin >> p->qq ;
head = p;
for(i = 2;i <= n;i++)
{
q = (CONT *)malloc(sizeof(CONT));
p->next = q ;
p = q;
system("CLS");
cout<< "总共需要输入"<< n << "个联系人的信息,"<<"现在你已输入" << i-1<< "个联系人信息."<< endl<< "请继续输入第"<<i << "个人的信息!!"<< endl;
cout << "输入姓名:";
cin >> p->name;
cout << "输入姓别:";
cin >> p->sex ;
cout << "输入班级:";
cin >> p->Class ;
cout << "输入住址:";
cin >> p->Adrre ;
cout << "输入手机号:";
cin >> p->Mobi ;
cout << "输入qq号:";
cin >> p->qq ;
}
p->next = NULL;
return head;
}
//插入联系人信息
CONT *InsertCon(CONT *head)
{
CONT *pc,*p,*q;
int choose ;
char name[10];
if(head ==NULL)
{
cout << "还没有添加任何联系人,请先添加联系人!!"<< endl;
return head;
}
pc = (CONT *)malloc(sizeof(CONT));
cout << "请依次输入被插入人的信息:姓名 /性别/班级/住址/手机号/qq号"<< endl;
cin >> pc->name >> pc->sex >> pc->Class >> pc->Adrre >> pc->Mobi >> pc->qq ;
pc ->next = NULL;
cout << "请选择自己喜欢的插入方式: "<< endl<< "1 插入在自己喜欢的人之前"<< endl << "2 插入在最后" << endl << "3 插入在开头" << endl;
do {
cin >> choose;
switch(choose)
{
case 1:
cout << "请输入自己喜欢的人的名字:";
cin >> name ;

if(strcmp(head->name ,name)==0)
{
pc->next = head;
head = pc;
return head;
}
else
{
q = head;
p = head->next ;

while(strcmp(p->name,name)!=0 && p->next !=NULL)
{
q = p;
p = p->next ;
}

if(strcmp(p->name ,name)==0)
{
pc->next = p ;
q = pc;
return head;
}
else
{
p->next = pc;
return head;
}
}
break;
case 2:
//cout << "请输入自己喜欢的人的名字:";
//cin >> name ;
p = head;
while(p->next != NULL)
p = p->next ;
p->next = pc;
return head;
break;
case 3:
//cout << "请输入自己喜欢的人的名字:";
//cin >> name ;
pc->next = head;
head = pc;
return head;
break;
default :
cout<<"error"<< endl;

break;
}
}while(choose!=1 &&choose != 2 && choose != 3);
}
//查找联系人信息
CONT *FindCon(CONT *head)
{
system("CLS");
char na[20];
CONT *p,*q;
if(head == NULL)
{
cout << "没有任何信息可查找,请先添加信息!!"<< endl;
return head;
}

cout << "请您输入要查找人的姓名:"<< endl;
cin >> na;
p = head;
while(p!= NULL)
{
if(strcmp(p->name ,na)==0)
{
q = p;
cout << "找到联系人,信息如下!!"<< endl;
cout << "姓名:" << q->name <<" 性别: " << q->sex << q->Class <<" 住址: " <<q->Adrre <<" 手机号: " <<q->Mobi <<" qq号: "
<< q->qq << endl;
break;
}
else
p = p->next ;
}

return head;
}

//修改联系人信息
CONT *AmendMessage(CONT *head)
{
CONT *p;
char nam[10];
char sex[5];
char Adrre[50];
char Class[20];
char Mobi[20];
char qq[10];
if(head == NULL)
{
cout<< "没有任何信息,请返回添加信息!!!"<< endl;
return head;
}
else
p = head;
cout << "请你输入要修改人的名字:";
cin >> nam;
while(p != NULL && p->name != nam)
{
if(strcmp(p->name ,nam)==0)
{
strcpy(p->name ,nam);
int choose;
do
{
cout << "请选择要修改哪一项的信息!!!"<< endl;
cout << "1 修改性别 2 修改班别 3 修改地址 4 修改手机号码 5 修改qq号 " << endl;
cin >> choose;
switch(choose)
{
case 1:
cout << "请输入要修改后的性别:";
cin >> sex;
strcpy(p->sex , sex);
cout << "修改成功!!!"<< endl;
system("CLS");
return head;
break;
case 2:
cout << "请输入修改后的班别:";
cin >> Class;
strcpy(p->Class ,Class);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 3:
cout << " 请输入修改后的地址:";
cin >> Adrre;
strcpy(p->Adrre ,Adrre);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 4:
cout << "请输入修改后的手机号码:";
cin >> Mobi;
strcpy(p->Mobi ,Mobi);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 5:
cout << "请输入修改后的qq号:";
cin >> qq;
strcpy(p->qq ,qq);
system("CLS");
cout << "修改成功!!!"<< endl;

return head;
break;
default:
cout << "输入错误,重新选择!!" << endl;
break;
}
}while(choose != 0);

}
else if(p->next != NULL && p->name != nam)
{
p = p->next ;
}
else
{
cout << "输入名字错误,请重新选择4修改!!!" << endl;
return head;
}

}
//return head;
}
//删除联系人
CONT *DeleteMessage(CONT *head)
{
CONT *p,*q,*t;
char name[20];
char elem[10];
cout << "请输入要删除联系人的姓名:";
cin >> name;

if(head == NULL)
{
cout << "没有任何信息可删除,请添加信息!!"<< endl;
return head;
}

else if(strcmp(head->name,name)==0 )
{
t = head;
head = head->next ;
return head;
}
else
{

p = head->next ;
q = head;
}
while(p !=NULL && p->name != name)
{
q = p;
p = p->next ;
}
if(strcmp(p->name ,name)==0)
{
q->next = p->next ;
free(p);
//t = p;
}
else
{
cout<< "表中无该元素!" << endl;
return head;
}
if(t)
{
// elem = t->name ;
free(t);

}

return head;
}
//保存文件
CONT *saveFileCONT(CONT *head )
{
char *fileName;
FILE *fp;
CONT *p;
p = (CONT *)malloc(sizeof(CONT));
if(p=NULL)
{
cout <<"分配内存失败"<< endl;
// exit(0);
}
if(head ==NULL)
{
cout <<"没有任何信息,请先添加信息!!"<< endl;
return head;
}
else
{
p = head;
fileName = (char *)malloc(sizeof(char));

cout << "请输入文件名:" << endl;
cin >> fileName;
fileName = strcat(fileName,".txt");
if( (fp = fopen(fileName,"a")) == NULL)
cout << "can't open file" << endl;

do
{
//fwrite(head,sizeof(CONT),1,fp);
//fprintf(fp,p->name,p->sex,p->Class,p->Adrre,p->Mobi,p->qq );
fprintf(fp," 姓名:");
fprintf(fp, p->name );
fprintf(fp," 性别:");
fprintf(fp, p->sex);
fprintf(fp," 班别:");
fprintf(fp, p->Class);
fprintf(fp," 地址:");
fprintf(fp, p->Adrre);
fprintf(fp," 手机号:");
fprintf(fp, p->Mobi);
fprintf(fp," qq号:");
fprintf(fp, p->qq);
fprintf(fp,"\n");
cout << endl;
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 班别:" <<p->Class <<" 住址:" <<p->Adrre <<"手机号:"
<< p->Mobi <<" qq号:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";

p=p->next ;
}while(p!=NULL);
}
fclose(fp);
return head;
}
//读取文件
CONT *readFileCONT(CONT *head)
{
char *fileName;
CONT *p;
FILE *fp;
fileName = (char *)malloc(sizeof(char));
cout << "请输入要打开的文件名:";
cin >> fileName;
fileName = strcat(fileName,".txt");
fp = fopen(fileName,"r");
if(fp == NULL)
{
cout << "文件没有任何信息,请先输入信息!!"<< endl;
}
else
{
while(!feof(fp))
{
p = (CONT *)malloc(sizeof(CONT));
if(p == NULL)
{
cout << "分配内存失败!!"<< endl;
return head;
}
fscanf(fp,p->name );
fscanf(fp,p->sex );
fscanf(fp,p->Class );
fscanf(fp,p->Adrre );
fscanf(fp,p->Mobi );
fscanf(fp,p->qq );
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 班别:" <<p->Class <<" 住址:" <<p->Adrre <<"手机号:"
<< p->Mobi <<" qq号:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p->next = head;
head = p;
p = p->next ;
}
fclose(fp);
cout<< "读取信息成功!!!"<< endl;
}
fclose(fp);
return head;
}
void showmessage(CONT *head)
{
CONT *p;
int i=0;
if(head == NULL )
{
cout << "没有任何信息可输出,请添加信息!!!"<< endl;
}
else
{
p = head;
cout << "*******************************联系表如下***************************************";
do
{
i++;
cout << "--------------------------------------------------------------------------------";
cout << i << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 班别:" <<p->Class <<" 住址:" <<p->Adrre <<"手机号:"
<< p->Mobi <<" qq号:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p = p->next ;
}while(p!=NULL);
}
cout << "*********************************联系表***************************************";
cout << endl;
}

6. C语言课程设计---电话薄管理(C语言高手进)

/*
vc 6.0运行通过-原创
链表做的
*/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define ID struct id
struct id
{
char name[20];
char tele[20];
ID *next;
};

int pc=0;

ID *creat()
{
ID *p1,*p2,*head;
char str[20];
p1=p2=head=NULL;
printf("\t\t\t 开始输入记录(姓名 # 结束)!\n");
while(1)
{
printf("请输入姓名:\n");scanf("%s",str);getchar();
if(strcmp(str,"#")==0) break;
p1=(ID*)malloc(sizeof(ID));
strcpy(p1->name,str);
printf("请输入电话号码:\n");scanf("%s",p1->tele);getchar();

if(head==NULL)
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
pc++;
}
p2->next=NULL;
return(head);
}

/*输入/添加记录*/
ID *insert(ID *head)
{
ID *temp,*p1,*p2;
printf("插入操作开始!!!\n");
temp=(ID *)malloc(sizeof(ID));
printf("请输入姓名:\n");scanf("%s",temp->name);getchar();
printf("请输入电话号码:\n");scanf("%s",temp->tele);getchar();

if (head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
p1=head;
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
}
p2->next=temp;
temp->next=p1;
}

printf("插入成功");
pc++;
return (head);
}

/*删除记录*/
ID *delet(ID *head)
{
ID *p1,*p2;
char str[20];
printf("请输入要删除的电话号码:");scanf("%s",str);getchar();
p1=head;
if (head==NULL)
{
printf("没有记录\n");
goto end;
}
while(p1!=NULL && strcmp(p1->tele,str))
{
p2=p1;p1=p1->next;
}
if(p1==NULL)
printf("未找到符合记录!\n");
else if(strcmp(p1->tele,str)==0)
{
if (p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("删除成功!!!\n");
pc--;
}
end:return head;
}

/*查找记录*/
ID *search(ID *head)
{
ID *p1,*p2;
int flag=0;
char c;
printf("请输入姓名的首字母:");scanf("%c",&c);getchar();
p1=head;
while( p1!=NULL)
{
if(p1->name[0]==c)
{
printf("姓名:%s\t电话号码:%s\n",p1->name,p1->tele);
flag=1;
}
p2=p1;p1=p1->next;
}
if(flag==0) printf("未找到符合记录!\n");
return head;
}

/*修改记录*/
ID *modify(ID *head)
{
ID *p1,*p2;
int mode;
char str[20];
printf("请输入要修改记录的姓名:");scanf("%s",str);getchar();
p1=head;
while( p1!=NULL)
{
if(strcmp(p1->name,str)==0)
{
printf("1.姓名:%s\t2.电话号码:%s\n",p1->name,p1->tele);
printf("请选择要修改选项:\n");
scanf("%d",&mode);getchar();
if(mode==1)
{
printf("请输入修改后的姓名\n");
scanf("%s",p1->name);getchar();
}
else if(mode==2)
{
printf("请输入修改后的电话号码\n");
scanf("%s",p1->tele);getchar();
}
else
printf("输入有误!\n");
break;
}
p2=p1;p1=p1->next;
}
if(p1==NULL) printf("未找到符合要求的记录!\n");
return head;
}

/*显示结果函数*/
void print(ID *head)
{
ID *p;
p=head;
printf("\t\t\t*****************\n");
printf("显示结果是:\n");
printf("姓名\t电话号码\n");
if(head!=NULL)
do
{
printf("%s\t%s\n",p->name,p->tele);
p=p->next;
} while(p!=NULL);
}

void main()
{
ID *head=NULL;
int choise;
printf("\t\t\t* * * * C语言课设* * * *\n");
while(1)
{
printf("\t\t 电话簿管理系统\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\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 0.退出\n");
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("请选择(0-6):");
scanf("%d",&choise);getchar();
switch(choise)
{
case 1: head=creat();
break;
case 2: print(head);
break;
case 3: head=search(head);
break;
case 4: head=insert(head);
break;
case 5: head=delet(head);
break;
case 0:
exit(0);
break;
default :printf("输入错误,请重新输入!\n");
}
}
}

热点内容
百度云解压密码忘记 发布:2024-11-26 14:35:36 浏览:863
最差拟合算法 发布:2024-11-26 14:21:17 浏览:739
指令式编程 发布:2024-11-26 14:16:24 浏览:19
阿里云ftp修改密码 发布:2024-11-26 14:12:11 浏览:617
mt4服务器ip 发布:2024-11-26 13:31:56 浏览:867
虚拟机编译得到bin文件 发布:2024-11-26 13:31:00 浏览:419
javaenumofenum 发布:2024-11-26 13:20:09 浏览:922
脚本中ge 发布:2024-11-26 13:14:24 浏览:376
安卓的gps怎么开 发布:2024-11-26 13:14:18 浏览:86
砍人脚本 发布:2024-11-26 13:14:15 浏览:257