续一下,还没完 public class Parser: Scanner
{
private static readonly Dictionary<int, int> leftPriority = new Dictionary<int, int>();
private static readonly Dictionary<int, int> rightPriority = new Dictionary<int, int>();
static Parser()
{
leftPriority.Add('+', 6); rightPriority.Add('+', 6);
leftPriority.Add('-', 6); rightPriority.Add('-', 6);
leftPriority.Add('*', 7); rightPriority.Add('*', 7);
leftPriority.Add('/', 7); rightPriority.Add('/', 7);
leftPriority.Add('%', 7); rightPriority.Add('%', 7);
leftPriority.Add('^', 10); rightPriority.Add('^', 9); // right associative
} public Parser(string data): base(data)
{
} public int ParseExpression(out double v, int limit)
{
ParseSimpleExpression(out v); // the expression must start with a simple expression
int op = this.Token;
// expand while operators have priorities higher than 'limit'
while (leftPriority.ContainsKey(op) && leftPriority[op] > limit)
{
Next();
double rhs; // right hand side
// read expression with higher priority
int nextop = ParseExpression(out rhs, rightPriority[op]);
switch (op)
{
case '+':
v = v + rhs;
break;
case '-':
v = v - rhs;
break;
case '*':
v = v * rhs;
break;
case '/':
v = v / rhs;
break;
case '%':
v = v % rhs;
break;
case '^':
v = Math.Pow(v, rhs);
break;
default:
throw new Exception("Expected an operator token, but was: '" + TokenToString(op) + "'");
}
op = nextop;
}
return op;
} private void ParseSimpleExpression(out double v)
{
if (this.Token == TK_NUMBER)
{
v = this.DoubleData;
Next();
return;
}
if (this.Token == TK_PI)
{
v = Math.PI;
Next();
return;
}
if (this.Token == TK_E)
{
v = Math.E;
Next();
return;
}
if (this.Token == TK_SIN)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Sin(v);
return;
}
if (this.Token == TK_COS)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Cos(v);
return;
}
if (this.Token == TK_TAN)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Tan(v);
return;
}
if (this.Token == TK_LG)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Log10(v);
return;
}
if (this.Token == TK_LN)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Log(v);
return;
}
throw new Exception("Unexpected token: '" + TokenToString(this.Token) + "'");
} private void ExpectToken(int t)
{
if (this.Token != t) throw new Exception(String.Format("Expected token: '{0]', but was '{1}'", TokenToString(t), TokenToString(this.Token)));
Next();
} public static new void Test()
{
double v;
string s = "3*6 + 2 - 10 % 99 + lg(8)/lg(2)";
new Parser(s).ParseExpression(out v, 0);
Console.WriteLine("{0} = {1}", s, v);
s = "ln(e^2)";
new Parser(s).ParseExpression(out v, 0);
Console.WriteLine("{0} = {1}", s, v);
s = "sin(pi/2)";
new Parser(s).ParseExpression(out v, 0);
Console.WriteLine("{0} = {1}", s, v);
foreach(EncodingInfo ei in Encoding.GetEncodings())
{
Encoding e = ei.GetEncoding();
Console.Write( "{0,-6} {1,-25} {2,-40}", ei.CodePage, ei.Name, ei.DisplayName);
Console.WriteLine();
} }
} public static double Evaluate(string s)
{
double v;
new Parser(s).ParseExpression(out v, 0);
return v;
}
}
}
{
private static readonly Dictionary<int, int> leftPriority = new Dictionary<int, int>();
private static readonly Dictionary<int, int> rightPriority = new Dictionary<int, int>();
static Parser()
{
leftPriority.Add('+', 6); rightPriority.Add('+', 6);
leftPriority.Add('-', 6); rightPriority.Add('-', 6);
leftPriority.Add('*', 7); rightPriority.Add('*', 7);
leftPriority.Add('/', 7); rightPriority.Add('/', 7);
leftPriority.Add('%', 7); rightPriority.Add('%', 7);
leftPriority.Add('^', 10); rightPriority.Add('^', 9); // right associative
} public Parser(string data): base(data)
{
} public int ParseExpression(out double v, int limit)
{
ParseSimpleExpression(out v); // the expression must start with a simple expression
int op = this.Token;
// expand while operators have priorities higher than 'limit'
while (leftPriority.ContainsKey(op) && leftPriority[op] > limit)
{
Next();
double rhs; // right hand side
// read expression with higher priority
int nextop = ParseExpression(out rhs, rightPriority[op]);
switch (op)
{
case '+':
v = v + rhs;
break;
case '-':
v = v - rhs;
break;
case '*':
v = v * rhs;
break;
case '/':
v = v / rhs;
break;
case '%':
v = v % rhs;
break;
case '^':
v = Math.Pow(v, rhs);
break;
default:
throw new Exception("Expected an operator token, but was: '" + TokenToString(op) + "'");
}
op = nextop;
}
return op;
} private void ParseSimpleExpression(out double v)
{
if (this.Token == TK_NUMBER)
{
v = this.DoubleData;
Next();
return;
}
if (this.Token == TK_PI)
{
v = Math.PI;
Next();
return;
}
if (this.Token == TK_E)
{
v = Math.E;
Next();
return;
}
if (this.Token == TK_SIN)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Sin(v);
return;
}
if (this.Token == TK_COS)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Cos(v);
return;
}
if (this.Token == TK_TAN)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Tan(v);
return;
}
if (this.Token == TK_LG)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Log10(v);
return;
}
if (this.Token == TK_LN)
{
Next();
ExpectToken('(');
ParseExpression(out v, 0);
ExpectToken(')');
v = Math.Log(v);
return;
}
throw new Exception("Unexpected token: '" + TokenToString(this.Token) + "'");
} private void ExpectToken(int t)
{
if (this.Token != t) throw new Exception(String.Format("Expected token: '{0]', but was '{1}'", TokenToString(t), TokenToString(this.Token)));
Next();
} public static new void Test()
{
double v;
string s = "3*6 + 2 - 10 % 99 + lg(8)/lg(2)";
new Parser(s).ParseExpression(out v, 0);
Console.WriteLine("{0} = {1}", s, v);
s = "ln(e^2)";
new Parser(s).ParseExpression(out v, 0);
Console.WriteLine("{0} = {1}", s, v);
s = "sin(pi/2)";
new Parser(s).ParseExpression(out v, 0);
Console.WriteLine("{0} = {1}", s, v);
foreach(EncodingInfo ei in Encoding.GetEncodings())
{
Encoding e = ei.GetEncoding();
Console.Write( "{0,-6} {1,-25} {2,-40}", ei.CodePage, ei.Name, ei.DisplayName);
Console.WriteLine();
} }
} public static double Evaluate(string s)
{
double v;
new Parser(s).ParseExpression(out v, 0);
return v;
}
}
}
解决方案 »
- 智能感知和MSDN
- ((Button)sender).Text = "Clicked!"; 我不明白
- 关于ajax执行服务器事件后回发激发客户端script求助
- VS2008编写XNA游戏,载入3D模型编译错误。
- (C#)图像处理:一张图像中的找点问题???
- 奇怪的错误:SqlDataAdapter”并不包含“Fill”的定义
- C#中MediaPlayer控件中如何显示频谱显示外观
- struct网页下载的一个难题!求达人解决,100分都送上,标题很长大家都来关注!!
- 客户端的windows应用程序从远程服务器实现下载的功能,都有那些好办法?
- 怎么确定DATAGRID行是否被选中?
- 用C#.NET能否编写出不利用任何网站的邮箱发送电子邮件?
- 问题
建议楼主问问题还是要找到要点啊
自己分析分析 出了问题的话贴出来
大家更容易帮你
呵呵