Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Calculator.ShPet1304/Calculator.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Solution>
<Project Path="Calculator/Calculator.csproj" />
<Project Path="CalculatorLibrary/CalculatorLibrary.csproj" Id="183936aa-c4e5-47fb-ac37-d8150bbaa797" />
</Solution>
18 changes: 18 additions & 0 deletions Calculator.ShPet1304/Calculator/Calculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\CalculatorLibrary\CalculatorLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.54.0" />
</ItemGroup>

</Project>
116 changes: 116 additions & 0 deletions Calculator.ShPet1304/Calculator/CalculatorFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using CalculatorLibrary;
using CalculatorMenu;
using Spectre.Console;


namespace CalculatorProgram
{
public class CalculatorFunctions
{
public bool reusedResult = false;
public double reusedNumber = 0;

public void Function()
{
bool endApp = false;

Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");

CalculatorLibrary.Calculator calculator = Calculator.Instance;

while (!endApp)
{
string? numInput1 = "";
string? numInput2 = "";
double result = 0;
double cleanNum1 = 0;
double cleanNum2 = 0;

var menu = new Menu();
string op = menu.ShowOperationsMenu();

if (op == "Square Root" || op == "10x" || op == "Trigonometric function (sin)" || op == "Trigonometric function (cos)" || op == "Trigonometric function (tan)")
{
if (cleanNum1 == 0 && !reusedResult)
{
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();

while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput1 = Console.ReadLine();
}
;
}
else if (reusedResult)
{
cleanNum1 = reusedNumber;
reusedResult = false;
Console.WriteLine("Reusing the previous result: {0}\n", cleanNum1);
}
try
{
result = calculator.DoOperation(cleanNum1, 0, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else
Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
}
else if (cleanNum1 == 0 && !reusedResult)
{
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput1 = Console.ReadLine();
}
}

else if (reusedResult)
{
cleanNum1 = reusedNumber;
reusedResult = false;
Console.WriteLine("Reusing the previous result: {0}\n", cleanNum1);
}
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();

while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput2 = Console.ReadLine();
}
try
{
result = calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
Console.WriteLine("------------------------\n");
AnsiConsole.MarkupLine("\nPress [grey]Enter[/] to return to menu...");
Console.ReadKey();
break;
}
}
}
}



63 changes: 63 additions & 0 deletions Calculator.ShPet1304/Calculator/Menu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using CalculatorLibrary;
using CalculatorProgram;
using Spectre.Console;

namespace CalculatorMenu
{
public class Menu
{
private static Calculator calcLib = Calculator.Instance;
private static CalculatorFunctions func = new CalculatorFunctions();

public string ShowMainMenu()
{
AnsiConsole.Clear();
AnsiConsole.Write(
new FigletText("Calculator")
.Color(Color.Blue));

var choice = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("[green]What would you like to do[/]?")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more options)[/]")
.AddChoices(new[] {
"New Calculation", "View Result History", "Use Previous Result", "View Calculator Usage Count", "Clear History", "Exit"
}));
return choice;
}

public string ShowOperationsMenu()
{
AnsiConsole.Clear();
var op = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("[green]What would you like to do[/]?")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more options)[/]")
.AddChoices(new[] {
"addition", "subtraction","multiplication", "division", "Square Root", "Taking the power", "10x", "Trigonometric function (sin)", "Trigonometric function (cos)", "Trigonometric function (tan)"
}));
return op;
}

public void ShowHistoryMenu()
{
AnsiConsole.Clear();
List<double> options = calcLib.PreviousResult();
if (options.Count == 0)
{
AnsiConsole.MarkupLine("[red]No history found.[/]");
return;
}
double selectedNum = AnsiConsole.Prompt(
new SelectionPrompt<double>()
.Title("Select a [green]previous result[/]:")
.AddChoices(options)
);
func.reusedNumber = selectedNum;
func.reusedResult = true;
func.Function();
}
}
}
56 changes: 56 additions & 0 deletions Calculator.ShPet1304/Calculator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using CalculatorLibrary;
using CalculatorMenu;


namespace CalculatorProgram
{
public class Program
{
private static Calculator calcLib = Calculator.Instance;
private static CalculatorFunctions calculatorFunctions = new CalculatorFunctions();

public static void Main(string[] args)
{
Programme();
}

static void Programme()
{
var menu = new Menu();
bool running = true;

while (running)
{
string selected = menu.ShowMainMenu();

switch (selected)
{
case "New Calculation":
calculatorFunctions.Function();
break;

case "View Result History":
calcLib.GetLastResult();
break;

case "Use Previous Result":
menu.ShowHistoryMenu();
break;

case "View Calculator Usage Count":
calcLib.GetUsageCount();
break;

case "Clear History":
calcLib.ClearHistory();
break;

case "Exit":
calcLib.Finish();
running = false;
break;
}
}
}
}
}
Loading