Skip to content

Commit

Permalink
Merge branch 'Branch_HW5' into 'main'
Browse files Browse the repository at this point in the history
HW5. Expression Tree

See merge request sgraham1/cpts321-hws!2
  • Loading branch information
sgraham1 committed Oct 20, 2023
2 parents b7a0ab9 + 4d3c179 commit aac6c4e
Show file tree
Hide file tree
Showing 14 changed files with 838 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ExpressionTreeApp/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
65 changes: 65 additions & 0 deletions ExpressionTreeApp/ExpressionTreeApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F5B38829-5358-4C2D-B687-6BA874254CB7}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ExpressionTreeApp</RootNamespace>
<AssemblyName>ExpressionTreeApp</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SpreadsheetEnginge\SpreadsheetEngine.csproj">
<Project>{84967d9d-09ab-49e5-a873-0caa8f925e33}</Project>
<Name>SpreadsheetEngine</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\Spreadsheet_Stephen_Graham\packages\StyleCop.Analyzers.1.1.118\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
<Analyzer Include="..\Spreadsheet_Stephen_Graham\packages\StyleCop.Analyzers.1.1.118\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
103 changes: 103 additions & 0 deletions ExpressionTreeApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// <copyright file="Program.cs" company="Stephen Graham - 011706998">
// Copyright (c) Stephen Graham - 011706998. All rights reserved.
// </copyright>

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

/// <summary>
/// console app to create and modify and expression tree.
/// </summary>
public class Program
{
private static ExpressionTree expressionTree = new ExpressionTree("3+2-1");

private static void Main(string[] args)
{
while (true)
{
try
{
int choice = -1;
DisplayMenu();
choice = Convert.ToInt32(Console.ReadLine());
MenuSwitch(choice);
}
catch (Exception e)
{
Console.WriteLine("Please try again.");
continue;
}
}
}

private static void DisplayMenu()
{
Console.WriteLine("Menu (Current expression = \"" + CurrentExpression() + "\" )");
Console.WriteLine("1 = Enter a new expression");
Console.WriteLine("2 = Set a variable value");
Console.WriteLine("3 = Evaluate Tree");
Console.WriteLine("4 = Quit");
}

private static void MenuSwitch(int option)
{
switch (option)
{
case 1:
ChangeExpression();
break;

case 2:
AddVariable();
break;

case 3:
Console.WriteLine("Evaluating...");
Console.WriteLine(expressionTree.Evaluate());
break;

case 4:
Console.WriteLine("Goodbye.");
System.Threading.Thread.Sleep(1000);
Environment.Exit(1);
break;

default:
Console.WriteLine("Please try again.");
break;
}
}

private static string CurrentExpression()
{
return expressionTree.GetExpression();
}

private static void ChangeExpression()
{
Console.Write("Enter a new expression: ");
expressionTree = new ExpressionTree(Console.ReadLine());
}

private static void AddVariable()
{
string variable;
double value;

Console.Write("Enter variable name: ");
variable = Console.ReadLine();

Console.Write("Enter variable value: ");
value = Convert.ToDouble(Console.ReadLine());

expressionTree.SetVariable(variable, value);
}
}
}
36 changes: 36 additions & 0 deletions ExpressionTreeApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ExpressionTreeApp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ExpressionTreeApp")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f5b38829-5358-4c2d-b687-6ba874254cb7")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions ExpressionTreeApp/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="StyleCop.Analyzers" version="1.1.118" targetFramework="net472" developmentDependency="true" />
</packages>
14 changes: 14 additions & 0 deletions ExpressionTreeApp/stylecop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// ACTION REQUIRED: This file was automatically added to your project, but it
// will not take effect until additional steps are taken to enable it. See the
// following page for additional information:
//
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md

"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
"settings": {
"documentationRules": {
"companyName": "Stephen Graham - 011706998"
}
}
}
144 changes: 144 additions & 0 deletions SpreadsheetEngineTest/ExpressionTreeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// <copyright file="ExpressionTreeTests.cs" company="Stephen Graham - 011706998">
// Copyright (c) Stephen Graham - 011706998. All rights reserved.
// </copyright>

namespace SpreadsheetEngine.Tests
{
using NUnit.Framework;

/// <summary>
/// Tests the ExpressionTree class.
/// </summary>
public class ExpressionTreeTests
{
private ExpressionTree expressionTree = new ExpressionTree("0");

/// <summary>
/// Creates expression and evaluates it. Only using one kind of operator.
/// </summary>
/// <param name="s"> expression string. </param>
/// <param name="d"> expected evaluate double. </param>
[TestCase("5+1+2", 8)]
[TestCase("5-1-2", 2)]
[TestCase("5*1*2", 10)]
[TestCase("5/1/2", 2.5)]
[TestCase("5+1+-2", 4)]
[TestCase("5-1--2", 6)]
[TestCase("5*1*-2", -10)]
[TestCase("5/1/-2", -2.5)]
[TestCase("5+1+-2+0", 4)]
[TestCase("5-1--2-0", 6)]
[TestCase("5*1*-2*0", 0)]
[TestCase("5/1/-2/0", double.NegativeInfinity)]
[TestCase("5/1/2/0", double.PositiveInfinity)]
public void TestSingleOperators(string s, double d)
{
this.expressionTree = new ExpressionTree(s);
Assert.AreEqual(d, this.expressionTree.Evaluate());
}

/// <summary>
/// Creates expression with different operations to test order of operations.
/// </summary>
/// <param name="s"> expression string. </param>
/// <param name="d"> expected evaluate double. </param>
[TestCase("5-3+1", 3)]
[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("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)]
public void TestOrderOfOperations(string s, double d)
{
this.expressionTree = new ExpressionTree(s);
Assert.AreEqual(d, this.expressionTree.Evaluate());
}

/// <summary>
/// Creates expression with different operations and parenthesis to test order of operations.
/// </summary>
/// <param name="s"> expression string. </param>
/// <param name="d"> expected evaluate double. </param>
[TestCase("10+1/8-3+4*6", 31.125)]
[TestCase("10+1/8-3+(4*6)", 31.125)]
[TestCase("10+1/8-(3+(4*6))", -16.875)]
[TestCase("(10+1)/8-(3+(4*6))", -25.625)]
[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)]
public void TestOrderOfOperationsWithParenthesis(string s, double d)
{
this.expressionTree = new ExpressionTree(s);
Assert.AreEqual(d, this.expressionTree.Evaluate());
}

/// <summary>
/// Sets expression tree and variable names and values, then evaluates expression tree.
/// </summary>
[Test]
public void TestVariables()
{
this.expressionTree = new ExpressionTree("This+better+work");

this.expressionTree.SetVariable("This", 1);
this.expressionTree.SetVariable("better", 2);
this.expressionTree.SetVariable("work", 3);

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

/// <summary>
/// Sets variables first before evaluating expression tree.
/// </summary>
[Test]
public void TestSetVariablesFirst()
{
this.expressionTree.SetVariable("This", 1);
this.expressionTree.SetVariable("better", 2);
this.expressionTree.SetVariable("work", 3);

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

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

/// <summary>
/// Sets expression, sets variables, and evaluates expression. Then changes expression with same variable names and evaulates again.
/// </summary>
[Test]
public void TestVariablesSwap()
{
this.expressionTree = new ExpressionTree("This+better+work");

this.expressionTree.SetVariable("This", 1);
this.expressionTree.SetVariable("better", 2);
this.expressionTree.SetVariable("work", 3);

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

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

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

/// <summary>
/// Sets expression with variable name and constants and evaluates.
/// </summary>
[Test]
public void TestVariablesAndConstants()
{
this.expressionTree = new ExpressionTree("10-This-better--1-work");

this.expressionTree.SetVariable("This", 1);
this.expressionTree.SetVariable("better", 2);
this.expressionTree.SetVariable("work", 3);

Assert.That(this.expressionTree.Evaluate(), Is.EqualTo(5));
}
}
}
Loading

0 comments on commit aac6c4e

Please sign in to comment.