Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Calculator
  8. {
  9. class Program
  10. {
  11.  
  12. static void Main(string[] args)
  13. {
  14. try
  15. {
  16. double x;
  17. double y;
  18.  
  19. double res = 0;
  20.  
  21. Console.BackgroundColor = ConsoleColor.Black;
  22. Console.ForegroundColor = ConsoleColor.DarkGreen;
  23.  
  24. Console.WriteLine("Please enter a number: \n");
  25. x = double.Parse(Console.ReadLine());
  26.  
  27. for (;;)
  28. {
  29. Console.WriteLine("\n Please enter another number: \n");
  30. y = double.Parse(Console.ReadLine());
  31. Console.WriteLine();
  32. Console.WriteLine("\nChoose your operation method (+,-,*,/)\n");
  33. Boolean eingabe = true;
  34. switch (Console.ReadLine())
  35.  
  36. {
  37. case "+":
  38. {
  39. double sum = Add(x, y);
  40. Console.WriteLine("\nThe sum of the operation is: " + sum + "\n");
  41. res = sum;
  42. break;
  43. }
  44.  
  45. case "-":
  46. {
  47. double dif = Sub(x, y);
  48. Console.WriteLine("\nThe difference of the operation is: " + dif + "\n");
  49. res = dif;
  50. break;
  51. }
  52.  
  53. case "*":
  54. {
  55. double mult = Mult(x, y);
  56. Console.WriteLine("\nThe product of the operation is: " + mult + "\n");
  57. res = mult;
  58. break;
  59. }
  60.  
  61. case "/":
  62. {
  63. double div = Div(x, y);
  64. Console.WriteLine("\nThe quotient of the operation is: " + div + "\n");
  65. res = div;
  66. break;
  67. }
  68.  
  69. default:
  70. {
  71. Console.WriteLine("\nYou did not enter a valid operator!\n");
  72. break;
  73. }
  74. }
  75.  
  76. Console.WriteLine("\nDo you want to use the previous result in the next operation? Y/N\n");
  77. while (eingabe)
  78. {
  79. string prev = Console.ReadLine();
  80. if (prev == "Y" || prev == "N")
  81. {
  82. if (prev == "Y")
  83. {
  84. eingabe = false;
  85. x = res;
  86.  
  87. }
  88. else
  89. {
  90. eingabe = false;
  91. Console.WriteLine("Please enter a number: \n");
  92. x = double.Parse(Console.ReadLine());
  93. }
  94. }
  95. }
  96. }
  97. } catch (Exception ex)
  98.  
  99. {
  100. Console.WriteLine("\n" + ex.Message + "\n");
  101.  
  102. }
  103. }
  104.  
  105. static double Add(double x, double y)
  106. {
  107. return x + y;
  108. }
  109.  
  110. static double Sub(double x, double y)
  111. {
  112. return x - y;
  113. }
  114.  
  115. static double Mult(double x, double y)
  116. {
  117. return x * y;
  118. }
  119.  
  120. static double Div(double x, double y)
  121. {
  122. if (y == 0)
  123. {
  124. throw new Exception("\nThe quotient of the operation is: infinity (Cannot divide by 0!)\n");
  125. }
  126. else
  127. {
  128. return x / y;
  129. }
  130. }
  131.  
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement