银行账户管理程序 c++实现

新知识点

  • 1.实现四舍五入函数 floor(x); 且为整数floor(x+0.5) 且 保留2位小数 floor(x*100+0.5)/100
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
class savingaccount{
  string  id;
  double  balance;
  int     lastdate;
  double  accumulation;
  float   rate;
  void    record(int date,float amount);
  double  accumulating(int date)
  {
      return accumulation+(date-lastdate)*balance;
  }

  public:
      savingaccount(string id,int lastdate,float rate):id(id),balance(0),lastdate(lastdate),accumulation(0),rate(rate)//参数列表只需传送不定值,剩下的初始化在初始化列表中完成
      {
          cout<<"id:"<<id<<"\t congrulations! it's created!"<<endl;
      }
      void withdraw(int date,int amount);
      void deposit(int date,int amount);
      void benefits(int date);
      void showing();

  };
  void savingaccount::record(int date,float amount)
  {
      accumulation=accumulating(date);
      lastdate=date;
      amount=floor(amount*100+0.5)/100;   // 四舍五入 取整函数   +0.5为了取整
      balance+=amount;
      cout<<"id:"<<id<<"\t  dealing amount:"<<amount<<"\t  balance:"<<balance<<endl;
  }
     void savingaccount::withdraw(int date,int amount)
     {
         if(amount>balance)
            cout<<"Error: saving is not enough";
         else
            record(date,-amount);
     }
     void savingaccount::deposit(int date,int amount)
     {
         record(date,amount);
     }
     void savingaccount::showing()
     {
         cout<<"id:"<<id<<"\t  balance:"<<balance<<endl;
     }
     void savingaccount::benefits(int date)
     {
         double interests=accumulating(date)/365*rate;
         record(date,interests);
         accumulation=0;
      }
int main(void)
{
    savingaccount banker1("001",1,0.015);
    savingaccount banker2("002",1,0.015);
    banker1.deposit(5,5000);
    banker2.deposit(25,10000);
    banker1.deposit(45,5500);
    banker2.withdraw(60,4000);
    banker1.benefits(90);
    banker2.benefits(90);
    banker1.showing();
    banker2.showing();
    return 0;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值