C语言银行账户日常管理作业附源码Word文件下载.docx
- 文档编号:22434310
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:15
- 大小:17.38KB
C语言银行账户日常管理作业附源码Word文件下载.docx
《C语言银行账户日常管理作业附源码Word文件下载.docx》由会员分享,可在线阅读,更多相关《C语言银行账户日常管理作业附源码Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
8、保存数据和读取文件功能:
voidsave(structaccount*head)。
save函数实现保存功能,退出程序时自动保存数据,
再次执行程序时自动从根目录中读取文件数据。
三、调试过程中遇到的问题及解决方案
一个比较大的问题就是文件的存储和读取问题,在存储和读取过程中会出现好多意想不到的错误,并且很难查找出来。
存储相对比较容易实现,但是读取就比较困难,控制读取的结束就遇到了问题,试过好多方法,例如feof()函数,但是都不能解决问题,最后通过定义一个变量N计录账户数量,保存时将该变量N保存在文件的最开始,当读取是首先将这个变量N读取出来,然后用for循环通过N控制读取结束。
调试过程中还有好多小问题,基本都是由于马虎或者程序不够严谨造成的,这里就不一一列举了。
#include<
stdio.h>
#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)。
account_numnamebalance\n"
===================================\n"
if(head!
=NULL)
do
%-15ld%-10s%-10.2f\n"
p->
account_num,p->
name,p->
balance)。
p=p->
next。
}while(p!
=NULL)。
printf("
loop1:
pressanykeyentermenu......\n"
getch()。
clrscr()。
structaccount*del(structaccount*head,longaccount_num)
structaccount*p1,*p2。
if(head==NULL)
{printf("
\ndatanull!
gotoend。
}
p1=head。
while(account_num!
=p1->
account_num&
&
p1->
next!
p2=p1。
p1=p1->
if(account_num==p1->
account_num)
if(p1==head)head=p1->
elsep2->
next=p1->
delete:
%ld\n"
account_num)。
n--。
else
%ldnotbeenfound!
end:
return(head)。
structaccount*find(structaccount*head,longaccount_num)
account_num:
p1->
account_num)。
key:
key)。
name:
%s\n"
name)。
balance:
%f\n"
structaccount*withdrawal(structaccount*head,longaccount_num)
longkey1。
floatmoney。
pleaseinputkey:
"
scanf("
%ld"
&
key1)。
if(key1==p1->
key){
printf("
howmuchdoyouwant:
%f"
money)。
if(p1->
balance-money>
=0){p1->
balance=p1->
balance-money。
success!
elseprintf("
Youcannotdothis!
}/*if*/
keyerror!
structaccount*saving(structaccount*head,longaccount_num)
floatmoney。
howmuchdoyouwanttosave:
balance+money。
\success!
structaccount*insert(structaccount*head,structaccount*new)
structaccount*p0,*p1,*p2。
p0=new。
if(head==NULL)
head=p0。
p0->
next=NULL。
while((p0->
account_num>
p1->
account_num)&
(p1->
=NULL))
}/*while*/
if(p0->
account_num<
if(head==p1)head=p0。
else
p2->
next=p0。
next=p1。
{
p1->
p0->
}
}/*else*/
n=n+1。
structaccount*order(structaccount*head)
structaccount*p1,*p2,*p0,*p3,*headnew。
intm=0。
headnew=NULL。
while(head!
p0=p2=head。
p1=p0->
while(p1!
p0->
account_num)p0=p1。
if(p0==head)head=p0->
while(p2->
=p0)p2=p2->
p2->
next=p0->
m++。
if(m==1)
headnew=p3=p0。
headnew->
{p3->
p3=p3->
p3->
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)
filewriteerror\n"
p=p->
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("
rb+"
fread(&
if(n==0)gotoloop。
fread(p,sizeof(structaccount),1,fp)。
head=p。
for(j=1。
j<
n。
j++)
{p1=(structaccount*)malloc(LEN)。
fread(p1,sizeof(structaccount),1,fp)。
p->
p=p1。
loop:
fclose(fp)。
LogonNow:
username:
%s"
user)。
\npassword:
password)。
if(password!
=123456||strcmp(user,username)!
=0){
passworderror!
pressanykeyclose......\n"
exit
(1)。
printf("
\n-------------------------\n"
|1-InsertanewAccount|\n"
|2-ListallAccounts|\n"
|3-FindaAccount|\n"
|4-DeleteaAccount|\n"
|5-Withdrawal|\n"
|6-Saving|\n"
|Q-quit|\n"
-------------------------\n"
PleaseinputKey\n"
i=getchar()。
while(i=='
\n'
)i=getchar()。
switch(i)
case'
1'
:
{clrscr()。
printf("
\ninputtheinsertedrecord:
new=(structaccount*)malloc(LEN)。
\n>
>
new->
head=insert(head,new)。
break。
}。
2'
head=order(head)。
print(head)。
case'
3'
Pleaseinputaccount_num:
scanf("
head=find(head,account_num)。
4'
\ninputthedeletednumber:
del_num)。
head=del(head,del_num)。
break。
5'
\ninputtheaccount_num:
head=withdrawal(head,account_num)。
6'
head=saving(head,account_num)。
case'
q'
save(head)。
exit
(1)。
}。
}while(i)/*while*/。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 银行 账户 日常 管理 作业 源码