第1个回答 2016-01-02
代码如下,,你可以学习学习:
#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个回答 2008-07-26
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "stdlib.h"
#define null 0
struct record
{
char name[20];
char phone[20];
char adress[40];
char postcode[10];
char e_mail[30];
}student[500];
struct LinkList
{
struct record US;
struct LinkList *next;
}a;
struct LinkList *head=null;
int num=0;
FILE *fp;
int menu_select();
int adduser();
int list();
int search();
int display();
int add();
int listbyname();
int dele();
int save();
int exit();
void main()
{
system("cls");
for(;;)
{
switch(menu_select())
{
case 0:adduser();break;
case 1:list();break;
case 2:search();break;
case 3:display();break;
case 4:add();break;
case 5:listbyname();break;
case 6:dele();break;
case 7:save();break;
case 8:exit(0);
}
}
}
menu_select()
{
char s[80];
int a;
printf("*_* press any key enter menu! *_* \n");
getch();
system("cls");
printf("\t\t********************MENU*********************\n\n");
printf("\t\t 0. 输入记录\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. Quit\n");
printf("\t\t***********************************************\n");
do{
printf("\n Enter you choice(0~11):");
scanf("%s",s);
a=atoi(s);
}
while (a<0||a>11);
return a;
}
adduser()
{
printf("\n\t\t**************** 请输入用户信息 ****************\n");
printf("\n\t\t输入姓名:");
scanf("%s",&student[num].name);
printf("\n\t\t输入电话号码:");
scanf("%s",&student[num].phone);
printf("\n\t\t输入地址:");
scanf("%s",&student[num].adress);
printf("\n\t\t输入邮编:");
scanf("%s",&student[num].postcode);
printf("\n\t\t输入e-mail:");
scanf("%s",&student[num].e_mail);
num++;
printf("\n\t\t是否继续添加?(Y/N):");
if (getch()=='y')
adduser();
return(0);
}
list()
{
int i;
system("cls");
if(num!=0)
{
printf("\n\t\t*************** 以下为通讯录所有信息************");
for (i=0;i<num;i++)
{
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\t邮编: %s",student[i].postcode);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\t\t");
if (i+1<num)
{
printf("\n\t\t__________________________");
system("pause");
}
}
printf("\n\t\t************************************************");
}
else
printf("\n\t\t通讯录中无任何纪录");
printf("\n\t\t按任意键返回主菜单:");
getch();
return(0);
}
search()
{
int mark=0;
int i;
int a=0;
printf("\n\t\t***************** 按姓名查找 *******************");
char name[20];
printf("\n\t\t请输入姓名:");
scanf("%s",name);
for(i=a;i<num;i++)
{
if (strcmp(student[i].name,name)==0)
{
printf("\n\t\t************* 以下是您查找的用户信息 ***********");
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t************************************************");
mark++;
if ((i+1)<num)
{
printf("\n\t\t是否继续查找相同名字的用户信息:(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
else
return(0);
}
else
{
printf("\n\t\t按任意键返回主菜单");
getch();
return(0);
}
}
}
if(mark!=0)
{
printf("\n\t\t没有相同姓名的用户纪录");
printf("\n\t\t按任意键返回主菜单");
getch();
return(0);
}
else
{
printf("\n\t\t没有相同姓名的用户纪录");
printf("\n\t\t按任意键返回主菜单");
getch();
return(0);
}
}
display()
{
int mark=0;
int i;
int a=0;
printf("\n\t\t****************** 按电话查找 ******************");
char phone[10];
printf("\n\t\t请输入电话号码:");
scanf("%s",phone);
for(i=0;i<num;i++)
{
if (strcmp(student[i].phone,phone)==0)
{
printf("\n\t\t************** 以下是您查找的用户信息 **********");
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t************************************************");
printf("\n\t\t按任意键返回主菜单:");
mark++;
getch();
return(0);
}
}
if (mark==0)
{
printf("\n\t\t没有改用户的信息");
printf("\n\t\t按任意键返回主菜单");
getch();
return(0);
}
return(0);
}
add()
{
int i;
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t文件打开失败");
}
for (i=0;i<num;i++)
{
if (fwrite(&student[i],sizeof(struct record),1,fp)!=1)
{
printf("\n\t\t写入文件错误!\n");
}
printf("\n\t\t**************** 请输入用户信息 ****************\n");
printf("\n\t\t输入姓名:");
scanf("%s",&student[num].name);
printf("\n\t\t输入电话号码:");
scanf("%s",&student[num].phone);
printf("\n\t\t输入地址:");
scanf("%s",&student[num].adress);
printf("\n\t\t输入邮编:");
scanf("%s",&student[num].postcode);
printf("\n\t\t输入e-mail:");
scanf("%s",&student[num].e_mail);
num++;
printf("\n\t\t是否继续添加?(Y/N):");
if (getch()=='y')
adduser();
return(0);
}
fclose(fp);
printf("\n\t\t通讯录文件已保存");
printf("\n\t\t按任意键退出程序\n\t\t");
exit(0);
return(0);
}
void deletebyphone()
{
int i,j;
int deletemark=0;
char phone[20];
printf("\n\t\t请输入要删除用户电话号码:");
scanf("%s",phone);
if(num==0)
{
printf("\n\t\t对不起,文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getch();
return;
}
for (i=0;i<num;i++)
{
if (strcmp(student[i].phone,phone)==NULL)
{
printf("\n\t\t以下是您要删除的用户纪录:");
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t是否删除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t删除成功");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyphone();
return;
}
else
return;
}
continue;
}
if (deletemark==0)
{
printf("\n\t\t没有该用户的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyphone();
return;
}
}
void deletebyname()
{
int a=0;
int findmark=0;
int j;
int deletemark=0;
int i;
char name[20];
printf("\n\t\t请输入要删除用户姓名:");
scanf("%s",name);
for (i=a;i<num;i++)
{
if (strcmp(student[i].name,name)==NULL)
{
printf("\n\t\t以下是您要删除的用户纪录:");
findmark++;
printf("\n\t\t________________________________");
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t________________________________");
printf("\n\t\t是否删除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t删除成功");
if((i+1)<num)
{
printf("\n\t\t是否继续删除相同姓名的用户信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf("\n\t\t是否继续删除相同姓名的用户信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
}
else
continue;
}
if ((deletemark==0)&&(findmark==0))
{
printf("\n\t\t没有该用户的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyphone();
return;
return;
}
else if (findmark!=0)
{
printf("\n\t\t没有重名信息");
printf("\n\t\t没有该用户的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyphone();
return;
return;
}
}
dele()
{
char choic;
printf("\n\t\t1-按电话号码删除 2-按姓名删除");
printf("\n\t\t请选择:");
choic=getch();
switch (choic)
{
case '1':deletebyphone();break;
case '2':deletebyname();break;
}
return(0);
}
listbyname()
{
int i,j;
struct record tmp;
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("\n\t\t排序成功,是否显示?(y/n)");
if (getch()=='y')
list();
return(0);
}
save()
{
int j;
FILE*fp;
fp=fopen("student.txt","w");
if (fp==NULL)
printf("can't open the file.");
if(num!=0)
{
for(j=0;j<num;j++)
{
fwrite(student,sizeof(student),1,fp);
}
}
printf("保存成功!");
fclose(fp);
return(0);
}