【C】C++编写的ATM自动取款机模拟程序分析 (1/208)

< 上一篇下一篇 >
本帖地址: 复制地址

修改 回帖 引用 楼主: ..﹎天涯;

用户形象图片

学习c++有一段时间了,前两天有个朋友要我帮她做个模拟ATM自动取款机的程序,于是花了一个晚上写了出来,其实这个程序也很简单,但是我觉得它对于刚学c++的人来说比较有用处,因为它可以帮助你更加深刻的理解面向对象程序设计的真谛-------以现实世界为模型编写程序。学习c++的真正目的也就在于此,真正的理解面向对象程序设计!

  

  // ************************************

  // * *

  // * function.h *

  // * *

  // ************************************

  

  #include<IOSTREAM.H>

  

  

  class consumer;

  

  class ATM

  // ATM取款机

  {

  public:

   ATM(consumer& cn):cnsm(cn)

   {

   }

   void welcome();

   // 登陆界面

   bool check_passwd(char n[],char pwd[]);

   // 核对密码

   void change_passwd();

   // 修改密码

   void fetchmoney();

   // 取款

   void information();

   // 查询信息

   void exitATM();

   // 退出系统

   void functionshow();

   // 功能界面

   void lock();

   // 锁机

  private:

   int times;

   // 记录密码输入次数

   consumer& cnsm;

  

  };

  

  class consumer

  // 用户

  {

  public:

   friend class ATM;

   consumer(char Name[],char Num[],

   float Money,char Password[]);

  protected:

   char* get_name();

   // 取得姓名

   char* get_num();

   // 取得卡号

   char* get_passwd();

   // 取得密码

   float get_money();

   // 取得余额

   void set_passwd(char pwd[]);

   // 设置密码

   void set_money(float m);

   // 取钱

  private:

   char passwd[8];

   // 用户密码

   char name[20];

   // 用户姓名

   char num[20];

   float money;

  };

  



  // ************************************

  // * *

  // * consumer类的成员函数 *

  // * *

  // ************************************

  

  #include"function.h"

  #include<STRING.H>

  

  consumer::consumer(char Name[],

  char Num[],float Money,char Password[])

  {

   strcpy(name,Name);

   strcpy(num,Num);

   money=Money;

   strcpy(passwd,Password);

  }

  

  float consumer::get_money()

  {

   return money;

  }

  

  char* consumer::get_name()

  {

   return name;

  }

  

  char* consumer::get_num()

  {

   return num;

  }

  

  char* consumer::get_passwd()

  {

   return passwd;

  }

  

  void consumer::set_money(float m)

  {

   money-=m;

  }

  

  void consumer::set_passwd(char pwd[])

  {

   strcpy(passwd,pwd);

  }

  



  // ************************************

  // * *

  // * ATM类的成员函数 *

  // * *

  // ************************************


  #include "function.h"

  #include <STRING.H>

  #include<STDLIB.H>

  

  void ATM::welcome()

  {

   times=0;

   cout<<"$

   欢迎使用若雪银行ATM自动取款机!~!

   "<<ENDL;

  

   char pwd[8],num[20],ch;

   int i=0;

   do

   {

   i=0;

   cout<<?XML:NAMESPACE PREFIX = ENDL<<"请输入卡号 /><ENDL<<"请输入卡号:";
</ENDL<<"请输入卡号:";>
   do

   {

   cin.get(ch);

   num[i++]=ch;

   }while(ch!='\n');

   num[i-1]='\0';

  

   i=0;

   cout<<"请输入密码:";

   do

   {

   cin.get(ch);

   pwd[i++]=ch;

   }while(ch!='\n');

   pwd[i-1]='\0';

  

   if(!check_passwd(num,pwd))

   {

   cout<<"你输入的卡号或密码有误,

   请重新输入"<<ENDL;

   times++;

   }

   else

   {

   functionshow();

   }

   }while(times<3);

   lock();

  }

  

  bool ATM::check_passwd(char num[],

  char pwd[])

  {

   if(strcmp(num,cnsm.get_num())==0&&strcmp

   (pwd,cnsm.get_passwd())==0)

   return true;

   else

   return false;

  }

  

  void ATM::functionshow()

  {

   int n;

  

   do

   {

   cout<<ENDL<<"请你输入相应的操作序号进行操作:

   "<<ENDL;

   cout<<"1) 修改密码 "<<ENDL

   <<"2) 取款 "<<ENDL

   <<"3) 查询余额 "<<ENDL

   <<"4) 退出系统 "<<ENDL;

   cout<<"$ >\\";

   cin>>n;

   while(n<1  n>4)

   {

   cout<<"请输入正确的操作序号!"<<ENDL;

   cout<<"$ >\\";

   cin>>n;

   }

  

   switch(n)

   {

   case 1: change_passwd();

   break;

   case 2: fetchmoney();

   break;

   case 3: information();

   break;

   case 4: exitATM();

   break;

   }

  

   }while(true);

  

  

  

  }

  

  void ATM::change_passwd()

  {

   char pwd[8],repwd[8];

  

   times=0;

   do

   {

   cout<<ENDL<<"请输入旧密码:";

   cin>>pwd;

   if(!check_passwd(cnsm.get_num(),pwd))

   times++;

   else

   break;

   }while(times<3);

  

   if(times==3)

   lock();

  

   int t=0;

   do

   {

   cout<<"请输入新密码:";

   cin>>pwd;

   cout<<"请再输入一次新密码:";

   cin>>repwd;

   if((t=strcmp(pwd,repwd))!=0)

   cout<<"你输入的两次密码不一样,

   请重新输入!"<<ENDL;

   }while(t!=0);

  

   cnsm.set_passwd(pwd);

   cout<<"密码修改成功,请牢记!"<<ENDL;

  

  }

  

  void ATM::fetchmoney()

  {

   float m;

   char ch;

   do

   {

  

   cout<<?XML:NAMESPACE PREFIX = ENDL<<"你要取多少钱 /><ENDL<<"你要取多少钱:"
</ENDL<<"你要取多少钱:">
   <<"\n$>\\"<<ENDL

   cin>>m;

   while(m<=0)

   {

   cout<<"请输入正确的数字!"<<ENDL;

   cout<<"$ >\\ ";

   cin>>m;

   }

  

   if(cnsm.get_money()-m<0)

   {

   cout<<"对不起,你的余额不足!"

   <<ENDL;

   }

   else

   {

   cout<<ENDL<<"操作成功,请收好钱!"

   <<ENDL;

   cnsm.set_money(m);

   }

   cout<<"是否要继续该项操作:(Y/N) "

   <<ENDL;

   cout<<"$ >\\ ";

   cin>>ch;

   while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y')

   {

   cout<<"$ >\\";

   cin>>ch;

   }

  

   }while(ch=='y'  ch=='Y');

  

  }

  

  void ATM::information()

  {

   cout<<"**********************************"<<ENDL;

   cout<<"*"<<ENDL;

   cout<<"* 用户姓名:"<<CNSM.GET_NAME()<<ENDL;

   cout<<"* 卡号: "<<CNSM.GET_NUM()<<ENDL;

   cout<<"* 余额: "<<CNSM.GET_MONEY()<<ENDL;

   cout<<"**********************************"<<ENDL;

  

  }

  

  void ATM::lock()

  {

   cout<<ENDL<<"对不起,由于你的操作有误,

   你的卡已经被没收! "<<ENDL;

   exit(1);

  }

  

  void ATM::exitATM()

  {

   cout<<ENDL<<"感谢你对本银行的支持,

   欢迎下次光临!"<<ENDL;

   cout<<"请取卡……"<<ENDL;

   exit(0);

  }

  

  

  

  

  // ************************************

 

回到帖子顶部

回帖 引用 1楼-

用户形象图片

只是空有面向对象模型,并没有实质的面向对象方法,
象你的着样的语句写发,只考虑到面向对象,而放弃快速的执行
回到帖子顶部
个人信息
  • 荣誉+3
  • 荣誉+2
  • 荣誉+1
  • 荣誉-1
  • 荣誉-2
  • 荣誉-3
发表留言
  • 文章不错!
  • 精华好文!
  • 支持原创文章!
  • 帖子图文并茂,好!
  • 真知灼见,说得好!
  • 恶意广告
  • 违规内容
  • 严重灌水
  • 重复发帖
  • 标题党
你确定要删除此楼层吗
扣20点经验值

快速回复进入高级回复

插入图片 选择表情

验证码 看不清?换一张(不区分大小写)

[完成后按Ctrl+Enter发表]
[回复须知]