【C语言及程序设计】项目2-15:模块化的简单银行系统设计
生活随笔
收集整理的這篇文章主要介紹了
【C语言及程序设计】项目2-15:模块化的简单银行系统设计
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問題描述:
https://edu.csdn.net/course/play/456/4808
?
// 銀行系統(tǒng).cpp: 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 //#include "stdafx.h" #include <stdlib.h> #pragma warning (disable: 4996)int PassTest(); void ibalance(); void withdraw(); void deposit(); void PassChange();int main() {int ichoice,testresult;printf("歡迎光臨草泥馬銀行\(zhòng)n\n");testresult = PassTest();while (testresult){printf("\n您可辦理下面的業(yè)務(wù):\n");printf(" 1.查詢余額\n");printf(" 2.取款\n");printf(" 3.存款\n");printf(" 4.改密碼\n");printf(" 0.退出\n");printf("請輸入(0-4):");scanf("%d", &ichoice);switch (ichoice){case 1:ibalance(); break; case 2:withdraw(); break;case 3:deposit(); break;case 4:PassChange(); break;default:break;}if (ichoice == 0){break;}}return 0; }int PassTest() {printf("請輸入6位密碼:");while (1){int pass, passstored, itry(1), righttime(0);FILE *fp;fp = fopen("D:\\code\\pass.dat", "r");if (fp == NULL){printf("File cannot open! ");exit(0);}for (int i = 0; i < 6; i++){scanf_s("%1d", &pass);fscanf(fp, "%1d", &passstored);if (pass == passstored){righttime++;}}if (righttime == 6){fclose(fp);return 1;break;}else if (itry<3){itry++;printf("密碼錯(cuò)誤累計(jì)%d次,請重新輸入:", itry - 1);continue;}else{printf("密碼錯(cuò)誤累計(jì)%d次,系統(tǒng)終止\n", itry);fclose(fp);return 0;break;}}}void ibalance() {double balance;FILE *fp;fp = fopen("D:\\code\\balance.dat", "r");if (fp == NULL){printf("File cannot open! ");exit(0);}fscanf(fp, "%lf", &balance);printf("余額:%.2lf\n\n", balance);fclose(fp);}void withdraw() {double cash, balance, diff;printf("請輸入取款額:");scanf("%lf", &cash);FILE *fp;fp = fopen("D:\\code\\balance.dat", "r");if (fp == NULL){printf("File cannot open! ");exit(0);}fscanf(fp, "%lf", &balance);if (balance>cash){diff = balance - cash;fclose(fp);fp = fopen("D:\\code\\balance.dat", "w");fprintf(fp, "%lf", diff);fclose(fp);fp = fopen("D:\\code\\balance.dat", "r");fscanf(fp, "%lf", &balance);printf("現(xiàn)余額:%.2lf\n\n", balance);fclose(fp);}else{printf("取款額大于余額,取款失敗\n\n");fclose(fp);}}void deposit() {double cash, balance, diff;printf("請輸入存款額:");scanf("%lf", &cash);FILE *fp;fp = fopen("D:\\code\\balance.dat", "r");if (fp == NULL){printf("File cannot open! ");exit(0);}fscanf(fp, "%lf", &balance);diff = balance + cash;fclose(fp);fp = fopen("D:\\code\\balance.dat", "w");if (fp == NULL){printf("File cannot open! ");exit(0);}fprintf(fp, "%lf", diff);fclose(fp);fp = fopen("D:\\code\\balance.dat", "r");if (fp == NULL){printf("File cannot open! ");exit(0);}fscanf(fp, "%lf", &balance);printf("現(xiàn)余額:%.2lf\n", balance);fclose(fp);}void PassChange() {while (1){int pass, pass2;printf("請輸入新密碼:");scanf("%d", &pass);if (pass>=100000 && pass <=999999){printf("請?jiān)佥斎胄旅艽a:");scanf("%d", &pass2);if (pass == pass2){FILE *fp;fp = fopen("D:\\code\\pass.dat", "w");fprintf(fp, "%d", pass);fclose(fp);printf("密碼修改成功\n");}break;}else{printf("密碼位數(shù)不足,請輸滿6位\n");continue;}}}
感想:
數(shù)據(jù)寫入硬盤(如寫入文本文檔)似乎是在fclose之后才生效
比如取款后顯示“現(xiàn)余額”,必須這么寫
fp = fopen("D:\\code\\balance.dat", "w");fprintf(fp, "%lf", diff);fclose(fp);fp = fopen("D:\\code\\balance.dat", "r");fscanf(fp, "%lf", &balance);printf("現(xiàn)余額:%.2lf\n\n", balance);fclose(fp);
?
?
?
?
=
轉(zhuǎn)載于:https://www.cnblogs.com/miyazakehime/p/9302200.html
總結(jié)
以上是生活随笔為你收集整理的【C语言及程序设计】项目2-15:模块化的简单银行系统设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 16GB内存只卖3799元!一加Ace
- 下一篇: Steam获史诗级更新:游戏“喜加一”终