Advertisement
mrAnderson33

перевод инфикса в постфикс и его решение

Jun 14th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace постфикс
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.              // var inFix = "1*2+3-4/5";
  11.  
  12.             var inFix = "((1+2)*(5+3))/((7-5)*(8-2))";
  13.  
  14.            // var inFix = "(a*(b+c)+d)/2";
  15.  
  16.             var postfix = ToPostfix(inFix);
  17.  
  18.             Console.WriteLine(postfix);
  19.  
  20.             var solving = SolvePostfix(postfix);
  21.  
  22.             Console.WriteLine(solving);
  23.         }
  24.  
  25.  
  26.         public static double SolvePostfix(string postFix)
  27.         {
  28.             var stackNumbers = new Stack<double>();
  29.  
  30.             foreach (var ch in postFix)
  31.                 if (Char.IsDigit(ch)) stackNumbers.Push(ch-48);
  32.                 else if (IsOperation(ch))
  33.                     stackNumbers.Push(OperationBetweenTwoOperands(stackNumbers.Pop(), stackNumbers.Pop(), ch));
  34.  
  35.             return stackNumbers.Pop();
  36.         }
  37.  
  38.         public static string ToPostfix(string inFix)
  39.         {
  40.             var postFix = string.Empty;
  41.  
  42.             var stackOperations = new Stack<char>();
  43.  
  44.             foreach (var ch in inFix)
  45.             {
  46.                 if (Char.IsLetterOrDigit(ch))
  47.                 {
  48.                     postFix += ch;
  49.                 }
  50.                 else if (IsOperation(ch))
  51.                 {
  52.                     if ((stackOperations.Count == 0) || ch == '(' || stackOperations.Peek() == '(')
  53.                     {
  54.                         stackOperations.Push(ch);
  55.                     }
  56.                     else if ((ch != ')') && (GetPriority(ch) > GetPriority(stackOperations.Peek())))
  57.                     {
  58.                         stackOperations.Push(ch);
  59.                     }
  60.                     else if ((ch != ')') && (GetPriority(ch) <= GetPriority(stackOperations.Peek())))
  61.                     {
  62.                         while ((stackOperations.Count != 0) && ((stackOperations.Peek() != '(') || (GetPriority(stackOperations.Peek()) >= GetPriority(ch))))
  63.                             postFix += stackOperations.Pop();
  64.                         stackOperations.Push(ch);
  65.                     }
  66.                     else if (ch == ')')
  67.                     {
  68.                         while (stackOperations.Peek() != '(') postFix += stackOperations.Pop();
  69.                         stackOperations.Pop();
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             while (stackOperations.Count != 0) postFix += stackOperations.Pop();
  75.  
  76.             return postFix;
  77.         }
  78.  
  79.         public static double OperationBetweenTwoOperands(double left,double right,char operation)
  80.         {
  81.             if (!IsOperation(operation)) throw new InvalidOperationException("Это не операция");
  82.             switch(operation)
  83.             {
  84.                 case '+' : return right + left;
  85.                 case '-' : return right - left;
  86.                 case '*' : return right * left;
  87.                 case '/' : return right / left;
  88.                 default : return 0;
  89.             }
  90.         }
  91.  
  92.         public static bool IsOperation(char x) =>
  93.             (x == '(' || x == '+' || x == '-' || x == '*' || x == '/' || x == ')');
  94.         public static int GetPriority(char operation)
  95.         {
  96.             switch (operation)
  97.             {
  98.                 case '(': return 0;
  99.                 case '+': return 1;
  100.                 case '-': return 1;
  101.                 case '*': return 2;
  102.                 case '/': return 2;
  103.             }
  104.             return -1;
  105.         }  
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement