c语言之初建银行系统

初次使用c语言创建银行系统
/*说明:
      在运行前,需要先建立passwor.txt文件,其中存储一个整数,表示密码,建立文件balance.txt,其中存储一个整数,表示余额,
      运行结束后,对数据所做的所有修改均可以保存在文件中,
需要注意的是,这只是一个简单的程序,还可以做进一步的改进:
    1、银行应该是多用户,而绝不可能是单个用户;
    2、应该要记录详细的业务过程;
    3、增加转账、挂失等功能;
    4、密码的加密存储以及密码输入隐藏;
    5、在程序的结构方面,100多行的程序放在一个main函数中,已经显得复杂了。*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int iPass;//用户验证时输入密码
   int iPassInFile;//记录正确的密码
   int iTry=0;//进入系统尝试的次数
   int iNewPass1,iNewPass2;//用于修改密码
   int iChoice;//用于选择系统功能
   double dBalance;//账户余额
   double dMoney;//存取金额
   FILE *fp;//用于文件操作

   printf("Welcome to CSDN bank!\n");
   if((fp=fopen("password.txt","r"))==NULL)
   {
       printf("password file cannot open!");
       exit(0);
   }
   fscanf(fp,"%d",&iPassInFile);
   fclose(fp);//密码放在文件中,先取出



   do
   {
       iTry++;
       printf("Please enter the Password:");
       scanf("%d",&iPass);
       if(iPass==iPassInFile)
        break;
       else
       {
           if(iTry<3)
            printf("You have tried %d times!\n",iTry);
           else
           {
               printf("You cannot enter the bank system!\n");
               exit(0);
           }
       }
   }
   while(1);//进入系统,密码三次不对将退出




   if((fp=fopen("balance.txt","r"))==NULL)
   {
       printf("data file cannot open!");
       exit(0);
   }
   fscanf(fp,"%lf",&dBalance);
   fclose(fp);//从文件中取出余额


   do
   {
       printf("\nYou can manage some business as follows:\n");
       printf("  1.check\n");
       printf("  2.draw\n");
       printf("  3.save\n");
       printf("  4.change password\n");
       printf("  0.quit\n");
       printf("please enter (0-4):");
       scanf("%d",&iChoice);
       switch(iChoice)
       {
       case 1:
        printf("your balance is %.2lf dollars.\n",dBalance);
        break;
       case 2:
        printf("please enter money:");
        scanf("%lf",&dMoney);
        if(dBalance>dMoney)
        {
            dBalance-=dMoney;
            printf("you have %.2lf dollars after drawed!\n",dBalance);

        }
        else
        {
            printf("you have no enough money!\n");
            break;
        }
       case 3:
        printf("please enter the money you want to save:");
        scanf("%lf",&dMoney);
        dBalance+=dMoney;
        printf("you have %.2lf dollars!\n",dBalance);
        break;
       case 4:
        printf("please enter new password:");
        scanf("%d",&iNewPass1);
        printf("please enter new password again:");
        scanf("%d",&iNewPass2);
        if(iNewPass1==iNewPass2)
        {
            iPassInFile=iNewPass1;
            printf("success!please keep secret!\n");
        }
        else
        {
            printf("two times not same,failed!");
        }
        break;
       case 0:
        printf("welcome next time!\n");
       }
   }
   while(iChoice);//办理业务



   if((fp=fopen("balance.txt","w"))==NULL)
   {
       printf("data file cannot open!");
       exit(0);
   }
   fprintf(fp,"%f",dBalance);
   fclose(fp);//保存余额



   if((fp=fopen("password.txt","w"))==NULL)
   {
       printf("password file cannot open!");
       exit(0);
   }
   fprintf(fp,"%d",iPassInFile);
   fclose(fp);//保存密码(可能修改过)

   return 0;

}
第一次创建的系统,只是使用的一个main函数,使单个主函数较大,还需要进一步改进。
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页