2013-05-06 50 views
-6

我很熱衷於創造一個汽車登記程序:C++ BST和文件處理

爲用戶添加,刪除,查找,編輯

菜單(改變對汽車的特定細節)汽車,並查看所有的車。然後使用二叉搜索樹將其存儲在內存中。所有的汽車都將從內存中寫入一個csv文件。在裝載系統時也應該讀回所有的車輛

汽車有2種類型的汽油和電動。 每一輛汽車都有屬性車ID,所有者,品牌,型號,號牌 汽油車已經屬性英里,充值 電動車有屬性力量,英里

class car 
{ 
string id 
string owner 
string make 
string model 
string numberplate 
virtual getkey()//gets key being searched etc. 
readfile(); 
writefile(); 
}; 

class petrol : public car 
{ 
string miles 
string topup 
}; 

class electric : public car 
{ 
string power 
string miles 
}; 


data structure: 

class node 
{ 
car *ptr 
node *left 
node *right 
}; 

class tree 
{ 
///insert delete etc. 
}; 

這會是一個實用的一流的設計和哪些功能可能需要包含?

+0

一個更實用的設計會應該爲BST的數據字段使用模板。 – 2013-05-06 18:53:31

+0

請你能詳細說明,所以我將不得不模板的功能? – user2355449 2013-05-06 18:58:39

+0

請參閱下面的答案。 – 2013-05-06 19:03:52

回答

0

初始BST和鏈接列表實現的問題是,它們要麼迫使您使用特定的數據類型,要麼繼承該數據類型(如您的數據類型)。如果我想要一個水果BST,我不能使用你的樹,因爲你的樹專用於汽車。

我建議一個抽象節點類和節點類派生的數據類:

struct Node 
{ 
    boost::shared_ptr<Node> left; 
    boost::shared_ptr<Node> right; 

    // Interface functions for descendants 
    virtual bool is_less_than(boost::shared_ptr<Node> other_node) const = 0; 
    virtual bool is_equal_to(boost::shared_ptr<Node> other_node) const = 0; 
}; 

我仍然認爲,最好的設計是使用模板:

template <class User_Data_Type> 
class Node 
{ 
    public: 
    boost::shared_ptr<Node> left; 
    boost::shared_ptr<Node> right; 
    User_Data_Type   m_data; 
}; 
+0

好的,我會玩一玩。謝謝 – user2355449 2013-05-06 19:30:50

+0

@ user2355449:如果答案有用,請點擊複選標記。 – 2013-05-06 22:07:22