我应该使用什么类型的阵列?

时间:2014-11-05 14:57:36

标签: c++ arrays

我有一个类的项目,我不知道我应该为这个程序使用什么类型的数组。我必须制作股票市场计划,用户购买,出售和查看股票清单并检查其账户余额。有两个文本文件包含以下数据:

Leon1111            5000.00
Wise2222            10000.00
Woo3333             3000.00
White4444           7000.00
Head5555            4000.00

Apple                  AAPL             450.00
Boeing                 BA               75.50
Intel                  INTC             22.30
Rambus                 RMBS             5.55
Sirius                 SIRI             3.15
Skyworks               SWKS             25.35
Xilinx                 XLNX             36.80

这是我到目前为止编写的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    ofstream outStream;
    int option;

    do
    {
        cout << "1) Check Stock Listings " << endl;
        cout << "2) Buy Stock " << endl;
        cout << "3) Sell Stock" << endl;
        cout << "4) Check Account Balance " << endl;
        cout << "5) Quit " << endl << endl;
        cout << "Please select an option : ";
        cin >> option;
        cout << endl;

        if (option == 1)
        {
            fstream CompaniesFile;
            CompaniesFile.open("Companies.txt");
            if (CompaniesFile.is_open())
            {
                string s;
                while (getline(CompaniesFile, s, '\n'))
                {
                    cout << s << endl;
                }
            }
            CompaniesFile.close();
        }

        else if (option == 2)
        {

        }

        else if (option == 3)
        {

        }

        else if (option == 4)
        {
            fstream AccountFile;
            AccountFile.open("Account.txt");
            if (AccountFile.is_open())
            {
                string t;
                while (getline(AccountFile, t))
                {

                    cout << t << endl;
                }
            }
            AccountFile.close();
        }

        else if (option == 5)
        {
            cout << "Program Terminated. Have a nice day!" << endl << endl;
        }

        else
        {
            cout << "Invalid Option entered" << endl;
        }
    }
    while (option != 5);

    return 0;
}

3 个答案:

答案 0 :(得分:1)

class cCompany
{
  std::string myName;
  std::string mySymbol;
  double myPrice;

public:
  cCompany(  const std::string& name,
             const std::string& symbol,
             double price )
  : myName( name ), mySymbol( symbol ), myPrice( price )
  {}
};

std::vector< cCompany > vCompany;

class cAccount
{
  std::string myName
  double myBalance;
public:
  cAccount( const std:string& name, double balance )
  : myName( name ), myBalance( balance )
{}
};

std:vector< cAccount > vAccount;

...

std::string name;
std::string symbol;
double price;
while ( CompaniesFile.good() )
{

    CompaniesFile >> name;
    CompaniesFile >> symbol;
    CompaniesFile >> price;
    vCompany.push_back( cCompany( name, symbol, price ));
}

答案 1 :(得分:0)

对于帐户持有人来说,您可能需要的不仅仅是名称和余额,所以如果我有我的druthers,我会为帐户持有人使用类的向量(或地图)。账户持有人类将持有该人持有的股票的名称,余额和矢量(或甚至更好的地图)以及股份数量。类似的东西:

class AccountHolder{
private:
     std::string name_;
     long long int balance_;  //balance in cents

     //to allow for something like buy("AAPL", 100);
     //  to be implemented as:
     //     void buy(std::string symbol, long int shares)
     //     {
     //         long int price = shares * sharePrice[symbol];
     //         if (balance_ >= price)
     //         {
     //             balance_ -= price;
     //             holdings_[symbol] += shares;
     //         } else
     //             throw(INSUFFICIENT_FUNDS);
     //     }
     std::map<std::string, long long int> holdings_;

public:
     ...
};

对于股票,我会使用地图,因为您只需要知道他们的名字(和/或符号)和价格。也许你可以将密钥作为符号,然后将价值作为价格,另一个符号和全名的地图。通过这种方式,您可以轻松找到股价:您需要做的就是

std::cout << "price of a single share from " << fullName["AAPL"]
          << " is: " << sharePrice["AAPL"] << "\n";

答案 2 :(得分:0)

尝试使您的应用程序更像OO(面向对象)。我的建议首先是你可以创建一些数据结构:

struct user { string name; double balance; }
struct stock { string name; double price; }
struct stockBought { string userName; string stockName; int share; }

然后使用某些东西来保存您的数据,例如,

list<user> users;
list<stock> stocks;
list<stockBought> stockBought;

然后你应该有一个函数来读取这两个文件

readUsersFromFile(users, fUsersFile);
readStocksFromFile(stocks, fStockFile);

那么你应该有一个更新列表的功能

update(users, 3, 1, 4000.0);  //update the users list: 3rd user, 1st column (5000.0 => 4000.0)
add(stockBought, "Leon1111", "AAPL", 1); //leon bought 1 share of AAPL

然后你必须实现5个选项所需的所有功能。继续前进时,添加更多实用功能/类。

完成第一个版本后。你可以改进你的代码,使其运行得更快(添加索引等)或看起来更好(更好的类)。