码迷,mamicode.com
首页 > Web开发 > 详细

张钰螣个人网站开发工作流程

时间:2021-04-12 12:35:25      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:++   tac   入队   大于   网站开发   网站   else   add   operator   


这个表显示的是栈外的运算符与栈内运算符之间对比的优先级表。
我们可以将上面的表达式入队,然后进行压栈和出栈操作,来得出后序表达式。在表达式后面加一个“#”运算符,那么表达式就是(A+B)*C+E/D#,入队后就是#D/E+C*)B+A(,“#”为队尾元素,”(“为队头元素。设置一个栈,栈顶元素为”#”。
转化规则:将队列中的元素进行遍历,如果是操作符则直接出队,遇到操作符的时候要与栈顶进行比较,如果优先级大于栈顶元素则该操作符出队,如果小于栈顶元素,则栈顶元素出栈,该元素入栈,成为新的栈顶,这样直到遍历结束,最后得到的结果就是后序表达式。这里如果优先级相等则直接将栈顶元素出栈,该元素也出队,并且不加入到后序表达式中。
这里用了luoweifu的转化图进行说明:

{
if (input == "+" || input == "-" || input == "*" || input == "/"|| input == "(" || input == ")" || input == "#")
{
return true;
}
else return false;
}
public Queue<string> SplitExpress(string express)
{
express += "#";
Queue<string> q = new Queue<string>();
string tempNum=string.Empty;
char[] arryExpress = express.ToArray<char>();
int i = 0;
int j = 0;
while (j<express.Length)
{
if (isOperateors(arryExpress[j].ToString()))
{
if (i != j)
{
tempNum = express.Substring(i, j - i);
q.Enqueue(tempNum);
q.Enqueue(arryExpress[j].ToString());
i = j + 1;
}
else
{
q.Enqueue(arryExpress[j].ToString());
i++;
}
}
j++;
}
return q;
}
public List<string> InorderToPostorder(Queue<string> q)
{
List<string> posterOrder = new List<string>();
Stack<string> inOrder = new Stack<string>();
inOrder.Push("#");
int count = q.Count;
for (int i = 0; i < count;i++ )
{
string item = q.Dequeue();
if (isOperateors(item))
{
string m = inOrder.First();
int n = Priority.isPriority(Priority.dicOperators[inOrder.First()],
Priority.dicOperators[item]);
while (n == 1)
{
string temp = inOrder.Pop();
if (temp != "(" && temp != ")")
{
posterOrder.Add(temp);
}
n = Priority.isPriority(Priority.dicOperators[inOrder.First()],
Priority.dicOperators[item]);
}
if (n == 2)
{
inOrder.Pop();
}
else if (n != -1)
{
inOrder.Push(item);
}
else
{
return null;
}
}
else
{
posterOrder.Add(item);
}
}
return inOrder.Count == 0 ? posterOrder : null;
}
public bool IsResult(List<string> PostorderExpress, out decimal result)
{
if (PostorderExpress != null)
{
try
{
PostorderExpress.Add("#");
string[] tempArry = PostorderExpress.ToArray();
int length = tempArry.Length;
int i = 0;
while (tempArry[i] != "#")
{
if (isOperateors(tempArry[i]))
{
tempArry[i - 2] = Arithmetic(tempArry[i - 2], tempArry[i - 1], tempArry[i]);
for (int j = i; j < length; j++)
{
if (j + 1 < length)
tempArry[j - 1] = tempArry[j + 1];
}
length -= 2;
i -= 2;
}
i++;
}
result = decimal.Parse(tempArry[0]);
return true;
}
catch (Exception e)
{
result = 0;
return false;
}
}
else
{
result = 0;
return false;
}
}

public string Arithmetic(string x,string y,string operators)
{
decimal a = decimal.Parse(x);
decimal b = decimal.Parse(y);
decimal result = 0;
switch (operators)
{
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
result = a / b;
break;
}
return result.ToString();
}

张钰螣个人网站开发工作流程

标签:++   tac   入队   大于   网站开发   网站   else   add   operator   

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

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