Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project complete #252

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions Calculator.Zeratron/Calculator/Calculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

</Project>
31 changes: 31 additions & 0 deletions Calculator.Zeratron/Calculator/Calculator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35222.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calculator", "Calculator.csproj", "{109262B1-B501-42F6-9F60-49F4FFC3DAA5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculatorLibrary", "..\CalculatorLibrary\CalculatorLibrary.csproj", "{298875EC-4731-4587-BDE7-67B43B510941}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{109262B1-B501-42F6-9F60-49F4FFC3DAA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{109262B1-B501-42F6-9F60-49F4FFC3DAA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{109262B1-B501-42F6-9F60-49F4FFC3DAA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{109262B1-B501-42F6-9F60-49F4FFC3DAA5}.Release|Any CPU.Build.0 = Release|Any CPU
{298875EC-4731-4587-BDE7-67B43B510941}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{298875EC-4731-4587-BDE7-67B43B510941}.Debug|Any CPU.Build.0 = Debug|Any CPU
{298875EC-4731-4587-BDE7-67B43B510941}.Release|Any CPU.ActiveCfg = Release|Any CPU
{298875EC-4731-4587-BDE7-67B43B510941}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B951862A-9772-406A-80BF-38AFA4CEA7EF}
EndGlobalSection
EndGlobal
137 changes: 137 additions & 0 deletions Calculator.Zeratron/Calculator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using CalculatorLibrary;
using System.Text.RegularExpressions;
using System.Threading;

namespace CalculatorProgram
{

class Program
{
static void Main(string[] args)
{
bool endApp = false;
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");

Calculator calculator = new Calculator();

double lastResult = 0;

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

Console.Clear();
Console.WriteLine(@$"Choose an operator from the following list:
a - Add
s - Subtract
m - Multiply
d - Divide
sr - Square Root
p - Power
p10 - Power of 10
sin - Sine
cos - Cosine
tan - Tangent
v - View operation history
c - Clear operation history");
Console.Write("Your option?: ");

string? op = Console.ReadLine().ToLower();

if (op == null || !Regex.IsMatch(op, "^(a|s|m|d|sr|p|p10|sin|cos|tan|v|c)$"))
{
Console.WriteLine("Error: Unrecognized input.");
Thread.Sleep(1000);
continue;
}
else
{
if (op == "c")
{
calculator.ClearLog();
Console.WriteLine("Clearing operation log.");
}
else if (op == "v")
{
var log = calculator.OperationLog();
Console.WriteLine("Operation log: ");
foreach (var operation in log)
{
Console.WriteLine(operation);
}
}
else
{
double cleanNum1 = 0;
double cleanNum2 = 0;

Console.Write("Type the first number or enter 'r' to use the last operation result: ");
numInput1 = Console.ReadLine().ToLower();

if (numInput1 == "r")
{
cleanNum1 = lastResult;
}
else
{
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}
}
if (op != "sr" && op != "p10" && op != "sin" && op != "cos" && op != "tan")
{
Console.Write("Type the second number or enter 'r' to use the last operation result: ");
numInput2 = Console.ReadLine().ToLower();

if (numInput2 == "r")
{
cleanNum2 = lastResult;
}
else
{
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer 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);
lastResult = result;
}
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
}
Console.WriteLine("------------------------\n");

Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine().ToLower() == "n")
{
endApp = true;
}

Console.WriteLine("\n");
}
}
calculator.Finish();
return;
}
}
}
128 changes: 128 additions & 0 deletions Calculator.Zeratron/CalculatorLibrary/CalculatorLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using Newtonsoft.Json;

namespace CalculatorLibrary
{
public class Calculator
{
JsonWriter writer;

private int operationCounter;

private List<string> log = new List<string>();

public Calculator()
{
StreamWriter logFile = File.CreateText("calculatorlog.json");
logFile.AutoFlush = true;
writer = new JsonTextWriter(logFile);
writer.Formatting = Formatting.Indented;
writer.WriteStartObject();
writer.WritePropertyName("Operations");
writer.WriteStartArray();
}

public double DoOperation(double num1, double num2, string op)
{
string symbol = "";
double result = double.NaN;

operationCounter++;

writer.WriteStartObject();
writer.WritePropertyName("Operand1");
writer.WriteValue(num1);
writer.WritePropertyName("Operand2");
writer.WriteValue(num2);
writer.WritePropertyName("Operation");

switch (op)
{
case "a":
result = num1 + num2;
symbol = "+";
writer.WriteValue("Add");
break;
case "s":
result = num1 - num2;
symbol = "-";
writer.WriteValue("Subtract");
break;
case "m":
result = num1 * num2;
symbol = "*";
writer.WriteValue("Multiply");
break;
case "d":
// Ask the user to enter a non-zero divisor.
if (num2 != 0)
{
result = num1 / num2;
}
symbol = "/";
writer.WriteValue("Divide");
break;
case "sr":
result = Math.Sqrt(num1);
symbol = "√";
writer.WriteValue("SquareRoot");
break;
case "p":
result = Math.Pow(num1, num2);
symbol = "^";
writer.WriteValue("Power");
break;
case "p10":
result = Math.Pow(10, num1);
symbol = "10^";
writer.WriteValue("PowerOf10");
break;
case "sin":
result = Math.Sin(num1 * Math.PI / 180);
symbol = "sin";
writer.WriteValue("Sine");
break;
case "cos":
result = Math.Cos(num1 * Math.PI / 180);
symbol = "cos";
writer.WriteValue("Cosine");
break;
case "tan":
result = Math.Tan(num1 * Math.PI / 180);
symbol = "tan";
writer.WriteValue("Tangent");
break;
default:
break;
}
writer.WritePropertyName("Result");
writer.WriteValue(result);
writer.WriteEndObject();

if (op == "sr" || op == "p10" || op == "sin" || op == "cos" || op == "tan")
log.Add($"Operation {operationCounter}: {symbol}{num1} = {result}");
else
log.Add($"Operation {operationCounter}: {num1} {symbol} {num2} = {result}");

return result;
}

public void Finish()
{
writer.WriteEndArray();
writer.WritePropertyName("OperationCount");
writer.WriteValue(operationCounter);
writer.WriteEndObject();
writer.Close();
}

public void ClearLog()
{
log.Clear();
}

public List<string> OperationLog()
{
return log;
}
}
}
13 changes: 13 additions & 0 deletions Calculator.Zeratron/CalculatorLibrary/CalculatorLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>