好友
阅读权限10
听众
最后登录1970-1-1
|
第八章.下
——————————
此章为代码章
解决问题:能处理有括号的运算。(括号暂时只有圆括号)(运算符号暂时只有-+*%/^)
——————————
比较第六章,除了添加了括号运算,还添加了负值运算。(有心人,其实不难发现,第六章的代码无法进行负值运算)
——————————
——————————
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
namespace ScriptCode
{
public class ScriptCodeU
{
/*
*/
public string Eval(string myScript)
{
string myResult = string.Empty;
//-----
myResult = eval_seniorElement(myScript);
myResult = SpecialTran_ToNormal(myResult);
return myResult;
}
/*用于解决没有括号的运算*/
private string eval_basicElement(string myScript)
{
string myResult;
//-----
/*定义储存变量*/
string LeftVar = string.Empty;//左变量
string RightVar = string.Empty;//右变量
string unRunCode = string.Empty;//非执行代码
bool SwitchOfRun = false;//运算开关
char nowRunChar = char.MinValue;//当前运算符号
string cmdResult;//计算结果
/*定义优先级*/
List<string> rankRunChar = new List<string>();
rankRunChar.Add("^");
rankRunChar.Add("*/%");
rankRunChar.Add("+-");
/*前期准备*/
string tempScript = myScript;
/*正式执行*/
for(int c=0;c<rankRunChar.Count; c++)
{
for (int i = 0; i < tempScript.Length; i++)
{
int CharType = judgeChar(tempScript[i], rankRunChar.ToArray(), c);
if (CharType == 0)
{//如果为:非当前优先级运算符号
if (SwitchOfRun)
{
//如果运算开关为真
cmdResult = cmd(nowRunChar, LeftVar, RightVar);
unRunCode += cmdResult+tempScript[i];
LeftVar = string.Empty;
RightVar = string.Empty;
SwitchOfRun = false;
}
else
{
//如果运算开关为假
unRunCode += LeftVar;
LeftVar = string.Empty;
unRunCode += tempScript[i];
}
}
else if (CharType == 1)
{//当前优先级运算符号
if (SwitchOfRun)
{
cmdResult = cmd(nowRunChar, LeftVar, RightVar);
LeftVar = string.Empty;
LeftVar += cmdResult;
RightVar = string.Empty;
SwitchOfRun = true;
nowRunChar = tempScript[i];
}
else
{
//如果运算开关为假
SwitchOfRun = true;
nowRunChar = tempScript[i];
}
}
else if (CharType == 2)
{
//数值
if (SwitchOfRun)
{
//如果运算开关为真
RightVar += tempScript[i];
}
else
{
//如果运算开关为假
LeftVar += tempScript[i];
}
}
}
//做最后一次运算
if (SwitchOfRun)
{
cmdResult = cmd(nowRunChar, LeftVar, RightVar);
unRunCode += cmdResult;
}else
{
unRunCode +=LeftVar;
}
//递交给下一次计算
tempScript = unRunCode;
//重置
unRunCode = string.Empty;
LeftVar = string.Empty;
RightVar = string.Empty;
SwitchOfRun = false;
nowRunChar = char.MinValue;
cmdResult = string.Empty;
}
myResult = tempScript;
//-----
return myResult;
}
/*用于解决有括号的运算,使用到basic*/
private string eval_seniorElement(string myScript)
{
string myResult = string.Empty;
//-----
/*首先定义以下变量*/
int IndexOfBracket = -1;//圆括号的编号
char TypeOfBracket = char.MinValue;//圆括号的类型
string unRunCode = string.Empty;//非执行代码
string ContentOfBracket = string.Empty;//括号里的内容
/*正式执行解析*/
/*首先判断是否存在圆括号*/
bool ExistOfBracket = myScript.IndexOf("(") != -1 && myScript.IndexOf(")") != -1;
if (ExistOfBracket)
{
//如果存在
for (int i=0;i<myScript.Length; i++)
{
int TypeOfChar = judgeCharOfBracket(myScript [i]);
if(TypeOfChar == -1)
{
//如果为非()
if(IndexOfBracket == -1)
{
//判断【圆括号的编号】为-1
unRunCode += myScript[i];
}else
{
//判断【圆括号的编号】非-1
ContentOfBracket += myScript[i];
}
}else if (TypeOfChar == 0)
{
//如果为(
if (TypeOfBracket != ')')
{
IndexOfBracket++;
}
TypeOfBracket = '(';
if (IndexOfBracket != 0)
{
ContentOfBracket += myScript[i];
}
}
else if ( TypeOfChar == 1)
{
//如果为)
if(TypeOfBracket == '(')
{
TypeOfBracket = ')';
if(IndexOfBracket > 0)
{
ContentOfBracket += myScript[i];
}else if (IndexOfBracket == 0)
{
unRunCode +=eval_seniorElement(ContentOfBracket);
/*下面为重置*/
ContentOfBracket = string.Empty;
IndexOfBracket = -1;
TypeOfBracket = char.MinValue;
}
}else if (TypeOfBracket == ')')
{
IndexOfBracket--;
if(IndexOfBracket == 0)
{
unRunCode += eval_seniorElement(ContentOfBracket);
/*下面为重置*/
ContentOfBracket = string.Empty;
IndexOfBracket = -1;
TypeOfBracket = char.MinValue;
}else
{
ContentOfBracket += myScript[i];
}
}
}
/*
*/
//MessageBox.Show("unRun:"+unRunCode +"\r\n"+"ContentOf:"+ContentOfBracket);
}
myResult = eval_basicElement(unRunCode);
}else
{
//如果不存在()
myResult= eval_basicElement(myScript);
}
//-----
return myResult;
}
/*加减乘除取余次方*/
/*请不要使用$作为运算符号,$在内部被使用于负号表示*/
private string plues(string X,string Y)
{
string Z;
//-----
double lx = double.Parse(X);
double ly = double.Parse(Y);
Z = (lx + ly).ToString();
//-----
return Z;
}
private string minus(string X,string Y)
{
string Z;
//-----
double lx = X .Length !=0?double.Parse(X):0;
double ly = double.Parse(Y);
Z = (lx - ly).ToString();
//-----
return Z;
}
private string multiply(string X,string Y)
{
string Z;
//-----
double lx = double.Parse(X);
double ly = double.Parse(Y);
Z = (lx * ly).ToString();
//-----
return Z;
}
private string divide(string X,string Y)
{
string Z;
//-----
double lx = double.Parse(X);
double ly = double.Parse(Y);
Z = (lx / ly).ToString();
//-----
return Z;
}
private string module(string X,string Y)
{
string Z;
//-----
double lx = double.Parse(X);
double ly = double.Parse(Y);
Z = (lx % ly).ToString();
//-----
return Z;
}
private string pow(string X,string Y)
{
string Z;
//-----
Z = Math.Pow(double.Parse(X), double.Parse(Y)).ToString();
//-----
return Z ;
}
/*统一运算*/
private string cmd(char myCmd,string X,string Y)
{
string myResult = string.Empty;
//-----
X = SpecialTran_ToNormal(X);
Y = SpecialTran_ToNormal(Y);
//
switch (myCmd)
{
case '+':
myResult = plues(X ,Y);
break;
case '-':
myResult = minus(X,Y);
break;
case '*':
myResult = multiply(X,Y);
break;
case '/':
myResult = divide(X,Y);
break;
case '%':
myResult = module(X,Y);
break;
case '^':
myResult = pow(X,Y);
break;
}
//
myResult = SpecialTran_ToInner(myResult);
//-----
return myResult;
}
/*判断字符类型*/
private int judgeChar(char myChar, string[] myRunChar, int myRank)
{
/*
0:非当前优先级运算符号
1:当前优先级运算符号
2:数值
3:未知符号
4:
*/
int myResult = 4;
//-----
if ((myChar >= '0' && myChar <= '9')||myChar =='.'||myChar =='$')//$用于标记负号
{
myResult = 2;//数值
}
bool isRunChar = false;
for (int i = 0; i < myRunChar.Length; i++)
{
if (myRunChar[i].IndexOf(myChar) != -1)
{
isRunChar = true;
break;
}
}
if (isRunChar)
{
if (myRunChar[myRank].IndexOf(myChar) != -1)
{
myResult = 1;
}
else
{
myResult = 0;
}
}
//-----
return myResult;
}
/*用于判断是否为圆括号类型*/
private int judgeCharOfBracket(char myChar)
{
/*
-1:非()
0:(
1:)
*/
int myResult = -1;
//-----
if(myChar == '(')
{
myResult = 0;
}else if (myChar == ')')
{
myResult = 1;
}
//-----
return myResult;
}
/*为了支持负值运算,使用$来表示负号*/
/*将-1转换成$1*/
private string SpecialTran_ToInner(string myValue)
{
return myValue.Replace('-','$');
}
/*将$1转换成-1*/
private string SpecialTran_ToNormal(string myValue)
{
return myValue.Replace('$', '-');
}
}
}
|
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|