From de177ffe71fa027634bcb1aa115d6d46c006dc5c Mon Sep 17 00:00:00 2001 From: anon Date: Thu, 5 Feb 2026 20:14:48 -0600 Subject: [PATCH 1/5] initializes project --- CalculatorLibrary/Calculator.cs | 68 ++++++++++++++++++++++ CalculatorLibrary/CalculatorLibrary.csproj | 13 +++++ 2 files changed, 81 insertions(+) create mode 100644 CalculatorLibrary/Calculator.cs create mode 100644 CalculatorLibrary/CalculatorLibrary.csproj diff --git a/CalculatorLibrary/Calculator.cs b/CalculatorLibrary/Calculator.cs new file mode 100644 index 00000000..1a6e79d8 --- /dev/null +++ b/CalculatorLibrary/Calculator.cs @@ -0,0 +1,68 @@ +using System.Diagnostics; +using Newtonsoft.Json; + +namespace CalculatorLibrary; + +public class Calculator +{ + readonly JsonWriter writer; + + public Calculator() + { + StreamWriter logFile = File.CreateText("calculatorlog.json"); + logFile.AutoFlush = true; + writer = new JsonTextWriter(logFile) { Formatting = Formatting.Indented }; + writer.WriteStartObject(); + writer.WritePropertyName("Operations"); + writer.WriteStartArray(); + } + + public double DoOperation(double num1, double num2, string op) + { + double result = double.NaN; + + writer.WriteStartObject(); + writer.WritePropertyName("Operand1"); + writer.WriteValue(num1); + writer.WritePropertyName("Operand2"); + writer.WriteValue(num2); + writer.WritePropertyName("Operation"); + + 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": + if (num2 != 0) + { + result = num1 / num2; + } + writer.WriteValue("Divide"); + break; + default: + break; + } + writer.WritePropertyName("Result"); + writer.WriteValue(result); + writer.WriteEndObject(); + + return result; + } + + public void Finish() + { + writer.WriteEndArray(); + writer.WriteEndObject(); + writer.Close(); + } +} diff --git a/CalculatorLibrary/CalculatorLibrary.csproj b/CalculatorLibrary/CalculatorLibrary.csproj new file mode 100644 index 00000000..22badd14 --- /dev/null +++ b/CalculatorLibrary/CalculatorLibrary.csproj @@ -0,0 +1,13 @@ + + + + net10.0 + enable + enable + + + + + + + From a34dff7c1157ab17b7319641d0d4a8284c7a9692 Mon Sep 17 00:00:00 2001 From: jlong Date: Tue, 10 Feb 2026 19:13:48 -0600 Subject: [PATCH 2/5] adds basic screen refresh and history options --- CalculatorLibrary/Calculator.cs | 52 +++++++++++++++++++++++++++++---- calculator.qua9k.csproj | 10 +++++++ 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 calculator.qua9k.csproj diff --git a/CalculatorLibrary/Calculator.cs b/CalculatorLibrary/Calculator.cs index 1a6e79d8..64937af9 100644 --- a/CalculatorLibrary/Calculator.cs +++ b/CalculatorLibrary/Calculator.cs @@ -1,4 +1,4 @@ -using System.Diagnostics; +using System.Text; using Newtonsoft.Json; namespace CalculatorLibrary; @@ -7,6 +7,8 @@ public class Calculator { readonly JsonWriter writer; + readonly List history = []; + public Calculator() { StreamWriter logFile = File.CreateText("calculatorlog.json"); @@ -20,6 +22,7 @@ public Calculator() public double DoOperation(double num1, double num2, string op) { double result = double.NaN; + StringBuilder operation = new(); writer.WriteStartObject(); writer.WritePropertyName("Operand1"); @@ -30,25 +33,33 @@ public double DoOperation(double num1, double num2, string op) switch (op) { - case "a": + case "+": result = num1 + num2; writer.WriteValue("Add"); break; - case "s": + case "-": result = num1 - num2; writer.WriteValue("Subtract"); break; - case "m": + case "*": result = num1 * num2; writer.WriteValue("Multiply"); break; - case "d": + case "/": if (num2 != 0) { result = num1 / num2; } writer.WriteValue("Divide"); break; + case "^": + result = Math.Pow(num1, num2); + writer.WriteValue("Exponentiate"); + break; + case "log": + result = Math.Log(num1, num2); + writer.WriteValue("Logarithm"); + break; default: break; } @@ -56,9 +67,40 @@ public double DoOperation(double num1, double num2, string op) writer.WriteValue(result); writer.WriteEndObject(); + operation.Append($"{num1} {op} {num2} = {result}"); + history.Add(operation.ToString()); + return result; } + public void PrintHistory() + { + int i = 1; + + Console.WriteLine( + """ + History + + """ + ); + foreach (string operation in history) + { + Console.WriteLine($"{i}: {operation}"); + i += 1; + } + } + + public void ClearHistory() + { + Console.WriteLine( + """ + History cleared. + + """ + ); + history.Clear(); + } + public void Finish() { writer.WriteEndArray(); diff --git a/calculator.qua9k.csproj b/calculator.qua9k.csproj new file mode 100644 index 00000000..dfb40caa --- /dev/null +++ b/calculator.qua9k.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + From 4d7f0de08516fe50c2d5953cdc419aa56ebc4d0f Mon Sep 17 00:00:00 2001 From: jlong Date: Tue, 10 Feb 2026 20:07:35 -0600 Subject: [PATCH 3/5] adds ui class --- CalculatorLibrary/Calculator.cs | 7 +++++ CalculatorLibrary/UserInterface.cs | 50 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 CalculatorLibrary/UserInterface.cs diff --git a/CalculatorLibrary/Calculator.cs b/CalculatorLibrary/Calculator.cs index 64937af9..82619e26 100644 --- a/CalculatorLibrary/Calculator.cs +++ b/CalculatorLibrary/Calculator.cs @@ -63,6 +63,7 @@ public double DoOperation(double num1, double num2, string op) default: break; } + result = Math.Round(result, 2); writer.WritePropertyName("Result"); writer.WriteValue(result); writer.WriteEndObject(); @@ -75,6 +76,12 @@ public double DoOperation(double num1, double num2, string op) public void PrintHistory() { + if (history.Count <= 0) + { + Console.WriteLine("The history is empty."); + return; + } + int i = 1; Console.WriteLine( diff --git a/CalculatorLibrary/UserInterface.cs b/CalculatorLibrary/UserInterface.cs new file mode 100644 index 00000000..e57d2134 --- /dev/null +++ b/CalculatorLibrary/UserInterface.cs @@ -0,0 +1,50 @@ +namespace CalculatorLibrary; + +public class UserInterface +{ + public static void PrintOperations(Dictionary operatorMap) + { + foreach (KeyValuePair kvp in operatorMap) + { + Console.WriteLine($" {kvp.Key} : {kvp.Value}"); + } + } + + public static void Pause() + { + Console.WriteLine(); + Console.Write("Press any key to continue."); + Console.ReadKey(); + } + + public static void PrintMenuOptions() + { + Console.Write( + """ + - 'n' + Enter : close the app. + - 'p' + Enter : use the previous result as the first operand. + - 'h' + Enter : view operation history. + - 'd' + Enter : delete operation history. + - Any other key to perform a new calculation. + + """ + ); + } + + public static void PrintHeader() + { + Console.WriteLine("Console Calculator in C#\r"); + Console.WriteLine("------------------------\n"); + } + + public static void GetOperand(out double validNum) + { + string? userInput = Console.ReadLine(); + + while (!double.TryParse(userInput, out validNum)) + { + Console.Write("Invalid input. Enter a numeric value: "); + userInput = Console.ReadLine(); + } + } +} From 212bb06b6b361051c933696d7a0cf90e94fd0429 Mon Sep 17 00:00:00 2001 From: jlong Date: Tue, 10 Feb 2026 20:23:59 -0600 Subject: [PATCH 4/5] reorgs files --- CalculatorLibrary/UserInterface.cs | 50 ------ {CalculatorLibrary => lib}/Calculator.cs | 0 .../CalculatorLibrary.csproj | 0 src/Calculator.csproj | 14 ++ src/Program.cs | 163 ++++++++++++++++++ src/calculatorlog.json | 8 + 6 files changed, 185 insertions(+), 50 deletions(-) delete mode 100644 CalculatorLibrary/UserInterface.cs rename {CalculatorLibrary => lib}/Calculator.cs (100%) rename {CalculatorLibrary => lib}/CalculatorLibrary.csproj (100%) create mode 100644 src/Calculator.csproj create mode 100644 src/Program.cs create mode 100644 src/calculatorlog.json diff --git a/CalculatorLibrary/UserInterface.cs b/CalculatorLibrary/UserInterface.cs deleted file mode 100644 index e57d2134..00000000 --- a/CalculatorLibrary/UserInterface.cs +++ /dev/null @@ -1,50 +0,0 @@ -namespace CalculatorLibrary; - -public class UserInterface -{ - public static void PrintOperations(Dictionary operatorMap) - { - foreach (KeyValuePair kvp in operatorMap) - { - Console.WriteLine($" {kvp.Key} : {kvp.Value}"); - } - } - - public static void Pause() - { - Console.WriteLine(); - Console.Write("Press any key to continue."); - Console.ReadKey(); - } - - public static void PrintMenuOptions() - { - Console.Write( - """ - - 'n' + Enter : close the app. - - 'p' + Enter : use the previous result as the first operand. - - 'h' + Enter : view operation history. - - 'd' + Enter : delete operation history. - - Any other key to perform a new calculation. - - """ - ); - } - - public static void PrintHeader() - { - Console.WriteLine("Console Calculator in C#\r"); - Console.WriteLine("------------------------\n"); - } - - public static void GetOperand(out double validNum) - { - string? userInput = Console.ReadLine(); - - while (!double.TryParse(userInput, out validNum)) - { - Console.Write("Invalid input. Enter a numeric value: "); - userInput = Console.ReadLine(); - } - } -} diff --git a/CalculatorLibrary/Calculator.cs b/lib/Calculator.cs similarity index 100% rename from CalculatorLibrary/Calculator.cs rename to lib/Calculator.cs diff --git a/CalculatorLibrary/CalculatorLibrary.csproj b/lib/CalculatorLibrary.csproj similarity index 100% rename from CalculatorLibrary/CalculatorLibrary.csproj rename to lib/CalculatorLibrary.csproj diff --git a/src/Calculator.csproj b/src/Calculator.csproj new file mode 100644 index 00000000..166dab57 --- /dev/null +++ b/src/Calculator.csproj @@ -0,0 +1,14 @@ + + + + + + + + Exe + net10.0 + enable + enable + + + diff --git a/src/Program.cs b/src/Program.cs new file mode 100644 index 00000000..e6410c0b --- /dev/null +++ b/src/Program.cs @@ -0,0 +1,163 @@ +using CalculatorLibrary; + +class Program +{ + static void Main() + { + bool endApp = false; + Calculator calc = new(); + Dictionary operatorMap = new() + { + { "+", "Add" }, + { "-", "Subtract" }, + { "*", "Multiply" }, + { "/", "Divide" }, + { "^", "Exponentiate" }, + { "log", "Logarithm" }, + }; + + double validOperand1 = double.MinValue; + string? postCalcChoice = ""; + + while (!endApp) + { + Console.Clear(); + UserInterface.PrintHeader(); + + if (postCalcChoice == "p") + { + Console.WriteLine($"Previous result: {validOperand1}"); + } + else + { + Console.Write("Enter the first operand: "); + GetOperand(out validOperand1); + } + + Console.Write("Enter the second operand: "); + GetOperand(out double validOperand2); + + Console.WriteLine("Choose an operator:"); + UserInterface.PrintOperations(operatorMap); + + Console.Write("Your option? "); + string? op = Console.ReadLine(); + double result = double.MinValue; + + if (op == null || !operatorMap.ContainsKey(op)) + { + Console.WriteLine("Error: Unrecognized input."); + } + else + { + try + { + result = calc.DoOperation(validOperand1, validOperand2, 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("An exception occurred.\n - Details: " + e.Message); + } + } + + UserInterface.Pause(); + UserInterface.PrintMenuOptions(); + + postCalcChoice = Console.ReadLine(); + + while (postCalcChoice == "h" || postCalcChoice == "d") + { + if (postCalcChoice == "h") + { + Console.Clear(); + UserInterface.PrintHeader(); + calc.PrintHistory(); + } + else + { + Console.Clear(); + UserInterface.PrintHeader(); + calc.ClearHistory(); + } + UserInterface.Pause(); + UserInterface.PrintMenuOptions(); + postCalcChoice = Console.ReadLine(); + } + + switch (postCalcChoice) + { + case "p": + validOperand1 = result; + break; + case "n": + Console.WriteLine("Goodbye!"); + endApp = true; + break; + default: + break; + } + } + + calc.Finish(); + + return; + } + + public static void GetOperand(out double validNum) + { + string? userInput = Console.ReadLine(); + + while (!double.TryParse(userInput, out validNum)) + { + Console.Write("Invalid input. Enter a numeric value: "); + userInput = Console.ReadLine(); + } + } +} + +static class UserInterface +{ + public static void PrintOperations(Dictionary operatorMap) + { + foreach (KeyValuePair kvp in operatorMap) + { + Console.WriteLine($" {kvp.Key} : {kvp.Value}"); + } + } + + public static void Pause() + { + Console.WriteLine(); + Console.Write("Press any key to continue."); + Console.ReadKey(); + } + + public static void PrintMenuOptions() + { + Console.Write( + """ + - 'n' + Enter : close the app. + - 'p' + Enter : use the previous result as the first operand. + - 'h' + Enter : view operation history. + - 'd' + Enter : delete operation history. + - Any other key to perform a new calculation. + + """ + ); + } + + public static void PrintHeader() + { + Console.WriteLine("Console Calculator in C#\r"); + Console.WriteLine("------------------------\n"); + } +} diff --git a/src/calculatorlog.json b/src/calculatorlog.json new file mode 100644 index 00000000..5817c05a --- /dev/null +++ b/src/calculatorlog.json @@ -0,0 +1,8 @@ +{ + "Operations": [ + { + "Operand1": 3.0, + "Operand2": 3.0, + "Operation": "Divide", + "Result": 1.0 + } \ No newline at end of file From ea21260ca92be8e05e1f919d1e97f1fc7ed8dee5 Mon Sep 17 00:00:00 2001 From: jlong Date: Sat, 14 Feb 2026 19:09:45 -0600 Subject: [PATCH 5/5] various small changes --- CodeReviews.Console.Calculator.sln | 56 ++++++++++++++ lib/Calculator.cs | 117 ----------------------------- lib/CalculatorLibrary.csproj | 13 ---- src/Calculator.cs | 112 +++++++++++++++++++++++++++ src/Program.cs | 93 ++++++----------------- src/UserInterface.cs | 36 +++++++++ src/calculator_log.json | 1 + src/calculatorlog.json | 8 -- 8 files changed, 226 insertions(+), 210 deletions(-) create mode 100644 CodeReviews.Console.Calculator.sln delete mode 100644 lib/Calculator.cs delete mode 100644 lib/CalculatorLibrary.csproj create mode 100644 src/Calculator.cs create mode 100644 src/UserInterface.cs create mode 100644 src/calculator_log.json delete mode 100644 src/calculatorlog.json diff --git a/CodeReviews.Console.Calculator.sln b/CodeReviews.Console.Calculator.sln new file mode 100644 index 00000000..6de92dc7 --- /dev/null +++ b/CodeReviews.Console.Calculator.sln @@ -0,0 +1,56 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator", "src\Calculator.csproj", "{23269A6C-8755-4F73-98C1-FEB1E64BE94C}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lib", "lib", "{3A8DF596-E814-FECC-DD4B-D8EF8AAC1A0D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculatorLibrary", "lib\CalculatorLibrary.csproj", "{D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Debug|x64.ActiveCfg = Debug|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Debug|x64.Build.0 = Debug|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Debug|x86.ActiveCfg = Debug|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Debug|x86.Build.0 = Debug|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Release|Any CPU.Build.0 = Release|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Release|x64.ActiveCfg = Release|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Release|x64.Build.0 = Release|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Release|x86.ActiveCfg = Release|Any CPU + {23269A6C-8755-4F73-98C1-FEB1E64BE94C}.Release|x86.Build.0 = Release|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Debug|x64.ActiveCfg = Debug|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Debug|x64.Build.0 = Debug|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Debug|x86.ActiveCfg = Debug|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Debug|x86.Build.0 = Debug|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Release|Any CPU.Build.0 = Release|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Release|x64.ActiveCfg = Release|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Release|x64.Build.0 = Release|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Release|x86.ActiveCfg = Release|Any CPU + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {23269A6C-8755-4F73-98C1-FEB1E64BE94C} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {D2B87518-0CC2-4DF9-B257-5FBBFB7BE093} = {3A8DF596-E814-FECC-DD4B-D8EF8AAC1A0D} + EndGlobalSection +EndGlobal diff --git a/lib/Calculator.cs b/lib/Calculator.cs deleted file mode 100644 index 82619e26..00000000 --- a/lib/Calculator.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System.Text; -using Newtonsoft.Json; - -namespace CalculatorLibrary; - -public class Calculator -{ - readonly JsonWriter writer; - - readonly List history = []; - - public Calculator() - { - StreamWriter logFile = File.CreateText("calculatorlog.json"); - logFile.AutoFlush = true; - writer = new JsonTextWriter(logFile) { Formatting = Formatting.Indented }; - writer.WriteStartObject(); - writer.WritePropertyName("Operations"); - writer.WriteStartArray(); - } - - public double DoOperation(double num1, double num2, string op) - { - double result = double.NaN; - StringBuilder operation = new(); - - writer.WriteStartObject(); - writer.WritePropertyName("Operand1"); - writer.WriteValue(num1); - writer.WritePropertyName("Operand2"); - writer.WriteValue(num2); - writer.WritePropertyName("Operation"); - - switch (op) - { - case "+": - result = num1 + num2; - writer.WriteValue("Add"); - break; - case "-": - result = num1 - num2; - writer.WriteValue("Subtract"); - break; - case "*": - result = num1 * num2; - writer.WriteValue("Multiply"); - break; - case "/": - if (num2 != 0) - { - result = num1 / num2; - } - writer.WriteValue("Divide"); - break; - case "^": - result = Math.Pow(num1, num2); - writer.WriteValue("Exponentiate"); - break; - case "log": - result = Math.Log(num1, num2); - writer.WriteValue("Logarithm"); - break; - default: - break; - } - result = Math.Round(result, 2); - writer.WritePropertyName("Result"); - writer.WriteValue(result); - writer.WriteEndObject(); - - operation.Append($"{num1} {op} {num2} = {result}"); - history.Add(operation.ToString()); - - return result; - } - - public void PrintHistory() - { - if (history.Count <= 0) - { - Console.WriteLine("The history is empty."); - return; - } - - int i = 1; - - Console.WriteLine( - """ - History - - """ - ); - foreach (string operation in history) - { - Console.WriteLine($"{i}: {operation}"); - i += 1; - } - } - - public void ClearHistory() - { - Console.WriteLine( - """ - History cleared. - - """ - ); - history.Clear(); - } - - public void Finish() - { - writer.WriteEndArray(); - writer.WriteEndObject(); - writer.Close(); - } -} diff --git a/lib/CalculatorLibrary.csproj b/lib/CalculatorLibrary.csproj deleted file mode 100644 index 22badd14..00000000 --- a/lib/CalculatorLibrary.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net10.0 - enable - enable - - - - - - - diff --git a/src/Calculator.cs b/src/Calculator.cs new file mode 100644 index 00000000..69714ef9 --- /dev/null +++ b/src/Calculator.cs @@ -0,0 +1,112 @@ +using System.Text; +using System.Text.Json; + +namespace calculator.qua9k.lib; + +public class Calculator +{ + private readonly Utf8JsonWriter _writer; + private readonly List _history = []; + + public Calculator() + { + var logFile = File.OpenWrite(path: "calculator_log.json"); + _writer = new Utf8JsonWriter(logFile); + _writer.WriteStartObject(); + _writer.WritePropertyName("Operations"); + _writer.WriteStartArray(); + } + + public double DoOperation(double num1, double num2, string op) + { + var result = double.NaN; + StringBuilder operation = new(); + + _writer.WriteStartObject(); + _writer.WritePropertyName("Operand1"); + _writer.WriteNumberValue(num1); + _writer.WritePropertyName("Operand2"); + _writer.WriteNumberValue(num2); + _writer.WritePropertyName("Operation"); + + switch (op) + { + case "+": + result = num1 + num2; + _writer.WriteStringValue("Add"); + break; + case "-": + result = num1 - num2; + _writer.WriteStringValue("Subtract"); + break; + case "*": + result = num1 * num2; + _writer.WriteStringValue("Multiply"); + break; + case "/": + if (num2 != 0) + { + result = num1 / num2; + } + _writer.WriteStringValue("Divide"); + break; + case "^": + result = Math.Pow(num1, num2); + _writer.WriteStringValue("Exponentiate"); + break; + case "log": + result = Math.Log(num1, num2); + _writer.WriteStringValue("Logarithm"); + break; + } + result = Math.Round(result, 2); + _writer.WritePropertyName("Result"); + _writer.WriteNumberValue(result); + _writer.WriteEndObject(); + + operation.Append($"{num1} {op} {num2} = {result}"); + _history.Add(operation.ToString()); + + return result; + } + + public void PrintHistory() + { + if (_history.Count <= 0) + { + Console.WriteLine("The history is empty."); + return; + } + + Console.WriteLine( + """ + History + + """ + ); + + for (var i = 0; i < _history.Count; i++) + { + Console.WriteLine($"{i}: {_history[i]}"); + } + } + + public void ClearHistory() + { + Console.WriteLine( + """ + History cleared. + + """ + ); + _history.Clear(); + } + + public void Finish() + { + _writer.WriteEndArray(); + _writer.WriteEndObject(); + _writer.Flush(); + _writer.Dispose(); + } +} \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs index e6410c0b..71eee411 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,10 +1,11 @@ -using CalculatorLibrary; +using calculator.qua9k.lib; -class Program +namespace calculator.qua9k; + +internal class Program { - static void Main() + private static void Main() { - bool endApp = false; Calculator calc = new(); Dictionary operatorMap = new() { @@ -13,11 +14,12 @@ static void Main() { "*", "Multiply" }, { "/", "Divide" }, { "^", "Exponentiate" }, - { "log", "Logarithm" }, + { "log", "Logarithm" } }; - double validOperand1 = double.MinValue; - string? postCalcChoice = ""; + var validOperand1 = double.MinValue; + var postCalcChoice = ""; + var endApp = false; while (!endApp) { @@ -35,61 +37,50 @@ static void Main() } Console.Write("Enter the second operand: "); - GetOperand(out double validOperand2); + GetOperand(out var validOperand2); Console.WriteLine("Choose an operator:"); UserInterface.PrintOperations(operatorMap); Console.Write("Your option? "); - string? op = Console.ReadLine(); - double result = double.MinValue; + var op = Console.ReadLine(); + var result = double.MinValue; if (op == null || !operatorMap.ContainsKey(op)) - { Console.WriteLine("Error: Unrecognized input."); - } else - { try { result = calc.DoOperation(validOperand1, validOperand2, 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("An exception occurred.\n - Details: " + e.Message); } - } UserInterface.Pause(); UserInterface.PrintMenuOptions(); postCalcChoice = Console.ReadLine(); - while (postCalcChoice == "h" || postCalcChoice == "d") + while (postCalcChoice is "h" or "d") { + Console.Clear(); + UserInterface.PrintHeader(); + if (postCalcChoice == "h") - { - Console.Clear(); - UserInterface.PrintHeader(); calc.PrintHistory(); - } else - { - Console.Clear(); - UserInterface.PrintHeader(); calc.ClearHistory(); - } + UserInterface.Pause(); UserInterface.PrintMenuOptions(); + postCalcChoice = Console.ReadLine(); } @@ -102,19 +93,15 @@ static void Main() Console.WriteLine("Goodbye!"); endApp = true; break; - default: - break; } } calc.Finish(); - - return; } - public static void GetOperand(out double validNum) + private static void GetOperand(out double validNum) { - string? userInput = Console.ReadLine(); + var userInput = Console.ReadLine(); while (!double.TryParse(userInput, out validNum)) { @@ -122,42 +109,4 @@ public static void GetOperand(out double validNum) userInput = Console.ReadLine(); } } -} - -static class UserInterface -{ - public static void PrintOperations(Dictionary operatorMap) - { - foreach (KeyValuePair kvp in operatorMap) - { - Console.WriteLine($" {kvp.Key} : {kvp.Value}"); - } - } - - public static void Pause() - { - Console.WriteLine(); - Console.Write("Press any key to continue."); - Console.ReadKey(); - } - - public static void PrintMenuOptions() - { - Console.Write( - """ - - 'n' + Enter : close the app. - - 'p' + Enter : use the previous result as the first operand. - - 'h' + Enter : view operation history. - - 'd' + Enter : delete operation history. - - Any other key to perform a new calculation. - - """ - ); - } - - public static void PrintHeader() - { - Console.WriteLine("Console Calculator in C#\r"); - Console.WriteLine("------------------------\n"); - } -} +} \ No newline at end of file diff --git a/src/UserInterface.cs b/src/UserInterface.cs new file mode 100644 index 00000000..1dd6a369 --- /dev/null +++ b/src/UserInterface.cs @@ -0,0 +1,36 @@ +namespace calculator.qua9k; + +internal static class UserInterface +{ + public static void PrintOperations(Dictionary operatorMap) + { + foreach (var kvp in operatorMap) Console.WriteLine($" {kvp.Key} : {kvp.Value}"); + } + + public static void Pause() + { + Console.WriteLine(); + Console.Write("Press any key to continue."); + Console.ReadKey(); + } + + public static void PrintMenuOptions() + { + Console.Write( + """ + - 'n' + Enter : close the app. + - 'p' + Enter : use the previous result as the first operand. + - 'h' + Enter : view operation history. + - 'd' + Enter : delete operation history. + - Any other key to perform a new calculation. + + """ + ); + } + + public static void PrintHeader() + { + Console.WriteLine("Console Calculator in C#\r"); + Console.WriteLine("------------------------\n"); + } +} \ No newline at end of file diff --git a/src/calculator_log.json b/src/calculator_log.json new file mode 100644 index 00000000..ac3fa14c --- /dev/null +++ b/src/calculator_log.json @@ -0,0 +1 @@ +{"Operations":[{"Operand1":3,"Operand2":3,"Operation":"Add","Result":6}]} \ No newline at end of file diff --git a/src/calculatorlog.json b/src/calculatorlog.json deleted file mode 100644 index 5817c05a..00000000 --- a/src/calculatorlog.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Operations": [ - { - "Operand1": 3.0, - "Operand2": 3.0, - "Operation": "Divide", - "Result": 1.0 - } \ No newline at end of file