Created
April 14, 2016 20:09
-
-
Save yurii-litvinov/c354787e51d88866d526e07c4d888c07 to your computer and use it in GitHub Desktop.
code review example 4 - tree.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; | |
using System.IO; | |
namespace ParseTree | |
{ | |
public class Tree | |
{ | |
private string[] tokens; | |
private Dictionary<string, Func<double, double, double>> operation; | |
private int counter; | |
/// <summary> | |
/// Initializes a new instance of the class | |
/// </summary> | |
/// <param name="path"></param> | |
public Tree(string path) | |
{ | |
string expression = ""; | |
using (StreamReader f = new StreamReader(path)) | |
{ | |
expression = f.ReadLine(); | |
} | |
this.operation = new Dictionary<string, Func<double, double, double>> | |
{ | |
{ "+", (x, y) => x + y }, | |
{ "-", (x, y) => x - y }, | |
{ "*", (x, y) => x * y }, | |
{ "/", (x, y) => x / y } | |
}; | |
this.tokens = expression.Split(new char[] { ' ' }); | |
this.counter = 0; | |
} | |
public double Calculate(Node tree) | |
{ | |
return tree.Calculate(); | |
} | |
public void PrintTree(Node tree) | |
{ | |
tree.Print(); | |
} | |
/// <summary> | |
/// Build tree | |
/// </summary> | |
public void Build(ref Node current) | |
{ | |
string token = tokens[counter++]; | |
if (token == ")") | |
{ | |
return; | |
} | |
if (operation.ContainsKey(token)) | |
{ | |
if (current != null) | |
{ | |
var currentOperation = current as NodeOperation; | |
Node newCurrent = currentOperation.AddOperand(new NodeOperation(token, operation[token])); | |
Build(ref newCurrent); | |
} | |
else | |
{ | |
current = new NodeOperation(token, operation[token]); | |
} | |
} | |
else | |
{ | |
double value; | |
if (double.TryParse(token, out value)) | |
{ | |
if (current != null) | |
{ | |
(current as NodeOperation).AddOperand(new NodeOperand(value)); | |
} | |
else | |
{ | |
current = new NodeOperand(value); | |
} | |
} | |
} | |
Build(ref current); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment