一个电话号码管理程序,在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:jrg9522@126.com\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');}
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索文件
, 数据
, 结构
, 输出缓存区
, c++、cout缓存区问题
, long
, 电话号码
void
电话号码簿、电话号码簿模板、北京市电话号码簿、党政机关电话号码簿、机关内部电话号码簿,以便于您获取更多的相关知识。
时间: 2024-11-02 17:11:13