在c ++中向array struct添加元素

时间:2013-03-19 04:54:59

标签: c++ arrays pointers struct

即时通讯在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;
    }

我将如何使用它将数据添加到Transactions数组:

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

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

2 个答案:

答案 0 :(得分:1)

而不是使用数组,你应该使用向量.. 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);
    }
}

希望它有所帮助:)

答案 1 :(得分: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;    
    }