在c++中向数组结构中添加元素

Adding elements to array struct in c++

本文关键字:添加 元素 结构 c++ 数组      更新时间:2023-10-16

我在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)

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

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

希望有帮助

您可以在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;    
    }