Home > Mobile >  Syntax error for the code found in a tutorial?
Syntax error for the code found in a tutorial?

Time:12-28

I was learning to do a program online and found this solution and I wanted to execute and learn from it.

I am keep getting syntax error, can someone help with formatting it? I tried but couldn't understand the code that much to see where we need to provide indentation etc.,

I couldn't find a python example online for that question, I could only find c answer.

class Node {

public:

virtual int Compute() = 0;

inline void SetLeft(Node* left) {

left_ = left;

}

inline void SetRight(Node* right) {

right_ = right;

}

protected:

Node* left_ = nullptr;

Node* right_ = nullptr;

};

class SumNode : public Node {

public:

inline int Compute() override {

return left_->Compute()   right_->Compute();

}

};

class SubNode : public Node {

public:

inline int Compute() override {

return left_->Compute() - right_->Compute();

}

};

class MulNode : public Node {

public:

inline int Compute() override {

return left_->Compute() * right_->Compute();

}

};

class NumNode : public Node {

public:

NumNode(int num) : num_(num) {}

inline int Compute() override {

return num_;

}

private:

int num_;

};

class Solution {

public:

vector<int> diffWaysToCompute(string expression) {

  

vector<Node*> nodes;

  

// parse the expression

for(int i = 0; i < expression.size();   i) {

  

switch(expression[i]) {

case ' ':

nodes.emplace_back(new SumNode());

break;

case '-':

nodes.emplace_back(new SubNode());

break;

case '*':

nodes.emplace_back(new MulNode());

break;

default:

int num = 0;

while(expression[i] >= '0' && expression[i] <= '9') {

num = num * 10   expression[i] - '0';

  i;

}

--i;

nodes.emplace_back(new NumNode(num));

break;

}

}

  

vector<Node*> trees = GenerateAllTrees(nodes, 0, nodes.size() - 1);

  

for(Node* node : nodes) {

delete node;

}

  

vector<int> results;

for(Node* tree : trees) {

results.emplace_back(tree->Compute());

delete tree;

}

return results;

  

}

  

vector<Node*> GenerateAllTrees(const vector<Node*> nodes, int beg, int end) {

  

if(beg == end) {

return {new NumNode(nodes[beg]->Compute())};

}

  

vector<Node*> results;

  

for(int i = beg; i <= end; i  ) {

  

// assuming the expression is well formed, operators will be in odd positions

if((i & 1) == 0) continue;

  

vector<Node*> left_trees = GenerateAllTrees(nodes, beg, i - 1);

vector<Node*> right_trees = GenerateAllTrees(nodes, i   1, end);

  

for(Node* left : left_trees) {

for(Node* right : right_trees) {

nodes[i]->SetLeft(left);

nodes[i]->SetRight(right);

Node* result = new NumNode(nodes[i]->Compute());

results.emplace_back(result);

}

}

for(Node* t : left_trees) {

delete t;

}

for(Node* t : right_trees) {

delete t;

}

  

}

  

return results;

   }

};

CodePudding user response:

You just need to import the modules you are using, exactly like Python, except the syntax is different:

#include <vector>
#include <string>

using namespace std;

class Node {
    public:
...

And because it is using emplace_back, if you are using g , you'll need to specify -std=c 11 on the command line.

Also, you need to understand this is not a runnable example. These are just classes you could use in your own code. To run this, you'll need to supply a main() function. Maybe:

#include <iostream>
int main()
{
    Solution s;
    vector<int> result = s.diffWaysToCompute( "3 9*7-16" );
    for( auto i : result )
        cout << i << "\n";
}
  •  Tags:  
  • c
  • Related