Created
April 14, 2016 20:14
-
-
Save yurii-litvinov/9032c9ff03cfe2481dfb500fee66e2f9 to your computer and use it in GitHub Desktop.
code review example 4 - NodeOperation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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