vencinachev

Stack-Expressions

Sep 21st, 2021 (edited)
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 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 DataStructures
  8. {
  9.     class Program
  10.     {
  11.        
  12.         static int Evaluate(string expression)
  13.         {
  14.             Stack<int> nums = new Stack<int>();
  15.             Stack<char> operations = new Stack<char>();
  16.  
  17.             foreach (var item in expression)
  18.             {
  19.                 if (char.IsDigit(item))
  20.                 {
  21.                     nums.Push(item - '0');
  22.                 }
  23.                 else if (item == '(')
  24.                 {
  25.                     nums.Push('(');
  26.                 }
  27.                 else if (item == '+' || item == '*')
  28.                 {
  29.                     operations.Push(item);
  30.                 }
  31.                 else if (item == ')')
  32.                 {
  33.                     char op = operations.Pop();
  34.                     if (op == '+')
  35.                     {
  36.                         int result = 0;
  37.                         while (nums.Peek() != '(')
  38.                         {
  39.                             result += nums.Pop();
  40.                         }
  41.                         nums.Pop(); // remove '('
  42.                         nums.Push(result);
  43.                     }
  44.                     else if (op == '*')
  45.                     {
  46.                         int result = 1;
  47.                         while (nums.Peek() != '(')
  48.                         {
  49.                             result *= nums.Pop();
  50.                         }
  51.                         nums.Pop(); // remove '('
  52.                         nums.Push(result);
  53.                     }
  54.                 }
  55.             }
  56.             return nums.Peek();
  57.            
  58.         }
  59.  
  60.         static int f(int x)
  61.         {
  62.             return 5 * x + 1;
  63.         }
  64.  
  65.         static int m(int x, int y)
  66.         {
  67.             return (x < y) ? x : y;
  68.         }
  69.  
  70.         static int s(int x, int y)
  71.         {
  72.             return x - y;
  73.         }
  74.  
  75.         static int EvalFunction(string expression)
  76.         {
  77.             Stack<int> nums = new Stack<int>();
  78.             Stack<char> func = new Stack<char>();
  79.  
  80.             foreach (var item in expression)
  81.             {
  82.                 if (char.IsDigit(item))
  83.                 {
  84.                     nums.Push(item - '0');
  85.                 }
  86.                 else if (item == 'f' || item == 'm' || item == 's')
  87.                 {
  88.                     func.Push(item);
  89.                 }
  90.                 else if (item == ')')
  91.                 {
  92.                     int function = func.Pop();
  93.                     if (function == 'f')
  94.                     {
  95.                         int x = nums.Pop();
  96.                         nums.Push(f(x));
  97.                     }
  98.                     else if (function == 'm')
  99.                     {
  100.                         int y = nums.Pop();
  101.                         int x = nums.Pop();
  102.                         nums.Push(m(x, y));
  103.                     }
  104.                     else if (function == 's')
  105.                     {
  106.                         int y = nums.Pop();
  107.                         int x = nums.Pop();
  108.                         nums.Push(s(x, y));
  109.                     }
  110.                 }
  111.             }
  112.             return nums.Peek();
  113.         }
  114.  
  115.         static void Main(string[] args)
  116.         {
  117.             Console.Write("Enter expression: ");
  118.             string expr = Console.ReadLine();
  119.             Console.WriteLine("{0} = {1}", expr, EvalFunction(expr));
  120.         }
  121.            
  122.     }
  123. }
  124.  
Add Comment
Please, Sign In to add comment