Skip to content

Instantly share code, notes, and snippets.

@yurii-litvinov
Created April 14, 2016 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurii-litvinov/9032c9ff03cfe2481dfb500fee66e2f9 to your computer and use it in GitHub Desktop.
Save yurii-litvinov/9032c9ff03cfe2481dfb500fee66e2f9 to your computer and use it in GitHub Desktop.
code review example 4 - NodeOperation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParseTree
{
public class NodeOperation : Node
{
private Node left;
private Node right;
private string signature;
private Func<double, double, double> perform;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
public NodeOperation(string sign, Func<double, double, double> performOperation)
{
this.perform = performOperation;
this.signature = sign;
}
public double Calculate()
{
return perform(left.Calculate(), right.Calculate());
}
public void Print()
{
Console.Write("({0} ", signature);
left.Print();
right.Print();
Console.Write(")");
}
/// <summary>
/// Add an operand
/// </summary>
public Node AddOperand(Node operand)
{
if (left == null)
{
return left = operand;
}
else
{
if (right == null)
{
return right = operand;
}
else
{
throw new ExtraNodeException();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment