即時在C++上做一項任務,我堅持要如何使用用戶定義的numShares和pricePerShare將新事務添加到我的工作中。在C++中向數組結構添加元素
我有一個交易結構,看起來像這樣:
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)
我會從這個假設,我真的需要作爲用戶輸入wo它是股份數量和每股價格,但其他信息自動填充,從數組中選擇。
@Andrew Glass你想要我們展示一下如何操作這些聲明嗎? – 4pie0 2013-03-19 05:03:07
我只是想知道我可以如何使用buildTransaction類創建一個新的事務的方式,以便當addTransactions函數運行而不是輸出10個單獨的事務時,它會輸出11. – 2013-03-19 08:09:43