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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,3 @@ $RECYCLE.BIN/
!.vscode/extensions.json
/Calculator.TomDonegan/TextFile1.txt
/Calculator.TomDonegan/Calculator.TomDonegan/Notes.txt
/Calculator
4 changes: 4 additions & 0 deletions Calculator/Calculator.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Solution>
<Project Path="CalculatorApp/CalculatorApp.csproj" />
<Project Path="CalculatorLibrary/CalculatorLibrary.csproj" Id="dd72f64f-91a3-4584-a368-a9469274340c" />
</Solution>
14 changes: 14 additions & 0 deletions Calculator/CalculatorApp/CalculatorApp.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>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

</Project>
135 changes: 135 additions & 0 deletions Calculator/CalculatorApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Program.cs
using System.Text.RegularExpressions;
using CalculatorLibrary;

namespace CalculatorProgram;

class Program
{
static void Main(string[] args)
{
bool endApp = false;
List<Calculation> calcHistory = new();

// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");

Calculator calculator = new Calculator();
while (!endApp)
{
// Declare variables and set to empty.
// Use Nullable types (with ?) to match type of System.Console.ReadLine
string? numInput1 = "";
string? numInput2 = "";
double result = 0;

// Ask the user to type the first number.
Console.Write("Type a number, and then press Enter (or press [ x ] to use last calculation result): ");
numInput1 = Console.ReadLine();

if (numInput1 == "x" && calcHistory.Count >= 1)
{
numInput1 = calcHistory[calcHistory.Count - 1].Result.ToString();
Console.WriteLine($"Your first number is {numInput1}");
}
else if (numInput1 == "x" && calcHistory.Count < 1)
{
Console.WriteLine("No history exists...");
}

double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}

// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter (or press [ x ] to use last calculation result): ");
numInput2 = Console.ReadLine();

if (numInput2 == "x" && calcHistory.Count >= 1)
{
numInput2 = calcHistory[calcHistory.Count - 1].Result.ToString();
Console.WriteLine($"Your first number is {numInput1}");
}
else if (numInput2 == "x" && calcHistory.Count < 1)
{
Console.WriteLine("No history exists...");
}

double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput2 = Console.ReadLine();
}

// Ask the user to choose an operator.
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.WriteLine("\tp - Power (first number to power of second number)");

Console.Write("Your option? ");

string? op = Console.ReadLine();

// Validate input is not null, and matches the pattern
if (op == null || !Regex.IsMatch(op, "[a|s|m|d|p]"))
{
Console.WriteLine("Error: Unrecognized input.");
}
else
{
try
{
result = calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else
{
calcHistory.Add(new Calculation()
{
FirstNum = cleanNum1,
SecondNum = cleanNum2,
Result = result
});
Console.WriteLine("Your result: {0:0.##}\n", result);
Console.WriteLine($"You have used the calculator {calcHistory.Count} times");
if (calcHistory.Count > 0)
{
Console.WriteLine("Would you like to clear the history [Y/N]?");
string userInput = Console.ReadLine().ToLower();
if (userInput == "y")
{
calcHistory.Clear();
Console.WriteLine("History cleared!");
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}


}
Console.WriteLine("------------------------\n");

// Wait for the user to respond before closing.
Console.Write("Press 'x' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "x") endApp = true;

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

namespace CalculatorLibrary;

public class Calculator
{
JsonWriter writer;

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)
{
double result = double.NaN; // Default value is "not-a-number" if an operation, such as division, could result in an error.
writer.WriteStartObject();
writer.WritePropertyName("Operand1");
writer.WriteValue(num1);
writer.WritePropertyName("Operand2");
writer.WriteValue(num2);
writer.WritePropertyName("Operation");
// Use a switch statement to do the math.
switch (op)
{
case "a":
result = num1 + num2;
writer.WriteValue("Add");
break;
case "s":
result = num1 - num2;
writer.WriteValue("Subtract");
break;
case "m":
result = num1 * num2;
writer.WriteValue("Multiply");
break;
case "d":
// Ask the user to enter a non-zero divisor.
if (num2 != 0)
{
result = num1 / num2;
}
writer.WriteValue("Divide");
break;
case "p":
result = Math.Pow(num1, num2);
writer.WriteValue("Power");
break;
// Return text for an incorrect option entry.
default:
break;
}
writer.WritePropertyName("Result");
writer.WriteValue(result);
writer.WriteEndObject();

return result;
}

public void Finish()
{
writer.WriteEndArray();
writer.WriteEndObject();
writer.Close();
}
}

public class Calculation
{
public double FirstNum { get; set; }
public double SecondNum { get; set; }
public double Result { get; set; }
}
13 changes: 13 additions & 0 deletions Calculator/CalculatorLibrary/CalculatorLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

</Project>