Написать программу, которая преобразует выражение из инфиксной формы в префиксную - C#
Формулировка задачи:
Используя класс Stack написать программу, которая преобразует выражение из инфиксной формы в префиксную.
т.е. по такому принципу:
Решение задачи: «Написать программу, которая преобразует выражение из инфиксной формы в префиксную»
textual
Листинг программы
class Program
{
static void Main(string[] args)
{
// string infix = "3 ^ 4 + ( 11 - ( 3 * 2 ) ) / 2";
string infix = "3 + 2";
string[] tokens = infix.Split(' ');
Stack<string> s = new Stack<string>();
List<string> outputList = new List<string>();
int n;
foreach (string c in tokens)
{
if (int.TryParse(c.ToString(), out n))
{
outputList.Add(c);
}
if (c == "(")
{
s.Push(c);
}
if (c == ")")
{
while (s.Count != 0 && s.Peek() != "(")
{
outputList.Add(s.Pop());
}
s.Pop();
}
if (isOperator(c) == true)
{
while (s.Count != 0 && Priority(s.Peek()) >= Priority(c))
{
outputList.Add(s.Pop());
}
s.Push(c);
}
}
while (s.Count != 0)//if any operators remain in the stack, pop all & add to output list until stack is empty
{
outputList.Add(s.Pop());
}
for (int i = 0; i < outputList.Count; i++)
{
Console.Write("{0}", outputList[i]);
}
Console.ReadLine();
}
static int Priority(string c)
{
if (c == "^")
{
return 3;
}
else if (c == "*" || c == "/")
{
return 2;
}
else if (c == "+" || c == "-")
{
return 1;
}
else
{
return 0;
}
}
static bool isOperator(string c)
{
if (c == "+" || c == "-" || c == "*" || c == "/" || c == "^")
{
return true;
}
else
{
return false;
}
}
}