-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (98 loc) · 4.24 KB
/
Program.cs
File metadata and controls
112 lines (98 loc) · 4.24 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
using System;
namespace ConsoleCalculator
{
class Program
{
static void Main(string[] args)
{
bool exit = false;
GetAppInfo();
GreetUser();
while(!exit)
{
try
{
Console.Write("Enter the first number: ");
int number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the operation sign; + or - or * or / or %: ");
// int operation = Convert.ToInt32(Console.ReadLine());
string operation = Console.ReadLine();
Console.Write("Enter the second number: ");
int number2 = Convert.ToInt32(Console.ReadLine());
/* Console.WriteLine("1 - Addition");
Console.WriteLine("2 - Subtraction");
Console.WriteLine("3 - Multiplication");
Console.WriteLine("4 - Division");
Console.WriteLine("5 - Remainder");
Console.WriteLine("6 - Exit");
*/
double result = 0;
switch (operation)
{
case "+":
result = number1 + number2;
PrintColorMessage(ConsoleColor.Green, "Result: " + result);
break;
case "-":
result = number1 - number2;
PrintColorMessage(ConsoleColor.Green, "Result: " + result);
break;
case "*":
result = number1 * number2;
PrintColorMessage(ConsoleColor.Green, "Result: " + result);
break;
case "/":
if (number2 != 0)
{
result = (double)number1 / number2;
PrintColorMessage(ConsoleColor.Green, "Result: " + result);
}
else
{
PrintColorMessage(ConsoleColor.Red, "Cannot divide by zero");
}
break;
case "%":
result = number1 % number2;
PrintColorMessage(ConsoleColor.Green, "Result: " + result);
break;
case "q":
exit = true;
break;
default:
PrintColorMessage(ConsoleColor.DarkRed, "Invalid operation number");
break;
}
}
catch (Exception ex)
{
PrintColorMessage(ConsoleColor.Red, "An error occured" +ex);
}
}
}
static void GetAppInfo()
{
// Set app vars
string appName = "Windows Console Calculator Application";
string appVersion = "1.0.0";
string appAuthor = "Hassan Bulega";
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("**************************************************************************");
Console.WriteLine("{0}: Version {1} by {2}", appName, appVersion, appAuthor);
Console.WriteLine("**************************************************************************");
Console.ResetColor();
}
static void GreetUser()
{
Console.WriteLine("What is your name?");
string inputName = Console.ReadLine();
Console.WriteLine("Hello {0}, Welcome to our Calculator its Maths time...", inputName);
}
static void PrintColorMessage(ConsoleColor color, string message)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
}
}
}