diff --git a/.codacy.yml b/.codacy.yml deleted file mode 100644 index 44c0e92e..00000000 --- a/.codacy.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -exclude_paths: - - "README.md" - - "**/README.md" \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..1ff0c423 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.github/workflows/project-submission.yml b/.github/workflows/project-submission.yml deleted file mode 100644 index d01ee8b5..00000000 --- a/.github/workflows/project-submission.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Project Submission - -on: - pull_request_target: - branches: [ "main" ] - types: [opened, reopened] - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -permissions: - pull-requests: write - issues: write - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - call-submission-workflow: - uses: the-csharp-academy/base-repo/.github/workflows/project-submission.yml@main diff --git a/.gitignore b/.gitignore index 5a62da38..9491a2fd 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ x86/ bld/ [Bb]in/ [Oo]bj/ +[Oo]ut/ [Ll]og/ [Ll]ogs/ @@ -57,14 +58,11 @@ dlldata.c # Benchmark Results BenchmarkDotNet.Artifacts/ -# .NET +# .NET Core project.lock.json project.fragment.lock.json artifacts/ -# Tye -.tye/ - # ASP.NET Scaffolding ScaffoldingReadMe.txt @@ -362,96 +360,4 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd - -## -## Visual studio for Mac -## - - -# globs -Makefile.in -*.userprefs -*.usertasks -config.make -config.status -aclocal.m4 -install-sh -autom4te.cache/ -*.tar.gz -tarballs/ -test-results/ - -# Mac bundle stuff -*.dmg -*.app - -# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore -# General -.DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk - -# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore -# Windows thumbnail cache files -Thumbs.db -ehthumbs.db -ehthumbs_vista.db - -# Dump file -*.stackdump - -# Folder config file -[Dd]esktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Windows Installer files -*.cab -*.msi -*.msix -*.msm -*.msp - -# Windows shortcuts -*.lnk - -# JetBrains Rider -.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 +FodyWeavers.xsd \ No newline at end of file diff --git a/CalculatorLibrary/CalculatorLibrary.cs b/CalculatorLibrary/CalculatorLibrary.cs new file mode 100644 index 00000000..7cfe1368 --- /dev/null +++ b/CalculatorLibrary/CalculatorLibrary.cs @@ -0,0 +1,114 @@ +namespace CalculatorLibrary +{ + // CalculatorLibrary.cs + using System.Diagnostics; + // Program.cs + using Newtonsoft.Json; + public class Calculator + { + // CalculatorLibrary.cs + 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(); + } + + // CalculatorLibrary.cs + 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; + case "sq": + if(num1 >= 0) + { + result = Math.Sqrt(num1); + } + writer.WriteValue("Square Root"); + break; + case "sq2": + if(num1 >= 0 && num2 != 0) + { + result = Math.Pow(num1, 1/num2); + } + writer.WriteValue("Nth Root"); + break; + case "10": + result = Math.Pow(10, num1); + writer.WriteValue("10^x"); + break; + case "cos": + result = Math.Cos(num1); + writer.WriteValue("Cosine"); + break; + case "sin": + result = Math.Sin(num1); + writer.WriteValue("Sine"); + break; + case "tan": + result = Math.Tan(num1); + writer.WriteValue("Tangent"); + break; + case "log": + if (num1 > 0) + { + result = Math.Log10(num1); + } + writer.WriteValue("Logarithm base 10"); + break; + + // Return text for an incorrect option entry. + default: + break; + } + writer.WritePropertyName("Result"); + writer.WriteValue(result); + writer.WriteEndObject(); + + return result; + } + // CalculatorLibrary.cs + 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 + + + + + + + diff --git a/ConsoleCalculator.Brandonscodedata.slnx b/ConsoleCalculator.Brandonscodedata.slnx new file mode 100644 index 00000000..68087e14 --- /dev/null +++ b/ConsoleCalculator.Brandonscodedata.slnx @@ -0,0 +1,4 @@ + + + + diff --git a/ConsoleCalculator.Brandonscodedata/CalcData.cs b/ConsoleCalculator.Brandonscodedata/CalcData.cs new file mode 100644 index 00000000..6ce6c9c3 --- /dev/null +++ b/ConsoleCalculator.Brandonscodedata/CalcData.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ConsoleCalculator.Brandonscodedata +{ + internal class CalcData + { + public int CalculationRound { get; init; } + + public double CalculationNumberOne { get; init; } + + public double CalculationNumberTwo { get; init; } + + public string CalculationOperator { get; init; } + + public double CalculationResult { get; init; } + + public CalcData(int round, double num1, double num2, string op, double result) + { + CalculationRound = round; + CalculationNumberOne = num1; + CalculationNumberTwo = num2; + CalculationOperator = op; + CalculationResult = result; + } + } +} diff --git a/ConsoleCalculator.Brandonscodedata/ConsoleCalculator.Brandonscodedata.csproj b/ConsoleCalculator.Brandonscodedata/ConsoleCalculator.Brandonscodedata.csproj new file mode 100644 index 00000000..8acb7da5 --- /dev/null +++ b/ConsoleCalculator.Brandonscodedata/ConsoleCalculator.Brandonscodedata.csproj @@ -0,0 +1,14 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + diff --git a/ConsoleCalculator.Brandonscodedata/Program.cs b/ConsoleCalculator.Brandonscodedata/Program.cs new file mode 100644 index 00000000..3a111390 --- /dev/null +++ b/ConsoleCalculator.Brandonscodedata/Program.cs @@ -0,0 +1,167 @@ +using CalculatorLibrary; +using System.Text.RegularExpressions; + +namespace ConsoleCalculator.Brandonscodedata +{ + internal class Program + { + private static List calculationHistory = new List(); + static void Main(string[] args) + { + bool usePreviousResult = false; + double savedResult = 0; + bool endApp = false; + // Display title as the C# console calculator app. + Console.WriteLine("Console Calculator in C#\r"); + Console.WriteLine("------------------------\n"); + // Program.cs + Calculator calculator = new Calculator(); + while (!endApp) + { + // Declare variables and set to empty. + // Use Nullable types (with ?) to match type of System.Console.ReadLine + string? numInput1 = ""; + string? numInput2 = ""; + double result = 0; + + // Ask the user to type the first number. + if (usePreviousResult) + { + numInput1 = savedResult.ToString(); + Console.WriteLine($"Using previous result: {numInput1}"); + } + 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.WriteLine("\tsq - Square Root of number1"); + Console.WriteLine("\tsq2 - number2 Root of number1"); + Console.WriteLine("\t10 - 10x"); + Console.WriteLine("\tcos - Cosine of number1"); + Console.WriteLine("\tsin - Sine of number1"); + Console.WriteLine("\t tan - Tangent of number1"); + Console.WriteLine("\tlog - Logarithm base 10 of number1"); + 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|sq2|10|cos|sin|tan|log)$")) + { + Console.WriteLine("Error: Unrecognized input."); + } + else + { + try + { + // Program.cs + result = calculator.DoOperation(cleanNum1, cleanNum2, op); + if (double.IsNaN(result)) + { + Console.WriteLine("This operation will result in a mathematical error.\n"); + } + else + { + CalcData currentCalcData = new CalcData(calculationHistory.Count + 1, cleanNum1, cleanNum2, op.Trim(), result); + calculationHistory.Add(currentCalcData); + + Console.WriteLine("Your result: {0:0.##}\n", result); + Console.WriteLine($"The Calculator was used {calculationHistory.Count} times."); + } + } + 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("Press 'h' and Enter to view calculation history:"); + Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); + string userMenuInput = Console.ReadLine(); + if (userMenuInput == "n") endApp = true; + if (userMenuInput == "h") + { + Console.WriteLine("\nCalculation History:"); + foreach (var calcData in calculationHistory) + { + Console.WriteLine($"Round {calcData.CalculationRound}: {calcData.CalculationNumberOne} {calcData.CalculationOperator} {calcData.CalculationNumberTwo} = {calcData.CalculationResult}"); + } + Console.WriteLine("Do you want to delete the calculation history? Press 'd' to delete."); + Console.WriteLine("Do you want to use the Result for another calculation? Press 'u' to use."); + + string historyInput = Console.ReadLine(); + if (historyInput == "d") + { + calculationHistory.Clear(); + Console.WriteLine("Calculation history deleted."); + } + if (historyInput == "u") + { + // Implement logic for using the result in another calculation + Console.WriteLine("Which calculation do you want to use? Enter the round number."); + if (int.TryParse(Console.ReadLine(), out int roundNumber)) + { + var selectedCalc = calculationHistory.FirstOrDefault(c => c.CalculationRound == roundNumber); + if (selectedCalc != null) + { + Console.WriteLine($"Using result {selectedCalc.CalculationResult} from round {roundNumber}."); + // You can now use selectedCalc.CalculationResult for further calculations + // For example, you could set it as numInput1 for the next calculation + savedResult = selectedCalc.CalculationResult; + usePreviousResult = true; + } + else + { + usePreviousResult=false; + Console.WriteLine("Invalid round number."); + } + + } + else + { + usePreviousResult = false; + Console.WriteLine("Invalid input. Please enter a valid round number."); + } + } + else + { + usePreviousResult = false; + } + + Console.WriteLine("\n"); // Friendly linespacing. + } + + } + calculator.Finish(); + return; + } + } +}