c#正则实现简单四则运算
2021-05-03 12:26
标签:pip new reac 能力 return complete false 操作 过滤器 一、实验目的 1.熟悉体系结构的风格的概念 2.理解和应用管道过滤器型的风格。 3、理解解释器的原理 4、理解编译器模型 二、实验环境 硬件: 软件:Python或任何一种自己喜欢的语言 三、实验内容 1、实现“四则运算”的简易翻译器。 结果要求: 1)实现加减乘除四则运算,允许同时又多个操作数,如:2+3*5-6 结果是11 2)被操作数为整数,整数可以有多位 3)处理空格 4)输入错误显示错误提示,并返回命令状态“CALC” 图1 实验结果示例 加强练习: 1、有能力的同学,可以尝试实现赋值语句,例如x=2+3*5-6,返回x=11。(注意:要实现解释器的功能,而不是只是显示) 2、尝试实现自增和自减符号,例如x++ 2、采用管道-过滤器(Pipes and Filters)风格实现解释器 图2 管道-过滤器风格 图 3 编译器模型示意图 本实验,实现的是词法分析和语法分析两个部分。 四、实验步骤: Regex regexMultiplicationAndDivision = new Regex(@"(([-+]?\d+(\.(\d+))?)((\*|\/)([-+]?\d+(\.(\d+))?))+)"); internal string Calculation(string expression) while (true) if (regexMultiplicationAndDivision.IsMatch(expression)) if (regexAdditionAndSubtraction.IsMatch(expression)) if (regexEliminate.IsMatch(expression)) if (regexComplete.Match(expression).Value == expression) if (iNotMatch == 3) } } string MultiplicationAndDivision(Match match) bool isPositive = true; foreach (char c in text) text = text.Replace("*+", "*"); string[] numbers = text.Split(‘,‘); double result = Convert.ToDouble(numbers[0]) >= 0 ? Convert.ToDouble(numbers[0]) : (-Convert.ToDouble(numbers[0])); for (int i = 1; i { } if (isPositive == false) return result >= 0 ? ("+" + result.ToString()) : result.ToString(); string AdditionAndSubtraction(Match match) string text = match.Value; string[] numbers = text.Split(‘,‘); return result >= 0 ? ("+" + result.ToString()) : result.ToString(); string Eliminate(Match match) } 对应结构图: 五、实验总结 对于体系结构应用的理解等。 c#正则实现简单四则运算 标签:pip new reac 能力 return complete false 操作 过滤器 原文地址:http://www.cnblogs.com/zscBlog/p/7746823.html
class Arithmetic
{
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+))?)\(");
{
if (regexError.IsMatch(expression))
{
throw new Exception();
}
{
int iNotMatch = 0;
{
expression = regexMultiplicationAndDivision.Replace(expression, MultiplicationAndDivision);
}
else
{
iNotMatch++;
}
{
expression = regexAdditionAndSubtraction.Replace(expression, AdditionAndSubtraction);
}
else
{
iNotMatch++;
}
{
expression = regexEliminate.Replace(expression, Eliminate);
}
else
{
iNotMatch++;
}
{
return Convert.ToDouble(regexComplete.Replace(expression, AdditionAndSubtraction)).ToString();
}
{
throw new Exception();
}
{
string text = match.Value;
{
if (c == ‘-‘)
{
isPositive = !isPositive;
}
}
text = text.Replace("*-", "*");
text = text.Replace("/+", "/");
text = text.Replace("/-", "/");
text = text.Replace("*", ",*");
text = text.Replace("/", ",/");
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;
}
}
{
result = -result;
}
}
{
text = text.Replace("(", "");
text = text.Replace(")", "");
text = text.Replace("++", "+");
text = text.Replace("+-", "-");
text = text.Replace("-+", "-");
text = text.Replace("--", "+");
text = text.Replace("+", ",+");
text = text.Replace("-", ",-");
double result = 0;
foreach (string number in numbers)
{
if (number != "")
{
result += Convert.ToDouble(number);
}
}
}
{
return match.Value.Substring(1, match.Value.Length - 2);
}