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
5 changes: 5 additions & 0 deletions Calculator.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Solution>
<Project Path="Calculator/Calculator.csproj" />
<Project Path="CalculatorLibrary/CalculatorLibrary.csproj" Id="20872eb0-c07f-4f10-8ccf-780139815f00" />
<Project Path="Menu/Menu.csproj" Id="8df53b1f-5192-45d6-b2ef-5d16d7eb2c4d" />
</Solution>
15 changes: 15 additions & 0 deletions Calculator/Calculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

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

class Program
{
static void Main(string[] args)
{
// Declare variables to run the app
bool endApp = false;
int cntUsing = 0;
double ans = 0;
List<string> history = new List<string>();

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

Calculator calculator = new Calculator();

while (!endApp)
{
cntUsing++;
int menuChoice = Menu.Menu.ShowMenu();

switch (menuChoice)
{
case 1: // New calculation
// Declare variables and set to empty
// Use Nullable types (with ?) wo 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: ");
numInput1 = Console.ReadLine();

double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
if (numInput1.ToLower().Equals("ans"))
{
cleanNum1 = ans;
break;
}
Console.Write("This is not a valid input. Please enter a numeric value: ");
numInput1 = 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("\tsq - Square Root");
Console.WriteLine("\tp - Power");
Console.WriteLine("\te - 10x");
Console.WriteLine("\tt - Trigonmetric functions");
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|sq|p|e|t]"))
{
Console.WriteLine("Error: Unrecognized input.");
}
else
{
double cleanNum2 = 0;
int functionChoice = 0;
if (op.Equals("t"))
{
Console.WriteLine("Type a number, then press Enter to choose: ");
Console.WriteLine("\t1. Sine");
Console.WriteLine("\t2. Cosine");
Console.WriteLine("\t3. Tangent");
Console.WriteLine("\t4. Cotangent");
string? functionInput = Console.ReadLine();
while (!int.TryParse(functionInput, out functionChoice) || functionChoice < 1 || functionChoice > 4)
{
Console.Write("This is not a valid input. Please enter a number from 1 to 3: ");
functionInput = Console.ReadLine();
}
}
else if (!op.Equals("sq") && !op.Equals("e"))
{
// Ask the user to type the second number
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();

while (!double.TryParse(numInput2, out cleanNum2))
{
if (numInput2.ToLower().Equals("ans"))
{
cleanNum2 = ans;
break;
}
Console.Write("This is not valid input. Please enter a numeric value: ");
numInput2 = Console.ReadLine();
}
}
try
{
result = calculator.DoOperation(cleanNum1, op, cleanNum2, functionChoice);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else
{
Console.WriteLine("Your result: {0:0.##}\n", result);
history.Add(calculator.SetHistoryString(cleanNum1, op, cleanNum2, functionChoice, result));
ans = result;
}
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
}
Console.WriteLine("------------------------\n");
break;
case 2: // Show history
Console.WriteLine("Calculation history:");
foreach(string s in history)
Console.WriteLine(s);
Console.WriteLine();
break;
case 3: // Delete history
history.Clear();
Console.WriteLine("Calculation history deleted.");
break;
default:
break;
}

// Wait for the user to respond before closing.
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($"You have used the calculator {cntUsing} times.");
Console.WriteLine("\n"); // Friendly linespacing.
calculator.Finish();
return;
}
}
71 changes: 71 additions & 0 deletions Calculator/bin/Debug/net9.0/Calculator.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v9.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v9.0": {
"Calculator/1.0.0": {
"dependencies": {
"CalculatorLibrary": "1.0.0",
"Menu": "1.0.0"
},
"runtime": {
"Calculator.dll": {}
}
},
"Newtonsoft.Json/13.0.4": {
"runtime": {
"lib/net6.0/Newtonsoft.Json.dll": {
"assemblyVersion": "13.0.0.0",
"fileVersion": "13.0.4.30916"
}
}
},
"CalculatorLibrary/1.0.0": {
"dependencies": {
"Newtonsoft.Json": "13.0.4"
},
"runtime": {
"CalculatorLibrary.dll": {
"assemblyVersion": "1.0.0.0",
"fileVersion": "1.0.0.0"
}
}
},
"Menu/1.0.0": {
"runtime": {
"Menu.dll": {
"assemblyVersion": "1.0.0.0",
"fileVersion": "1.0.0.0"
}
}
}
}
},
"libraries": {
"Calculator/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"Newtonsoft.Json/13.0.4": {
"type": "package",
"serviceable": true,
"sha512": "sha512-pdgNNMai3zv51W5aq268sujXUyx7SNdE2bj1wZcWjAQrKMFZV260lbqYop1d2GM67JI1huLRwxo9ZqnfF/lC6A==",
"path": "newtonsoft.json/13.0.4",
"hashPath": "newtonsoft.json.13.0.4.nupkg.sha512"
},
"CalculatorLibrary/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"Menu/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
Binary file added Calculator/bin/Debug/net9.0/Calculator.dll
Binary file not shown.
Binary file added Calculator/bin/Debug/net9.0/Calculator.exe
Binary file not shown.
Binary file added Calculator/bin/Debug/net9.0/Calculator.pdb
Binary file not shown.
12 changes: 12 additions & 0 deletions Calculator/bin/Debug/net9.0/Calculator.runtimeconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net9.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "9.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
Binary file not shown.
Binary file added Calculator/bin/Debug/net9.0/CalculatorLibrary.pdb
Binary file not shown.
Binary file added Calculator/bin/Debug/net9.0/Menu.dll
Binary file not shown.
Binary file added Calculator/bin/Debug/net9.0/Menu.pdb
Binary file not shown.
Binary file added Calculator/bin/Debug/net9.0/Newtonsoft.Json.dll
Binary file not shown.
3 changes: 3 additions & 0 deletions Calculator/bin/Debug/net9.0/calculator.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Starting Calculator Log
Started 2/6/2026 3:31:44 PM
3 / 5 = 0.6
16 changes: 16 additions & 0 deletions Calculator/bin/Debug/net9.0/calculatorlog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Operations": [
{
"Operand1": 90.0,
"Operand2": 0.0,
"Operation": "Cosine",
"Result": -0.4480736255645752
},
{
"Operand1": 30.0,
"Operand2": 3201.0,
"Operation": "Add",
"Result": 3231.0
}
]
}
Loading