码迷,mamicode.com
首页 > Windows程序 > 详细

c#正则实现简单四则运算

时间:2017-10-28 15:28:32      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:部分   convert   error   自增   string   pip   四则运算   visio   express   

一、实验目的

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  编译器模型示意图

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

四、实验步骤:

     


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.Replace(expression, MultiplicationAndDivision);
}
else
{
iNotMatch++;
}

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

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

if (regexComplete.Match(expression).Value == expression)
{
return Convert.ToDouble(regexComplete.Replace(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.Replace("*+", "*");
text = text.Replace("*-", "*");
text = text.Replace("/+", "/");
text = text.Replace("/-", "/");
text = text.Replace("*", ",*");
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 < 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.Replace("(", "");
text = text.Replace(")", "");
text = text.Replace("++", "+");
text = text.Replace("+-", "-");
text = text.Replace("-+", "-");
text = text.Replace("--", "+");
text = text.Replace("+", ",+");
text = text.Replace("-", ",-");

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);
}

}

对应结构图:

 技术分享

五、实验总结

对于体系结构应用的理解等。

c#正则实现简单四则运算

标签:部分   convert   error   自增   string   pip   四则运算   visio   express   

原文地址:http://www.cnblogs.com/sun-jf/p/7746869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!