1、C语言 银行账户管理作业附源码银行账户管理系统 1、设计思路用结构体链表来存放个人银行账户的信息,通过静态分配结构体数组的大小,然后通过对链表的插入、查找、删除、排序、保存操作实现账户管理的功能。程序退出时自动将内存中的数据保存到文件中,再次运行时程序会自动从文件中读取数据扫内存中。程序启动时要求输入用户名和密码用户名:admin 密码:1234562、各个功能的实现1、建立账户功能:struct account *insert(struct account *head, struct account *new);该函数通过对链表进行插入操作,从而实现建立账户的功能。2、显示所有账户功能:vo
2、id print(struct account *head);对链表进行输出,列出所有账户。3、按照账号搜索的功能:struct account *find(struct account *head,long account_num);对指针进行遍历,找到符合条件的数据。4、按照账号删除账户:struct account *del(struct account *head,long account_num);对链表进行查找,如有匹配的则执行删除,若没有找的不做任何操作。5、模拟取钱功能:struct account *Withdrawal (struct account *head,long
3、account_num); 用户输入账号,然后对链表进行查找,要求用户输入账户密码,若匹配则可进行取钱(对余额做减法)。6、模拟存钱功能:struct account *saving(struct account *head,long account_num);与取钱类似,对余额做加法。7、排序功能:struct account *order (struct account *head); 根据账号对链表进行排序8、保存数据和读取文件功能:void save(struct account *head);save函数实现保存功能,退出程序时自动保存数据, 再次执行程序时自动从根目录中读取文件数据
4、。三、调试过程中遇到的问题及解决方案一个比较大的问题就是文件的存储和读取问题,在存储和读取过程中会出现好多意想不到的错误,并且很难查找出来。存储相对比较容易实现,但是读取就比较困难,控制读取的结束就遇到了问题,试过好多方法,例如feof()函数,但是都不能解决问题,最后通过定义一个变量N计录账户数量,保存时将该变量N保存在文件的最开始,当读取是首先将这个变量N读取出来,然后用for循环通过N控制读取结束。 调试过程中还有好多小问题,基本都是由于马虎或者程序不够严谨造成的,这里就不一一列举了。#include #define NULL 0#define LEN sizeof(struct acc
5、ount) struct account long account_num; long key; char name20; float balance; struct account *next; ; int n;int *k;void print(struct account *head) struct account *p; p=head; if(head=NULL) printf(NO DATA!n);goto loop1; printf(nNow,These %d accounts are:n,n); printf(account_num name balance n); printf
6、(=n); if(head!=NULL) do printf(%-15ld%-10s%-10.2fn,p-account_num,p-name,p-balance); p=p-next; while(p!=NULL);printf(=n); loop1:printf(press any key enter menu.n); getch(); clrscr(); struct account *del(struct account *head,long account_num) struct account *p1,*p2; if (head=NULL) printf(ndata null!n)
7、; goto end; 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; else p2-next=p1-next; printf(delete:%ldn,account_num); n-; else printf(%ld not been found!n,account_num); printf(press any key enter menu.n); getch(); c
8、lrscr(); end: return(head); struct account *find(struct account *head,long account_num) struct account *p1,*p2; if (head=NULL) printf(ndata null!n); goto end; 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:%ldn,p1-acco
9、unt_num); printf(key:%ldn,p1-key); printf(name:%sn,p1-name); printf(balance:%fn,p1-balance); else printf(%ld not been found!n,account_num); printf(press any key enter menu.n); getch(); clrscr(); end: return(head); struct account *withdrawal(struct account *head,long account_num) struct account *p1,*
10、p2; long key1;float money; if (head=NULL) printf(ndata null!n); goto end; p1=head; while(account_num!=p1-account_num & p1-next!=NULL) p2=p1; p1=p1-next; if(account_num=p1-account_num) printf(please input key:); scanf(%ld,&key1); if(key1=p1-key) printf(how much do you want:); scanf(%f,&money); if(p1-
11、balance-money=0) p1-balance=p1-balance-money ;printf(success!n); else printf(You can not do this!n); /*if*/ else printf(key error!); /*if*/ else printf(%ld not been found!n,account_num); printf(press any key enter menu.n); getch(); clrscr();end: return(head); struct account *saving(struct account *h
12、ead,long account_num) struct account *p1,*p2; float money; if (head=NULL) printf(ndata null!n); goto end; p1=head; while(account_num!=p1-account_num & p1-next!=NULL) p2=p1; p1=p1-next; if(account_num=p1-account_num) printf(how much do you want to save:); scanf(%f,&money); p1-balance=p1-balance+money
13、; printf(success!n);printf(press any key enter menu.n); getch(); clrscr(); /*if*/ else printf(%ld not been found!n,account_num);end: return(head); struct account *insert(struct account *head, struct account *new) struct account *p0,*p1,*p2; p1=head; p0=new; if(head=NULL) head=p0; p0-next=NULL; else
14、while(p0-account_nump1-account_num) & (p1-next!=NULL) p2=p1; p1=p1-next; /* while */ if(p0-account_numaccount_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(press any key enter menu.n); getch(); clrscr(); return(head); s
15、truct account *order (struct account *head) struct account *p1,*p2,*p0,*p3,*headnew ; int m=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+; i
16、f(m=1) headnew=p3=p0; headnew-next=NULL; else p3-next=p0; p3=p3-next; p3-next=NULL; return(headnew); void save(struct account *head) FILE *fp; struct account *p ; p=head; if(fp=fopen(data,wb)=NULL) printf(cannot open filen); return; fwrite(&n,sizeof(int),1,fp); while(p!=NULL) if(fwrite(p,sizeof(stru
17、ct account),1,fp)!=1) printf(file write errorn);p=p-next; fclose(fp); main() struct account *head,*new,*p,*p1; FILE *fp; long del_num,account_num; int j; long password; char i;char user10;char username10=admin; head=NULL; p1=p=( struct account*) malloc(LEN); fp=fopen(data,rb+); fread(&n,sizeof(int),
18、1,fp); if(n=0)goto loop; fread(p,sizeof(struct account),1,fp); head=p; for(j=1;jnext=p1; p=p1; p1-next=NULL; loop:fclose (fp); printf(Logon Now:n); printf(username:); scanf(%s,&user) ; printf(npassword:); scanf(%ld,&password) ; if(password!=123456|strcmp(user,username)!=0) printf(password error!n);
19、printf(press any key close.n); getch(); exit(1); clrscr(); do printf (n -n); printf (| 1-Insert a new Account |n); printf (| 2-List all Accounts |n); printf (| 3-Find a Account |n); printf (| 4-Delete a Account |n); printf (| 5-Withdrawal |n); printf (| 6-Saving |n); printf (| Q-quit |n); printf ( -
20、n); printf (Please input Keyn); i=getchar(); while (i=n) i=getchar(); switch(i) case 1 : clrscr(); printf(ninput the inserted record:); new=(struct account *) malloc(LEN); printf(naccount_num:); scanf(%ld,&new-account_num); printf(nkey:); scanf(%ld,&new-key); printf(nname:); scanf(%s,&new-name); pri
21、ntf(nbalance:); scanf(%f,&new-balance); head=insert(head,new); break; ; case 2 : clrscr(); head=order(head); print (head); break; ; case 3 : clrscr(); printf(Please input account_num:n); scanf(%ld,&account_num); head=find(head,account_num); break; ; case 4 : clrscr(); printf(ninput the deleted numbe
22、r:); scanf(%ld,&del_num); head=del(head,del_num); print (head); break; ; case 5 : clrscr(); printf(ninput the account_num:); scanf(%ld,&account_num); head=withdrawal(head,account_num); break; ; case 6 : clrscr(); printf(ninput the account_num:); scanf(%ld,&account_num); head=saving(head,account_num); break; ;case q : save(head); exit(1); break; ; while (i)/*while*/;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1