Jitendra Kumar

Jitendra Kumar

  • NA
  • 189
  • 22k

How to do this dynamical. Then I can put the value of myself

Aug 22 2018 2:49 AM
class Program
{
static void Main(string[] args)
{
Stack Stack = new Stack();
string[] expression = { "10", "1","2","+","2","+","5","+", "+" };
for (int i = 0; i < expression.Length; i++)
{
Console.Write(expression[i]);
}
int result;
Console.WriteLine();
int n;
foreach (string c in expression)
{
if (int.TryParse(c, out n))
{
Stack.Push(n);
}
if (c == "+")
{
int x = Convert.ToInt32(Stack.Pop());
int y = Convert.ToInt32(Stack.Pop());
result = x + y;
Stack.Push(result);
}
if (c == "-")
{
int x = Convert.ToInt32(Stack.Pop());
int y = Convert.ToInt32(Stack.Pop());
result = y - x;
Stack.Push(result);
}
if (c == "*")
{
int x = Convert.ToInt32(Stack.Pop());
int y = Convert.ToInt32(Stack.Pop());
result = x * y;
Stack.Push(result);
}
if (c == "/")
{
int x = Convert.ToInt32(Stack.Pop());
int y = Convert.ToInt32(Stack.Pop());
result = y / x;
Stack.Push(result);
}
}
Console.WriteLine("enter any postfix expression");
Console.WriteLine("result of expression: {0}", Stack.Peek());
Console.ReadLine();
}
}