银行信贷账户管理任务
银行信贷账户管理任务的基本功能是:
①设计一个可以最多存储100个固定大小记录的信贷处理管理系统。
②系统中可以进行新账户的插入,老账户信息的更新,以及老账户的删除。
③系统可以对账户信息进行按账户编号、用户姓名、余额多少等的从大到小或从小到大排序。
④系统能统计账户余额值,计算余额总数,并显示计算结果。
⑤比较用二进制文件或者文本文件两种形式进行信息存储时文件的大小。
⑥如果系统数据改用顺序文件存储,记录大小不确定,请编程进行账户信息管理,然后与固定大小账户信息的管理比较,理解顺序文件和随机文件数据大小差异和管理操作的区别,以及管理任务实现的异同。
查看代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct LNode *PtrToNode;
typedef struct LNode{
int num;
char name[10];
int balance;
PtrToNode Next;
}*List,*Position;
List CreateNullList(void); //创建链表
void Input(int num,char name[],int balance,List llist); //向链表添加新元素
void Node_free(List llist); //清空链表
int file_size(char* filename); //获取文件名为filename的文件大小
int Length(char* filename); //获取文件名为filename的文件行数
void Print(char* filename); //遍历输出
int Password(); //密码验证
int Menu(); //菜单
void Insert(int num,char name[],int balance); //插入
void Update(int num,int balance,List llist); //更新
void Delete(int num,List llist); //删除
int Seek(int num,char name[],List llist); //查找
void Sort1(List llist); //按账户编号从大到小排序
void Sort2(List llist); //按账户编号从小到大排序
void Sort3(List llist); //按用户姓名从大到小排序
void Sort4(List llist); //按用户姓名从小到大排序
void Sort5(List llist); //按余额多少从大到小排序
void Sort6(List llist); //按余额多少从小到大排序
void Sum(List llist); //统计余额总数
void Text2Bin(char* f1,char* f2); //文本文件转二进制文件
main()
{
FILE *fp1,*fp2;
List llist,llist1;
llist=CreateNullList();
llist1=CreateNullList();
int i,j,temp,m,acc,ch,a,b;
int num,balance;
char name[10];
m=1;
while(m)
{
printf("\n-----------------------------\n");
printf("1:插入\n");
printf("2:更新\n");
printf("3:删除\n");
printf("4:按账户编号从大到小排序\n");
printf("5:按账户编号从小到大排序\n");
printf("6:按用户姓名从大到小排序\n");
printf("7:按用户姓名从小到大排序\n");
printf("8:按余额多少从大到小排序\n");
printf("9:按余额多少从小到大排序\n");
printf("10:统计余额总数\n");
printf("11:比较二进制文件和文本文件存储大小\n");
printf("12:删除文本文件\n");
printf("0:退出\n");
printf("-----------------------------\n");
m=Menu();
switch(m)
{
case 1:
{
if(Password()==1)
{
printf("请输入账户编号 用户姓名 余额:\n");
while(~scanf("%d %s %d",&num,name,&balance))
{
if(Length("test1.txt")!=-1)
{
if(Seek(num,name,llist)==0)
{
if(Length("test1.txt")>=100)
{
printf("账户已满,无法录入\n");
break;
}
else
Insert(num,name,balance);
}
else
{
printf("已有此账户,是否更新?yes:1 or no:0\n");
scanf("%d",&ch);
if(ch==1)
Update(num,balance,llist);
break;
}
}
else
Insert(num,name,balance);
}
}
else
printf("错误过多,退出\n");
break;
}
case 2:
{
if(Password()==1)
{
printf("-----------------------------\n");
Print("test1.txt");
printf("-----------------------------\n");
printf("请输入账户编号 余额:\n");
scanf("%d %d",&num,&acc);
Update(num,acc,llist);
}
else
printf("错误过多,退出\n");
break;
}
case 3:
{
if(Password()==1)
{
printf("-----------------------------\n");
Print("test1.txt");
printf("-----------------------------\n");
printf("请输入账户编号:\n");
scanf("%d",&num);
printf("-----------------------------\n");
printf("yes:1 or no:0\n");
scanf("%d",&ch);
if(ch==1)
Delete(num,llist);
}
else
printf("错误过多,退出\n");
break;
}
case 4:
{
Sort1(llist);
Print("test1.txt");
break;
}
case 5:
{
Sort2(llist);
Print("test1.txt");
break;
}
case 6:
{
Sort3(llist);
Print("test1.txt");
break;
}
case 7:
{
Sort4(llist);
Print("test1.txt");
break;
}
case 8:
{
Sort5(llist);
Print("test1.txt");
break;
}
case 9:
{
Sort6(llist);
Print("test1.txt");
break;
}
case 10:
{
Sum(llist);
printf("-----------------------------\n");
Print("test1.txt");
break;
}
case 11:
{
a=file_size("test1.txt");
printf("文本文件存储大小为%d字节\n",a);
Text2Bin("test1.txt","test2.dat");
b=file_size("test2.dat");
remove("test2.dat");
printf("二进制文件存储大小为%d字节\n",b);
if(a>b)
printf("文本文件大小 > 二进制文件大小\n");
else if(a<b)
printf("文本文件大小 < 二进制文件大小\n");
else
printf("文本文件大小 = 二进制文件大小\n");
break;
}
case 12:
{
remove("test1.txt");
printf("删除成功\n");
break;
}
case 0:
{
printf("退出系统");
break;
}
default:
{
printf("请重新输入:\n");
break;
}
}
}
}
List CreateNullList(void)
{
List llist; //创建结点
llist=(List)malloc(sizeof(struct LNode)); //申请空间
llist->Next=NULL; //头结点
return llist; //用函数值返回
}
void Input(int num,char name[],int balance,List llist) {
List Node;
Node=(List)malloc(sizeof(struct LNode));
Node->num=num;
strcpy(Node->name,name);
Node->balance=balance;
Node->Next=llist->Next; //将头指针所指向的下一个结点的地址,赋给新创建结点的next
llist->Next=Node; //将新创建的结点的地址赋给头指针的下一个结点
}
void Node_free(List llist)
{
Position temp,next;
temp=llist->Next; //指向头结点
while(temp)
{
next=temp->Next; //记录第二个结点地址
free(temp); //释放第一个结点
temp=next; //头结点指向第二个结点
}
llist->Next=NULL;
}
int file_size(char* filename)
{
FILE *fp=fopen(filename,"r");
if(!fp) return -1;
fseek(fp,0L,SEEK_END);
int size=ftell(fp);
fclose(fp);
return size;
}
int Length(char* filename)
{
char s[100];
FILE *fp=fopen(filename,"r");
if(!fp) return -1;
int lines=0;
if(fp)
{
while((fgets(s,100,fp)) != NULL)
if(s[strlen(s)-1]=='\n') lines++;
fclose(fp);
}
return lines;
}
void Print(char* filename)
{
int num,balance;
char name[10];
FILE *fp=fopen(filename,"r");
if(fp == NULL)
{
printf("错误!");
getchar( );
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%d %s %d\n",&num,name,&balance);
printf("%d %s %d\n",num,name,balance);
}
fclose(fp);
}
int Password()
{
char password[]={"021013"};
char code[10];
int i,temp=0;
printf("请输入密码:\n");
for(i=0;i<3;i++)
{
scanf("%s",code);
if(strcmp(code,password)==0)
return 1;
else
printf("请重新输入:\n");
}
return 0;
}
int Menu()
{
char m[10];
scanf("%s",m);
if(strcmp(m,"1")==0)
return 1;
else if(strcmp(m,"2")==0)
return 2;
else if(strcmp(m,"3")==0)
return 3;
else if(strcmp(m,"4")==0)
return 4;
else if(strcmp(m,"5")==0)
return 5;
else if(strcmp(m,"6")==0)
return 6;
else if(strcmp(m,"7")==0)
return 7;
else if(strcmp(m,"8")==0)
return 8;
else if(strcmp(m,"9")==0)
return 9;
else if(strcmp(m,"10")==0)
return 10;
else if(strcmp(m,"11")==0)
return 11;
else if(strcmp(m,"12")==0)
return 12;
else if(strcmp(m,"0")==0)
return 0;
else
return -1;
}
void Insert(int num,char name[],int balance)
{
FILE *fp;
fp=fopen("test1.txt","a+");
if(fp == NULL)
{
printf("错误!");
getchar( );
exit(1);
}
printf("请输入账户编号 用户姓名 余额:\n");
fprintf(fp,"%d %s %d\n",num,name,balance);
fclose(fp);
}
void Update(int num,int balance,List llist)
{
FILE *fp,*ft;
int flag=0;
fp=fopen("test1.txt","rt+");
ft=fopen("test2.txt","wt");
if(fp==NULL || ft==NULL){
printf("错误!\n");
return;
}
while(!feof(fp)){
fscanf(fp,"%d %s %d\n",&llist->num,llist->name,&llist->balance);
while(1){
if(num!=llist->num){
fprintf(ft,"%d %s %d\n",llist->num,llist->name,llist->balance);
break;
}
else
{
flag=1;
fprintf(ft,"%d %s %d\n",llist->num,llist->name,llist->balance+balance);
break;
}
}
}
Node_free(llist);
fclose(fp);
fclose(ft);
remove("test1.txt");
rename("test2.txt","test1.txt");
if(flag==1)
printf("更新成功!\n");
else
printf("无此账户\n");
}
void Delete(int num,List llist)
{
FILE *fp,*ft;
int flag=0;
fp=fopen("test1.txt","r+");
ft=fopen("test2.txt","w");
if(fp==NULL || ft==NULL){
printf("错误!\n");
return;
}
while(!feof(fp)){
fscanf(fp,"%d %s %d\n",&llist->num,llist->name,&llist->balance);
while(1){
if(num!=llist->num){
fprintf(ft,"%d %s %d\n",llist->num,llist->name,llist->balance);
break;
}
else
{
flag=1;
break;
}
}
}
Node_free(llist);
fclose(fp);
fclose(ft);
remove("test1.txt");
rename("test2.txt","test1.txt");
if(flag==1)
printf("删除成功!\n");
else
printf("无此账户\n");
}
int Seek(int num,char name[],List llist)
{
FILE *fp;
int flag=0;
fp=fopen("test1.txt","r+");
if(fp==NULL)
{
printf("错误!\n");
return;
}
while(!feof(fp))
{
fscanf(fp,"%d %s %d\n",&llist->num,llist->name,&llist->balance);
if(num==llist->num&&strcmp(name,llist->name)==0)
{
flag=1;
break;
}
}
Node_free(llist);
fclose(fp);
return flag;
}
void Sort1(List llist)
{
FILE *f1,*f2;
Position ele1,ele2;
int temp1,temp2;
char temp3[10];
f1=fopen("test1.txt","r+");
f2=fopen("test2.txt","w");
if(f1==NULL || f2==NULL){
printf("错误!\n");
return;
}
while(!feof(f1))
{
fscanf(f1,"%d %s %d\n",&temp1,temp3,&temp2);
Input(temp1,temp3,temp2,llist);
}
ele1=llist->Next;
while(ele1)
{
ele2=ele1;
while(ele2->Next)
{
if(ele1->num<ele2->Next->num)
{
temp1=ele1->num;
temp2=ele1->balance;
strcpy(temp3,ele1->name);
ele1->num=ele2->Next->num;
ele2->Next->num=temp1;
ele1->balance=ele2->Next->balance;
ele2->Next->balance=temp2;
strcpy(ele1->name,ele2->Next->name);
strcpy(ele2->Next->name,temp3);
continue;
}
ele2=ele2->Next;
}
fprintf(f2,"%d %s %d\n",ele1->num,ele1->name,ele1->balance);
ele1=ele1->Next;
}
Node_free(llist);
fclose(f1);
fclose(f2);
remove("test1.txt");
rename("test2.txt","test1.txt");
printf("排序成功\n");
}
void Sort2(List llist)
{
FILE *f1,*f2;
Position ele1,ele2;
int temp1,temp2;
char temp3[10];
f1=fopen("test1.txt","r+");
f2=fopen("test2.txt","w");
if(f1==NULL || f2==NULL){
printf("错误!\n");
return;
}
while(!feof(f1))
{
fscanf(f1,"%d %s %d\n",&temp1,temp3,&temp2);
Input(temp1,temp3,temp2,llist);
}
ele1=llist->Next;
while(ele1)
{
ele2=ele1;
while(ele2->Next)
{
if(ele1->num>ele2->Next->num)
{
temp1=ele1->num;
temp2=ele1->balance;
strcpy(temp3,ele1->name);
ele1->num=ele2->Next->num;
ele2->Next->num=temp1;
ele1->balance=ele2->Next->balance;
ele2->Next->balance=temp2;
strcpy(ele1->name,ele2->Next->name);
strcpy(ele2->Next->name,temp3);
continue;
}
ele2=ele2->Next;
}
fprintf(f2,"%d %s %d\n",ele1->num,ele1->name,ele1->balance);
ele1=ele1->Next;
}
Node_free(llist);
fclose(f1);
fclose(f2);
remove("test1.txt");
rename("test2.txt","test1.txt");
printf("排序成功\n");
}
void Sort3(List llist)
{
FILE *f1,*f2;
Position ele1,ele2;
int temp1,temp2;
char temp3[10];
f1=fopen("test1.txt","r+");
f2=fopen("test2.txt","w");
if(f1==NULL || f2==NULL){
printf("错误!\n");
return;
}
while(!feof(f1))
{
fscanf(f1,"%d %s %d\n",&temp1,temp3,&temp2);
Input(temp1,temp3,temp2,llist);
}
ele1=llist->Next;
while(ele1)
{
ele2=ele1;
while(ele2->Next)
{
if(strcmp(ele1->name,ele2->Next->name)<0)
{
temp1=ele1->num;
temp2=ele1->balance;
strcpy(temp3,ele1->name);
ele1->num=ele2->Next->num;
ele2->Next->num=temp1;
ele1->balance=ele2->Next->balance;
ele2->Next->balance=temp2;
strcpy(ele1->name,ele2->Next->name);
strcpy(ele2->Next->name,temp3);
continue;
}
ele2=ele2->Next;
}
fprintf(f2,"%d %s %d\n",ele1->num,ele1->name,ele1->balance);
ele1=ele1->Next;
}
Node_free(llist);
fclose(f1);
fclose(f2);
remove("test1.txt");
rename("test2.txt","test1.txt");
printf("排序成功\n");
}
void Sort4(List llist)
{
FILE *f1,*f2;
Position ele1,ele2;
int temp1,temp2;
char temp3[10];
f1=fopen("test1.txt","r+");
f2=fopen("test2.txt","w");
if(f1==NULL || f2==NULL){
printf("错误!\n");
return;
}
while(!feof(f1))
{
fscanf(f1,"%d %s %d\n",&temp1,temp3,&temp2);
Input(temp1,temp3,temp2,llist);
}
ele1=llist->Next;
while(ele1)
{
ele2=ele1;
while(ele2->Next)
{
if(strcmp(ele1->name,ele2->Next->name)>0)
{
temp1=ele1->num;
temp2=ele1->balance;
strcpy(temp3,ele1->name);
ele1->num=ele2->Next->num;
ele2->Next->num=temp1;
ele1->balance=ele2->Next->balance;
ele2->Next->balance=temp2;
strcpy(ele1->name,ele2->Next->name);
strcpy(ele2->Next->name,temp3);
continue;
}
ele2=ele2->Next;
}
fprintf(f2,"%d %s %d\n",ele1->num,ele1->name,ele1->balance);
ele1=ele1->Next;
}
Node_free(llist);
fclose(f1);
fclose(f2);
remove("test1.txt");
rename("test2.txt","test1.txt");
printf("排序成功\n");
}
void Sort5(List llist)
{
FILE *f1,*f2;
Position ele1,ele2;
int temp1,temp2;
char temp3[10];
f1=fopen("test1.txt","r+");
f2=fopen("test2.txt","w");
if(f1==NULL || f2==NULL){
printf("错误!\n");
return;
}
while(!feof(f1))
{
fscanf(f1,"%d %s %d\n",&temp1,temp3,&temp2);
Input(temp1,temp3,temp2,llist);
}
ele1=llist->Next;
while(ele1)
{
ele2=ele1;
while(ele2->Next)
{
if(ele1->balance<ele2->Next->balance)
{
temp1=ele1->num;
temp2=ele1->balance;
strcpy(temp3,ele1->name);
ele1->num=ele2->Next->num;
ele2->Next->num=temp1;
ele1->balance=ele2->Next->balance;
ele2->Next->balance=temp2;
strcpy(ele1->name,ele2->Next->name);
strcpy(ele2->Next->name,temp3);
continue;
}
ele2=ele2->Next;
}
fprintf(f2,"%d %s %d\n",ele1->num,ele1->name,ele1->balance);
ele1=ele1->Next;
}
Node_free(llist);
fclose(f1);
fclose(f2);
remove("test1.txt");
rename("test2.txt","test1.txt");
printf("排序成功\n");
}
void Sort6(List llist)
{
FILE *f1,*f2;
Position ele1,ele2;
int temp1,temp2;
char temp3[10];
f1=fopen("test1.txt","r+");
f2=fopen("test2.txt","w");
if(f1==NULL || f2==NULL){
printf("错误!\n");
return;
}
while(!feof(f1))
{
fscanf(f1,"%d %s %d\n",&temp1,temp3,&temp2);
Input(temp1,temp3,temp2,llist);
}
ele1=llist->Next;
while(ele1)
{
ele2=ele1;
while(ele2->Next)
{
if(ele1->balance>ele2->Next->balance)
{
temp1=ele1->num;
temp2=ele1->balance;
strcpy(temp3,ele1->name);
ele1->num=ele2->Next->num;
ele2->Next->num=temp1;
ele1->balance=ele2->Next->balance;
ele2->Next->balance=temp2;
strcpy(ele1->name,ele2->Next->name);
strcpy(ele2->Next->name,temp3);
continue;
}
ele2=ele2->Next;
}
fprintf(f2,"%d %s %d\n",ele1->num,ele1->name,ele1->balance);
ele1=ele1->Next;
}
Node_free(llist);
fclose(f1);
fclose(f2);
remove("test1.txt");
rename("test2.txt","test1.txt");
printf("排序成功\n");
}
void Sum(List llist)
{
FILE *f1,*f2;
Position ele1,ele2,temp;
int temp1,temp2;
char temp3[10];
f1=fopen("test1.txt","r+");
f2=fopen("test2.txt","w");
if(f1==NULL || f2==NULL){
printf("错误!\n");
return;
}
while(!feof(f1))
{
fscanf(f1,"%d %s %d\n",&temp1,temp3,&temp2);
Input(temp1,temp3,temp2,llist);
}
ele1=llist->Next;
while(ele1)
{
ele2=ele1;
while(ele2->Next)
{
if(ele1->num==ele2->Next->num)
{
ele1->balance=ele1->balance+ele2->balance;
temp=ele2->Next; //令temp等于找到的相等的数的地址
ele2->Next=temp->Next; //使链表跳过删除点
free(temp); //释放内存空间
continue;
}
else
ele2=ele2->Next;
}
fprintf(f2,"%d %s %d\n",ele1->num,ele1->name,ele1->balance);
ele1=ele1->Next;
}
Node_free(llist);
fclose(f1);
fclose(f2);
remove("test1.txt");
rename("test2.txt","test1.txt");
printf("统计成功\n");
}
void Text2Bin(char* f1,char* f2)
{
int temp1,temp2;
char temp3[10];
FILE *fp1=fopen(f1,"r");
FILE *fp2=fopen(f2,"wb+");
if(fp1==NULL || fp2==NULL){
printf("错误!\n");
return;
}
while(!feof(fp1))
{
fscanf(fp1,"%d %s %d\n",&temp1,temp3,&temp2);
fprintf(fp2,"%d %s %d\n",temp1,temp3,temp2);
}
fclose(fp1);
fclose(fp2);
}