using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int c = 30;
int d = 2;
Stack<Object> oldStack = new Stack<Object>();
Object[] obj = new Object[] { a, "*", "(", "(", b, "+", c, ")", "/", d, ")" };
for (int i = obj.Length - 1; i >= 0; i--)
{
oldStack.Push(obj[i]);
//Console.WriteLine(obj[i].ToString());
}
//前面正确
Stack<Object> newStack = new Stack<Object>();
while (oldStack.Count > 0)
{
Object o = oldStack.Pop();
// Console.WriteLine(oldStack.Count.ToString());
if (o.ToString() != ")")
{
//Console.WriteLine(o);
newStack.Push(o);
}
else
{
Object temp1 = null;
Stack<Object> temp = new Stack<Object>();
temp1 = newStack.Pop();
while (temp1.ToString() != "(")
{
temp.Push(temp1);
temp1 = newStack.Pop();
}
Object[] temp2 = new Object[temp.Count];
int i = 0;
while (temp.Count != 0)
{
temp2[i] = temp.Pop();
i++;
}
for (int j = 0; j < i; j++)
{
string opt = temp2[j].ToString();
if (opt == "*")
{
int opd = Convert.ToInt32(temp2[j - 1]) * Convert.ToInt32(temp2[j + 1]);
Object[] temp3 = new Object[i - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp2[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp2[m];
}
temp2 = temp3;
i = temp2.Length;
}
if (opt == "/")
{
int opd = Convert.ToInt32(temp2[j - 1]) / Convert.ToInt32(temp2[j + 1]);
Object[] temp3 = new Object[i - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp2[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp2[m];
}
temp2 = temp3;
i = temp2.Length;
}
}
for (int j = 0; j < i; j++)
{
string opt = temp2[j].ToString();
if (opt == "+")
{
int opd = Convert.ToInt32(temp2[j - 1]) + Convert.ToInt32(temp2[j + 1]);
Object[] temp3 = new Object[i - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp2[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp2[m];
}
temp2 = temp3;
i = temp2.Length;
}
if (opt == "-")
{
int opd = Convert.ToInt32(temp2[j - 1]) - Convert.ToInt32(temp2[j + 1]);
Object[] temp3 = new Object[i - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp2[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp2[m];
}
temp2 = temp3;
i = temp2.Length;
}
}
// Console.WriteLine(temp2[0]);
newStack.Push(temp2[0]);
}
// Console.WriteLine(newStack.Count.ToString());
//Console.WriteLine(newStack.Pop());
}
object[] temp4 = new object[newStack.Count];
int p=0;
while (newStack.Count != 0)
{
temp4[p++] = newStack.Pop();
}
for (int j = 0; j < p; j++)
{
string opt = temp4[j].ToString();
if (opt == "*")
{
int opd = Convert.ToInt32(temp4[j - 1]) * Convert.ToInt32(temp4[j + 1]);
Object[] temp3 = new Object[p - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp4[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp4[m];
}
temp4 = temp3;
p = temp4.Length;
}
if (opt == "/")
{
int opd = Convert.ToInt32(temp4[j - 1]) / Convert.ToInt32(temp4[j + 1]);
Object[] temp3 = new Object[p - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp4[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp4[m];
}
temp4 = temp3;
p = temp4.Length;
}
}
for (int j = 0; j < p; j++)
{
string opt = temp4[j].ToString();
if (opt == "+")
{
int opd = Convert.ToInt32(temp4[j - 1]) + Convert.ToInt32(temp4[j + 1]);
Object[] temp3 = new Object[p - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp4[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp4[m];
}
temp4 = temp3;
p = temp4.Length;
}
if (opt == "-")
{
int opd = Convert.ToInt32(temp4[j - 1]) - Convert.ToInt32(temp4[j + 1]);
Object[] temp3 = new Object[p - 2];
for (int m = 0; m < j - 1; m++)
{
temp3[m] = temp4[m];
}
temp3[j - 1] = opd;
for (int m = j + 2; m < j - 1; m++)
{
temp3[m - 2] = temp4[m];
}
temp4 = temp3;
p = temp4.Length;
}
}
Console.WriteLine(temp4[0].ToString());
Console.ReadKey();
}
}
}