中缀到后缀转换器
我一直在研究这个中缀到后缀/polis 符号转换器。 尽管如此,我认为这个解决方案还不够。 特别是 j (编辑:现在称为索引)变量困扰着我。
你们有什么建议吗?或者也许有更好的方法来实现它?还是我担心太多了?
public static string[] InfixToPostfix(string[] infixArray)
{
var stack = new Stack<string>();
var postfix = new string[infixArray.Length];
int index = 0;
string st;
for (int i = 0; i < infixArray.Length; i++)
{
if (!(MyMath.MathOperators.Contains(infixArray[i])))
{
postfix[index] = infixArray[i];
index++;
}
else
{
if (infixArray[i].Equals("("))
{
stack.Push("(");
}
else if (infixArray[i].Equals(")"))
{
st = stack.Pop();
while (!(st.Equals("(")))
{
postfix[index] = st;
index++;
st = stack.Pop();
}
}
else
{
while (stack.Count > 0)
{
st = stack.Pop();
if (RegnePrioritet(st) >= RegnePrioritet(infixArray[i]))
{
postfix[index] = st;
index++;
}
else
{
stack.Push(st);
break;
}
}
stack.Push(infixArray[i]);
}
}
}
while (stack.Count > 0)
{
postfix[index] = stack.Pop();
index++;
}
return postfix.TakeWhile(item => item != null).ToArray();
}
I have been working on this infix to postfix/polis notation converter.
Although, I do not feel the solution is adequate.
Specifically the j (EDIT: Now called index) variable is bugging me.
Do you guys have any suggestions? Or perhaps there is a much better way to accomplish it? Or do I just worry too much?
public static string[] InfixToPostfix(string[] infixArray)
{
var stack = new Stack<string>();
var postfix = new string[infixArray.Length];
int index = 0;
string st;
for (int i = 0; i < infixArray.Length; i++)
{
if (!(MyMath.MathOperators.Contains(infixArray[i])))
{
postfix[index] = infixArray[i];
index++;
}
else
{
if (infixArray[i].Equals("("))
{
stack.Push("(");
}
else if (infixArray[i].Equals(")"))
{
st = stack.Pop();
while (!(st.Equals("(")))
{
postfix[index] = st;
index++;
st = stack.Pop();
}
}
else
{
while (stack.Count > 0)
{
st = stack.Pop();
if (RegnePrioritet(st) >= RegnePrioritet(infixArray[i]))
{
postfix[index] = st;
index++;
}
else
{
stack.Push(st);
break;
}
}
stack.Push(infixArray[i]);
}
}
}
while (stack.Count > 0)
{
postfix[index] = stack.Pop();
index++;
}
return postfix.TakeWhile(item => item != null).ToArray();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果将
array
替换为Stack
,则无需使用index
变量来跟踪当前位置。然后,您可以使用
postfixStack.ToArray()
返回结果我的实现:
If you replace
array
by aStack<string>
, you don't have to keep track where you are with theindex
variable.You can then return your result with
postfixStack.ToArray()
My implementation:
这是维基百科上有关中缀、后缀和后缀信息的非常基本的实现(仅数字、括号和 +-/* 运算符)。调车场算法。可以整理一下&改进了,我会为自己的工作做这件事,但在我将其定制得面目全非之前,我先在这里发布:
Here is a very basic implementation (numbers, parentheses and +-/* operators only) of the information on wikipedia about infix, postfix & the Shunting Yard algorithm. Could be neatened up & improved on, which i'll do for my own work, but I'm posting here before I customize it beyond recognition:
试试这个。我花了一段时间来编写此代码,但适用于我在互联网上找到的每个示例。
Try this. It takes me a while to code this but works for every examples that i found on internet.