Advertisement
Guest User

Math praser

a guest
Jul 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 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. using System.Data;
  7.  
  8. namespace calcu
  9. {
  10.     class Program
  11.     {  
  12.         // Function to transform an infix expression to its postfix form.
  13.         static string InfixPostfix(string infix)
  14.         {
  15.             string postfix = "";
  16.             Stack<char> opers = new Stack<char>();
  17.             if(infix[0] == '-')
  18.             {
  19.                 infix = "0" + infix;
  20.             }
  21.             for (int i = 0; i < infix.Length; i++)
  22.             {
  23.                 if (infix[i] == ' ') { continue; }
  24.                 if (!"+*/-()".Contains(infix[i]))
  25.                 {
  26.                     postfix += infix[i];
  27.                 }
  28.                 else if ("+*/-".Contains(infix[i]))
  29.                 {
  30.                     postfix += ' ';
  31.                     while ((opers.Count != 0) && opers.Peek() != '(' && IsFirstStronger(opers.Peek(), infix[i]))
  32.                     {
  33.                         postfix += opers.Pop();
  34.                         postfix += ' ';
  35.                     }
  36.                     opers.Push(infix[i]);
  37.                 }
  38.                 else if (infix[i] == '(')
  39.                 {
  40.                     opers.Push('(');
  41.                 }
  42.                 else if(infix[i] == ')')
  43.                 {
  44.                     while(opers.Count()!= 0 && opers.Peek() != '(')
  45.                     {
  46.                         postfix += ' ';
  47.                         postfix += opers.Pop();
  48.                        
  49.                     }
  50.                     opers.Pop();
  51.                 }
  52.             }
  53.             while (opers.Count != 0)
  54.             {
  55.                 postfix += ' ';
  56.                 postfix += opers.Pop();
  57.             }
  58.  
  59.             return postfix;
  60.         }
  61.         // Function to compare the precedence of two operators giving as input
  62.         // and returns true if the first has higher order than the second.
  63.         static bool IsFirstStronger(char nibber1, char nibber2)
  64.         {
  65.             Dictionary<char, int> d = new Dictionary<char, int>()
  66.             {
  67.                 {'*', 2},
  68.                 {'/', 2},
  69.                 {'+', 1},
  70.                 {'-', 1}
  71.             };
  72.             if (d[nibber1] == d[nibber2])
  73.             {
  74.                 return true;
  75.             }
  76.             return d[nibber1] > d[nibber2];
  77.         }
  78.         // Function to evaluate a postfix expression.
  79.         static float EvaluatePostfix(string postfix)
  80.         {
  81.             float result = 0f;
  82.             List<string> expression = new List<string>();
  83.             expression = postfix.Split(' ').ToList();
  84.             string temp = "";
  85.             Stack<string> oprand = new Stack<string>();
  86.             foreach (string ele in expression)
  87.             {
  88.                 if (!"+-/*".Contains(ele))
  89.                 {
  90.                     oprand.Push(ele);
  91.                 }
  92.                 else if ("+-/*".Contains(ele))
  93.                 {
  94.                     oprand.Push(Compute(oprand.Pop(), oprand.Pop(), ele[0]).ToString());
  95.                 }
  96.             }
  97.             return float.Parse(oprand.Pop());
  98.         }
  99.         static float Compute(string num2, string num1, char oper)
  100.         {
  101.             float intarino1 = float.Parse(num1);
  102.             float intarino2 = float.Parse(num2);
  103.             switch (oper)
  104.             {
  105.                 case '+':
  106.                     return intarino1 + intarino2;
  107.                 case '-':
  108.                     return intarino1 - intarino2;
  109.                 case '*':
  110.                     return intarino1 * intarino2;
  111.                 case '/':
  112.                     return intarino1 / intarino2;
  113.                 default:
  114.                     return 5;
  115.             }
  116.         }
  117.  
  118.  
  119.  
  120.  
  121.         static void Main()
  122.         {
  123.             string input;
  124.             while (true)
  125.             {
  126.                 Console.WriteLine("mathematical expression (type x to exit):");
  127.                 input = Console.ReadLine();
  128.                 Console.WriteLine($"postfix notation : {InfixPostfix(input)}");
  129.                 Console.WriteLine($"result = {EvaluatePostfix(InfixPostfix(input))}");
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement