gpt4 book ai didi

c++ - 使用现有代码在数组中分配数据

转载 作者:行者123 更新时间:2023-11-28 06:58:16 25 4
gpt4 key购买 nike

这个程序的目的是显示交易和余额。有点像这张照片。 enter image description here目前我正在努力创建 RecordDeposit 和 RecordWithdraw 函数。我不知道如何将 Transaction 参数存储到 TransactionList 数组中。 Balance 和 Amount 来自不同的结构体,如何结合这两个变量并计算结果显示?

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Customer
{
string CustomerName;
string UserID;
string Pin;
};

struct Account
{
Customer Holder;
string AccountNo;
double Balance;
double TotalDeposit;
double TotalWithdrawal;
Transaction Trans;
Transaction TransactionList[100];
int TransactionCount;
};

struct Transaction
{
string TransactionDate;
string TransactionDescription;
double TransactionAmount;
};
Customer* CreateCustomer(const string& name, const string& id, const string& pin);

Transaction* CreateTransaction(const string& date, const string& description, const double& amount);

Account* CreateAccount(const Customer& holder, const string& number, const string& date, const double&balance, const double& deposit, const double& withdraw);

void RecordDeposit(Account* account, Transaction* transaction);

void RecordWithdraw(Account* account, Transaction* transaction);

void PrintReport(Account* account);


int main()
{
Customer* Mary = CreateCustomer("Mary Jones", "235718", "5074");
Customer* John = CreateCustomer("John Smith", "375864", "3251");

Account* MaryAccount = CreateAccount(*Mary, "06-3121-10212357", "01/03/2014", 100, 100, 0);
Account* JohnAccount = CreateAccount(*John, "06-3121-10213758", "10/03/2014", 0, 0, 0);

RecordDeposit(MaryAccount, CreateTransaction("02/03/2014", "Deposit", 90) );
RecordWithdraw(MaryAccount, CreateTransaction("04/03/2014", "ATM Withdrawal", 150) );
RecordDeposit(MaryAccount, CreateTransaction("05/03/2014", "Deposit", 20) );
RecordWithdraw(MaryAccount, CreateTransaction("05/03/2014", "Withdraw", 100) );
RecordWithdraw(MaryAccount, CreateTransaction("05/03/2014", "Withdraw", 50) );
RecordDeposit(JohnAccount, CreateTransaction("11/03/2014", "Deposit", 20) );
RecordDeposit(JohnAccount, CreateTransaction("12/03/2014", "Deposit", 80) );
RecordWithdraw(JohnAccount, CreateTransaction("12/03/2014", "Withdraw", 50) );

return 0;
}

Customer* CreateCustomer(const string& name, const string& id, const string& pin)
{
Customer *c = new Customer;

c->CustomerName = name;
c->UserID = id;
c->Pin = pin;

return c;
}

Transaction* CreateTransaction(const string& date, const string& description, const double& amount)
{
Transaction *t = new Transaction;

t->TransactionDate = date;
t->TransactionDescription = description;
t->TransactionAmount = amount;

return t;
}

Account* CreateAccount(const Customer& holder, const string& number, const string& date, const double&balance, const double& deposit, const double& withdraw)
{
Account *a = new Account;

a->Holder = holder;
a->AccountNo = number;
a->Trans.TransactionDate = date;
a->Balance = balance;
a->TotalDeposit = deposit;
a->TotalWithdrawal = withdraw;

return a;
}

void RecordDeposit(Account* account, Transaction* transaction)
{
}

void RecordWithdraw(Account* account, Transaction* transaction)
{
}


void PrintReport(Account* account)
{
}

最佳答案

我复制并粘贴了你的代码,但它没有编译。您需要将 Transaction 结构移动到 Account 结构之前,因为它使用的是 transaction 结构中的内容。

现在回答你的问题,如果我可以帮助它存储交易,我不会使用数组,我会使用 Vector。原因是对于数组,您必须确保其中有足够的空间来输入新事务,然后导航到您拥有的下一个空闲槽。这是相当多的代码。

但是使用 vector 你可以做这样的事情:

struct Transaction
{
// change this from an array to a vector.
std::vector<Transaction> transactionList;
};

void RecordDeposit(Account* account, Transaction* transaction)
{
account->transactionList.push_back(transaction);
}

对于 vector ,您调用 push_back 它将把它放在下一个空闲空间中。无需不断遍历数组并检查下一个可用空间。 Vectors 也可以随着数据的增长而增长。

希望这对您有所帮助。

关于c++ - 使用现有代码在数组中分配数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22886203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com