using System;
using System.Collections.Generic;
using System.Text;
namespace 波兰表达式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new 后缀表达式计算("12/4+#").run());
}
}
class 后缀表达式计算
{
string exp;
float[] a;
int top;
public 后缀表达式计算(string v)
{
exp = v;
a = new float[v.Length];
top = -1;
}
public float run()
{
int i=0;
float v1;
float v2;
while (exp[i] != '#')
{
switch(exp[i])
{
case '+':
v1 = a[top--];
v2 = a[top--];
a[++top] = v2 + v1;
break;
case '-':
v1 = a[top--];
v2 = a[top--];
a[++top] = v2 - v1;
break;
case '*':
v1 = a[top--];
v2 = a[top--];
a[++top] = v2 * v1;
break;
case '/':
v1 = a[top--];
v2 = a[top--];
a[++top] = v2 / v1;
break;
default:
a[++top] = float.Parse(exp[i].ToString());
break;
}
i++;
}
return a[0];
}
}
}