码迷,mamicode.com
首页 > 编程语言 > 详细

【C语言及程序设计】项目2-15:模块化的简单银行系统设计

时间:2018-07-12 23:51:58      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:定义   height   span   void   exit   not   while   else   switch   

问题描述:

https://edu.csdn.net/course/play/456/4808

技术分享图片

 

// 银行系统.cpp: 定义控制台应用程序的入口点。
//

#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("欢迎光临草泥马银行\n\n");
	testresult = PassTest();

	while (testresult)
	{
		printf("\n您可办理下面的业务:\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("密码错误累计%d次,请重新输入:", itry - 1);
			continue;
		}
		else
		{
			printf("密码错误累计%d次,系统终止\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("现余额:%.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("现余额:%.2lf\n", balance);

	fclose(fp);

}


void PassChange()
{
	while (1)
	{
		int pass, pass2;
		printf("请输入新密码:");
		scanf("%d", &pass);

		if (pass>=100000 && pass <=999999)
		{
			printf("请再输入新密码:");
			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("密码位数不足,请输满6位\n");
			continue;
		}
		
	}
	
}

  

感想:

数据写入硬盘(如写入文本文档)似乎是在fclose之后才生效

比如取款后显示“现余额”,必须这么写

		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("现余额:%.2lf\n\n", balance);
		fclose(fp);

  

 

 

 

 

=

【C语言及程序设计】项目2-15:模块化的简单银行系统设计

标签:定义   height   span   void   exit   not   while   else   switch   

原文地址:https://www.cnblogs.com/miyazakehime/p/9302200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!