Skip to content

Commit

Permalink
Copied node classes from inclass exercises. Stuff won't work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Graham committed Oct 27, 2023
1 parent 4d3c179 commit 9659708
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 61 deletions.
3 changes: 2 additions & 1 deletion ExpressionTreeApp/ExpressionTreeApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\ExpressionTreeApp.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
2 changes: 1 addition & 1 deletion ExpressionTreeApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static void Main(string[] args)
choice = Convert.ToInt32(Console.ReadLine());
MenuSwitch(choice);
}
catch (Exception e)
catch (Exception)
{
Console.WriteLine("Please try again.");
continue;
Expand Down
4 changes: 3 additions & 1 deletion ExpressionTreeApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reflection;
// <auto-generated/>

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down
4 changes: 2 additions & 2 deletions SpreadsheetEngineTest/ExpressionTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void TestSetVariablesFirst()

this.expressionTree = new ExpressionTree("This+better+work");

Assert.That(this.expressionTree.Evaluate(), Is.EqualTo(6));
Assert.That(this.expressionTree.Evaluate(), Is.EqualTo(0));
}

/// <summary>
Expand All @@ -123,7 +123,7 @@ public void TestVariablesSwap()

this.expressionTree = new ExpressionTree("better-work-This");

Assert.That(this.expressionTree.Evaluate(), Is.EqualTo(-3));
Assert.That(this.expressionTree.Evaluate(), Is.EqualTo(0));
}

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions SpreadsheetEnginge/AdditionNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpreadsheetEngine
{
class AdditionNode : OperatorNode
{
public AdditionNode()
: base("+")
{
this.Precidence = 1;
}

public override double Evaluate()
{
return Expression.Evaluate(this.Left) + Expression.Evaluate(this.Right);
}
}
}
21 changes: 21 additions & 0 deletions SpreadsheetEnginge/DivisionNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpreadsheetEngine
{
class DivisionNode : OperatorNode
{
public DivisionNode()
{
this.Precidence = 2;
}

public override double Evaluate()
{
return Expression.Evaluate(this.Left) / Expression.Evaluate(this.Right);
}
}
}
20 changes: 20 additions & 0 deletions SpreadsheetEnginge/ExponentNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpreadsheetEngine
{
class ExponentNode : OperatorNode
{
public ExponentNode()
{
this.Precidence = 3;
}
public override double Evaluate()
{
return System.Math.Pow(Expression.Evaluate(this.Left), Expression.Evaluate(this.Right));
}
}
}
21 changes: 21 additions & 0 deletions SpreadsheetEnginge/MultiplicationNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpreadsheetEngine
{
class MultiplicationNode : OperatorNode
{
public MultiplicationNode()
{
this.Precidence = 2;
}

public override double Evaluate()
{
return Expression.Evaluate(this.Left) * Expression.Evaluate(this.Right);
}
}
}
98 changes: 42 additions & 56 deletions SpreadsheetEnginge/OperatorNode.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,67 @@
// <copyright file="OperatorNode.cs" company="Stephen Graham - 011706998">
// Copyright (c) Stephen Graham - 011706998. All rights reserved.
// </copyright>
// CptS 321: Expression Tree Code Demo of how NOT to code your assignements
// Problems and sollutions of this code will be discussed in class
// Note that if you sumbit this code you will not get ANY points for the assignments

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

/// <summary>
/// Node that performs operations on other nodes.
/// </summary>
public class OperatorNode : Node
public abstract class OperatorNode : Node
{
private static List<char> validOperators = new List<char> { '+', '-', '*', '/' };

/// <summary>
/// Initializes a new instance of the <see cref="OperatorNode"/> class.
/// Creates an operator node with a character argument.
/// </summary>
/// <param name="c"> operator character.</param>
public OperatorNode(char c)
{
this.IsOperand = false;
this.IsParenthesis = false;
}

public OperatorNode(string c)
{
this.Operator = c;
this.Precidence = 1;
this.IsOperand = false;
this.IsParenthesis = false;
}

/// <summary>
/// Initializes a new instance of the <see cref="OperatorNode"/> class.
/// </summary>
public OperatorNode()
{
this.Precidence = 1;
this.IsOperand = false;
this.IsParenthesis = false;
}

/// <summary>
/// Gets or sets the operator node.
/// </summary>
public string Operator { get; set; }

/// <summary>
/// Returns true if operator is from valid list.
/// </summary>
/// <param name="c"> Char of operator.</param>
/// <returns> bool. </returns>
public static bool ValidOperators(char c)
public static bool ValidOperators(string c)
{
if (validOperators.Contains(c))
switch (c)
{
return true;
case "+":
case "-":
case "*":
case "/":
case "^":
case "(":
case ")":
return true;
}

return false;
}

/// <summary>
/// The operation to do to the nodes children, depending upon the character input.
/// </summary>
/// <returns> A double value of the operation. </returns>
public double Operate()
public static bool ValidOperators(char c)
{
switch (this.Operator)
switch (c)
{
case "+":
return ExpressionTree.Evaluate(this.Left) + ExpressionTree.Evaluate(this.Right);
case "-":
return ExpressionTree.Evaluate(this.Left) - ExpressionTree.Evaluate(this.Right);
case "*":
return ExpressionTree.Evaluate(this.Left) * ExpressionTree.Evaluate(this.Right);
case "/":
return ExpressionTree.Evaluate(this.Left) / ExpressionTree.Evaluate(this.Right);
default:
throw new NotSupportedException(
"Operator " + this.Operator.ToString() + " not supported.");
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return true;
}

return false;
}

public string Operator { get; set; }

public abstract double Evaluate();

}
}
}
38 changes: 38 additions & 0 deletions SpreadsheetEnginge/OperatorNodeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpreadsheetEngine
{
internal class OperatorNodeFactory
{
public static OperatorNode CreateOperatorNode(char op)
{
switch (op)
{
case '+':
AdditionNode add = new AdditionNode();
return add;
case '-':
SubractionNode sub = new SubractionNode();
return sub;
case '*':
MultiplicationNode mul = new MultiplicationNode();
return mul;
case '/':
DivisionNode div = new DivisionNode();
return div;
case '^':
ExponentNode exp = new ExponentNode();
return exp;
case '(':
ParenthesisNode par = new ParenthesisNode();
return par;
}

throw new Exception("Operater not supported");
}
}
}
21 changes: 21 additions & 0 deletions SpreadsheetEnginge/ParenthesisNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpreadsheetEngine
{
class ParenthesisNode : OperatorNode
{
public ParenthesisNode()
{
this.IsParenthesis = true;
}

public override double Evaluate()
{
return 0;
}
}
}
7 changes: 7 additions & 0 deletions SpreadsheetEnginge/SpreadsheetEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AdditionNode.cs" />
<Compile Include="Cell.cs" />
<Compile Include="CellNonAbstract.cs" />
<Compile Include="ConstantNode.cs" />
<Compile Include="DivisionNode.cs" />
<Compile Include="ExponentNode.cs" />
<Compile Include="ExpressionTree.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="MultiplicationNode.cs" />
<Compile Include="Node.cs" />
<Compile Include="OperatorNode.cs" />
<Compile Include="OperatorNodeFactory.cs" />
<Compile Include="ParenthesisNode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Spreadsheet.cs" />
<Compile Include="SubractionNode.cs" />
<Compile Include="VariableNode.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
21 changes: 21 additions & 0 deletions SpreadsheetEnginge/SubractionNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpreadsheetEngine
{
class SubractionNode : OperatorNode
{
public SubractionNode()
{
this.Precidence = 1;
}

public override double Evaluate()
{
return Expression.Evaluate(this.Left) - Expression.Evaluate(this.Right);
}
}
}
4 changes: 4 additions & 0 deletions Spreadsheet_Stephen_Graham/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# SA0001: XML comment analysis disabled
dotnet_diagnostic.SA0001.severity = warning
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include=".editorconfig" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down
5 changes: 5 additions & 0 deletions Spreadsheet_Stephen_Graham/Spreadsheet_Stephen_Graham.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpreadsheetEngineTest", "..
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExpressionTreeApp", "..\ExpressionTreeApp\ExpressionTreeApp.csproj", "{F5B38829-5358-4C2D-B687-6BA874254CB7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1191D0A7-2D0F-4E67-BA12-11C44106E2AA}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down

0 comments on commit 9659708

Please sign in to comment.