Advertisement
Guest User

prefix

a guest
Apr 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 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 Postfix
  8. {
  9. class Program
  10. {
  11. public static int cnt = 0;
  12. static double Add(double a, double b)
  13. {
  14. if (a > 0 && b > 0)
  15. {
  16. cnt++;
  17. }
  18. return a + b;
  19. }
  20. static double Substract(double a, double b)
  21. {
  22. return a - b;
  23. }
  24. static double Multiply(double a, double b)
  25. {
  26. return a * b;
  27. }
  28. static double Divide(double a, double b)
  29. {
  30. return a / b;
  31. }
  32.  
  33. delegate double MyDelegate(double a, double b);
  34. static double Evaluate(List<string> tokens)
  35. {
  36. IDictionary<string, MyDelegate> dict = new Dictionary<string, MyDelegate>
  37. {
  38. {"+",Add},
  39. {"-",Substract},
  40. {"*",Multiply},
  41. {"/",Divide}
  42. };
  43.  
  44. Stack<double> stack = new Stack<double>();
  45. foreach (var token in tokens)
  46. {
  47. try
  48. {
  49. stack.Push(double.Parse(token));
  50. }
  51. catch (Exception)
  52. {
  53. double op1 = stack.Pop();
  54. double op2 = stack.Pop();
  55.  
  56. stack.Push(dict[token](op1, op2));
  57. }
  58. }
  59. return stack.Pop();
  60.  
  61. }
  62. static void Main(string[] args)
  63. {
  64. List<string> list = new List<string>();
  65. for (int i = 0; i < args.Length; i++)
  66. {
  67. list.Add(args[i]);
  68. }
  69. list.Reverse();
  70. Evaluate(list);
  71. Console.WriteLine(cnt);
  72.  
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement