Write a program to convert postfix expression to infix
expression.



Write a program to convert postfix expression to infix expression...

Answer / Ekta Agrawal

[C# code example:]

using System;
using System.Collections.Generic;
using System.Linq;

public class PostfixToInfix {
private static double EvaluatePostfix(Stack<double> stack, string postfix) {
var tokens = postfix.Split(' ');

foreach (var token in tokens) {
if (!double.TryParse(token, out double value)) {
switch (token[0]) {
case '+':
stack.Push(stack.Pop() + stack.Pop());
break;
case '-':
stack.Push(-stack.Pop() + stack.Pop());
break;
case '*':
stack.Push(stack.Pop() * stack.Pop());
break;
case '/':
double dividend = stack.Pop();
double divisor = stack.Pop();
stack.Push(dividend / divisor);
break;
}
} else {
stack.Push(double.Parse(token));
}
}

return stack.Pop();
}

public static string InfixToPostfix(string infix) {
var operators = new List<char> { '+', '-', '*', '/' };
var precedences = new Dictionary<char, int> { { '+', 1 }, { '-', 1 }, { '*', 2 }, { '/', 2 } };

var postfix = new Stack<string>();
var tokens = infix.Split(' ');
var operandStack = new Stack<string>();

foreach (var token in tokens) {
if (char.IsDigit(token[0])) {
postfix.Push(token);
} else if (operators.Contains(token[0])) {
while (operandStack.Count > 0 && precedences[operandStack.Peek()[0]] >= precedences[token[0]]) {
postfix.Push(" "+ operandStack.Pop() + " ");
}
operandStack.Push(token);
} else if (token == "(") {
operandStack.Push(token);
} else if (token == ")") {
while (operandStack.Peek() != "(") {
postfix.Push(" "+ operandStack.Pop() + " ");
}
operandStack.Pop();
}
}

while (operandStack.Count > 0) {
postfix.Push(" "+ operandStack.Pop() + " ");
}

return string.Join("", postfix);
}

public static void Main() {
var infix = "( 3 + ( 4 * 5 ) )";
Console.WriteLine($"Infix expression: {infix}");
Console.WriteLine($"Postfix expression: {InfixToPostfix(infix)}");

var postfix = "3 4 5 * +"";
double result = EvaluatePostfix(new Stack<double>(), postfix);
Console.WriteLine($"Result: {result}");
}
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Code Interview Questions

How to export 2 datatables of a single dataset to 2 different worksheets of a single MSExcel file ?

1 Answers   Eastcom Systems,


Write a function which accepts list of nouns as input parameter and return the same list in the plural form. Conditions: i) if last letter is r then append s ii) if word ends with y then replace it by ies iii) call this function in main() and produce the required output. for eg:- if chair is input it should give chairs as output.

1 Answers   Mind Tree,


how does the below eqation proceed to be solved: x*=y+z options: x=x*y+z or x=x*(y+z)

1 Answers  


Give the code for Handling Mouse Events?

1 Answers  


. Write a program to print the following outputs using for loops $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

3 Answers  


Write a program which has a function and that function should take 2 or 3 or any number of strings and it should return the largest common prefix of all those strings. If there is no common prefix it should return an empty string. for eg:- INPUT OUTPUT glo {glory,glorious,glod} gl {glad,glow} {calendar,phone} empty string

2 Answers   Mind Tree,


c# code to Count number of 1's in a given range of integer (0 to n)

1 Answers  


write a Program to copy the string using switch case.

0 Answers   Mind Tree, Wipro,


Write a program. there are 1..n numbers placed in an array in random fashion with one integer missing. find the missing number.

1 Answers   Infosys, Mind Tree,


How to Create a Treeview Menu in ASP.NET with C#?

1 Answers  


Write a function which accepts a sentence as input parameter.Each word in that sentence is to be reversed. Space should be there between each words.Return the sentence with reversed words to main function and produce the required output. for eg:- i/p: jack jill jung kill o/p: kcaj llij gnuj llik

1 Answers   Mind Tree,


Code for Searching for Multiple Matches with the MatchCollection Class?

1 Answers   TCS,


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)