采用链表存储的银行系统

来源:互联网 发布:leoweather.js 官网 编辑:程序博客网 时间:2024/05/06 23:00
//银行.h
  1: #include <stdio.h>
  2: #include <stdlib.h>
  3: #include <string.h>
  4: #include <time.h>
  5: #include <conio.h>
  6: #define FailureBeep() _beep(2000,300)
  7: #define SucceedBeep() _beep(600,500)
  8: #define NameLength 20        //用户名最多20个英文字母或10个汉字
  9: #define DEPOSIT 1            //存款
 10: #define DRAW -1                //取款
 11: #define MAX 200    
 12: #define DataFileName "data.jo"
 13: #define ReadMode "rb"
 14: #define WriteMode "wb"
 15: /*
 16:  *HOME键71 244
 17:  *pageup键 73 244
 18:  *pagedown键 81 244
 19:  *End键 79 244
 20:  */
 21: #define HOME 71
 22: #define PAGEUP 73
 23: #define PAGEDOWN 81
 24: #define END 79
 25: #define SKEY 224
 26: #define RETKEY '/r'
 27: int UserCont=0;                //总用户数
 28: typedef struct HistoryNode
 29: {
 30:     int year,month,day;            //该次记录的日期
 31:     int hour,minute,second;        //该次记录的时间
 32:     double money,blance;        //该次记录的交易金额和当前余额
 33:     int Operation;                //该次记录的交易类型
 34:     struct HistoryNode * next;        //下一个记录节点
 35: }*HistoryList,HistoryNode;
 36: typedef struct UserNode 
 37: {
 38:     char name[NameLength] ;            //存储用户名
 39:     unsigned int userAccount;        //存储用户账号
 40:     double blance;                    //存储用户余额
 41:     struct date            //存储开户日期
 42:     {
 43:         int year,month,day;
 44:     }date;
 45:     char password[36];                //存储用户密码
 46:     HistoryList userHistoryHead,rHistory;        //用户历史交易记录的链表
 47:     int HistoryTop;                    //用户历史交易笔数
 48:     struct UserNode * next;                //下一个用户节点
 49: }UserNode ,* UserList;
 50: UserList UserHead;
 51: UserList rUser;
 52: FILE * datafile;
 53: void InitEnvironmen();
 54: int printHead();
 55: int printHead2();
 56: UserList GetUser();
 57: UserList NewUserNode();
 58: void AddUser(UserList user);
 59: void Deposit();
 60: void Draw();
 61: void Query();
 62: void CheckHistory();
 63: void EXIT();
 64: int printHead()
 65: {
 66:     int length=70;
 67:     int i;
 68:     system("cls");
 69:     system("mode con:lines=30 cols=80");
 70:     printf("      ");     
 71:     for(i=0;i
 72:         printf("*");
 73:     puts("");printf("      ");
 74:     printf("*");
 75:     for(i=0;i
 76:         printf(" ");
 77:     puts("*");printf("      ");
 78:     printf("*");
 79:     for(i=0;i
 80:         printf(" ");
 81:     puts("*");printf("      ");
 82: 
 83:     puts("*/t/t  ①开户  ②存款  ③取款  ④查询  ⑤交易记录 ⑥退出        *");
 84:     printf("      ");
 85:     printf("*");
 86:     for(i=0;i
 87:         printf(" ");
 88:     puts("*");printf("      ");
 89:     printf("*");
 90:     for(i=0;i
 91:         printf(" ");
 92:     puts("*");printf("      ");
 93:     for(i=0;i
 94:         printf("*");
 95:     puts("");
 96:     
 97:     printf("请按下你想要进行的操作的数字键:");
 98:     return getche();
 99: }
100: 
101: int printHead2()
102: {
103:     int length=53;
104:     int i;
105:     system("cls");
106:     system("mode con:lines=30 cols=64");
107:     printf("      ");
108:     for(i=0;i
109:         printf("*");
110:     puts("");
111:     printf("      ");printf("*");
112:     for(i=0;i
113:         printf(" ");
114:     puts("*");
115:     printf("      ");printf("*");
116:     for(i=0;i
117:         printf(" ");
118:     puts("*");
119: 
120:     printf("      ");puts("*/t/t ①按账号       ②按账户名    /t/t  *");
121:     printf("      ");printf("*");
122:     for(i=0;i
123:         printf(" ");
124:     puts("*");
125:     printf("      ");printf("*");
126:     for(i=0;i
127:         printf(" ");
128:     puts("*");printf("      ");
129:     for(i=0;i
130:         printf("*");
131:     puts("");
132: 
133:     printf("请按下你想要进行的操作的数字键:");
134: 
135: 
136:     return getche();
137: }
138: 
139: void InitEnvironmen()
140: {
141:     int i=0,j=0;
142:     UserList r;
143:     HistoryList rh;
144:     UserHead = (UserList)malloc(sizeof(UserNode));
145:     UserHead ->next = NULL;
146:     UserHead ->HistoryTop = 0;
147:     //UserHead->userHistoryHead = (HistoryList)malloc(sizeof(HistoryNode));
148: //    UserHead->userHistoryHead->next= NULL;
149:     //UserHead->rHistory = UserHead->userHistoryHead;
150:     rUser = UserHead;
151:     fopen_s(&datafile,DataFileName,ReadMode);
152:     if(datafile==NULL)
153:         return ;
154:     else
155:     {
156:         fread(&UserCont,sizeof(int),1,datafile);
157:         i=UserCont;
158:         while(i--)
159:         {
160:             r=(UserList)malloc(sizeof(UserNode));
161:             fread(r,sizeof(UserNode),1,datafile);
162:             r->next = NULL;
163:             r->userHistoryHead = (HistoryList)malloc(sizeof(HistoryNode));
164:             r->userHistoryHead->next= NULL;
165:             r->rHistory = r->userHistoryHead;    
166:             AddUser(r);
167:             j=r->HistoryTop;
168:             while(j--)
169:             {
170:                 rh = (HistoryList)malloc(sizeof(HistoryNode));
171:                 fread(rh,sizeof(HistoryNode),1,datafile);
172:                 //rh->next = NULL;
173:                 rh->next = r->rHistory->next;
174:                 r->rHistory->next = rh;
175:                 r->rHistory = r->rHistory->next;
176:             }
177:         }
178:     }
179:     fclose(datafile);
180: }
181: 
182: UserList GetUser()
183: {
184:     int useraccount;
185:     char username[NameLength];
186:     UserList r=NULL;
187:     int choice;
188: retry_getuser:
189:     choice = printHead2();
190:     if(choice=='1')
191:     {
192:         printf("请输入您的账号:");
193:         scanf("%d",&useraccount);
194:         r = UserHead->next;
195:         while(r)
196:         {
197:             if(r->userAccount==useraccount)
198:                 return r;
199:             else
200:                 r=r->next;
201:         }
202:         return NULL;
203:     }
204:     else if(choice=='2')
205:     {
206:         puts("/n请输入您的账户名: ");
207:         fflush(stdin);
208:         //fgets(username,NameLength,stdin);
209:         scanf("%s",username);
210:         r=UserHead->next;
211:         while(r)
212:         {
213:             if(strcmp(r->name,username)==0)
214:                 return r;
215:             else
216:                 r=r->next;
217:         }
218:         return NULL;
219:     }
220:     else
221:     {
222:         puts("没有对应操作。");
223:         _sleep(4000);
224:         goto retry_getuser;
225:     }
226: }
227: 
228: UserList NewUserNode()
229: {
230:     time_t t;
231:     struct tm*gt;
232:     UserList tmp = (UserList)malloc(sizeof(UserNode));
233:     time(&t);
234:     gt = localtime(&t);
235:     UserCont++;
236:     printf("请输入帐户名:");
237:     scanf("%s",tmp->name);
238:     tmp->blance = 0;
239:     tmp->HistoryTop = 0;
240:     tmp->userAccount = UserCont;
241:     
242:     tmp->date.year = gt->tm_year+1900;
243:     tmp->date.month = gt->tm_mon+1;
244:     tmp->date.day = gt->tm_mday;
245: 
246:     tmp->userHistoryHead = (HistoryList)malloc(sizeof(HistoryNode));
247:     tmp->userHistoryHead->next = NULL;
248:     tmp->rHistory = tmp->userHistoryHead;
249:     return tmp;
250: }
251: 
252: void AddUser(UserList user)
253: {
254:     if(user)
255:     {
256:         user->next = rUser->next;
257:         rUser->next = user;
258:         rUser = user;
259:     }
260: }
261: 
262: void AddHistory(UserList user,HistoryList rt)
263: {
264:     rt->next = user->rHistory->next;
265:     user->rHistory->next = rt;
266:     user->rHistory = user->rHistory->next;
267: }
268: 
269: HistoryList NewHistoryNode(UserList user,double money, int opration)
270: {
271:     HistoryList rt;
272:     time_t t;
273:     struct tm*gt;
274:     rt=(HistoryList)malloc(sizeof(HistoryNode));
275:     time(&t);
276:     gt = localtime(&t);
277:     rt->year = gt->tm_year+1900;
278:     rt->month = gt->tm_mon+1;
279:     rt->day = gt->tm_mday;
280:     rt->hour = gt->tm_hour;
281:     rt->minute = gt->tm_min;
282:     rt->second = gt->tm_sec;
283:     rt->Operation = opration;
284:     rt->money = money;
285:     rt->blance = user->blance;
286:     rt->next = NULL;
287:     user->HistoryTop++;
288:     return rt;
289: }
290: 
291: void Deposit()
292: {
293:     float depositMoney;
294:     int useraccount;
295:     UserList r = GetUser();
296:     if(r)
297:     {
298:         printf("请输入您要存的钱:");
299:         scanf("%f",&depositMoney);
300:         r->blance+=depositMoney;
301:         AddHistory(r,NewHistoryNode(r,depositMoney,DEPOSIT));    
302:     }
303:     else
304:     {
305:         puts("不存在的用户。");
306:         _sleep(4000);
307:         return ;
308:     }
309: }
310: 
311: void Draw()
312: {
313:     float drawMoney;
314:     UserList r = GetUser();
315:     if(r)
316:     {
317:         printf("请输入您要取的钱:");
318:         scanf("%f",&drawMoney);
319:         r->blance-=drawMoney;
320:         AddHistory(r,NewHistoryNode(r,drawMoney,DRAW));
321:     }
322:     else
323:     {
324:         puts("不存在的用户。");
325:         _sleep(400);
326:         return ;
327:     }
328: }
329: 
330: void Query()
331: {
332:     UserList r = GetUser();
333:     if(r)
334:     {
335:         printf("/n/t/t开户日期     账号/t账户名/t账户余额/n"
336:             "/t/t%4d/%2d/%2d/t   %d     %s/t   %g",
337:             r->date.year,r->date.month,r->date.day,r->userAccount,r->name,r->blance);
338:         puts("/n/n/n查询更多详细信息请按5查询交易记录……/n");
339:                                                                                 
340:     }
341:     else
342:     {
343:         puts("不存在的用户。");
344:         _sleep(400);
345:         return ;
346:     }
347: }
348: 
349: void printHistory(HistoryList position,int AllPages,int CurrutPage)
350: {
351:     int i;
352:     printf("    日期        时间    交易类型    交易金额      余额 /n");
353: 
354:         for(i=0;i<10&&position;i++)
355:         {
356:             printf("  %4d-%2d-%2d  %2d:%2d:%2d",
357:                     position->year,
358:                     position->month,
359:                     position->day,
360:                     position->hour,
361:                     position->minute,
362:                     position->second);
363:             if(position->Operation==DEPOSIT)
364:                 printf("    存款        ");
365:             else if(position->Operation==DRAW)
366:                 printf("    取款        ");
367:             printf("%6.1f    %6.1f /n",position->money,
368:                                       position->blance);
369:             
370:             position = position->next;
371:         }
372:         printf("/n/n           共%d页        第%d页/n/n"
373:                 "        PageUp:上一页     PageDown:下一页/n/n       Home:第一页   End:最后一页   Enter:返回/n",
374:                 AllPages,CurrutPage);
375: }
376: 
377: void printUserInfo(UserList r)
378: {
379:     system("cls");
380:     printf("/n/n    /t开户日期    账号    账户名/n"
381:             "    /t%4d/%2d/%2d        %d/t%s/n/n",
382:             r->date.year,r->date.month,r->date.day,r->userAccount,r->name);
383: }
384: 
385: void CheckHistory()
386: {
387:     UserList r = GetUser();
388:     int historypage,i=0,j=0;
389:     HistoryList* hr,rhistory;
390:     int key,skey;
391:     if(r)
392:     {
393:         if(r->HistoryTop>0)
394:         {
395:             historypage = r->HistoryTop/10;
396:             hr = (HistoryList*)malloc((historypage+1)*(sizeof(HistoryNode)));
397:             rhistory = r->userHistoryHead->next;
398:             while(i<=historypage)
399:             {
400:                 hr[i] = rhistory;
401:                 while(j<10&&rhistory->next)
402:                 {
403:                     rhistory = rhistory->next;
404:                     j++;
405:                 }
406:                 j=0;
407:                 i++;
408:             }
409:             i=0;
410:             printUserInfo(r);
411:             printHistory(hr[i],historypage+1,i+1);
412:             while((key = getch())!=RETKEY)
413:             {
414:                 if(key==SKEY)
415:                 {
416:                     if((skey=getch())==HOME)
417:                         i=0;
418:                     else if(skey==END)
419:                         i=historypage;
420:                     else if(skey==PAGEUP)
421:                     {
422:                         if(i>0)
423:                             i--;
424:                         else
425:                         {
426:                             puts("已经是第一页");
427:                             _sleep(1000);
428:                         }
429:                     }
430:                     else if(skey==PAGEDOWN)
431:                     {
432:                         if(i
433:                             i++;
434:                         else
435:                         {
436:                             puts("已经是最后一页");
437:                             _sleep(1000);
438:                         }
439:                     }
440:                     else
441:                     {
442:                         puts("没有对应的操作");
443:                         _sleep(500);
444:                     }
445: 
446:                     
447:                 }
448:                 else
449:                 {
450:                     puts("没有对应操作");
451:                     _sleep(1000);
452:                 }
453: 
454:                 printUserInfo(r);
455:                 printHistory(hr[i],historypage+1,i+1);
456: 
457:             }
458: 
459: 
460:         }
461:         else
462:         {
463:             puts("没有该用户的记录。");
464:             return ;
465:         }
466:     }
467:     else
468:     {
469:         puts("不存在的用户。");
470:         _sleep(400);
471:         return ;
472:     }
473: }
474: 
475: void SaveData()
476: {
477:     FILE * datafile;
478:     int i,j,n,pcont=0;
479:     float m;
480:     fopen_s(&datafile,DataFileName,WriteMode);
481:     if(datafile)
482:     {
483:         i=UserCont;
484:         puts("/n开始保存数据,请稍等……/n");
485:         for(n=0;n<69;n++)
486:             printf("-");
487:         printf("/r");
488:         m=70.0/(float)i;
489:         fwrite(&UserCont,sizeof(int),1,datafile);
490:         while(i--&&UserHead)
491:         {
492:             UserHead = UserHead->next;
493:             fwrite(UserHead,sizeof(UserNode),1,datafile);
494:             j=UserHead->HistoryTop;
495:             UserHead->userHistoryHead = UserHead->userHistoryHead->next;
496:             while(j--&&UserHead->userHistoryHead)
497:             {
498:                 fwrite(UserHead->userHistoryHead,sizeof(HistoryNode),1,datafile);
499:                 UserHead->userHistoryHead=UserHead->userHistoryHead->next;
500:             }
501:             for(n=0;n
502:             {
503:                 if(pcont<70)
504:                 {
505:                     printf("*");
506:                     _sleep(5);
507:                     pcont++;
508:                 }
509:             }
510:             _sleep(40);
511:         }
512:         puts("/n成功保存数据.");
513:         _sleep(500);
514:         return ;
515:     }
516:     else
517:         puts("保存数据失败./n即将自动退出.");
518:     fclose(datafile);
519: }
520: 
521: void EXIT()
522: {
523:     int length=60;
524:     int i;
525:     system("cls");
526:     system("mode con:lines=30 cols=70");
527:     printf("      ");
528:     for(i=0;i
529:         printf("*");
530:     puts("");
531:     printf("      ");printf("*");
532:     for(i=0;i
533:         printf(" ");
534:     puts("*");
535:     printf("      ");printf("*");
536:     for(i=0;i
537:         printf(" ");
538:     puts("*");
539: 
540:     printf("      ");puts("*/t/t ①保存并退出     ②直接退出   ③取消  /t  /t *");
541:     printf("      ");printf("*");
542:     for(i=0;i
543:         printf(" ");
544:     puts("*");
545:     printf("      ");printf("*");
546:     for(i=0;i
547:         printf(" ");
548:     puts("*");printf("      ");
549:     for(i=0;i
550:         printf("*");
551:     puts("");
552: 
553:     printf("/t请按下你想要进行的操作的数字键:");
554:     switch(getch())
555:     {
556:     case '1':
557:         SaveData();
558:         exit(0);
559:     case '2':
560:         puts("警告:直接退出将不保存数据而直接退出,可能造成您的数据丢失!请慎重选择!");
561:         printf("请再次确认:直接退出吗?(Y/N)N/b");
562:         if(getche()=='y')
563:             exit(0);
564:         else
565:             return ;
566:     case '3':
567:         return ;
568:     default: 
569:         break;
570: 
571:     }
572: 
573: }
574: 

//main.c

  1: #include <stdio.H>
  2: #include "银行.h"
  3: int main()
  4: {
  5:     InitEnvironmen();
  6:     while(1)
  7:     {
  8:         switch(printHead())
  9:         {
 10:         case 49: AddUser(NewUserNode());break; 
 11:         case 50: Deposit();break;
 12:         case 51: Draw();break;
 13:         case 52: Query();break; 
 14:         case 53: CheckHistory();break;
 15:         case 54: EXIT();break;
 16:         default: printf("没有对应的操作……");break;
 17:         }
 18: 
 19:         system("pause"); 
 20:     }
 21:     return 0;
 22: }
原创粉丝点击