Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System;
static void Main(string[] args)
{
string inFix= string.Empty;
Console.Write("Enter InFix Expression: ");
inFix = Console.ReadLine().Replace(" ", string.Empty);
Console.WriteLine("\nPostFix: {0}",ConvertToPostFix(inFix));
Console.ReadKey();
}
private static string ConvertToPostFix(string inFix)
{
StringBuilder postFix = new StringBuilder();
char arrival;
Stack<char> oprerator = new Stack<char>();//Creates a new Stack
foreach (char c in inFix.ToCharArray())//Iterates characters in inFix
{
if (Char.IsNumber(c))
postFix.Append(c);
else if (c == '(')
oprerator.Push(c);
else if (c == ')')//Removes all previous elements from Stack and puts them in
//front of PostFix.
{
arrival = oprerator.Pop();
while (arrival != '(')
{
postFix.Append(arrival);
arrival = oprerator.Pop();
}
}
else
{
if (oprerator.Count != 0 && Predecessor(oprerator.Peek(), c))//If find an operator
{
arrival = oprerator.Pop();
while (Predecessor(arrival, c))
{
postFix.Append(arrival);
if (oprerator.Count == 0)
break;
arrival = oprerator.Pop();
}
oprerator.Push(c);
}
else
oprerator.Push(c);//If Stack is empty or the operator has precedence
}
}
while (oprerator.Count > 0)
{
arrival = oprerator.Pop();
postFix.Append(arrival);
}
return postFix.ToString();
}
private static bool Predecessor(char firstOperator, char secondOperator)
{
string opString = "(+-*/%";
int firstPoint, secondPoint;
int[] precedence = { 0, 12, 12, 13, 13, 13 };// "(" has less prececence
firstPoint = opString.IndexOf(firstOperator);
secondPoint = opString.IndexOf(secondOperator);
return (precedence[firstPoint] >= precedence[secondPoint]) ? true : false;
}

error:
VB
prog.cs(5,7): error CS1525: Unexpected symbol `void', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
prog.cs(5,24): warning CS0658: `]' is invalid attribute target. All attributes in this attribute section will be ignored
prog.cs(66,69): warning CS0658: `false' is invalid attribute target. All attributes in this attribute section will be ignored
prog.cs(67,1): error CS8025: Parsing error
Posted
Updated 7-Aug-14 9:28am
v2

1 solution

Your Main method must be contained in a class...

C#
public class App
{
    public static void Main(string [] args)
    {
        // ...
    }
}
 
Share this answer
 
Comments
Rob Grainger 7-Aug-14 14:32pm    
For future reference, use the "code" button above the editor panel when submitting code - makes it much easier for respondents to read, and spot your problem.
CPallini 7-Aug-14 15:29pm    
5. Indeed C# has no 'functions', only 'methods'. :-)
[no name] 8-Aug-14 0:42am    
You found it. +5
juliet-coder 24-Aug-19 15:54pm    
Thanks a lot for this solution! My code now works!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900