码迷,mamicode.com
首页 > 其他好文 > 详细

计应191西 康文龙 3组

时间:2021-06-02 19:27:51      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:value   spl   ssi   gen   字典   运算   stringbu   ESS   add   

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Class1
{
private static Dictionary<string, int> _operatorLevel;

public static void Main(string[] arg)
{
Console.WriteLine("Type in the source expr");
string sourceExpression = Console.ReadLine();
Console.WriteLine(InsertBlank(sourceExpression));
string rpnExperssion = ConvertToRPN(InsertBlank(sourceExpression));
Console.WriteLine(rpnExperssion);
Console.WriteLine(GetResult(rpnExperssion));
Console.ReadLine();
}

public static double GetValue(double left, double right, char _operator)
{
switch (_operator)
{
case ‘+‘:
return left+right;
case ‘-‘:
return left-right;
case ‘*‘:
return left*right;
case ‘/‘:
return left/right;
}
return 0;
}

public static double GetResult(string source)
{
Stack<string> stack = new Stack<string>();
var list = source.Split(‘ ‘);
for (int i = 0; i < list.Length; i++)
{
string current = list[i];
if (Regex.IsMatch(current, "^([0-9]{1,}){1}quot;))
{
stack.Push(current);
}
else if (OperatorLevel.ContainsKey(current))
{
double right = double.Parse(stack.Pop());
double left = double.Parse(stack.Pop());
stack.Push(GetValue(left, right, current[0]).ToString());
}
}
return double.Parse(stack.Pop());
}
public static string ConvertToRPN(string source)
{
StringBuilder result = new StringBuilder();
Stack<string> stack = new Stack<string>();
string[] list = source.Split(‘ ‘);
for (int i = 0; i < list.Length ; i++)
{
string current = list[i];
if (Regex.IsMatch(current, "^([0-9]{1,}){1}quot;))
{
result.Append(current + " ");
}
else if (OperatorLevel.ContainsKey(current))
{
if (stack.Count > 0)
{
var prev = stack.Peek();
if (prev == "(")
{
stack.Push(current);
continue;
}
if (current == "(")
{
stack.Push(current);
continue;
}
if (current == ")")
{
while (stack.Count > 0 && stack.Peek() != "(")
{
result.Append(stack.Pop() + " ");
}
//Pop the "("
stack.Pop();
continue;
}
if (OperatorLevel[current] < OperatorLevel[prev])
{
while (stack.Count > 0)
{
var top = stack.Pop();
if (top != "(" &&
top != ")")
{
result.Append(top + " ");
}
else
{
break;
}
}
stack.Push(current);
}
else
{
stack.Push(current);
}
}
else
{
stack.Push(current);
}
}
}
if (stack.Count > 0)
{
while (stack.Count > 0)
{
var top = stack.Pop();
if (top != "(" && top != ")")
{
result.Append(top + " ");
}
}
}
return result.ToString();
}

public static string InsertBlank(string source)
{
StringBuilder sb = new StringBuilder();
var list = source.ToCharArray();
foreach (var temp in list)
{
if (OperatorLevel.ContainsKey(temp.ToString()))
{
sb.Append(" ");
sb.Append(temp.ToString());
sb.Append(" ");
}
else
{
sb.Append(temp);
}
}
return sb.ToString();
}

//运算符字典 方便查询运算符优先级
public static Dictionary<string, int> OperatorLevel
{
get
{
if(_operatorLevel==null)
{
_operatorLevel = new Dictionary<string, int>();
_operatorLevel.Add("+", 0);
_operatorLevel.Add("-", 0);
_operatorLevel.Add("(", 1);
_operatorLevel.Add("*", 1);
_operatorLevel.Add("/", 1);
_operatorLevel.Add(")", 0);
}
return _operatorLevel;
}
}
}
}

计应191西 康文龙 3组

标签:value   spl   ssi   gen   字典   运算   stringbu   ESS   add   

原文地址:https://www.cnblogs.com/k1913z/p/14833821.html

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