-
Notifications
You must be signed in to change notification settings - Fork 369
Add Calculator project files #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShPet1304
wants to merge
1
commit into
TheCSharpAcademy:main
Choose a base branch
from
ShPet1304:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <Solution> | ||
| <Project Path="Calculator/Calculator.csproj" /> | ||
| <Project Path="CalculatorLibrary/CalculatorLibrary.csproj" Id="183936aa-c4e5-47fb-ac37-d8150bbaa797" /> | ||
| </Solution> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\CalculatorLibrary\CalculatorLibrary.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Spectre.Console" Version="0.54.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| using CalculatorLibrary; | ||
| using CalculatorMenu; | ||
| using Spectre.Console; | ||
|
|
||
|
|
||
| namespace CalculatorProgram | ||
| { | ||
| public class CalculatorFunctions | ||
| { | ||
| public bool reusedResult = false; | ||
| public double reusedNumber = 0; | ||
|
|
||
| public void Function() | ||
| { | ||
| bool endApp = false; | ||
|
|
||
| Console.WriteLine("Console Calculator in C#\r"); | ||
| Console.WriteLine("------------------------\n"); | ||
|
|
||
| CalculatorLibrary.Calculator calculator = Calculator.Instance; | ||
|
|
||
| while (!endApp) | ||
| { | ||
| string? numInput1 = ""; | ||
| string? numInput2 = ""; | ||
| double result = 0; | ||
| double cleanNum1 = 0; | ||
| double cleanNum2 = 0; | ||
|
|
||
| var menu = new Menu(); | ||
| string op = menu.ShowOperationsMenu(); | ||
|
|
||
| if (op == "Square Root" || op == "10x" || op == "Trigonometric function (sin)" || op == "Trigonometric function (cos)" || op == "Trigonometric function (tan)") | ||
| { | ||
| if (cleanNum1 == 0 && !reusedResult) | ||
| { | ||
| Console.Write("Type a number, and then press Enter: "); | ||
| numInput1 = Console.ReadLine(); | ||
|
|
||
| while (!double.TryParse(numInput1, out cleanNum1)) | ||
| { | ||
| Console.Write("This is not valid input. Please enter a numeric value: "); | ||
| numInput1 = Console.ReadLine(); | ||
| } | ||
| ; | ||
| } | ||
| else if (reusedResult) | ||
| { | ||
| cleanNum1 = reusedNumber; | ||
| reusedResult = false; | ||
| Console.WriteLine("Reusing the previous result: {0}\n", cleanNum1); | ||
| } | ||
| try | ||
| { | ||
| result = calculator.DoOperation(cleanNum1, 0, 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("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); | ||
| } | ||
| } | ||
| else if (cleanNum1 == 0 && !reusedResult) | ||
| { | ||
| Console.Write("Type a number, and then press Enter: "); | ||
| numInput1 = Console.ReadLine(); | ||
| while (!double.TryParse(numInput1, out cleanNum1)) | ||
| { | ||
| Console.Write("This is not valid input. Please enter a numeric value: "); | ||
| numInput1 = Console.ReadLine(); | ||
| } | ||
| } | ||
|
|
||
| else if (reusedResult) | ||
| { | ||
| cleanNum1 = reusedNumber; | ||
| reusedResult = false; | ||
| Console.WriteLine("Reusing the previous result: {0}\n", cleanNum1); | ||
| } | ||
| Console.Write("Type another number, and then press Enter: "); | ||
| numInput2 = Console.ReadLine(); | ||
|
|
||
| while (!double.TryParse(numInput2, out cleanNum2)) | ||
| { | ||
| Console.Write("This is not valid input. Please enter a numeric value: "); | ||
| numInput2 = Console.ReadLine(); | ||
| } | ||
| 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); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); | ||
| } | ||
| Console.WriteLine("------------------------\n"); | ||
| AnsiConsole.MarkupLine("\nPress [grey]Enter[/] to return to menu..."); | ||
| Console.ReadKey(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| using CalculatorLibrary; | ||
| using CalculatorProgram; | ||
| using Spectre.Console; | ||
|
|
||
| namespace CalculatorMenu | ||
| { | ||
| public class Menu | ||
| { | ||
| private static Calculator calcLib = Calculator.Instance; | ||
| private static CalculatorFunctions func = new CalculatorFunctions(); | ||
|
|
||
| public string ShowMainMenu() | ||
| { | ||
| AnsiConsole.Clear(); | ||
| AnsiConsole.Write( | ||
| new FigletText("Calculator") | ||
| .Color(Color.Blue)); | ||
|
|
||
| var choice = AnsiConsole.Prompt( | ||
| new SelectionPrompt<string>() | ||
| .Title("[green]What would you like to do[/]?") | ||
| .PageSize(10) | ||
| .MoreChoicesText("[grey](Move up and down to reveal more options)[/]") | ||
| .AddChoices(new[] { | ||
| "New Calculation", "View Result History", "Use Previous Result", "View Calculator Usage Count", "Clear History", "Exit" | ||
| })); | ||
| return choice; | ||
| } | ||
|
|
||
| public string ShowOperationsMenu() | ||
| { | ||
| AnsiConsole.Clear(); | ||
| var op = AnsiConsole.Prompt( | ||
| new SelectionPrompt<string>() | ||
| .Title("[green]What would you like to do[/]?") | ||
| .PageSize(10) | ||
| .MoreChoicesText("[grey](Move up and down to reveal more options)[/]") | ||
| .AddChoices(new[] { | ||
| "addition", "subtraction","multiplication", "division", "Square Root", "Taking the power", "10x", "Trigonometric function (sin)", "Trigonometric function (cos)", "Trigonometric function (tan)" | ||
| })); | ||
| return op; | ||
| } | ||
|
|
||
| public void ShowHistoryMenu() | ||
| { | ||
| AnsiConsole.Clear(); | ||
| List<double> options = calcLib.PreviousResult(); | ||
| if (options.Count == 0) | ||
| { | ||
| AnsiConsole.MarkupLine("[red]No history found.[/]"); | ||
| return; | ||
| } | ||
| double selectedNum = AnsiConsole.Prompt( | ||
| new SelectionPrompt<double>() | ||
| .Title("Select a [green]previous result[/]:") | ||
| .AddChoices(options) | ||
| ); | ||
| func.reusedNumber = selectedNum; | ||
| func.reusedResult = true; | ||
| func.Function(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| using CalculatorLibrary; | ||
| using CalculatorMenu; | ||
|
|
||
|
|
||
| namespace CalculatorProgram | ||
| { | ||
| public class Program | ||
| { | ||
| private static Calculator calcLib = Calculator.Instance; | ||
| private static CalculatorFunctions calculatorFunctions = new CalculatorFunctions(); | ||
|
|
||
| public static void Main(string[] args) | ||
| { | ||
| Programme(); | ||
| } | ||
|
|
||
| static void Programme() | ||
| { | ||
| var menu = new Menu(); | ||
| bool running = true; | ||
|
|
||
| while (running) | ||
| { | ||
| string selected = menu.ShowMainMenu(); | ||
|
|
||
| switch (selected) | ||
| { | ||
| case "New Calculation": | ||
| calculatorFunctions.Function(); | ||
| break; | ||
|
|
||
| case "View Result History": | ||
| calcLib.GetLastResult(); | ||
| break; | ||
|
|
||
| case "Use Previous Result": | ||
| menu.ShowHistoryMenu(); | ||
| break; | ||
|
|
||
| case "View Calculator Usage Count": | ||
| calcLib.GetUsageCount(); | ||
| break; | ||
|
|
||
| case "Clear History": | ||
| calcLib.ClearHistory(); | ||
| break; | ||
|
|
||
| case "Exit": | ||
| calcLib.Finish(); | ||
| running = false; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method is doing a lot of things at once (getting input, validation, logic, displaying results). It makes it a bit hard to follow. Consider breaking it down into smaller helper methods, for example: GetValidNumber() could handle the input parsing loop separately. 😁