-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
208 lines (189 loc) · 6.81 KB
/
Copy pathProgram.cs
File metadata and controls
208 lines (189 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
namespace BasicCalculatorConsoleApp
{
class Program
{
static void Main(string[] args)
{
List<string> history = new List<string>();
bool keepRunning = true;
while (keepRunning)
{
Console.Clear();
ShowMainMenu();
int choice = ReadIntFromConsole();
switch (choice)
{
case 1:
PerformCalculationMenu(history);
break;
case 2:
DisplayHistoryMenu(history);
break;
case 3:
DisplayHelpMenu();
break;
case 4:
keepRunning = false;
break;
default:
Console.WriteLine("Invalid choice. Please select a valid option.");
break;
}
}
}
static void ShowMainMenu()
{
Console.WriteLine("Main Menu:");
Console.WriteLine("1. Perform Calculation");
Console.WriteLine("2. View History");
Console.WriteLine("3. Help");
Console.WriteLine("4. Quit");
Console.WriteLine("Enter your choice (1-4):");
}
static void PerformCalculationMenu(List<string> history)
{
while (true)
{
Console.Clear();
Console.WriteLine("Enter your calculation (or 'back' to return to main menu):");
string input = Console.ReadLine().Trim().ToLower();
if (input == "back")
{
break;
}
PerformCalculation(input, history);
Console.WriteLine("Press any key to continue or type 'back' to return to the main menu.");
input = Console.ReadLine().Trim().ToLower();
if (input == "back")
{
break;
}
}
}
static void DisplayHistoryMenu(List<string> history)
{
while (true)
{
Console.Clear();
Console.WriteLine("Calculation History (type 'back' to return to the main menu):");
foreach (var entry in history)
{
Console.WriteLine(entry);
}
Console.WriteLine("\nPress any key to refresh or type 'back' to return to the main menu.");
string input = Console.ReadLine().Trim().ToLower();
if (input == "back")
{
break;
}
}
}
static void DisplayHelpMenu()
{
while (true)
{
Console.Clear();
Console.WriteLine("Help - How to use the calculator (type 'back' to return to the main menu):");
Console.WriteLine(" - To perform calculations, enter the operation followed by numbers.");
Console.WriteLine(" - Supported operations: +, -, *, /, ^, sqrt, log.");
Console.WriteLine(" - Enter 'back' at any point to return to the main menu.");
Console.WriteLine("\nPress any key to refresh or type 'back' to return to the main menu.");
string input = Console.ReadLine().Trim().ToLower();
if (input == "back")
{
break;
}
}
}
static void PerformCalculation(string input, List<string> history)
{
double num1, num2;
string operation;
if (input == "sqrt" || input == "log")
{
Console.WriteLine("Enter the number:");
num1 = ReadDoubleFromConsole();
num2 = 0;
operation = input;
}
else
{
string[] parts = input.Split(' ');
if (parts.Length != 3)
{
Console.WriteLine("Invalid format. Use [number] [operation] [number].");
return;
}
if (!double.TryParse(parts[0], out num1) || !double.TryParse(parts[2], out num2))
{
Console.WriteLine("Invalid numbers. Please enter valid numbers.");
return;
}
operation = parts[1];
}
try
{
double result = PerformAdvancedOperation(num1, num2, operation);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Result: {result}");
history.Add($"{num1} {operation} {num2} = {result}");
Console.ResetColor();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
Console.ResetColor();
}
}
static double PerformAdvancedOperation(double num1, double num2, string operation)
{
return operation switch
{
"+" => Add(num1, num2),
"-" => Subtract(num1, num2),
"*" => Multiply(num1, num2),
"/" => Divide(num1, num2),
"^" => Math.Pow(num1, num2),
"sqrt" => Math.Sqrt(num1),
"log" => Math.Log(num1),
_ => throw new InvalidOperationException("Unsupported operation."),
};
}
static double ReadDoubleFromConsole()
{
while (true)
{
if (double.TryParse(Console.ReadLine().Trim(), out double number))
{
return number;
}
Console.WriteLine("Invalid input. Please enter a valid number.");
}
}
static int ReadIntFromConsole()
{
while (true)
{
if (int.TryParse(Console.ReadLine().Trim(), out int number))
{
return number;
}
Console.WriteLine("Invalid input. Please enter a valid number.");
}
}
static double Add(double num1, double num2) => num1 + num2;
static double Subtract(double num1, double num2) => num1 - num2;
static double Multiply(double num1, double num2) => num1 * num2;
static double Divide(double num1, double num2)
{
if (num2 == 0)
{
throw new DivideByZeroException("Cannot divide by zero.");
}
return num1 / num2;
}
}
}