forked from application-development-08664/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (49 loc) · 1.54 KB
/
Program.cs
File metadata and controls
60 lines (49 loc) · 1.54 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
using System;
class Program
{
static void Main()
{
bool running = true;
while (running)
{
int first = ReadInt("Enter first number: ");
int second = ReadInt("Enter second number: ");
string operation = ReadOperation();
if (operation == "=")
{
running = false;
continue;
}
double? result = Calculator.Calculate(first, second, operation);
if (result == null && (operation == "/" || operation == "%") && second == 0)
Console.WriteLine("Cannot divide or modulo by zero");
else
Console.WriteLine($"Result: {Math.Round(result.Value, 2)}");
Console.WriteLine();
}
Console.WriteLine("Program terminated.");
}
static int ReadInt(string message)
{
int number;
while (!int.TryParse(Read(message), out number))
Console.WriteLine("Invalid input. Please enter a whole number.");
return number;
}
static string ReadOperation()
{
while (true)
{
Console.Write("Choose operation (+, -, *, /, %, =): ");
string op = Console.ReadLine();
if (op == "+" || op == "-" || op == "*" || op == "/" || op == "%" || op == "=")
return op;
Console.WriteLine("Incorrect operation. Try again.");
}
}
static string Read(string message)
{
Console.Write(message);
return Console.ReadLine();
}
}