2011-01-12 61 views
0

嘿大家,
我正在爲我的c + +類小型項目寫一個小應用程序。
這是關於管理電信公司的客戶,線路和服務。
我應該使用一個Consumption類,它應該代表每月服務/線路的消耗。數據結構來跟蹤「消費」

class Consumption 
{ 
private: 
    Ligne* m_line;//the line related to this consumption 
    Service* m_service;//service used by this line 
    int m_month; 
    int m_year; 
    int m_units;//units of m_service used in the m_month of m_year 
public: 
    //some getters and setters 
    double price();//returns m_units * the service's price/unit 
}; 

一條線最多可以使用2個服務/月。
消費將用於爲某一行開具賬單。
我的問題是,跟蹤創建的消費量的最佳方式是什麼?我的意思是什麼數據結構可能是最好的使用?我應該對班級做任何改變嗎?
請注意,我沒有使用文件來存儲任何東西。
任何幫助表示讚賞...

+1

這很大程度上取決於你想要在'Consumption`對象集合上執行什麼操作。你想隨機訪問?你想要插入嗎?收藏上典型的行爲是什麼? – sharptooth 2011-01-12 13:54:27

+0

基本上,當爲某一行製作帳單時,需要運行集合以識別每個消費的行和服務,然後添加與行號匹配的行,並在添加行時確保一切都得到尊重(不超過2個服務/行/月等)..所以我正在尋找最好的方式來組織這 – meno 2011-01-12 15:14:51

回答

0

最好的結構存儲的東西像數組樣式與保持恆定訪問時間任何元素是矢量。它就像你有動態大小的數組。

第二個選擇是deque。如果你計劃擁有非常多的數據,因爲它具有更好的存儲管理能力,並且你可以從前面和後面編輯它,而不是像向量一樣編輯它。

最後一個可能是列表。如果您計劃對數據進行大量編輯,例如刪除插入新元素(而不僅僅是訪問它們),則應該考慮這一點,因爲它在插入/擦除元素時具有線性複雜性,而前2個元素具有線性加上額外的線性時間位置和結束之間的元素數量。

所以結論:

  • 載體 - 最簡單的
  • 雙端隊列 - 好大的數據存儲
  • 列表 - 良好的儲存

的大EDITTING這些都是STL序列容器和包括的標題應該看起來像:

#include <vector> 

here's reference for all STL containers

編輯︰ 我看到你的意思是別的,但我留在這裏,並添加到它。

你真的需要消費類嗎?我的意思是,如果將所有關於Line的數據都保留在Line類中,那將更合乎邏輯。這意味着如果需要,我會將每個月使用的服務存儲在向量中,或者我會立即計算價格並記住上個月的服務。我也會做一個叫做program的類來存儲使用的服務,並讓它處理使用的服務的數量。如果已經有2個服務,它可以丟棄新的服務,或者重寫舊的服務。你也可以讓程序返回價格,然後乘以使用時間。事情是這樣的:

#include <vector> 

using namespace std; 

class Service{ 
public: 
    int getPrice(){return monthPrice;} 
    void setPrice(const int &p) { monthPrice = p; } 
private: 
    int monthPrice; 
}; 

class Program{ 
public: 
    Program(){servicesUsed = 0; monthTime = 0;} 
    Program(const int &t) : monthTime(t) {servicesUsed = 0;} 
    void setTime(int t){monthTime = t;} 
    void addService(Service &s); 
    int price(); //calculate price of program 
private: 
    int monthTime; //time that program was used 
    Service services[2]; //services used in program 
    int servicesUsed; 
}; 

void Program::addService(Service &s){ 
    if(servicesUsed < 2){ // Discarding solution 
     services[servicesUsed] = s; 
     servicesUsed++; 
    } 
} 

int Program::price(){ 
    int pP = 0; 
    for(int i = 0; i < servicesUsed; i++){ 
     pP += services[i].getPrice(); 
    } 
    pP *= monthTime; 
    return pP; 
} 

class Line{ 
public: 
    Program *addMonth(const int &t); //will return handle for month 
    int consuption(); //calculate full line consuption 
private: 
    vector<Program> monthList; //store data about services per month 
}; 

Program *Line::addMonth(const int &t){ 
    monthList.push_back(Program(t)); 
    return &monthList.back(); 
} 

int Line::consuption(){ 
    int p = 0; 
    for(unsigned int i = 0; i < monthList.size(); i++){ 
     p += monthList[i].price(); 
    } 
    return p; 
} 

int main(){ 
    //showcase 
    Program *handle; 
    Service s1,s2,s3; 
    s1.setPrice(50); 
    s2.setPrice(75); 
    s3.setPrice(100); //probably read from file 
    Line line; 
    handle = line.addMonth(30); // monthTime probably also from file 
    handle->addService(s1); 
    handle->addService(s2); 
    handle->addService(s3); 
    handle = line.addMonth(60); 
    handle->addService(s3); 
    handle->addService(s2); 
    int p = line.consuption(); 
    return 0; 
} 

應該工作正常altought你可能需要更多的修改它;)

0

您使用的是不錯的辦法來解決問題。

你說:什麼結構,是解決(你的問題)

你首先必須模型數據需要保存和必須如何組織數據,我會建議使用Entity Relationship Model那是最好的常用於數據庫設計。

你有圖返回到代碼編輯器來描述在C++中如何表示該模型。

然後你就可以選擇哪個數據結構對你最好。我建議看看STL的,不是很漂亮,但效率很高,你不需要重新發明輪子:)