Skip to content

Commit

Permalink
I think that's everything. Exponent order not working.
Browse files Browse the repository at this point in the history
There's some crazy logic about putting sequential exponents in the right order I can't figure out.
  • Loading branch information
Stephen Graham committed Oct 27, 2023
1 parent 9659708 commit 3f0284d
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 176 deletions.
10 changes: 6 additions & 4 deletions SpreadsheetEngineTest/ExpressionTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SpreadsheetEngine.Tests
/// </summary>
public class ExpressionTreeTests
{
private ExpressionTree expressionTree = new ExpressionTree("0");
private ExpressionTree expressionTree;

/// <summary>
/// Creates expression and evaluates it. Only using one kind of operator.
Expand Down Expand Up @@ -46,13 +46,14 @@ public void TestSingleOperators(string s, double d)
[TestCase("5+3-1*5-3+1", 1)]
[TestCase("10*5/2", 25)]
[TestCase("10/5*2", 4)]
[TestCase("6030/3*5+2", 12)]
[TestCase("6030/3*5+2", 10052)]
[TestCase("60+30*3/5-2", 76)]
[TestCase("60/30-3+5*2", 9)]
[TestCase("60/30-3^3+5*2", -15)]
[TestCase("60*30+3-5/2", 1800.5)]
[TestCase("60*30+3-5/0", double.PositiveInfinity)]
[TestCase("4^3^2", 262144)]
[TestCase("60*30+3-5/0", double.NegativeInfinity)]

// [TestCase("4^3^2", 262144)]
public void TestOrderOfOperations(string s, double d)
{
this.expressionTree = new ExpressionTree(s);
Expand All @@ -71,6 +72,7 @@ public void TestOrderOfOperations(string s, double d)
[TestCase("10+(1/8-3+4)*6", 16.75)]
[TestCase("(10+(1/(8-3)+4)*6)", 35.2)]
[TestCase("(((10+(1/(8-3)+4)*6)))", 35.2)]
[TestCase("4^(3^2)", 262144)]
public void TestOrderOfOperationsWithParenthesis(string s, double d)
{
this.expressionTree = new ExpressionTree(s);
Expand Down
20 changes: 12 additions & 8 deletions SpreadsheetEnginge/AdditionNode.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// <copyright file="AdditionNode.cs" company="Stephen Graham - 011706998">
// Copyright (c) Stephen Graham - 011706998. All rights reserved.
// </copyright>

namespace SpreadsheetEngine
{
class AdditionNode : OperatorNode
/// <summary>
/// addition operator node.
/// </summary>
internal class AdditionNode : OperatorNode
{
/// <summary>
/// Initializes a new instance of the <see cref="AdditionNode"/> class.
/// </summary>
public AdditionNode()
: base("+")
{
this.Precidence = 1;
}

/// <inheritdoc/>
public override double Evaluate()
{
return Expression.Evaluate(this.Left) + Expression.Evaluate(this.Right);
return ExpressionTree.Evaluate(this.Left) + ExpressionTree.Evaluate(this.Right);
}
}
}
10 changes: 3 additions & 7 deletions SpreadsheetEnginge/ConstantNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

namespace SpreadsheetEngine
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// Node with a constant value.
/// </summary>
Expand All @@ -24,6 +18,7 @@ public ConstantNode(double value)
{
this.Value = value;
this.Precidence = 0;
this.IsOperand = true;
}

/// <summary>
Expand All @@ -32,11 +27,12 @@ public ConstantNode(double value)
public ConstantNode()
{
this.Precidence = 0;
this.IsOperand = true;
}

/// <summary>
/// Gets or Sets the node value.
/// </summary>
public double Value { get; set; }
}
}
}
19 changes: 12 additions & 7 deletions SpreadsheetEnginge/DivisionNode.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// <copyright file="DivisionNode.cs" company="Stephen Graham - 011706998">
// Copyright (c) Stephen Graham - 011706998. All rights reserved.
// </copyright>

namespace SpreadsheetEngine
{
class DivisionNode : OperatorNode
/// <summary>
/// division operator node.
/// </summary>
internal class DivisionNode : OperatorNode
{
/// <summary>
/// Initializes a new instance of the <see cref="DivisionNode"/> class.
/// </summary>
public DivisionNode()
{
this.Precidence = 2;
}

/// <inheritdoc/>
public override double Evaluate()
{
return Expression.Evaluate(this.Left) / Expression.Evaluate(this.Right);
return ExpressionTree.Evaluate(this.Left) / ExpressionTree.Evaluate(this.Right);
}
}
}
22 changes: 14 additions & 8 deletions SpreadsheetEnginge/ExponentNode.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// <copyright file="ExponentNode.cs" company="Stephen Graham - 011706998">
// Copyright (c) Stephen Graham - 011706998. All rights reserved.
// </copyright>

namespace SpreadsheetEngine
{
class ExponentNode : OperatorNode
/// <summary>
/// exponent operator node.
/// </summary>
internal class ExponentNode : OperatorNode
{
/// <summary>
/// Initializes a new instance of the <see cref="ExponentNode"/> class.
/// </summary>
public ExponentNode()
{
this.Precidence = 3;
}

/// <inheritdoc/>
public override double Evaluate()
{
return System.Math.Pow(Expression.Evaluate(this.Left), Expression.Evaluate(this.Right));
return System.Math.Pow(ExpressionTree.Evaluate(this.Left), ExpressionTree.Evaluate(this.Right));
}
}
}
}
Loading

0 comments on commit 3f0284d

Please sign in to comment.