我想執行下面的程序,但它顯示錯誤「程序不包含適用於入口點CSC的靜態主要方法」...嘗試評估算法表達式使用堆棧(BODMAS)。如果有人知道請解決這個問題。不能執行c#程序不包含靜態主要方法錯誤
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"));
}
}
我如上編輯。 現在接近無效預計的分級數字代表錯誤即將到來
'main' - >'Main'。 – dasblinkenlight