1

我在 C++ 上做作业,我坚持如何使用用户定义的 numShares 和 pricePerShare 向我的工作添加新事务。

我有一个看起来像这样的事务结构:

struct Transaction
{
string stockSymbol;     // String containing the stock symbol, e.g. "AAPL"
string buyerName;       // String containing the buyer's name e.g. "Mr Brown"
int buyerAccount;       // Integer containing an eight digit account code
int numShares;          // Integer containing the number of sold shares
int pricePerShare;      // Integer containing the buy price per share
};

这是 buildTransaction 类:

static Transaction* buildTransactions(int numTransactions)
{
    int maxShareVolume = 100000;
    int maxSharePrice = 1000;

    Transaction *transactions = new Transaction[numTransactions];

        for(int idx = 0; idx < numTransactions; idx++)
        {
            transactions[idx].stockSymbol = pickRandomStockSymbol();

            std::string buyerName = pickRandomBuyer();
            transactions[idx].buyerName = buyerName;
            transactions[idx].buyerAccount = lookupBuyerAccount(buyerName);

            transactions[idx].numShares = 1 + rand() % maxShareVolume;
            transactions[idx].pricePerShare = 1 + rand() % maxSharePrice;       
        }

        return transactions;
    }

我将如何使用它来将数据添加到事务数组中:

void Analyser::addTransactions(Transaction* transactions, int numTransactions)

我会由此假设,作为用户输入,我真正需要的只是股票数量和每股价格,但其他信息会自动填充,从数组中选择。

4

2 回答 2

2

而不是使用数组,你应该使用向量.. buildTransactions 会这样写:

std::vector<Transaction> buildTransactions(int numTransactions)
{
    int maxShareVolume = 100000;
    int maxSharePrice = 1000;
    std::vector<Transaction> transactions;

    for(int idx = 0; idx < numTransactions; idx++)
    {
        Transaction t;
        t.stockSymbol = pickRandomStockSymbol();
        std::string buyerName = pickRandomBuyer();
        t.buyerName = buyerName;
        t.buyerAccount = lookupBuyerAccount(buyerName);
        t.numShares = 1 + rand() % maxShareVolume;
        t.pricePerShare = 1 + rand() % maxSharePrice;

        transactions.push_back(t);
    }

    return transactions;
}

通过编辑 buildTransactions 函数,您可以轻松地将更多数据添加到您的 addTransactions 函数中:

void Analyser::addTransactions(std::vector<Transaction> &transactions, int numTransactions)
{
    for(int idx = 0; idx < numTransactions; idx++)
    {
        Transaction t;
        t.stockSymbol = pickRandomStockSymbol();
        std::string buyerName = pickRandomBuyer();
        t.buyerName = buyerName;
        t.buyerAccount = lookupBuyerAccount(buyerName);

        std::cout << "Enter number of shares for transaction: ";
        std::cin >> t.numShares;
        std::cout << "Enter price per share for transaction: ";
        std::cin >> t.pricePerShare;

        transactions.push_back(t);
    }
}

希望能帮助到你 :)

于 2013-03-19T09:00:00.830 回答
0

您可以在 addTransactions() 方法的循环中获取股份数量和每股价格作为输入,如下所示:

for(int idx = 0; idx < numTransactions; idx++)
    {
        transactions[idx].stockSymbol = pickRandomStockSymbol();

        std::string buyerName = pickRandomBuyer();
        transactions[idx].buyerName = buyerName;
        transactions[idx].buyerAccount = lookupBuyerAccount(buyerName);

        ctd::cout<<"Enter number of shares for transaction "<<idx+1<<std::endl;
        std::cin>>transactions[idx].numShares;
        ctd::cout<<"Enter price per share for transaction "<<idx+1<<std::endl;
        std::cin>>transactions[idx].pricePerShare;    
    }
于 2013-03-19T07:24:58.667 回答