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