C#中出现意外符号`void' [英] Unexpected symbol `void' in C#

查看:218
本文介绍了C#中出现意外符号`void'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

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

推荐答案

你的主要方法必须包含在一个类......



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

public class App
{
    public static void Main(string [] args)
    {
        // ...
    }
}


这篇关于C#中出现意外符号`void'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆