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..9957b97d --- /dev/null +++ b/silvermax.Calculator/Calculator/Program.cs @@ -0,0 +1,248 @@ +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; + + 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": + string? numInput1 = ""; + string? numInput2 = ""; + double result = 0; + + if (previousResult != 0) + { + 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 + { + 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(); + } + + 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(); + } + + 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()?.Trim().ToLower(); + + 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); + 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; + } + } + catch (Exception e) + { + 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: "); + var squareResponse = Console.ReadLine(); + double num = 0; + + while (!double.TryParse(squareResponse, out num)) + { + Console.WriteLine("Invalid input please input a valid number"); + 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++; + previousResult = result; + PrintQuestions(latestQuestions); + Console.WriteLine($"The calculator has been used {calculatorUsedTimes} times"); + break; + + case "3": + Console.WriteLine("Input the number: "); + var reply = Console.ReadLine(); + double num1 = 0; + + while (!double.TryParse(reply, out num1)) + { + Console.WriteLine("Invalid input please input a valid number"); + reply = Console.ReadLine(); + } + + Console.WriteLine("Input a number to raise to: "); + var powerResponse = Console.ReadLine(); + double power = 0; + + while (!double.TryParse(powerResponse, out power)) + { + Console.WriteLine("Invalid input please input a valid number"); + 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++; + 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"); + + 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 (endInput == "c") + { + latestQuestions.Clear(); + } + + Console.WriteLine("\n"); + Console.Clear(); + } + + calculator.Finish(); + } + + 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/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..b05d529c --- /dev/null +++ b/silvermax.Calculator/CalculatorLibrary/CalculatorProgram.cs @@ -0,0 +1,104 @@ +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); + } + + 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; + } +} + +