using System.Collections.Generic;
public static void Main()
var input = new Queue<string>("( 1 + 2 ) * 3 ^ 4".Split(' '));
Console.WriteLine(AddSubtract(input));
private static double AddSubtract(Queue<string> tokens)
var value = MultiplyDivide(tokens);
while(tokens.Count > 0 && tokens.Peek() is "+" or "-")
var op = tokens.Dequeue();
var next = MultiplyDivide(tokens);
value = op is "+" ? value + next : value - next;
private static double MultiplyDivide(Queue<string> tokens)
var value = Power(tokens);
while(tokens.Count > 0 && tokens.Peek() is "*" or "/")
var op = tokens.Dequeue();
var next = Power(tokens);
value = op is "*" ? value * next : value / next;
private static double Power(Queue<string> tokens)
var value = Unary(tokens);
if(tokens.Count > 0 && tokens.Peek() is "^")
value = Math.Pow(value, Power(tokens));
private static double Unary(Queue<string> tokens)
private static double Const(Queue<string> tokens)
var value = AddSubtract(tokens);
return double.Parse(tokens.Dequeue());