Skip to content
Closed
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
12 changes: 1 addition & 11 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,4 @@ $RECYCLE.BIN/
.idea/
*.sln.iml

##
## Visual Studio Code
##
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.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="Calculator/Calculator.csproj" />
<Project Path="CalculatorLibrary/CalculatorLibrary.csproj" />
</Solution>
14 changes: 14 additions & 0 deletions Calculator/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>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

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

namespace CalculatorProgram
{

public class Program
{
static void Main(string[] args)
{
bool endApp = false;
int calcCtr = 0;
List<Calculations> calculatorHistory = new List<Calculations>();
Calculator calculator = new Calculator();
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");

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

// Ask the user to type the first number or list to see history.
Console.Write("Type a number or \"list\" to see your history, and then press Enter: ");
userInput1 = Console.ReadLine();
string notNullUserInput1 = userInput1 ?? "";
double cleanNum1 = 0;
if (notNullUserInput1.ToLower().Equals("list"))
{
if (calculatorHistory.Count == 0)
{
Console.WriteLine("No calculations yet! Press enter to continue..");
Console.ReadLine();
continue;
}
int i = 1;
foreach (Calculations calc in calculatorHistory)
{
Console.WriteLine("(" + i + ") " + calc.ToString());
i++;
}
Console.WriteLine("Enter the index of the calculation result you want to reuse, Enter clear to delete history" +
"\nor press Enter to continue: ");
string? indexInput = Console.ReadLine();
string notNullIndexInput = indexInput ?? "";
if (notNullIndexInput.Equals(""))
{
Console.WriteLine("Continuing without selecting a previous result.");
continue;
}
else if (notNullIndexInput.ToLower().Equals("clear"))
{
calculatorHistory.Clear();
Console.WriteLine("History cleared! Press enter to continue..");
Console.ReadLine();
continue;
}

if (!int.TryParse(notNullIndexInput, out int indexInputInt))
{
Console.Write("This is not valid input.");
}
else if (indexInputInt > 0 && indexInputInt <= calculatorHistory.Count)
{
cleanNum1 = calculatorHistory[indexInputInt - 1].result;
Console.WriteLine("You have selected: " + cleanNum1);
}
}
else
{
while (!double.TryParse(userInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter a numeric value or \"list\": ");
userInput1 = Console.ReadLine();
}
}



// Ask the user to type the second number.
Console.Write("Type another number, and then press Enter: ");
userInput2 = Console.ReadLine();

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

string? op = "";

while (true)
{
// Ask the user to choose an operator.
Console.WriteLine("First Number: " + cleanNum1);
Console.WriteLine("Second Number: " + cleanNum2);
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");
Console.WriteLine("\tmod - modulus (remainder)");
Console.Write("Your option? ");

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

// Validate input is not null, and matches the pattern
if (op == null || !Regex.IsMatch(op, "^(a|s|m|d|p|mod)$"))
{
Console.WriteLine("Error: Unrecognized input. Press enter to retry");
Console.ReadLine();
Console.Clear();
}
else
{
break;
}
}

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);
calcCtr++;
calculatorHistory.Add(new Calculations
{
number1 = (int)cleanNum1,
number2 = (int)cleanNum2,
operation = op,
result =result
});
}
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.WriteLine("Calculations so far: " + calcCtr);
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;

Console.WriteLine("\n"); // Friendly linespacing.
}
calculator.Finish();
return;
}

public struct Calculations
{
public int number1;
public int number2;
public string operation;
public double result;

public Calculations(int num1, int num2, string op, double res)
{
number1 = num1;
number2 = num2;
operation = op;
result = res;
}

private String ConvertOperationToSymbol(string operation)
{
switch (operation)
{
case "a":
return "+";
case "s":
return "-";
case "m":
return "*";
case "d":
return "/";
case "p":
return "^";
case "mod":
return "%";
default:
return operation;
}
}
public override string ToString()
{
return $"{number1} {ConvertOperationToSymbol(operation)} {number2} = {result}";
}
}
}
}
79 changes: 79 additions & 0 deletions Calculator/CalculatorLibrary/CalculatorLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Diagnostics;
using Newtonsoft.Json;

namespace CalculatorLibrary
{

public class Calculator
{
JsonWriter writer;
public Calculator()
{
StreamWriter logFile = File.CreateText("calculator.json");
logFile.AutoFlush = true;
writer = new JsonTextWriter(logFile);
writer.Formatting = Formatting.Indented;
writer.WriteStartObject();
writer.WritePropertyName("Operations");
writer.WriteStartArray();
}
public void Finish()
{
writer.WriteEndArray();
writer.WriteEndObject();
writer.Close();
}
public double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use 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("pow");
break;
case "mod":
result = num1 % num2;
writer.WriteValue("modulus");
break;
// Return text for an incorrect option entry.
default:
break;
}
result = Math.Round(result, 2);
writer.WritePropertyName("Result");
writer.WriteValue(result);
writer.WriteEndObject();
return result;
}
}


}
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>