Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Postfix
- {
- class Program
- {
- public static int cnt = 0;
- static double Add(double a, double b)
- {
- if (a > 0 && b > 0)
- {
- cnt++;
- }
- return a + b;
- }
- static double Substract(double a, double b)
- {
- return a - b;
- }
- static double Multiply(double a, double b)
- {
- return a * b;
- }
- static double Divide(double a, double b)
- {
- return a / b;
- }
- delegate double MyDelegate(double a, double b);
- static double Evaluate(IList<string> tokens)
- {
- IDictionary<string, MyDelegate> dict = new Dictionary<string, MyDelegate>
- {
- {"+",Add},
- {"-",Substract},
- {"*",Multiply},
- {"/",Divide}
- };
- Stack<double> stack = new Stack<double>();
- foreach (var token in tokens)
- {
- try
- {
- stack.Push(double.Parse(token));
- }
- catch (Exception)
- {
- double op1 = stack.Pop();
- double op2 = stack.Pop();
- stack.Push(dict[token](op2, op1));
- }
- }
- return stack.Pop();
- }
- static void Main(string[] args)
- {
- IList<string> list = new List<string>();
- for (int i = 0; i < args.Length; i++)
- {
- list.Add(args[i]);
- }
- Evaluate(list);
- Console.WriteLine(cnt);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement