fork download
  1. using System;
  2.  
  3. // Program.cs
  4. namespace Calculator
  5. {
  6. public class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Application app = new Application();
  11. app.Run();
  12. }
  13. }
  14. }
  15.  
  16. // Application.cs
  17. namespace Calculator
  18. {
  19. public class Application
  20. {
  21. private const string AppTitle = "Mini Calculator";
  22.  
  23. public void Run()
  24. {
  25. Console.Title = AppTitle;
  26.  
  27. Console.WriteLine("###############");
  28. Console.WriteLine(AppTitle);
  29. Console.WriteLine("###############\n");
  30.  
  31. string s;
  32.  
  33. do
  34. {
  35. var userInputs = Input();
  36. var result = Calculate(userInputs);
  37. OutputResult(result);
  38.  
  39. Console.WriteLine("Do you like to solve another arithmetical problem? (y/n)");
  40. s = Console.ReadLine().Trim().ToLower();
  41. } while (s.StartsWith("y"));
  42.  
  43. Console.WriteLine("\nPress any key to exit...");
  44. Console.ReadKey();
  45. }
  46.  
  47. public class UserInputs
  48. {
  49. public double Number1 { get; set; }
  50. public double Number2 { get; set; }
  51. public int Operator { get; set; }
  52. }
  53.  
  54. public static UserInputs Input()
  55. {
  56. Console.WriteLine("Please enter your first number: ");
  57. double number1 = double.Parse(Console.ReadLine());
  58. Console.WriteLine($"You chose: {number1}");
  59.  
  60. Console.WriteLine("Please enter your second number: ");
  61. double number2 = double.Parse(Console.ReadLine());
  62. Console.WriteLine($"You chose: {number2}");
  63.  
  64. Console.WriteLine("Choose your operator by number and hit enter:");
  65. Console.WriteLine("> 1.) Addition");
  66. Console.WriteLine("> 2.) Subtraction");
  67. Console.WriteLine("> 3.) Multiplication");
  68. Console.WriteLine("> 4.) Division");
  69.  
  70. int op = 0;
  71.  
  72. while (true)
  73. {
  74. op = int.Parse(Console.ReadLine());
  75. Console.WriteLine($"You chose: {op}");
  76.  
  77. if (op < 1 || op > 4)
  78. {
  79. Console.WriteLine("Your input is invalid. Try again, with numerals 1 - 4.");
  80. continue;
  81. }
  82.  
  83. break;
  84. }
  85.  
  86. return new UserInputs() { Number1 = number1, Number2 = number2, Operator = op };
  87. }
  88.  
  89. public double Calculate(UserInputs userInputs)
  90. {
  91. double result = 0;
  92.  
  93. switch (userInputs.Operator)
  94. {
  95. case 1:
  96. result = Calculation.Addition(userInputs.Number1, userInputs.Number2);
  97. break;
  98. case 2:
  99. result = Calculation.Subtraction(userInputs.Number1, userInputs.Number2);
  100. break;
  101. case 3:
  102. result = Calculation.Multiplication(userInputs.Number1, userInputs.Number2);
  103. break;
  104. case 4:
  105. result = Calculation.Division(userInputs.Number1, userInputs.Number2);
  106. break;
  107. }
  108.  
  109. return result;
  110. }
  111.  
  112. public void OutputResult(double result)
  113. {
  114. Console.WriteLine($"Result: {result}");
  115. }
  116. }
  117. }
  118.  
  119. // Calculation.cs
  120. namespace Calculator
  121. {
  122. public static class Calculation
  123. {
  124. public static double Addition(double x, double y)
  125. {
  126. return x + y;
  127. }
  128.  
  129. public static double Subtraction(double x, double y)
  130. {
  131. return x - y;
  132. }
  133.  
  134. public static double Multiplication(double x, double y)
  135. {
  136. return x * y;
  137. }
  138.  
  139. public static double Division(double x, double y)
  140. {
  141. return x / y;
  142. }
  143. }
  144. }
  145.  
Success #stdin #stdout 0.03s 25960KB
stdin
1.23
4.56
0
1
y
-1.23
-4.56
3
n
stdout
###############
Mini Calculator
###############

Please enter your first number: 
You chose: 1.23
Please enter your second number: 
You chose: 4.56
Choose your operator by number and hit enter:
> 1.) Addition
> 2.) Subtraction
> 3.) Multiplication
> 4.) Division
You chose: 0
Your input is invalid. Try again, with numerals 1 - 4.
You chose: 1
Result: 5.79
Do you like to solve another arithmetical problem? (y/n)
Please enter your first number: 
You chose: -1.23
Please enter your second number: 
You chose: -4.56
Choose your operator by number and hit enter:
> 1.) Addition
> 2.) Subtraction
> 3.) Multiplication
> 4.) Division
You chose: 3
Result: 5.6088
Do you like to solve another arithmetical problem? (y/n)

Press any key to exit...