我有兩個單獨的程序,都處理與中綴和後綴評估表達式樹。一個基於結構,而另一個基於類。現在我被困在上面寫着我的任務的一部分(類版本):類樹vs結構TreeNode
「說完createExpressionTree()方法的實現」
和
「它與您之前的實現非常相似,只不過您將使用」類樹「的實例而不是」結構樹節點「的實例。」
這兩者比以下有更多的內容,但我認爲你可以得到事物的要點,所以我問的是:類實現與結構類實現的相似程度如何?我是否可以複製並粘貼以下所示的舊代碼並進行微調?我一直在嘗試,但在訪問私人會員等方面遇到問題。
因此,這裏的分類和我createExpressionTree的新版本是應該去用它
#ifndef TREE_H
#define TREE_H
#include <vector>
#include <stack>
#include <sstream>
#include <map>
# define TYPE_NUMBER 0
# define TYPE_VARIABLE 1
# define TYPE_OPERATOR 2
class Tree
{
public:
Tree(std::string input,Tree *leftSubTree=NULL,Tree *rightSubTree=NULL);
Tree(const Tree &inTree); //COPY CONSTRUCTOR
~Tree(); //DESTRUCTOR
int evaluate(std::map< std::string, int > ipMap); //EVALUATE THE EXPRESSION
void postOrderPrint();
void inOrderPrint();
private:
Tree *leftPtr;
std::string Op;
Tree *rightPtr;
int NodeType;
};
與樹類
Tree::Tree(std::string input,Tree *leftSubTree,Tree *rightSubTree){
Op = input;
leftPtr = leftSubTree;
rightPtr = rightSubTree;
int num;
if (input == "+"|input == "-"|input == "*"|input == "/")
NodeType = TYPE_OPERATOR;
else if(std::istringstream(Op)>>num)
NodeType = TYPE_NUMBER;
else
NodeType = TYPE_VARIABLE;
}
// copy constructor
Tree::Tree(const Tree &inTree){
Op = inTree.Op;
NodeType = inTree.NodeType;
if (inTree.leftPtr == NULL){
leftPtr = NULL;
}
else {
leftPtr = new Tree(*(inTree.leftPtr));
}
if (inTree.rightPtr == NULL){
rightPtr = NULL;
}
else {
rightPtr = new Tree(*(inTree.rightPtr));
}
}
// tree destructor
Tree::~Tree(){
std::cout << "Tree destructor called" << std::endl;
if (leftPtr != NULL) {
delete(leftPtr);
leftPtr = NULL;
}
if (rightPtr != NULL) {
delete(rightPtr);
rightPtr = NULL;
}
}
#endif
新createExpressionTree相關的代碼,我會喜歡一些幫助與:
void arithmetic_expression::createExpressionTree(std::vector<std::string> expression)
{
std::stack <Tree> localStack;
std::string Op;
//Very similar to old implementation
}
這裏是以前的實施th Ë結構treeNode的,並且在完成前面的createExpressionTree
struct treeNode {
treeNode *leftPtr; /* pointer to left subtree */
std::string Op; /* integer data value */
treeNode *rightPtr; /* pointer to right subtree */
};
typedef struct treeNode TreeNode;
typedef TreeNode * TreeNodePtr;
以前createExpressionTree
void arithmetic_expression::createExpressionTree(std::vector<std::string> expression)
{
std::stack <TreeNodePtr> localStack;
std::string Op;
TreeNodePtr ptr;
for(int i=0; i<expression.size();i++)
{
Op = expression[i];
ptr = createNewTreeNode(Op);
if(char_is_operator(Op))
{
// adding element to right tree
if (localStack.empty())
{
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
return;
}
else
{
ptr->rightPtr = localStack.top();
localStack.pop();
}
// adding element to left tree
if (localStack.empty()) {
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
return;
}
else
{
ptr->leftPtr = localStack.top();
localStack.pop();
}
}
// pushing element to stack
localStack.push(ptr);
}
if (localStack.empty()) {
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
}
else
{
topPtr = localStack.top();
localStack.pop();
if (!localStack.empty()) {
std::cout<< "Invalid expression: tree not created " << std::endl;
topPtr = NULL;
}
}
}
我沒有訪問leftPtr/rightPtr就像我在結構版本中所做的那樣。那麼我該如何爲班級版本做些什麼呢?也許這是一個更具體的問題,我應該問 – user1325578
如果你想訪問私人會員字段有兩種方法去實現它。第一種方法是在Tree類中創建一個返回私有變量的訪問器方法。第二種方法是在樹類中聲明朋友void arithmetic_expression :: createExpressionTree(std :: vector表達式),以便它可以訪問私有字段。我相信,一般來說,你想要做第一種方法,因爲使用朋友被認爲是駭人聽聞。兩者都將允許您訪問私有字段,並允許您重用您的createExpressionTree。 –