2014-11-05 69 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

相關問題