#include #include struct bankdata{ /*定义一个结构体*/ int num; char name[20]; float balance; } create(FILE *ptr)/*创建具有结构体的空文档*/ {int num; struct bankdata data={-1,"NONAME",0}; for(num=0;num<=15;num++){ fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fwrite(&data,sizeof(struct bankdata),1,ptr); } } view(FILE *ptr)/*查看文档内容*/ {int num; struct bankdata data; clrscr(); for(num=0;num<=15;num++){ fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fread(&data,sizeof(struct bankdata),1,ptr); printf("%-10d%-16s%-10f\n",data.num,data.name,data.balance); } } add(FILE *ptr)/*添加一个帐目*/ {int num; struct bankdata data; printf("Enter new account number(0-15):\n"); scanf("%d",&num); fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fread(&data,sizeof(struct bankdata),1,ptr); if(data.num!=-1) printf("Account %d already contains informaton.\n",num); else{ printf("Enter name.\n"); scanf("%s",data.name); printf("Enter balance.\n"); scanf("%f",&data.balance); data.num=num; } fseek(ptr,data.num*sizeof(struct bankdata),SEEK_SET); fwrite(&data,sizeof(struct bankdata),1,ptr); } delete(FILE *ptr)/*删除一个帐目*/ {int num,num2; int num1[16]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; char name1[20]; char name2[20]; struct bankdata blankdata={-1,"NONAME",0}; struct bankdata data; printf("Enter delete name.\n"); scanf("%s",name1); for(num=0;num<=15;num++){ fseek(ptr,num*(sizeof(struct bankdata))+sizeof(int),SEEK_SET); fread(name2,20,1,ptr); if(strcmp(name1,name2)==0) num1[num]=num; } for(num=0,num2=0;num<=15;num++){ if(num1[num]!=-1) { fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fread(&data,sizeof(struct bankdata),1,ptr); printf("%-10d%-16s%-10f\n",data.num,data.name,data.balance); num2++; } } if(num2>1){ printf("Which account do you want to delete?\n"); scanf("%d",&num); fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fwrite(&blankdata,sizeof(struct bankdata),1,ptr); } if(num2==1){ fseek(ptr,data.num*sizeof(struct bankdata),SEEK_SET); fwrite(&blankdata,sizeof(struct bankdata),1,ptr); } if(num2==0) printf("There is no your name.\n"); } modify(FILE *ptr)/*修改帐目*/ {int num,num2; int num3[16]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; float num1; char name1[20]; char name2[20]; struct bankdata data; printf("Enter modify name.\n"); scanf("%s",name1); for(num=0;num<=15;num++){ fseek(ptr,num*(sizeof(struct bankdata))+sizeof(int),SEEK_SET); fread(name2,20,1,ptr); if(strcmp(name1,name2)==0) num3[num]=num; } for(num=0,num2=0;num<=15;num++){ if(num3[num]!=-1) { fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fread(&data,sizeof(struct bankdata),1,ptr); printf("%-10d%-16s%-10f\n",data.num,data.name,data.balance); num2++; } } if(num2>1){ printf("Which account do you want to modify?\n"); scanf("%d",&num); fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fread(&data,sizeof(struct bankdata),1,ptr); printf("Enter charge(+) or payment(-)\n"); scanf("%f",&num1); data.balance+=num1; fseek(ptr,num*sizeof(struct bankdata),SEEK_SET); fwrite(&data,sizeof(struct bankdata),1,ptr); } if(num2==1){ printf("Enter charge(+) or payment(-)\n"); scanf("%f",&num1); data.balance+=num1; fseek(ptr,data.num*sizeof(struct bankdata),SEEK_SET); fwrite(&data,sizeof(struct bankdata),1,ptr); } if(num2==0) printf("There is no your name.\n"); } main() {FILE *fptr; int choice=0; if((fptr=fopen("d:\\account.txt","r++"))==NULL) printf("File couldn't be opened.\n"); else{ while(choice!=5){ printf("\nEnter your choice\n"/*菜单*/ "0-create account\n" "1-view account\n" "2-add account\n" "3-delete account\n" "4-modify account\n" "5-quit\n" ); scanf("%d",&choice); switch(choice){ case 0:create(fptr);break; case 1:view(fptr);break; case 2:add(fptr);break; case 3:delete(fptr);break; case 4:modify(fptr);break; } } } fclose(fptr); return(0); }
| | |