一個電話號碼管理程序,在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("打開文件失敗!即將退出系統。\n");
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("打開文件失敗!即將退出系統。\n");
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("\n\n\n\t\t\t\t文件清理完畢!");
}
void del() //***************************************刪除函數√
{
long i=0,in=0;
char ch;
FILE *f;
if((f=fopen("telefon.dat","rb+"))==NULL)
{
printf("打開文件失敗!即將退出系統。\n");
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("請輸入要刪除的資料序號:\n");
scanf("%ld",&in);
if(in<=0)
printf("[注意]:請輸入大於0的編號!\n");
}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("\n\n\n\n\n\n\t\t\t\t沒有資料可刪除!");
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("打開文件失敗!即將退出系統。\n");
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("\n\n\n\n\n\n\t\t\t請輸入要查找的電話號碼:");
scanf("%ld",&in);
if(in<4000000||in>4999999)
printf("\t\t\t[注意]:請輸入4000000至4999999的號碼!\n");
}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("╔══╦══════════╦═══════════════╦════╗\n");
printf("║序號║ 訂戶名 ║ 訂戶地址 ║ 電 話 ║\n");
printf("╚══╩══════════╩═══════════════╩════╝\n");
printf("┌──┬──────────┬───────────────┬────┐\n");
i=0;
while(i<fh.fc)
{
if (tel[i].usetel==in)
{
panduan++;
printf("│%-4d│%-20s│%-30s│%-8ld│\n",i+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (panduan!=panyiyang)
printf("├──┼──────────┼───────────────┼────┤\n");
}
i++;
}
printf("└──┴──────────┴───────────────┴────┘\n");
}
else
{
printf("\n\t\t\t沒有找到!\n");
}
}
else
{
printf("\n\n\n\n\n\n\t\t\t\t沒有資料可查找!");
}
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("打開文件失敗!即將退出系統。\n");
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("\n\n\n\n\n\n\t\t\t請輸入要查找的訂戶名:");
scanf("%s",&in);
while(i<fh.fc)
{
if(strcmp(tel[i].usename,in)==0)
{
panyiyang++;
}
i++;
}
if(panyiyang!=0)
{
system("cls");
printf("╔══╦══════════╦═══════════════╦════╗\n");
printf("║序號║ 訂戶名 ║ 訂戶地址 ║ 電 話 ║\n");
printf("╚══╩══════════╩═══════════════╩════╝\n");
printf("┌──┬──────────┬───────────────┬────┐\n");
i=0;
while(i<fh.fc)
{
if (strcmp(tel[i].usename,in)==0)
{
panduan++;
printf("│%-4d│%-20s│%-30s│%-8ld│\n",i+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (panduan!=panyiyang)
printf("├──┼──────────┼───────────────┼────┤\n");
}
i++;
}
printf("└──┴──────────┴───────────────┴────┘\n");
}
else
{
printf("\n\t\t\t沒有找到!\n");
}
}
else
{
printf("\n\n\n\n\n\n\t\t\t\t沒有資料可查找!");
}
fclose(f);
getch();
}
void edit() //**************************************修改函數√
{
long in=0;
char ch;
FILE *f;
if((f=fopen("telefon.dat","rb+"))==NULL)
{
printf("打開文件失敗!即將退出系統。\n");
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("請輸入要修改的資料序號:\n");
scanf("%ld",&in);
if(in<=0)
printf("[注意]:請輸入大於0的編號!\n");
}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的號碼!\n");
}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("\n\n\n\n\n\n\t\t\t\t沒有資料可修改!");
getch();
}
fclose(f);
}
void display() //**********************************查看函數√
{
long i=0;
FILE *f;
if((f=fopen("telefon.dat","rb"))==NULL)
{
printf("打開文件失敗!即將退出系統。\n");
getch();
exit(0);
}
system("cls");
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if(fh.fc>0)
{
printf("╔══╦══════════╦═══════════════╦════╗\n");
printf("║序號║ 訂戶名 ║ 訂戶地址 ║ 電 話 ║\n");
printf("╚══╩══════════╩═══════════════╩════╝\n");
printf("┌──┬──────────┬───────────────┬────┐\n");
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│\n",i+1,tel[i].usename,tel[i].useadd,tel[i].usetel);
if (i!=fh.fc-1)
printf("├──┼──────────┼───────────────┼────┤\n");
if ((i+1)%10==0) //-------------------------------------------10個號碼分頁顯示
{getch();}
i++;
}
printf("└──┴──────────┴───────────────┴────┘\n");
}
else
{
printf("\n\n\n\n\n\n\t\t\t\t沒有資料可顯示!");
}
fclose(f);
}
void addnew() //************************************新增函數√
{
char ch;
FILE *f;
if((f=fopen("telefon.dat","rb+"))==NULL)
{
printf("\n\n\n\t\t\t\t打開文件失敗!即將退出系統。\n");
getch();
exit(0);
}
do
{
fseek(f,0,0);
fread(&fh,sizeof(struct fileh),1,f);
if(fh.fc<200)
{
fh.fc++;
printf("\n\n\n\t\t\t請您輸入姓名:");
scanf("%s",&tel[fh.fc-1].usename);
printf("\t\t\t請您輸入地址:");
scanf("%s",&tel[fh.fc-1].useadd);
do
{
printf("\t\t\t請您輸入電話號碼:");
scanf("%ld",&tel[fh.fc-1].usetel);
if(tel[fh.fc-1].usetel<4000000||tel[0].usetel>4999999)
printf("\t\t\t[注意]:請輸入4000000至4999999的號碼!\n");
}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("\t\t\t增加信息完畢!是否繼續填加?(y/n):");
ch=getch();
}
else
{
printf("\t\t\t超過極限了。不能再存儲了!");
ch='n';
}
}while(ch=='y'||ch=='Y');
fclose(f);
}
void quit() //************************************退出函數
{
qingli();
printf("\n\t\t\t歡迎使用,本系統!\n\n");
printf("\t\t\t作者:金日國\n");
printf("\t\t\tE-mail:[email protected]\n");
printf("\t\t\tQQ:70939594\n");
printf("\t\t\t軟件性質:自由軟件\n");
printf("\t\t\t修改說明:\n");
printf("\t\t\t\t本次美化了界面;\n");
printf("\t\t\t\t但用戶輸入錯誤BUG還沒改;\n");
printf("\t\t\t\t在漢字輸入方式輸入數字和\n\t\t\t\t英文輸入方式輸入的數字其實\n\t\t\t\t不一樣,這個BUG還沒改。;\n\t\t\thelp me!");
getch();
}
void main() //************************************主函數
{
char ch;
FILE *f;
if((f=fopen("telefon.dat","ab"))==NULL)
{
printf("\n\n\n\t\t\t\t打開文件失敗!即將退出系統。\n");
getch();
exit(0);
}
fclose(f);
do
{
system("cls");
printf("\n\t\t\t╔═══════════════╗\n");
printf("\t\t\t║ 電話號碼存儲系統V1.3 ║\n");
printf("\t\t\t╚═══════════════╝\n");
printf("\t\t\t┌───────────────┐\n");
printf("\t\t\t│ 1.加入新電話信息 │\n");
printf("\t\t\t├───────────────┤\n");
printf("\t\t\t│ 2.修改信息 │\n");
printf("\t\t\t├───────────────┤\n");
printf("\t\t\t│ 3.刪除信息 │\n");
printf("\t\t\t├───────────────┤\n");
printf("\t\t\t│ 4.按電話號碼查找信息 │\n");
printf("\t\t\t├───────────────┤\n");
printf("\t\t\t│ 5.按名稱查找信息 │\n");
printf("\t\t\t├───────────────┤\n");
printf("\t\t\t│ 6.查看信息 │\n");
printf("\t\t\t├───────────────┤\n");
printf("\t\t\t│ 7.退出系統 │\n");
printf("\t\t\t└───────────────┘\n");
printf("\t\t\t 請按操作編號:");
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');
}