C++程序 实现交易应用
交易是一个涉及购买和销售货物和服务的主要经济概念,以及买方向卖方支付的补偿。在C ++中建立交易应用程序可以作为了解C ++基本概念的好例子。此外,对于初学者来说,它可以作为迷你项目,帮助他们增强对C ++的理解,并在初始阶段构建他们的简历。
问题陈述:
在C++中构建交易应用程序,其中执行的操作如下:
- 获取帐户信息
- 存款
- 取款
- 购买加密货币
- 出售加密货币
- 检查交易
要执行的操作
交易包括有关利润损失和货币的知识。因此,程序的变量如下:
- 用户余额
- 利润损失检查器
- 比特币和狗币
- 选择
- 取款金额
- 预测
- 总资产净值
现在来看一下我们将执行的操作:
1. 获取帐户信息:
在此操作中,我们只需打印有关用户帐户的信息,即我们要观察的所有变量是利润损失,货币,金额等。
2. 存款:
在存款中,我们可以将钱存入我们的帐户中。
3. 取款:
在取款中,我们可以取出我们帐户中的钱。
4. 购买加密货币:
我们将使用此操作购买加密货币。根据我们的需求,我们可以购买比特币或狗币。
5. 出售加密货币:
我们将出售加密货币,并将售出后获得的金额加入我们的帐户。
6. 检查交易:
在此操作中,我们将打印迄今为止执行的所有交易。
交易应用程序的代码:
# include <iostream>
# include <vector>
using namespace std;
class Account {
private:
string name;
string address;
int account_number;
int dogecoin;
int bitcoin;
int withdraw;
int deposit;
int dogecoin_value;
int bitcoin_value;
vector<int>history;
public:
Account() {
name = "yourname";
address = "youraddress";
account_number = 123456789;
dogecoin = 0;
bitcoin = 0;
withdraw = 0;
deposit = 0;
dogecoin_value = 100;
bitcoin_value = 500;
}
void Get_account_information() {
cout << "Name : " << name << endl;
cout << "Address : " << address << endl;
cout << "Account Number : " << account_number << endl;
cout << "Dogecoin : " << dogecoin << endl;
cout << "Bitcoin : " << bitcoin << endl;
cout << "Withdraw : " << withdraw << endl;
cout << "Deposit : " << deposit << endl;
}
int Deposit(int amount) {
deposit += amount;
return 1;
}
void Withdraw(int amount) {
if (amount < deposit)
withdraw += amount;
}
void History() {
cout << "Transaction History" << endl;
for (int i = 0; i < history.size(); i++)
cout << history[i] << endl;
}
int buy_crypto() {
int type, amount;
cout << "Choose Crypto Type 1: Dogecoin, 2: Bitcoin ";
cin >> type;
cout << "Enter amount to buy ";
cin >> amount;
if (type == 1) {
if (dogecoin_value * amount <= deposit) {
dogecoin += amount;
deposit -= amount * dogecoin_value;
return 1;
}
}
else if (type == 2) {
if (bitcoin_value * amount <= deposit) {
bitcoin += amount;
deposit -= amount * bitcoin_value;
return 1;
}
}
return 0;
}
int sell_crypto() {
int type, amount;
cout << "Choose Crypto Type 1: Dogecoin, 2: Bitcoin ";
cin >> type;
cout << "Enter amount to sell ";
cin >> amount;
if (type == 1) {
if (dogecoin >= amount) {
dogecoin -= amount;
deposit += amount * dogecoin_value;
return 1;
}
}
else if (type == 2) {
if (bitcoin >= amount) {
bitcoin -= amount;
deposit += amount * bitcoin_value;
return 1;
}
}
return 0;
}
};
int main()
{
Account person;
int amount, choice;
bool check;
while (1) {
cout << " "
"******************************************"
"***************************** \n";
cout << endl;
cout << "Press 1 if want to have your Account Info "
<< endl;
cout << "Press 2 if want to Deposit your money "
<< endl;
cout << "Press 3 if want to withdraw your money "
<< endl;
cout << "Press 4 if want to know your history "
<< endl;
cout << "Press 5 if want to know your Buy Crypto "
<< endl;
cout << "Press 6 if want to know your Sell Crypto "
<< endl;
cout << "Else press any invalid key for exit\n"
<< endl;
cout << " "
"******************************************"
"***************************** \n";
cin >> choice;
int ans;
switch (choice) {
case 1:
person.Get_account_information();
break;
case 2:
cout << "Enter amount to deposit : ";
cin >> amount;
ans = person.Deposit(amount);
if (ans)
cout << "Successfully deposited money"
<< endl;
else
cout << "Failed\n";
break;
case 3:
cout << "Enter amount to withdrawn : ";
cin >> amount;
person.Withdraw(amount);
if (ans)
cout << "Successfully withdrawn Amount"
<< endl;
else
cout << "Not Enough Balance\n";
break;
case 4:
person.History();
break;
case 5:
ans = person.buy_crypto();
if (ans)
cout << "Successful Transaction" << endl;
else
cout << "Better Luck next time\n";
break;
case 6:
ans = person.sell_crypto();
if (ans)
cout << "Successful Transaction" << endl;
else
cout << "Not Enough Cryptocoins\n";
break;
default:
exit(0);
break;
}
}
}
输出
***********************************************************************
按 1 查询您的账户信息
按 2 存入资金
按 3 取出资金
按 4 查询您的历史记录
按 5 查询您的加密货币购买记录
按 6 查询您的加密货币出售记录
否则按任意无效键退出
***********************************************************************
输出:
阅读交易应用程序输出1
阅读交易应用程序输出2
阅读交易应用程序输出3