无法执行c#程序不包含静态主方法错误

时间:2016-02-26 11:34:07

标签: c#

我正在尝试执行以下程序,但它显示错误“programe不包含适用于入口点CSC的静态主方法”...尝试使用堆栈(BODMAS)评估算术表达式。如果有人知道请解决此问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
    public class Class1
    {

        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();


            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();

            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();

            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;

                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);

                    values.Push(Convert.ToInt32(sbuf.ToString()));

                }

                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);

                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }

                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }

            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));


            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }

        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }

        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;

            }
            return 0;
        }

        public static void main(String[] args)
        {
            Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }

    }

}

我编辑如下。

namespace ConsoleApplication1
{
    public class Class1
    {

        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();


            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();

            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();

            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;

                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);

                    values.Push(Convert.ToInt32(sbuf.ToString()));

                }

                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);

                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }

                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }

            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));


            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }

        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }

        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;

            }
            return 0;
        }
    }
      public static void main(String[] args)
        {  
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }

}

namespace ConsoleApplication1
{
    public class Class1
    {

        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();


            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();

            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();

            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;

                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);

                    values.Push(Convert.ToInt32(sbuf.ToString()));

                }

                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);

                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }

                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }

            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));


            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }

        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }

        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;

            }
            return 0;
        }
    }
      public static void main(String[] args)
        {  
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }

}

namespace ConsoleApplication1
{
    public class Class1
    {

        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();


            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();

            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();

            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;

                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);

                    values.Push(Convert.ToInt32(sbuf.ToString()));

                }

                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);

                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }

                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }

            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));


            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }

        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }

        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;

            }
            return 0;
        }
    }
      public static void main(String[] args)
        {  
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }

}

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace ConsoleApplication1
    {
        public class Class1
        {

            public static string evaluate1(String expression)
            {
                char[] charArr = expression.ToCharArray();


                // Stack for numbers: 'values'
                Stack<Int32> values = new Stack<Int32>();

                // Stack for Operators: 'ops'
                Stack<Char> ops = new Stack<Char>();

                for (int i = 0; i < charArr.Length; i++)
                {
                    // Current token is a whitespace, skip it
                    if (charArr[i] == ' ')
                        continue;

                    // Current token is a number, push it to stack for numbers
                    if (charArr[i] >= '0' && charArr[i] <= '9')
                    {
                        StringBuilder sbuf = new StringBuilder();
                        //StringBuffer sbuf = new StringBuffer();
                        // There may be more than one digits in number
                        while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                            sbuf.Append(charArr[i++]);

                        values.Push(Convert.ToInt32(sbuf.ToString()));

                    }

                    // Current token is an opening brace, push it to 'ops'
                    else if (charArr[i] == '(')
                        ops.Push(charArr[i]);

                    // Closing brace encountered, solve entire brace
                    else if (charArr[i] == ')')
                    {
                        while (ops.Peek() != '(')
                            values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                        ops.Pop();
                    }

                    // Current token is an operator.
                    else if (charArr[i] == '+' || charArr[i] == '-' ||
                             charArr[i] == '*' || charArr[i] == '/')
                    {
                        // While top of 'ops' has same or greater precedence to current
                        // token, which is an operator. Apply operator on top of 'ops'
                        // to top two elements in values stack
                        while (hasPrecedence(charArr[i], ops.Peek()))
                            values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

                        // Push current token to 'ops'.
                        ops.Push(charArr[i]);
                    }
                }

                // Entire expression has been parsed at this point, apply remaining
                // ops to remaining values
                while (!ops.Equals(0))
                    values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));


                // Top of 'values' contains result, return it
                return values.Pop().ToString();
            }

            // Returns true if 'op2' has higher or same precedence as 'op1',
            // otherwise returns false.
            public static bool hasPrecedence(char op1, char op2)
            {
                if (op2 == '(' || op2 == ')')
                    return false;
                if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                    return false;
                else
                    return true;
            }

            // A utility method to apply an operator 'op' on operands 'a' 
            // and 'b'. Return the result.
            public static int applyOp(char op, int b, int a)
            {
                switch (op)
                {
                    case '+':
                        return a + b;
                    case '-':
                        return a - b;
                    case '*':
                        return a * b;

                }
                return 0;
            }
        }
          public static void main(String[] args)
            {  
            Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
            }

    }

我编辑如上。 现在接近void预期的类num委托错误即将到来

2 个答案:

答案 0 :(得分:3)

那是因为您的代码中存在拼写错误。

main应该Main被视为Main中的Visual studio方法。

答案 1 :(得分:0)

对我来说,您的初始代码似乎没问题,但您只需要创建一个新类并从Class1中提取下面的main方法并添加到新创建的类<​​/ p>

public class MainClass
{
    public static void main(String[] args)
    {
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
    }
}

注意:在您的编辑中,我认为您已多次粘贴代码。