From 57801dd41c948096193725793e9052634c286dd8 Mon Sep 17 00:00:00 2001 From: SylvesterNdwata Date: Tue, 28 Apr 2026 16:13:36 +0200 Subject: [PATCH 1/3] First Version Calculator Project --- silvermax.Calculator/Calculator.slnx | 4 + .../Calculator/Calculator.csproj | 14 ++ silvermax.Calculator/Calculator/Program.cs | 175 ++++++++++++++++++ .../CalculatorLibrary.csproj | 13 ++ .../CalculatorLibrary/CalculatorProgram.cs | 81 ++++++++ 5 files changed, 287 insertions(+) create mode 100644 silvermax.Calculator/Calculator.slnx create mode 100644 silvermax.Calculator/Calculator/Calculator.csproj create mode 100644 silvermax.Calculator/Calculator/Program.cs create mode 100644 silvermax.Calculator/CalculatorLibrary/CalculatorLibrary.csproj create mode 100644 silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs diff --git a/silvermax.Calculator/Calculator.slnx b/silvermax.Calculator/Calculator.slnx new file mode 100644 index 00000000..93931338 --- /dev/null +++ b/silvermax.Calculator/Calculator.slnx @@ -0,0 +1,4 @@ + + + + diff --git a/silvermax.Calculator/Calculator/Calculator.csproj b/silvermax.Calculator/Calculator/Calculator.csproj new file mode 100644 index 00000000..477b00a5 --- /dev/null +++ b/silvermax.Calculator/Calculator/Calculator.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/silvermax.Calculator/Calculator/Program.cs b/silvermax.Calculator/Calculator/Program.cs new file mode 100644 index 00000000..0c774e3e --- /dev/null +++ b/silvermax.Calculator/Calculator/Program.cs @@ -0,0 +1,175 @@ +using System.Text.RegularExpressions; +using CalculatorLibrary; +class Program +{ + static void Main(string[] args) + { + Calculator calculator = new Calculator(); + bool endApp = false; + int CalculatorUsedTimes = 0; + List latestQuestions = new(); + double previousResult = 0; + // Display title as the C# console calculator app. + Console.WriteLine("Console Calculator in C#\r"); + Console.WriteLine("------------------------\n"); + + while (!endApp) + { + Console.WriteLine(@"What Calculation would you like to perform? + 1. Four-function Arithmetic + 2. SquareRoot + 3. Power Calculation + 4. Trigonometry"); + + var readResult = Console.ReadLine(); + switch (readResult) + { + case "1": + + // Declare variables and set to empty. + // Use Nullable types (with ?) to match type of System.Console.ReadLine + string? numInput1 = ""; + string? numInput2 = ""; + double result = 0; + + if (previousResult != 0) + { + // Ask the user to type the first number. + Console.WriteLine("Would you like to use the previous answer?Type y or n: "); + string? response = Console.ReadLine(); + if (response == "y") + { + numInput1 = Convert.ToString(previousResult); + } + } + else + { + Console.Write("Type a number, and then press Enter: "); + numInput1 = Console.ReadLine(); + } + + double cleanNum1 = 0; + while (!double.TryParse(numInput1, out cleanNum1)) + { + Console.Write("This is not valid input. Please enter a numeric value: "); + numInput1 = Console.ReadLine(); + } + + // Ask the user to type the second number. + Console.Write("Type another number, and then press Enter: "); + numInput2 = Console.ReadLine(); + + double cleanNum2 = 0; + while (!double.TryParse(numInput2, out cleanNum2)) + { + Console.Write("This is not valid input. Please enter a numeric 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.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]")) + { + 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 + { + Console.WriteLine("Your result: {0:0.##}\n", result); + latestQuestions.Add($"{cleanNum1} {op} {cleanNum2} = {result}"); + CalculatorUsedTimes++; + previousResult = result; + } + } + catch (Exception e) + { + Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); + } + } + break; + + case "2": + Console.WriteLine("Input the number: "); + readResult = Console.ReadLine(); + + double num = 0; + + while (!double.TryParse(readResult, out num)) + { + Console.WriteLine("Invalid input please input a valid number"); + readResult = Console.ReadLine(); + } + + result = calculator.SquareRoot(num); + Console.WriteLine("Your result: {0:0.##}\n", result); + latestQuestions.Add($"The square root of {num} is {result}"); + CalculatorUsedTimes++; + previousResult = result; + + break; + + case "3": + Console.WriteLine("Input the number: "); + readResult = Console.ReadLine(); + double num1 = 0; + + while (!double.TryParse(readResult, out num1)) + { + Console.WriteLine("Invalid input please input a valid number"); + readResult = Console.ReadLine(); + } + + Console.WriteLine("input a number to raise to: "); + readResult = Console.ReadLine(); + double power = 0; + + while (!double.TryParse(readResult, out power)) + { + Console.WriteLine("Invalid input please input a valid number"); + readResult = Console.ReadLine(); + } + + result = calculator.PowerCalculate(num1, power); + Console.WriteLine("Your result: {0:0.##}\n", result); + latestQuestions.Add($"The power of {num1} to {power} is {result}"); + CalculatorUsedTimes++; + previousResult = result; + + break; + } + Console.WriteLine("------------------------\n"); + + // Wait for the user to respond before closing. + Console.Write("Press 'n' and Enter to close the app or press c to clear questions history, or press any other key and Enter to continue: "); + if (Console.ReadLine() == "n") + { + endApp = true; + } + else if (Console.ReadLine() == "c") + { + latestQuestions.Clear(); + } + + Console.WriteLine("\n"); + } + calculator.Finish(); + return; + } +} \ No newline at end of file diff --git a/silvermax.Calculator/CalculatorLibrary/CalculatorLibrary.csproj b/silvermax.Calculator/CalculatorLibrary/CalculatorLibrary.csproj new file mode 100644 index 00000000..2c2f89b6 --- /dev/null +++ b/silvermax.Calculator/CalculatorLibrary/CalculatorLibrary.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs b/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs new file mode 100644 index 00000000..0ae7b926 --- /dev/null +++ b/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs @@ -0,0 +1,81 @@ +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; + // 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 double SquareRoot(double num) + { + return Math.Sqrt(num); + } + + public double PowerCalculate(double num, double power) + { + return Math.Pow(num, power); + } + + +} + + From 344ae3ed8a59d5fba05b91c6604b2868762ae1ee Mon Sep 17 00:00:00 2001 From: SylvesterNdwata Date: Fri, 1 May 2026 13:39:43 +0200 Subject: [PATCH 2/3] feat: Add trigonometry calculations and fix errors from PR --- silvermax.Calculator/Calculator/Program.cs | 143 +++++++++++++----- .../CalculatorLibrary/CalculatorProgram.cs | 23 +++ 2 files changed, 131 insertions(+), 35 deletions(-) diff --git a/silvermax.Calculator/Calculator/Program.cs b/silvermax.Calculator/Calculator/Program.cs index 0c774e3e..233776cb 100644 --- a/silvermax.Calculator/Calculator/Program.cs +++ b/silvermax.Calculator/Calculator/Program.cs @@ -1,15 +1,16 @@ using System.Text.RegularExpressions; using CalculatorLibrary; + class Program { static void Main(string[] args) { Calculator calculator = new Calculator(); bool endApp = false; - int CalculatorUsedTimes = 0; + int calculatorUsedTimes = 0; List latestQuestions = new(); double previousResult = 0; - // Display title as the C# console calculator app. + Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); @@ -22,25 +23,27 @@ 3. Power Calculation 4. Trigonometry"); var readResult = Console.ReadLine(); + switch (readResult) { case "1": - - // Declare variables and set to empty. - // Use Nullable types (with ?) to match type of System.Console.ReadLine string? numInput1 = ""; string? numInput2 = ""; double result = 0; if (previousResult != 0) { - // Ask the user to type the first number. - Console.WriteLine("Would you like to use the previous answer?Type y or n: "); - string? response = Console.ReadLine(); + Console.WriteLine("Would you like to use the previous answer? Type y or n: "); + string? response = Console.ReadLine()?.Trim().ToLower(); if (response == "y") { numInput1 = Convert.ToString(previousResult); } + else + { + Console.WriteLine("Type a number, and then press Enter: "); + numInput1 = Console.ReadLine(); + } } else { @@ -55,7 +58,6 @@ 3. Power Calculation numInput1 = Console.ReadLine(); } - // Ask the user to type the second number. Console.Write("Type another number, and then press Enter: "); numInput2 = Console.ReadLine(); @@ -66,7 +68,6 @@ 3. Power Calculation 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"); @@ -74,9 +75,8 @@ 3. Power Calculation Console.WriteLine("\td - Divide"); Console.Write("Your option? "); - string? op = Console.ReadLine(); + string? op = Console.ReadLine().Trim().ToLower(); - // Validate input is not null, and matches the pattern if (op == null || !Regex.IsMatch(op, "[a|s|m|d]")) { Console.WriteLine("Error: Unrecognized input."); @@ -93,8 +93,25 @@ 3. Power Calculation else { Console.WriteLine("Your result: {0:0.##}\n", result); - latestQuestions.Add($"{cleanNum1} {op} {cleanNum2} = {result}"); - CalculatorUsedTimes++; + switch (op) + { + case "a": + latestQuestions.Add($"{cleanNum1} + {cleanNum2} = {result}"); + break; + + case "s": + latestQuestions.Add($"{cleanNum1} - {cleanNum2} = {result}"); + break; + + case "m": + latestQuestions.Add($"{cleanNum1} * {cleanNum2} = {result}"); + break; + + case "d": + latestQuestions.Add($"{cleanNum1} / {cleanNum2} = {result}"); + break; + } + calculatorUsedTimes++; previousResult = result; } } @@ -103,73 +120,129 @@ 3. Power Calculation Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); } } + PrintQuestions(latestQuestions); + Console.WriteLine($"The calculator has been used {calculatorUsedTimes} times"); break; case "2": Console.WriteLine("Input the number: "); - readResult = Console.ReadLine(); - + var squareResponse = Console.ReadLine(); double num = 0; - while (!double.TryParse(readResult, out num)) + while (!double.TryParse(squareResponse, out num)) { Console.WriteLine("Invalid input please input a valid number"); - readResult = Console.ReadLine(); + squareResponse = Console.ReadLine(); } result = calculator.SquareRoot(num); Console.WriteLine("Your result: {0:0.##}\n", result); latestQuestions.Add($"The square root of {num} is {result}"); - CalculatorUsedTimes++; + calculatorUsedTimes++; previousResult = result; - + PrintQuestions(latestQuestions); + Console.WriteLine($"The calculator has been used {calculatorUsedTimes} times"); break; case "3": Console.WriteLine("Input the number: "); - readResult = Console.ReadLine(); + var reply = Console.ReadLine(); double num1 = 0; - while (!double.TryParse(readResult, out num1)) + while (!double.TryParse(reply, out num1)) { Console.WriteLine("Invalid input please input a valid number"); - readResult = Console.ReadLine(); + reply = Console.ReadLine(); } - Console.WriteLine("input a number to raise to: "); - readResult = Console.ReadLine(); + Console.WriteLine("Input a number to raise to: "); + var powerResponse = Console.ReadLine(); double power = 0; - while (!double.TryParse(readResult, out power)) + while (!double.TryParse(powerResponse, out power)) { Console.WriteLine("Invalid input please input a valid number"); - readResult = Console.ReadLine(); + powerResponse = Console.ReadLine(); } result = calculator.PowerCalculate(num1, power); Console.WriteLine("Your result: {0:0.##}\n", result); latestQuestions.Add($"The power of {num1} to {power} is {result}"); - CalculatorUsedTimes++; + calculatorUsedTimes++; previousResult = result; + PrintQuestions(latestQuestions); + Console.WriteLine($"The calculator has been used {calculatorUsedTimes} times"); + break; + + + case "4": + Console.WriteLine("Choose a trigonometric function:"); + Console.WriteLine("\tsin - Sine"); + Console.WriteLine("\tcos - Cosine"); + Console.WriteLine("\ttan - Tangent"); + Console.Write("Your option? "); + + string? trigFunc = Console.ReadLine()?.Trim().ToLower(); + + if (trigFunc == null || (trigFunc != "sin" && trigFunc != "cos" && trigFunc != "tan")) + { + Console.WriteLine("Error: Unrecognized trigonometric function."); + break; + } + + Console.Write("Enter the angle in degrees: "); + var angleInput = Console.ReadLine(); + double angle = 0; + while (!double.TryParse(angleInput, out angle)) + { + Console.WriteLine("Invalid input, please enter a valid number: "); + angleInput = Console.ReadLine(); + } + + double trigResult = calculator.Trigonometry(trigFunc, angle); + Console.WriteLine("Your result: {0:0.##}\n", trigResult); + latestQuestions.Add($"{trigFunc}({angle}°) = {trigResult}"); + calculatorUsedTimes++; + previousResult = trigResult; + PrintQuestions(latestQuestions); + Console.WriteLine($"The calculator has been used {calculatorUsedTimes} times"); break; - } + + default: + Console.WriteLine("Error: Unrecognized option."); + break; + } // closes switch + Console.WriteLine("------------------------\n"); - // Wait for the user to respond before closing. - Console.Write("Press 'n' and Enter to close the app or press c to clear questions history, or press any other key and Enter to continue: "); - if (Console.ReadLine() == "n") + Console.Write("Press 'n' and Enter to close the app, 'c' to clear question history, or any other key to continue: "); + var endInput = Console.ReadLine(); + + if (endInput == "n") { endApp = true; } - else if (Console.ReadLine() == "c") + else if (endInput == "c") { latestQuestions.Clear(); } Console.WriteLine("\n"); + Console.Clear(); } + calculator.Finish(); - return; } -} \ No newline at end of file + + static void PrintQuestions(List list) + { + Console.WriteLine("Questions History: \n"); + foreach (var qn in list) + { + Console.WriteLine(qn); + } + Console.WriteLine(); + } +} + diff --git a/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs b/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs index 0ae7b926..b05d529c 100644 --- a/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs +++ b/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs @@ -75,7 +75,30 @@ public double PowerCalculate(double num, double power) return Math.Pow(num, power); } + public double Trigonometry(string function, double angleInDegrees) + { + double angleInRadians = angleInDegrees * (Math.PI / 180); + + writer.WriteStartObject(); + writer.WritePropertyName("Operation"); + writer.WriteValue($"Trig-{function}"); + writer.WritePropertyName("Operand1"); + writer.WriteValue(angleInDegrees); + + double result = function switch + { + "sin" => Math.Sin(angleInRadians), + "cos" => Math.Cos(angleInRadians), + "tan" => Math.Tan(angleInRadians), + _ => double.NaN + }; + writer.WritePropertyName("Result"); + writer.WriteValue(result); + writer.WriteEndObject(); + + return result; + } } From 6c5dd6ea1c70346f42e12e77735aecda1d1896eb Mon Sep 17 00:00:00 2001 From: SylvesterNdwata Date: Fri, 1 May 2026 13:42:20 +0200 Subject: [PATCH 3/3] feat: Add trigonometry calculations and fix errors from PR --- silvermax.Calculator/Calculator/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/silvermax.Calculator/Calculator/Program.cs b/silvermax.Calculator/Calculator/Program.cs index 233776cb..9957b97d 100644 --- a/silvermax.Calculator/Calculator/Program.cs +++ b/silvermax.Calculator/Calculator/Program.cs @@ -75,7 +75,7 @@ 3. Power Calculation Console.WriteLine("\td - Divide"); Console.Write("Your option? "); - string? op = Console.ReadLine().Trim().ToLower(); + string? op = Console.ReadLine()?.Trim().ToLower(); if (op == null || !Regex.IsMatch(op, "[a|s|m|d]")) {