c语言程序设计——简单的通讯录系统(源代码)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include
#include
#define size 50
static int h;
static char key[15];
int i=0;
struct friend_type
{
int num;
char name[10];
char tel[13];
char email[15];
char ph[12];
char QQ[10];
char addr[20];
}fre[size];
void save()
{
FILE*fp;
int i;
if((fp=fopen("friend.txt","wb"))==NULL)
{printf("Can not open file\n");
return;
}
for(i=0;i
printf("file write error\n");
fclose(fp);
}
void input()
{ char ch[10];
int i=0;
clrscr();
printf("Are you sure to creat information?(y/n)\n(if you press y,the old information will be deleted!)\n");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0)
{h=0;
do
{ clrscr();
printf("\nname: ");
scanf("%s",fre[i].name);
printf("\ntelphone number: ");
scanf("%s",fre[i].tel);
printf("\nmobilephone number: ");
scanf("%s",fre[i].ph);
printf("\nemail: ");
scanf("%s",fre[i].email);
printf("\nQQ: ");
scanf("%s",fre[i].QQ);
printf("address: ");
scanf("%s",fre[i].addr);
h++;
i++;
printf("\nif want continue input student information(y/n): ");
scanf("%s",ch);
}while(strcmp(ch,"y")==0||strcmp(ch,"Y")==0);}
save();
}
void output()
{ int n=0;
FILE*fp;
clrscr();
fp=fopen("friend.txt","rb");
printf(" name tel mobilephone e-mail QQ address\n");
do
{fread(&fre[n],sizeof(struct friend_type),1,fp);
printf(" %-10s %-10s %-12s %-15s %-10s %-20s\n",fre[n].name,fre[n].tel,fre[n].ph,fre[n].email,fre[n].QQ,fre[n].addr);
n++;
}while(n
printf("Finished!Press any key to return.");
getch();
}
void look()
{
int n=0,a=0,i=0;
char b[20];
FILE*fp;
clrscr();
fp=fopen("friend.txt","rb");
printf("\n\n\n\t\t\t1.search by name\n\t\t\t2.search by telephone number\n\t\t\t3.search by mobilephone number\n\t\t\t4.return to main menu");
printf("\n\n Enter your choice : ");
scanf("%d",&a);
switch(a)
{
case 1:printf("\n");
printf("\nenter you want look name:");
scanf("%s",b);
do{
if(strcmp(fre[n].name,b)==0)
{printf(" name tel mobilephone e-mail QQ address\n");
printf(" %-10s %-10s %-11s %-15s %-10s %-20s\n",fre[n].name,fre[n].tel,fre[n].ph,fre[n].email,fre[n].QQ,fre[n].addr);}
n++;
}while(n
case 2:printf("\n");
printf("\nenter you want look telephone number:");
scanf("%s",b);
do{
if(strcmp(fre[i].tel,b)==0)
{printf(" name tel mobilephone e-mail QQ address\n");
printf(" %-10s %-10s %-11s %-15s %-10s %-20s\n",fre[i].name,fre[i].tel,fre[i].ph,fre[i].email,fre[i].QQ,fre[i].addr);}
i++;
}while(i
case 3:printf("\n");
printf("\nenter you want look mobilephone num
ber:");
scanf("%s",b);
do{
if(strcmp(fre[n].ph,b)==0)
{printf(" name tel mobilephone e-mail QQ address\n");
printf(" %-10s %-10s %-11s %-15s %-10s %-20s\n",fre[n].name,fre[n].tel,fre[n].ph,fre[n].email,fre[n].QQ,fre[n].addr);}
n++;
}while(n
case 4:break;
default:printf("The choice is between 1 to 3"); sleep(1);look();
}
fclose(fp);
printf("\nFinished!Press any key to return.");
getch();
}
void change()
{ int n=0;
char ch[20],a[20];
FILE*fp;
clrscr();
fp=fopen("friend.txt","wb+");
printf("\n enter you want change name:");
scanf("%s",a);
do
{
if(strcmp(fre[n].name,a)==0)
{ printf(" name tel mobilephone e-mail QQ address\n");
printf(" %-10s %-10s %-11s %-15s %-10s %-20s\n",fre[n].name,fre[n].tel,fre[n].ph,fre[n].email,fre[n].QQ,fre[n].addr);
printf("\n you sure want change student name(y/n): ");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0) { printf("\nname:"); scanf("%s",fre[n].name);}
printf("\n you sure want change telephone number(y/n): ");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0) { printf("\ntelephone number:"); scanf("%s",fre[n].tel);}
printf("\n you sure want change student mobilephone number(y/n): ");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0) { printf("\nmobilephone number:"); scanf("%s",fre[n].ph);}
printf("\n you sure want change student e-mail(y/n): ");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0) { printf("\ne-mail:"); scanf("%s",fre[n].email);}
printf("\n you sure want change student QQ(y/n): ");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0) { printf("\nQQ:"); scanf("%s",fre[n].QQ);}
printf("\n you sure want change student address(y/n): ");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0) { printf("\naddress:"); scanf("%s",fre[n].addr);}
}
n++;
}while(n
printf("\nFinished!Press any key to return.");
getch();
}
void del()
{
int i,j;
char c[20],ch[20];
clrscr();
printf("\nname you want to delete: ") ;
scanf("%s",c);
for(i=0;i
printf("\n you sure want delete (y/n): ");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0)
{for(j=i;j<=2;j++)
fre[j]=fre[j+1];
printf("\t\t\tyou had delete %s\n",c);
h--;
}
printf("\nFinished!Press any key to return.");
getch();
}
void add()
{
int i=h;
char ch;
clrscr();
do
{
printf("\nname: ");
scanf("%s",fre[i].name);
printf("\ntelphone number: ");
scanf("%s",fre[i].tel);
printf("\nmobilephone number: ");
scanf("%s",fre[i].ph);
printf("\nemail: ");
scanf("%s",fre[i].email);
printf("\nQQ: ");
scanf("%s",fre[i].QQ);
printf("address: ");
scanf("%s",fre[i].addr);
h++;
i++;
printf("\nif want continue input student information(y/n): ");
scanf("%s",ch);
}while(strcmp(ch,"y")==0||strcmp(ch,"Y")==0);
}
void save_h()
{ FILE *fp1;
fp1=fopen("h.txt","wt");
if(fp1==NULL)
{ printf("Can not open file\n");
printf("Press any key to return.\n");
getch();
}
fputc(h,fp1);
fclose(fp1);
exit(0);
}
void read_h()
{ FILE *fp1;
fp1=fopen("h.txt","rt");
h=fgetc(fp1);
fclose(fp1);
}
void save_key()
{
char a[15],b[15],ch;
FILE *fp2;
fp2=fopen("key.txt","wt");
clrscr();
printf("\n\n\n\n\n\t\tAre you sure to change the key?(y/n)\n\t\t");
scanf("%s",ch);
if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0)
{ clrscr();
printf("\n\n\n\n\n\t\t\tinput new key:");
scanf("%s",a);
printf("\n\n\n\n\n\t\t\tinput new key again:");
scanf("%s",b);
if(strcmp(a,b)==0)
{
strcpy( key,a);
}
else save_key();
if(fp2==NULL)
{ printf("Can not open file\n");
printf("Press any key to return.\n");
getch();
}
fputs(key,fp2);
fclose(fp2);
}
}
void read_key()
{
FILE *fp2;
fp2=fopen("key.txt","rt");
strcpy(key,"123456");
if(fp2==NULL)
{
printf("\n\n\t\t*first key is 123456\n");
}
fgets(key,15,fp2);
fclose(fp2);
}
void main()
{char k[20];
int a;
char b,ch[20];
clrscr();
b=020;
read_key();
printf("\n\n\n\n\n\n\n\t\t\tplease input key:\n\t\t\t");
scanf("%s",k);
if(strcmp(k,key)==0)
{read_h();
do
{clrscr();
printf("\n\n *******************************************************************\n");
printf(" *"); printf("\t\twelcome enter the contacks system"); printf("\t\t *");
printf("\n *******************************************************************\n");
printf(" %c\t",b); printf("\t\t1.Input all information"); printf("\t\t\t\t*\n");
printf(" %c\t",b); printf("\t\t2.Check all information"); printf("\t\t\t\t*\n");
printf(" %c\t",b); printf("\t\t3.Search information"); printf("\t\t\t\t*\n");
printf(" %c\t",b); printf("\t\t4.Change information"); printf("\t\t\t\t*\n");
printf(" %c\t",b); printf("\t\t5.Delete information"); printf("\t\t\t\t*\n");
printf(" %c\t",b); printf("\t\t6.Add information"); printf("\t\t\t\t*\n");
printf(" %c\t",b); printf("\t\t7.Change key "); printf("\t\t\t\t*\n");
printf(" %c\t",b); printf("\t\t8.Exit "); printf("\t\t\t\t*\n");
printf(" *******************************************************************\n");
printf(" Enter your choice : ");
scanf("%d",&a);
switch(a)
{
case 1:printf("\
n");input();break;
case 2:printf("\n");output();break;
case 3:printf("\n");look();break;
case 4:printf("\n");change();break;
case 5:printf("\n");del();break;
case 6:printf("\n");add();break;
case 7:printf("\n");save_key();break;
case 8:printf("\n");save_h();break;
default:printf("\nThe choice is between 1 to 8.\n");sleep(1);
} save();
}while(a!=0);}
else printf("wrong key!");
getch();
}