使用现有代码分配数组中的数据

时间:2014-04-05 20:10:12

标签: c++ arrays function structure

这个程序的目的是显示交易和平衡。像这样的照片。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)
{
}

1 个答案:

答案 0 :(得分:1)

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

现在回答你的问题,如果我可以帮助它来存储交易,我就不会使用数组,我会使用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);
}

使用向量,您可以调用push_back,它会将其放在下一个可用空间中。没有你不得不经常循环数组并检查下一个空闲空间。此外,随着数据的增长,矢量也会增长。

希望这有帮助。