Advertisement
vencinachev

Expressions

Jun 8th, 2021
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Algo
  9. {
  10.     class Program
  11.     {
  12.         static int Evaluate2(string expr)
  13.         {
  14.             Stack<char> op = new Stack<char>();
  15.             Stack<int> nums = new Stack<int>();
  16.  
  17.             foreach (var ch in expr)
  18.             {
  19.                 if (ch == '+' || ch == '*')
  20.                 {
  21.                     op.Push(ch);
  22.                 }
  23.                 else if (ch >= '0' && ch <= '9')
  24.                 {
  25.                     nums.Push(ch - '0');
  26.                 }
  27.                 else if (ch == '(')
  28.                 {
  29.                     nums.Push('(');
  30.                 }
  31.                 else if (ch == ')')
  32.                 {
  33.                     int result = 0;
  34.                     if (op.Peek() == '+')
  35.                     {
  36.                         while (nums.Peek() != '(')
  37.                         {
  38.                             result += nums.Peek();
  39.                             nums.Pop();
  40.                         }
  41.                     }
  42.                     else if (op.Peek() == '*')
  43.                     {
  44.                         result = 1;
  45.                         while (nums.Peek() != '(')
  46.                         {
  47.                             result *= nums.Peek();
  48.                             nums.Pop();
  49.                         }
  50.                     }
  51.                     nums.Pop(); // remove (
  52.                     nums.Push(result);
  53.                     op.Pop();
  54.                 }
  55.             }
  56.             return nums.Peek();
  57.         }
  58.  
  59.         static int Evaluate1(string expr)
  60.         {
  61.             Stack<char> op = new Stack<char>();
  62.             Stack<int> nums = new Stack<int>();
  63.  
  64.             foreach (var ch in expr)
  65.             {
  66.                 if (ch == '+' || ch == '*')
  67.                 {
  68.                     op.Push(ch);
  69.                 }
  70.                 else if (ch >= '0' && ch <= '9')
  71.                 {
  72.                     nums.Push(ch - '0');
  73.                 }
  74.                 else if (ch == ')')
  75.                 {
  76.                     char oper = op.Pop();
  77.                     int result = 0;
  78.                     if (oper == '+')
  79.                     {
  80.                         result = nums.Pop() + nums.Pop();
  81.                     }
  82.                     else if (oper == '*')
  83.                     {
  84.                         result = nums.Pop() * nums.Pop();
  85.                     }
  86.                     nums.Push(result);
  87.                 }
  88.             }
  89.             return nums.Peek();
  90.         }
  91.  
  92.         static void Main(string[] args)
  93.         {
  94.             Console.WriteLine(Evaluate1("+(2,*(3,8))"));
  95.         }
  96.     }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement