using System using System Collections Generic using System Linq using

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PolNotReverse {
class Program {
static void Main(string[] args) {
PostfixNotationExpression pne = new PostfixNotationExpression();
string[] result = pne.ConvertToPostfixNotation("( 2 * 3 ) / 5 + 10");
foreach(string str in result) {
Console.Write(str.Trim(new char[] {' '}));
}
Console.ReadLine();
}
}
public class PostfixNotationExpression {
public PostfixNotationExpression() {
operators = new List<string>(standart_operators);
}
private List<string> operators;
private List<string> standart_operators =
new List<string>(new string[] { "(", ")", "+", "-", "*", "/", "^" });
private IEnumerable<string> Separate(string input) {
int pos = 0;
while(pos < input.Length) {
string s = string.Empty + input[pos];
if(!standart_operators.Contains(input[pos].ToString())) {
if(Char.IsDigit(input[pos]))
for(int i = pos + 1; i < input.Length &&
(Char.IsDigit(input[i]) || input[i] == ',' || input[i] == '.'); i++)
s += input[i];
else if(Char.IsLetter(input[pos]))
for(int i = pos + 1; i < input.Length &&
(Char.IsLetter(input[i]) || Char.IsDigit(input[i])); i++)
s += input[i];
}
yield return s;
pos += s.Length;
}
}
private byte GetPriority(string s) {
switch(s) {
case "(":
case ")":
return 0;
case "+":
case "-":
return 1;
case "*":
case "/":
return 2;
case "^":
return 3;
default:
return 4;
}
}
public string[] ConvertToPostfixNotation(string input) {
List<string> outputSeparated = new List<string>();
Stack<string> stack = new Stack<string>();
foreach(string c in Separate(input)) {
if(operators.Contains(c)) {
if(stack.Count > 0 && !c.Equals("(")) {
if(c.Equals(")")) {
string s = stack.Pop();
while(s != "(") {
outputSeparated.Add(s);
s = stack.Pop();
}
} else if(GetPriority(c) > GetPriority(stack.Peek()))
stack.Push(c);
else {
while(stack.Count > 0 && GetPriority(c) <= GetPriority(stack.Peek()))
outputSeparated.Add(stack.Pop());
stack.Push(c);
}
} else
stack.Push(c);
} else
outputSeparated.Add(c);
}
if(stack.Count > 0)
foreach(string c in stack)
outputSeparated.Add(c);
return outputSeparated.ToArray();
}
public decimal result(string input) {
Stack<string> stack = new Stack<string>();
Queue<string> queue = new Queue<string>(ConvertToPostfixNotation(input));
string str = queue.Dequeue();
while(queue.Count >= 0) {
if(!operators.Contains(str)) {
stack.Push(str);
str = queue.Dequeue();
} else {
decimal summ = 0;
try {
switch(str) {
case "+": {
decimal a = Convert.ToDecimal(stack.Pop());
decimal b = Convert.ToDecimal(stack.Pop());
summ = a + b;
break;
}
case "-": {
decimal a = Convert.ToDecimal(stack.Pop());
decimal b = Convert.ToDecimal(stack.Pop());
summ = b - a;
break;
}
case "*": {
decimal a = Convert.ToDecimal(stack.Pop());
decimal b = Convert.ToDecimal(stack.Pop());
summ = b * a;
break;
}
case "/": {
decimal a = Convert.ToDecimal(stack.Pop());
decimal b = Convert.ToDecimal(stack.Pop());
summ = b / a;
break;
}
case "^": {
decimal a = Convert.ToDecimal(stack.Pop());
decimal b = Convert.ToDecimal(stack.Pop());
summ = Convert.ToDecimal(Math.Pow(Convert.ToDouble(b), Convert.ToDouble(a)));
break;
}
}
} catch(Exception ex) {
Console.WriteLine(ex.Message);
}
stack.Push(summ.ToString());
if(queue.Count > 0)
str = queue.Dequeue();
else
break;
}
}
return Convert.ToDecimal(stack.Pop());
}
}
}