AdditionAndSubtraction); } else { iNotMatch++; } if (regexE

时间:2022-05-22 08:48:25

1.熟悉体系布局的气势派头的观点

2.理解和应用管道过滤器型的气势派头。

3、理解解释器的道理

4、理解编译器模型

二、尝试环境

硬件: 

软件:Python或任何一种本身喜欢的语言

三、尝试内容

1、实现“四则运算”的简易翻译器。

功效要求:

1)实现加减乘除四则运算,,允许同时又多个操纵数,如:2+3*5-6 功效是11

2)被操纵数为整数,整数可以有多位

3)措置惩罚惩罚空格

4)输入错误显示错误提示,并返回命令状态“CALC”

AdditionAndSubtraction); } else { iNotMatch++; } if (regexE

图1    尝试功效示例

加强操练:

1、有能力的同学,可以测验考试实现赋值语句,例如x=2+3*5-6,返回x=11。(注意:要实现解释器的成果,而不是只是显示)

2、测验考试实现自增和自减标记,例如x++ 

2、给与管道-过滤器(Pipes and Filters)气势派头实现解释器

AdditionAndSubtraction); } else { iNotMatch++; } if (regexE

图2  管道-过滤器气势派头

AdditionAndSubtraction); } else { iNotMatch++; } if (regexE

图 3  编译器模型示意图

本尝试,实现的是词法分析和语法分析两个部分。

四、尝试法式:


class Arithmetic
{

Regex regexMultiplicationAndDivision = new Regex(@"(([-+]?\d+(\.(\d+))?)((\*|\/)([-+]?\d+(\.(\d+))?))+)");
Regex regexAdditionAndSubtraction = new Regex(@"\((([-+]?\d+(\.(\d+))?)((\+|\-)([-+]?\d+(\.(\d+))?))+)\)");
Regex regexEliminate = new Regex(@"\([-+]?\d+(\.(\d+))?\)");
Regex regexComplete = new Regex(@"(([-+]?\d+(\.(\d+))?)((\+|\-)([-+]?\d+(\.(\d+))?))*)");
Regex regexError = new Regex(@"\)\(|\)(\d+(\.(\d+))?)|(\d+(\.(\d+))?)\(");

internal string Calculation(string expression)
{
if (regexError.IsMatch(expression))
{
throw new Exception();
}

while (true)
{
int iNotMatch = 0;

if (regexMultiplicationAndDivision.IsMatch(expression))
{
expression = regexMultiplicationAndDivision.WordStr(expression, MultiplicationAndDivision);
}
else
{
iNotMatch++;
}

if (regexAdditionAndSubtraction.IsMatch(expression))
{
expression = regexAdditionAndSubtraction.WordStr(expression, AdditionAndSubtraction);
}
else
{
iNotMatch++;
}

if (regexEliminate.IsMatch(expression))
{
expression = regexEliminate.WordStr(expression, Eliminate);
}
else
{
iNotMatch++;
}

if (regexComplete.Match(expression).Value == expression)
{
return Convert.ToDouble(regexComplete.WordStr(expression, AdditionAndSubtraction)).ToString();
}

if (iNotMatch == 3)
{
throw new Exception();
}

}

}

string MultiplicationAndDivision(Match match)
{
string text = match.Value;

bool isPositive = true;

foreach (char c in text)
{
if (c == ‘-‘)
{
isPositive = !isPositive;
}
}

text = text.WordStr("*+", "*");
text = text.WordStr("*-", "*");
text = text.WordStr("/+", "http://www.mamicode.com/");
text = text.WordStr("/-", "http://www.mamicode.com/");
text = text.WordStr("*", ",*");
text = text.WordStr("http://www.mamicode.com/", ",/");

string[] numbers = text.Split(‘,‘);

double result = Convert.ToDouble(numbers[0]) >= 0 ? Convert.ToDouble(numbers[0]) : (-Convert.ToDouble(numbers[0]));

for (int i = 1; i < numbers.Length;i++ )
{
if (numbers[i] != "")
{
switch (numbers[i][0])
{
case ‘*‘:
result *= Convert.ToDouble(numbers[i].Substring(1, numbers[i].Length - 1));
break;
case ‘/‘:
result /= Convert.ToDouble(numbers[i].Substring(1, numbers[i].Length - 1));
break;
}
}

}

if (isPositive == false)
{
result = -result;
}

return result >= 0 ? ("+" + result.ToString()) : result.ToString();
}

string AdditionAndSubtraction(Match match)
{

string text = match.Value;
text = text.WordStr("(", "");
text = text.WordStr(")", "");
text = text.WordStr("++", "+");
text = text.WordStr("+-", "-");
text = text.WordStr("-+", "-");
text = text.WordStr("--", "+");
text = text.WordStr("+", ",+");
text = text.WordStr("-", ",-");

string[] numbers = text.Split(‘,‘);
double result = 0;
foreach (string number in numbers)
{
if (number != "")
{
result += Convert.ToDouble(number);
}
}

return result >= 0 ? ("+" + result.ToString()) : result.ToString();
}

string Eliminate(Match match)
{
return match.Value.Substring(1, match.Value.Length - 2);
}

}

对应布局图:

AdditionAndSubtraction); } else { iNotMatch++; } if (regexE

五、尝试总结

对付体系布局应用的理解等。