Welcome

首页 / 软件开发 / C语言 / 用C实现的一个电话号码簿

用C实现的一个电话号码簿2010-05-18一个电话号码管理程序,在VC++ 6.0上通过。

#include<stdio.h>
#include<string.h>
struct fileh //*************************************定义结构体(文件信息缓存区)
{
long fc;
}fh;
struct telsys //*********************************定义结构体(文件数据缓存区)
{
char usename[30];
char useadd[50];
long usetel;
}tel[200];


void addnew(); //*********************************定义函数类型
void del();
void display();
void edit();
void findname();
void findtel();
void quit();
void qingli();
void qingli() //*************************************信息文件的清理 (清理垃圾数据)√
{
long i=0;
FILE *f;
if((f=fopen("telefon.dat","rb"))==NULL)
{
printf("打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
while(i<fh.fc)
{
fseek(f,sizeof(struct fileh)+(i)*sizeof(struct telsys),0);
fread(&tel[i],sizeof(struct telsys),1,f);
i++;
}
fclose(f);

i=0;
if((f=fopen("telefon.dat","wb+"))==NULL)
{
printf("打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
while(i<fh.fc)
{
fseek(f,0,0);
fwrite(&fh,sizeof(struct fileh),1,f);
fseek(f,sizeof(struct fileh)+(i)*sizeof(struct telsys),0);
fwrite(&tel[i],sizeof(struct telsys),1,f);
i++;
}
fclose(f);
printf(" 文件清理完毕!");
}
void del() //***************************************删除函数√
{
long i=0,in=0;
char ch;
FILE *f;
if((f=fopen("telefon.dat","rb+"))==NULL)
{
printf("打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if (fh.fc>0)
{
do
{
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
display();
do
{
printf("请输入要删除的资料序号: ");
scanf("%ld",&in);
if(in<=0)
printf("[注意]:请输入大于0的编号! ");
}while(in<=0);
if(in<=fh.fc)
{
i=in;
while(i<=fh.fc)
{
fseek(f,sizeof(struct fileh)+(i)*sizeof(struct telsys),0);
fread(&tel[i],sizeof(struct telsys),1,f);
strcpy(tel[i-1].usename,tel[i].usename);
strcpy(tel[i-1].useadd,tel[i].useadd);
tel[i-1].usetel=tel[i].usetel;
i++;
}
fh.fc--;
fseek(f,0,0);
fwrite(&fh,sizeof(struct fileh),1,f);
i=in;
while(i<=fh.fc)
{
fseek(f,sizeof(struct fileh)+(i-1)*sizeof(struct telsys),0);
fwrite(&tel[i-1],sizeof(struct telsys),1,f);
i++;
}
printf("删除信息完毕!");
}
else
{
printf("没有此序号的资料!(-_-;)");
}
if (fh.fc>0)
{
printf("是否继续修改?(y/n):");
ch=getch();
}
else
{
ch="n";
printf("没有资料可删除!");
getch();
}
}while(ch=="y"||ch=="Y");
}
else
{
printf(" 没有资料可删除!");
getch();
}
fclose(f);
}
void findtel() //***********************************按号查找函数√
{
long i=0,in=0,pan=0,panyiyang=0,panduan=0;
FILE *f;
if((f=fopen("telefon.dat","rb"))==NULL)
{
printf("打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if (fh.fc>0)
{
fseek(f,sizeof(struct fileh),0);
fread(&tel,fh.fc*sizeof(struct telsys),1,f);
do
{
printf(" 请输入要查找的电话号码:");
scanf("%ld",&in);
if(in<4000000||in>4999999)
printf(" [注意]:请输入4000000至4999999的号码! ");
}while(in<4000000||in>4999999);
while(i<fh.fc)
{
fseek(f,sizeof(struct fileh)+(i)*sizeof(struct telsys),0);
fread(&tel[i],sizeof(struct telsys),1,f);
if(tel[i].usetel==in)
{
panyiyang++;
}
i++;
}
if(panyiyang!=0)
{
system("cls");
printf("╔══╦══════════╦═══════════════╦════╗ ");
printf("║序号║ 订户名 ║ 订户地址 ║电 话 ║ ");
printf("╚══╩══════════╩═══════════════╩════╝ ");
printf("┌──┬──────────┬───────────────┬────┐ ");
i=0;
while(i<fh.fc)
{
if (tel[i].usetel==in)
{
panduan++;
printf("│%-4d│%-20s│%-30s│%-8ld│ ",i+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (panduan!=panyiyang)
printf("├──┼──────────┼───────────────┼────┤ ");
}
i++;
}
printf("└──┴──────────┴───────────────┴────┘ ");
}
else
{
printf(" 没有找到! ");
}
}
else
{
printf(" 没有资料可查找!");
}
fclose(f);
getch();
}
void findname() //**********************************按名查找函数
{
long i=0,pan=0,panyiyang=0,panduan=0;
char in[30];
FILE *f;
if((f=fopen("telefon.dat","rb"))==NULL)
{
printf("打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if (fh.fc>0)
{
fseek(f,sizeof(struct fileh),0);
fread(&tel,fh.fc*sizeof(struct telsys),1,f);
printf(" 请输入要查找的订户名:");
scanf("%s",&in);
while(i<fh.fc)
{
if(strcmp(tel[i].usename,in)==0)
{
panyiyang++;
}
i++;
}
if(panyiyang!=0)
{
system("cls");
printf("╔══╦══════════╦═══════════════╦════╗ ");
printf("║序号║ 订户名 ║ 订户地址 ║电 话 ║ ");
printf("╚══╩══════════╩═══════════════╩════╝ ");
printf("┌──┬──────────┬───────────────┬────┐ ");
i=0;
while(i<fh.fc)
{
if (strcmp(tel[i].usename,in)==0)
{
panduan++;
printf("│%-4d│%-20s│%-30s│%-8ld│ ",i+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (panduan!=panyiyang)
printf("├──┼──────────┼───────────────┼────┤ ");
}
i++;
}
printf("└──┴──────────┴───────────────┴────┘ ");
}
else
{
printf(" 没有找到! ");
}
}
else
{
printf(" 没有资料可查找!");
}
fclose(f);
getch();
}
void edit() //**************************************修改函数√
{
long in=0;
char ch;
FILE *f;
if((f=fopen("telefon.dat","rb+"))==NULL)
{
printf("打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if(fh.fc>0)
{
do
{
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
display();
do
{
printf("请输入要修改的资料序号: ");
scanf("%ld",&in);
if(in<=0)
printf("[注意]:请输入大于0的编号! ");
}while(in<=0);
if(in<=fh.fc)
{
printf("请您输入姓名:");
scanf("%s",&tel[in-1].usename);
printf("请您输入地址:");
scanf("%s",&tel[in-1].useadd);
do
{
printf("请您输入电话号码:");
scanf("%ld",&tel[in-1].usetel);
if(tel[in-1].usetel<4000000||tel[in-1].usetel>4999999)
printf("[注意]:请输入4000000至4999999的号码! ");
}while(tel[in-1].usetel<4000000||tel[in-1].usetel>4999999);
fseek(f,sizeof(struct fileh)+(in-1)*sizeof(struct telsys),0);
fwrite(&tel[in-1],sizeof(struct telsys),1,f);
printf("修改信息完毕!");
}
else
{
printf("没有此序号的资料!(-_-;)");
}
printf("是否继续修改?(y/n):");
ch=getch();
}while(ch=="y"||ch=="Y");
}
else
{
printf(" 没有资料可修改!");
getch();
}
fclose(f);
}
void display() //**********************************查看函数√
{
long i=0;
FILE *f;
if((f=fopen("telefon.dat","rb"))==NULL)
{
printf("打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
system("cls");
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if(fh.fc>0)
{
printf("╔══╦══════════╦═══════════════╦════╗ ");
printf("║序号║ 订户名 ║ 订户地址 ║电 话 ║ ");
printf("╚══╩══════════╩═══════════════╩════╝ ");
printf("┌──┬──────────┬───────────────┬────┐ ");
while(i<fh.fc)
{
fseek(f,sizeof(struct fileh)+(i)*sizeof(struct telsys),0);
fread(&tel[i],sizeof(struct telsys),1,f);
printf("│%-4d│%-20s│%-30s│%-8ld│ ",i+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (i!=fh.fc-1)
printf("├──┼──────────┼───────────────┼────┤ ");
if ((i+1)%10==0) //-------------------------------------------10个号码分页显示
{getch();}
i++;
}
printf("└──┴──────────┴───────────────┴────┘ ");
}
else
{
printf(" 没有资料可显示!");
}
fclose(f);
}
void addnew() //************************************新增函数√
{
char ch;
FILE *f;

if((f=fopen("telefon.dat","rb+"))==NULL)
{
printf(" 打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
do
{
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if(fh.fc<200)
{
fh.fc++;
printf(" 请您输入姓名:");
scanf("%s",&tel[fh.fc-1].usename);
printf(" 请您输入地址:");
scanf("%s",&tel[fh.fc-1].useadd);
do
{
printf(" 请您输入电话号码:");
scanf("%ld",&tel[fh.fc-1].usetel);
if(tel[fh.fc-1].usetel<4000000||tel[0].usetel>4999999)
printf(" [注意]:请输入4000000至4999999的号码! ");
}while(tel[fh.fc-1].usetel<4000000||tel[0].usetel>4999999);
fseek(f,0,0);
fwrite(&fh,sizeof(struct fileh),1,f);
fseek(f,sizeof(struct fileh)+(fh.fc-1)*sizeof(struct telsys),0);
fwrite(&tel[fh.fc-1],sizeof(struct telsys),1,f);
printf(" 增加信息完毕!是否继续填加?(y/n):");
ch=getch();
}
else
{
printf(" 超过极限了。不能再存储了!");
ch="n";
}
}while(ch=="y"||ch=="Y");
fclose(f);
}
void quit() //************************************退出函数
{
qingli();
printf(" 欢迎使用,本系统! ");
printf(" 作者:金日国 ");
printf(" E-mail:jrg9522@126.com ");
printf(" QQ:70939594 ");
printf(" 软件性质:自由软件 ");
printf(" 修改说明: ");
printf(" 本次美化了界面; ");
printf(" 但用户输入错误BUG还没改; ");
printf(" 在汉字输入方式输入数字和 英文输入方式输入的数字其实 不一样,这个BUG还没改。; help me!");
getch();
}
void main() //************************************主函数
{
char ch;
FILE *f;
if((f=fopen("telefon.dat","ab"))==NULL)
{
printf(" 打开文件失败!即将退出系统。 ");
getch();
exit(0);
}
fclose(f);
do
{
system("cls");
printf(" ╔═══════════════╗ ");
printf(" ║ 电话号码存储系统V1.3 ║ ");
printf(" ╚═══════════════╝ ");
printf(" ┌───────────────┐ ");
printf(" │ 1.加入新电话信息 │ ");
printf(" ├───────────────┤ ");
printf(" │ 2.修改信息 │ ");
printf(" ├───────────────┤ ");
printf(" │ 3.删除信息 │ ");
printf(" ├───────────────┤ ");
printf(" │ 4.按电话号码查找信息 │ ");
printf(" ├───────────────┤ ");
printf(" │ 5.按名称查找信息 │ ");
printf(" ├───────────────┤ ");
printf(" │ 6.查看信息 │ ");
printf(" ├───────────────┤ ");
printf(" │ 7.退出系统 │ ");
printf(" └───────────────┘ ");
printf(" 请按操作编号:");
ch=getch();
switch (ch)
{
case "1":
system("cls");
addnew();
break;
case "2":
system("cls");
edit();
break;
case "3":
system("cls");
del();
break;
case "4":
system("cls");
findtel();
break;
case "5":
system("cls");
findname();
break;
case "6":
display();
getch();
break;
case "7":
system("cls");
quit();
break;
}
}while(ch!="7");
}