C语言 银行账户管理作业附源码.docx
《C语言 银行账户管理作业附源码.docx》由会员分享,可在线阅读,更多相关《C语言 银行账户管理作业附源码.docx(13页珍藏版)》请在冰豆网上搜索。
C语言银行账户管理作业附源码
银行账户管理系统
1、设计思路
用结构体链表来存放个人银行账户的信息,通过静态分配结构体数组的大小,然后通过对链表的插入、查找、删除、排序、保存操作实现账户管理的功能。
程序退出时自动将内存中的数据保存到文件中,再次运行时程序会自动从文件中读取数据扫内存中。
程序启动时要求输入用户名和密码
用户名:
admin密码:
123456
2、各个功能的实现
1、建立账户功能:
structaccount*insert(structaccount*head,structaccount*new);该函数通过对链表进行插入操作,从而实现建立账户的功能。
2、显示所有账户功能:
voidprint(structaccount*head);对链表进行输出,列出所有账户。
3、按照账号搜索的功能:
structaccount*find(structaccount*head,longaccount_num);对指针进行遍历,找到符合条件的数据。
4、按照账号删除账户:
structaccount*del(structaccount*head,longaccount_num);对链表进行查找,如有匹配的则执行删除,若没有找的不做任何操作。
5、模拟取钱功能:
structaccount*Withdrawal(structaccount*head,longaccount_num);用户输入账号,然后对链表进行查找,要求用户输入账户密码,若匹配则可进行取钱(对余额做减法)。
6、模拟存钱功能:
structaccount*saving(structaccount*head,longaccount_num);与取钱类似,对余额做加法。
7、排序功能:
structaccount*order(structaccount*head);根据账号对链表进行排序
8、保存数据和读取文件功能:
voidsave(structaccount*head);save函数实现保存功能,退出程序时自动保存数据,
再次执行程序时自动从根目录中读取文件数据。
三、调试过程中遇到的问题及解决方案
一个比较大的问题就是文件的存储和读取问题,在存储和读取过程中会出现好多意想不到的错误,并且很难查找出来。
存储相对比较容易实现,但是读取就比较困难,控制读取的结束就遇到了问题,试过好多方法,例如feof()函数,但是都不能解决问题,最后通过定义一个变量N计录账户数量,保存时将该变量N保存在文件的最开始,当读取是首先将这个变量N读取出来,然后用for循环通过N控制读取结束。
调试过程中还有好多小问题,基本都是由于马虎或者程序不够严谨造成的,这里就不一一列举了。
#include
#defineNULL0
#defineLENsizeof(structaccount)
structaccount
{
longaccount_num;
longkey;
charname[20];
floatbalance;
structaccount*next;
};
intn;int*k;
voidprint(structaccount*head)
{
structaccount*p;
p=head;
if(head==NULL){printf("NODATA!
\n");gotoloop1;}
printf("\nNow,These%daccountsare:
\n",n);
printf("account_numnamebalance\n");
printf("===================================\n");
if(head!
=NULL)
do
{
printf("%-15ld%-10s%-10.2f\n",p->account_num,p->name,p->balance);
p=p->next;
}while(p!
=NULL);
printf("===================================\n");
loop1:
printf("pressanykeyentermenu......\n");
getch();clrscr();
}
structaccount*del(structaccount*head,longaccount_num)
{
structaccount*p1,*p2;
if(head==NULL)
{printf("\ndatanull!
\n");
gotoend;
}
p1=head;
while(account_num!
=p1->account_num&&p1->next!
=NULL)
{
p2=p1;
p1=p1->next;
}
if(account_num==p1->account_num)
{
if(p1==head)head=p1->next;
elsep2->next=p1->next;
printf("delete:
%ld\n",account_num);
n--;
}
else
printf("%ldnotbeenfound!
\n",account_num);
printf("pressanykeyentermenu......\n");
getch();clrscr();
end:
return(head);
}
structaccount*find(structaccount*head,longaccount_num)
{
structaccount*p1,*p2;
if(head==NULL)
{printf("\ndatanull!
\n");
gotoend;
}
p1=head;
while(account_num!
=p1->account_num&&p1->next!
=NULL)
{
p2=p1;
p1=p1->next;
}
if(account_num==p1->account_num)
{
printf("account_num:
%ld\n",p1->account_num);
printf("key:
%ld\n",p1->key);
printf("name:
%s\n",p1->name);
printf("balance:
%f\n",p1->balance);
}
else
printf("%ldnotbeenfound!
\n",account_num);
printf("pressanykeyentermenu......\n");
getch();clrscr();
end:
return(head);
}
structaccount*withdrawal(structaccount*head,longaccount_num)
{
structaccount*p1,*p2;
longkey1;floatmoney;
if(head==NULL)
{printf("\ndatanull!
\n");
gotoend;
}
p1=head;
while(account_num!
=p1->account_num&&p1->next!
=NULL)
{
p2=p1;
p1=p1->next;
}
if(account_num==p1->account_num)
{
printf("pleaseinputkey:
");
scanf("%ld",&key1);
if(key1==p1->key){
printf("howmuchdoyouwant:
");
scanf("%f",&money);
if(p1->balance-money>=0){p1->balance=p1->balance-money;printf("success!
\n");}
elseprintf("Youcannotdothis!
\n");
}/*if*/
elseprintf("keyerror!
");
}/*if*/
else
printf("%ldnotbeenfound!
\n",account_num);
printf("pressanykeyentermenu......\n");
getch();
clrscr();
end:
return(head);
}
structaccount*saving(structaccount*head,longaccount_num)
{
structaccount*p1,*p2;
floatmoney;
if(head==NULL)
{printf("\ndatanull!
\n");
gotoend;
}
p1=head;
while(account_num!
=p1->account_num&&p1->next!
=NULL)
{
p2=p1;
p1=p1->next;
}
if(account_num==p1->account_num)
{
printf("howmuchdoyouwanttosave:
");
scanf("%f",&money);
p1->balance=p1->balance+money;
printf("\success!
\n");printf("pressanykeyentermenu......\n");
getch();
clrscr();
}/*if*/
else
printf("%ldnotbeenfound!
\n",account_num);
end:
return(head);
}
structaccount*insert(structaccount*head,structaccount*new)
{
structaccount*p0,*p1,*p2;
p1=head;
p0=new;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->account_num>p1->account_num)&&(p1->next!
=NULL))
{
p2=p1;
p1=p1->next;
}/*while*/
if(p0->account_num<=p1->account_num)
{
if(head==p1)head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}/*else*/
n=n+1;
printf("\success!
\n");printf("pressanykeyentermenu......\n");
getch();
clrscr();
return(head);
}
structaccount*order(structaccount*head)
{
structaccount*p1,*p2,*p0,*p3,*headnew;
intm=0;
headnew=NULL;
while(head!
=NULL)
{
p0=p2=head;
p1=p0->next;
while(p1!
=NULL)
{
if(p1->account_numaccount_num)p0=p1;
p1=p1->next;
}
if(p0==head)head=p0->next;
else
{
while(p2->next!
=p0)p2=p2->next;
p2->next=p0->next;
}
m++;
if(m==1)
{
headnew=p3=p0;
headnew->next=NULL;
}
else
{p3->next=p0;
p3=p3->next;
p3->next=NULL;
}
}
return(headnew);
}
voidsave(structaccount*head)
{FILE*fp;
structaccount*p;
p=head;
if((fp=fopen("data","wb"))==NULL)
{printf("cannotopenfile\n");
return;
}
fwrite(&n,sizeof(int),1,fp);
while(p!
=NULL){
if(fwrite(p,sizeof(structaccount),1,fp)!
=1)
printf("filewriteerror\n");p=p->next;}
fclose(fp);
}
main()
{
structaccount*head,*new,*p,*p1;
FILE*fp;
longdel_num,account_num;
intj;longpassword;
chari;charuser[10];charusername[10]="admin";
head=NULL;
p1=p=(structaccount*)malloc(LEN);
fp=fopen("data","rb+");
fread(&n,sizeof(int),1,fp);
if(n==0)gotoloop;
fread(p,sizeof(structaccount),1,fp);
head=p;
for(j=1;j{p1=(structaccount*)malloc(LEN);
fread(p1,sizeof(structaccount),1,fp);
p->next=p1;
p=p1;}
p1->next=NULL;
loop:
fclose(fp);
printf("LogonNow:
\n");
printf("username:
");
scanf("%s",&user);
printf("\npassword:
");
scanf("%ld",&password);
if(password!
=123456||strcmp(user,username)!
=0){
printf("passworderror!
\n");
printf("pressanykeyclose......\n");
getch();exit
(1);}
clrscr();
do
{
printf("\n-------------------------\n");
printf("|1-InsertanewAccount|\n");
printf("|2-ListallAccounts|\n");
printf("|3-FindaAccount|\n");
printf("|4-DeleteaAccount|\n");
printf("|5-Withdrawal|\n");
printf("|6-Saving|\n");
printf("|Q-quit|\n");
printf("-------------------------\n");
printf("PleaseinputKey\n");
i=getchar();
while(i=='\n')i=getchar();
switch(i)
{
case'1':
{clrscr();
printf("\ninputtheinsertedrecord:
");
new=(structaccount*)malloc(LEN);
printf("\n>>account_num:
");
scanf("%ld",&new->account_num);
printf("\n>>key:
");
scanf("%ld",&new->key);
printf("\n>>name:
");
scanf("%s",&new->name);
printf("\n>>balance:
");
scanf("%f",&new->balance);
head=insert(head,new);
break;
};
case'2':
{clrscr();
head=order(head);
print(head);
break;
};
case'3':
{clrscr();
printf("Pleaseinputaccount_num:
\n");
scanf("%ld",&account_num);
head=find(head,account_num);
break;
};
case'4':
{clrscr();
printf("\ninputthedeletednumber:
");
scanf("%ld",&del_num);
head=del(head,del_num);
print(head);
break;
};
case'5':
{clrscr();
printf("\ninputtheaccount_num:
");
scanf("%ld",&account_num);
head=withdrawal(head,account_num);
break;
};
case'6':
{clrscr();
printf("\ninputtheaccount_num:
");
scanf("%ld",&account_num);
head=saving(head,account_num);
break;
};
case'q':
{
save(head);
exit
(1);
break;};
};
}while(i)/*while*/;
}