-->
当前位置:首页 > 题库 > 正文内容

程序填空题:跨币种转账 - C/C++ 隐藏的实现

Luz2年前 (2022-09-06)题库1111
AccountCNY类表示人民币账户,AccountUSD类表示美元账户,账户余额为私有数据成员。a.transfer(b,100)表示从a账户转出100元(a账户币种)至b账户。当a和b账户的币种相同时,a的余额减少100,b的余额增加相同值;当a、b账户币种不同时,a的余额减少100,但b的余额增加值应进行汇率换算。<br>
请将下述代码补充完整,使其能正常运行。假设汇率为1美元兑6.5元人民币。
c++
#include <iostream>
#include <stdio.h>
using namespace std;

class AccountUSD;
class AccountCNY {
private:
double dBalance {0};
public:
void deposit(double fAmount){ //存款函数

}

double balance(){
return dBalance;
}

bool transfer(AccountCNY& b, double fAmount){
if (dBalance < fAmount)
return false;
dBalance -= fAmount;
b.dBalance +=
return true;
}

bool transfer(, double fAmount);
friend
};

class AccountUSD {
private:
double dBalance {0};
public:
void deposit(double fAmount){

}

double balance(){
return dBalance;
}

bool transfer(AccountUSD& b, double fAmount){
if (dBalance < fAmount)
return false;

b.dBalance += fAmount;
return true;
}

bool transfer(AccountCNY& b, double fAmount);
friend
};

bool AccountCNY::transfer(AccountUSD& b, double fAmount){
if (dBalance < fAmount)
return false;
dBalance -= fAmount;
b.dBalance +=
return true;
}

bool AccountUSD::transfer(AccountCNY& b, double fAmount){
if (dBalance < fAmount)
return false;
dBalance
b.dBalance +=
return true;
}

int main()
{
AccountCNY a, b;
a.deposit(1000); b.deposit(1000);
printf("Deposit CNY 1000 to a & b\n");
printf("CNY Account a: %.2f\n",a.balance());
printf("CNY Account b: %.2f\n",b.balance());

AccountUSD c,d;
c.deposit(1000); d.deposit(1000);
printf("Deposit USD 1000 to c & d\n");
printf("USD Account c: %.2f\n",c.balance());
printf("USD Account d: %.2f\n",d.balance());

a.transfer(b,100);
printf("Transfer CNY 100 from a --> b\n");
printf("CNY Account a: %.2f\n",a.balance());
printf("CNY Account b: %.2f\n",b.balance());

c.transfer(d,100);
printf("Transfer USD 100 from c --> d\n");
printf("USD Account c: %.2f\n",c.balance());
printf("USD Account d: %.2f\n",d.balance());

a.transfer(d,650);
printf("Transfer CNY 650 from a --> d\n");
printf("CNY Account a: %.2f\n",a.balance());
printf("USD Account d: %.2f\n",d.balance());

c.transfer(a,100);
printf("Transfer USD 100 from c --> a\n");
printf("USD Account c: %.2f\n",c.balance());
printf("CNY Account a: %.2f\n",a.balance());

return 0;
}


请注意:函数题只需要提交相关代码片段,不要提交完整程序。


### 感觉不会?  那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)








答案:
第1空:dBalance += fAmount;

第2空:fAmount;

第3空:AccountUSD& b

第4空:class AccountUSD;

第5空:dBalance += fAmount;

第6空:dBalance -= fAmount;

第7空:class AccountCNY;

第8空:fAmount/6.5;

第9空:-= fAmount;

第10空:fAmount*6.5;

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。