c#正則實現簡單四則運算
一、實驗目的
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#正則實現簡單四則運算