...
/Solution Review: Evaluate Postfix Expression Using a Stack
Solution Review: Evaluate Postfix Expression Using a Stack
This review provides a detailed analysis to help you solve the "Postfix Expression Using a Stack" challenge.
We'll cover the following...
Solution: Numbers as stack elements
Press + to interact
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace chapter_4{class Challenge_6{static int evaluatePostFix(string exp){Stack<int> stack = new Stack<int>();char character;int x, y;//1.Scan expression character by character,//2.If character is a number push it in stack//3.If character is operator then pop two elements from stack//perform the operation and put the result back in stack//At the end, Stack will contain result of whole expression.for (int i = 0; i < exp.Length; i++){character = exp[i];if (!Char.IsDigit(character)){x = stack.Pop();y = stack.Pop();switch (character){case '+':stack.Push(y + x);break;case '-':stack.Push(y - x);break;case '*':stack.Push(y * x);break;case '/':stack.Push(y / x);break;}}elsestack.Push(character - '0');}return stack.Pop();}static void Main(string[] args){Console.WriteLine(evaluatePostFix("921*-8-4+"));}}}
Check each character of the string from left to right. If you find a digit, ...