# 解答C++作业04

//ChkgAcct.h
//ChkgAcct classes

#ifndef CHKGACCT_H
#define CHKGACCT_H

class ChkgAcct {
public:
ChkgAcct();
void deposit(double amt);
bool checkWithdraw(double amt);
bool atmWithdraw(double amt);
double getBalance();
int  setTrack(int type ,double amt);
//push the type and amt into track
void getTrack(int i,int &cTracktype,double &cTrackamt);
//pop type and amount of number i transaction
private:
double balance;
int Tracktype[51];
//store the type of transaction
double Trackamt[51];
//store the amount

};

#endif

//ChkgAcct.cpp

#include "ChkgAcct.h"
#include<iostream>

ChkgAcct::ChkgAcct(){
balance = 100.0;
for(int i=1;i<=50;i++)
{
Tracktype[i]=0;
Trackamt[i]=0;
}
//initialize the track
}

void ChkgAcct::deposit(double amt){
balance=balance+amt;
}

bool ChkgAcct::checkWithdraw(double amt){
if (amt<=balance) {
balance=balance-amt;
return true;
}
else{
balance=balance-10;
return false;
}
}

bool ChkgAcct::atmWithdraw(double amt){
if (amt<=balance) {
balance=balance-amt-1;
return true;
}
else
return false;
}

double ChkgAcct::getBalance(){
return balance;
}

int ChkgAcct::setTrack(int type ,double amt)
{
static int i=0;
//store the total times of the transaction
i++;
if(i<=50)
{
Tracktype[i]=type;
Trackamt[i]=amt;
}
//if the times of the transaction is less than 50
else
///if is more than 50,ie 51, using push
{
i=50;
for(int j=1;j<50;j++)
{
Tracktype[j]=Tracktype[j+1];
Trackamt[j]=Trackamt[j+1];
}
Tracktype[50]=type;
Trackamt[50]=amt;
}
return i;
}

void ChkgAcct::getTrack(int i,int &cTracktype,double &cTrackamt)
{
cTracktype=Tracktype[i];
cTrackamt=Trackamt[i];
}

//client.cpp
//create 10 account to test

#include "ChkgAcct.h"
#include <iostream>
#include <stdlib.h>//use system() to clear screem
#include <conio.h>//use getch(),putchar()
#include <string>

using namespace std;

int main(){
ChkgAcct client[10];
int accountID;  //the ID of account

int type;   //type of transaction
double amount=0; //amount of transaction

int typeGet;  //type of transaction got from every account
double amountGet; //amount of transaction got from every account

char ch,flag;
string str;   //store input as the string type of amount
///////do start loop
do{
str="";
system("cls"); //function to clear the screem
cout<<"***********************************************"<<endl;
cout<<"Please input your account ID,from 0 to 9 :"<<endl;
cout<<"input:";
//////1 make sure the input is 0 to 9
int i=0;
do{
ch=getch();
if(ch>='0'&&ch<='9')
{
putchar(ch);
i++;
}
}while(i<1);
accountID=ch-'0';
cout<<endl;
//////~1

if(accountID>=0&&accountID<=9)
cout<<"Account "<<accountID<<" ,you have: "
<<client[accountID].getBalance()<<" left."<<endl;

flag='n';  //set the flag to test if the client want to quit

do{
cout<<"***********************************************"<<endl;
cout<<"please choose the type of your transaction: /n"
<<"1、Deposit/n"
<<"2、Checkwithdrawal/n"
<<"3、ATMwithdrawal /n"
<<"4、Check Account/n"
<<"5、Recent 50 transactions"<<endl;
cout<<"6-9、 to logout"<<endl;
cout<<"input:";

//////~2 make sure the input is number
int i=0;
do{
ch=getch();
if(ch>='0'&&ch<='9')
{
putchar(ch);
i++;
}
}while(i<1);
type=ch-'0';
cout<<endl;
//////~2

if(type>=1&&type<=3) {
cout<<"***********************************************"<<endl;
cout<<"input the amount(positive value): "<<endl;
cout<<"input:";
//////3 make sure the first number is not zero
do{
ch=getch();

}while(ch=='0'||ch<='0'||ch>'9');
str=ch;
putchar(ch);
i=1;
/////~3
//////4  make sure input is number ,and enter means finish
do
{
ch=getch();
if(ch>='0'&&ch<='9'&&ch!=13)
{
putchar(ch);
str=str+ch;
i++;
} //ignore other input
}while(ch!=13);
cout<<endl;
//////~4

//////5   translate string to double
amount=0;
for(int j=0;j<i;j++)
amount=amount*10+(str[j]-'0');
///////~5

/////6 check if the client want to cancel
/////   and the input is y/n
cout<<endl<<"are you sure?y/n:"<<endl;

i=0;
do{
ch=getch();
if(ch=='y'||ch=='n')
{
putchar(ch);
i++;
}
}while(i<1);
cout<<endl;
/////~6

if(ch=='n') {
system("cls");
cout<<"you cancel the transaction "<<endl;
continue;
}
}   //end of if

system("cls");
cout<<"***********************************************"<<endl;
switch(type){
case 1:{
client[accountID].deposit(amount);
cout<<"succeed!"<<endl;
cout<<"you Deposit "<<amount<<" $."<<endl; client[accountID].setTrack(1,amount); ///set the track with type and amount } break; case 2:{ if(!client[accountID].checkWithdraw(amount)) { cout<<"fail!"<<endl; cout<<"your account do not have enough money!"<<endl; cout<<"$10.00 service charge is deducted! "<<endl;
client[accountID].setTrack(4,amount);
}
else
{
cout<<"succeed!"<<endl;
cout<<"you withdraw "<<amount<<" $."<<endl; client[accountID].setTrack(2,amount); } } break; case 3:{ if(!client[accountID].atmWithdraw(amount)) { cout<<"fail!"<<endl; cout<<"your account do not have enough money!"<<endl; client[accountID].setTrack(5,amount); } else { cout<<"succeed!"<<endl; cout<<"you withdraw "<<amount<<"$."<<endl;
cout<<"a $1 service charge is deducted "<<endl; client[accountID].setTrack(3,amount); } } break; case 4:{ cout<<"you have: " <<client[accountID].getBalance()<<"$ "
<<" left."<<endl;
}
break;
case 5:{
//////7 output the resent transaction
for(int i=50;i>=1;i--)
{
client[accountID].getTrack(i,typeGet,amountGet);
if(typeGet>0&&typeGet<6)
{
switch (typeGet){
case 1:cout<<i<<"、deposite:";break;
case 2:cout<<i<<"、checkWithdraw:";break;
case 3:cout<<i<<"、amtWithdraw:";break;
case 4:cout<<i<<"、fail checkWithdraw:";break;
case 5:cout<<i<<"、fail atmWithdraw:";break;
}
cout<<amountGet<<" \$."<<endl;
}
}
//////~ 7
}
break;

default:{
//////8  check if the client want to logout
/////    make sure the input is y/n
cout<<endl<<"do you want to logout? y/n"<<endl;
int i=0;
do{
ch=getch();
if(ch=='y'||ch=='n')
{
putchar(ch);
i++;
}
}while(i<1);
flag=ch;
cout<<endl;
/////~ 8
}
break;
}   //end of switch
}while(flag!='y');
////yes means that the client is to logout

}while(true);
///////~do continue loop

return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：解答C++作业04 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)