Guest User

Untitled

a guest
May 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. using System;
  2. namespace CalculatorDelegate
  3. {
  4. class Program
  5.  
  6. {
  7. private static double Item1 { get; set; }
  8. private static double Item2 { get; set; }
  9. private static string Action { get; set; }
  10. private delegate double Del(double a, double b);
  11.  
  12. private static void Main(string[] args)
  13. {
  14. while (true)
  15. {
  16. Console.WriteLine(ParseInput());
  17. }
  18. }
  19.  
  20. private static double ParseInput()
  21. {
  22. Console.WriteLine("Enter a mathematical expression, please");
  23. var userInput = Console.ReadLine();
  24. var userInputLength = userInput.Length;
  25. Del handler;
  26.  
  27. for (var i = 1; i < userInputLength; i++)
  28. {
  29.  
  30. if ( userInput[i].Equals('+') || userInput[i].Equals('-') ||
  31. userInput[i].Equals('*') | userInput[i].Equals('/'))
  32. {
  33. Action = Convert.ToString(userInput[i]);
  34. double.TryParse(userInput.Substring(0, i), out var tempItem1);
  35. double.TryParse(userInput.Substring(i + 1, userInputLength - i - 1), out var tempItem2);
  36.  
  37. Item1 = tempItem1;
  38. Item2 = tempItem2;
  39.  
  40. if (Action.Equals("/") && Item2 == 0)
  41. {
  42. Console.WriteLine("You can't divide by zero");
  43. return 0;
  44. }
  45.  
  46. break;
  47. }
  48. }
  49.  
  50. switch (Action)
  51. {
  52. case "+":
  53. handler = GetSum;
  54. break;
  55.  
  56. case "-":
  57. handler = GetDiff;
  58. break;
  59.  
  60. case "*":
  61. handler = GetMult;
  62. break;
  63.  
  64. case "/":
  65. handler = GetDiv;
  66. break;
  67.  
  68. default:
  69. return 0;
  70. }
  71.  
  72. return handler(Item1, Item2);
  73. }
  74.  
  75. private static double GetSum(double a, double b)
  76. {
  77. return a + b;
  78. }
  79.  
  80. private static double GetDiff(double a, double b)
  81. {
  82. return a - b;
  83. }
  84.  
  85. private static double GetMult(double a, double b)
  86. {
  87. return a * b;
  88. }
  89.  
  90. private static double GetDiv(double a, double b)
  91. {
  92. return a / b;
  93. }
  94.  
  95. }
  96. }
Add Comment
Please, Sign In to add comment