我做了Infix
到Postfix
转换器,并认为它的工作原理,但当我回去并向我的老师展示他测试的例子之一,结果是错误的。 :|C#中缀后缀转换器没有给出正确的答案,如果少数情况下
如果有人能够帮助我解决这个问题,并且让我知道什么是错的,我将不胜感激。
我跳过了按钮部分的零件用于输入数字,只公布了其对重定向console.writeline
我的文本框和按钮,做所有的工作休息,
private void button20_Click(object sender, EventArgs e)
{
try
{
string infix = textBox1.Text;
infixTopostfix obj = new infixTopostfix(infix);
textBox1.Text = string.Empty;
Console.WriteLine("{0} is the Postfix of {1}", obj.createPrifex(), infix);
}
catch (Exception e1)
{
Console.WriteLine(e1.ToString());
}
}
以上部分并给出最终结果。
这里是主类:
class infixTopostfix
{
public infixTopostfix(string strTemp)
{
strInput = strTemp;
}
private int isOperand(char chrTemp)
{
char[] op = new char[6] { '*', '/', '+', '-', '^', '(' };
foreach (char chr in op)
if (chr == chrTemp)
{
return 1;
}
return 0;
}
private int isOperator(char chrTemp)
{
char[] op = new char[5] { '*', '/', '+', '-', '^' };
foreach (char chr in op)
if (chr == chrTemp)
{
return 1;
}
return 0;
}
private string strResualt = null;
private string strInput = null;
public string createPrifex()
{
int intCheck = 0;
//int intStackCount = 0;
object objStck = null;
for (int intNextToken = 0; intNextToken <= strInput.Length - 1; intNextToken++)
{
intCheck = isOperand(strInput[intNextToken]);
if (intCheck == 1)
stkOperatore.Push(strInput[intNextToken]);
else
if (strInput[intNextToken] == ')')
{
int c = stkOperatore.Count;
for (int intStackCount = 0; intStackCount <= c - 1; intStackCount++)
{
objStck = stkOperatore.Pop();
intCheck = isOperator(char.Parse(objStck.ToString()));
if (intCheck == 1)
{
strResualt += objStck.ToString();
}
}
}
else
strResualt += strInput[intNextToken];
}//end of for(int intNextToken...)
int intCount = stkOperatore.Count;
if (intCount > 0)
{
int c = stkOperatore.Count;
for (int intStackCount = 0; intStackCount <= c - 1; intStackCount++)
{
objStck = stkOperatore.Pop();
intCheck = isOperator(char.Parse(objStck.ToString()));
if (intCheck == 1)
{
strResualt += Convert.ToString(objStck);
}
}
}
return strResualt;
}
private System.Collections.Stack stkOperatore = new System.Collections.Stack();
}
}
这里是失败的输入:
甲^ B ^(CD /(E + F)) - ( G + H)^ L * Z + Y
这段代码的结果是不正确的:
ABCDEF +/- ^^ GH + -LZY + *^
正确的结果:
ABCDEF +/- ^^ GH + L^Z * -Y +
哈哈我谷歌调车码算法,看来你是对的!通过维基百科来判断,我正在尝试使用该算法将中缀转换为后缀,无论如何,我认为我的问题在于运营商的优先级 – Pedram