2014-11-05 70 views
0

我有一个类的项目,我不知道这个程序应该使用什么类型的数组。我必须制定一个股票市场计划,用户可以购买,出售和查看股票列表,并检查他们的账户余额。有迹象表明,包含以下数据的两个文本文件:我应该使用什么类型的数组?

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; 
} 
+1

很好,你不想让我们为你做,但你需要在你的问题更具体。你想要在数组中存储什么?它将如何使用? – 2014-11-05 14:59:14

+0

为什么downvote? – Engine 2014-11-05 15:00:42

+2

为什么不创建一个结构体,然后建立一个结构体数组 – 2014-11-05 15:01:43

回答

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)); 
} 
+0

谢谢,这应该会给我一些指导 – Kramer 2014-11-05 15:10:56

+1

即使Microsoft不使用匈牙利符号了。 – sjdowling 2014-11-05 15:35:00

0

可能需要比账户持有人的姓名和余额多一点,所以如果我有我的干员,我会使用一个类的矢量(或地图)作为账户持有人。账户持有人类将持有名称,余额,然后是该持有人持有的股票的矢量(或更好的地图)以及股份数量。喜欢的东西:

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"; 
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个选项所需的所有功能。继续添加更多实用功能/类。

一旦你完成第一个版本。你可以擦亮你的代码,让它运行得更快(增加索引等)或者看起来更好(更好的类)。

+0

谢谢我会尽力使用这个 – Kramer 2014-11-05 15:49:26

相关问题