0

我无法正确表达我的问题,所以请耐心等待。举个例子可能会更好。

这是我参加 P&Q&R 时得到的结果

PQR:
TTT = T
FFF = F
TTF = F
FFT = T
TFF = F
FTT = T
TFT = T
FTF = F

它只是查看 R 并从那里获取值,P&Q 似乎被完全忽略了。如果我为 P&Q&~R 运行,值将被反转,但如果我运行例如 ~P&~Q&R,值将保持不变。

目前我的代码太长了,但我正在努力缩短它以避免重复代码。不过,目前我只想解决这个问题。

这是应该评估 FTF 的代码片段:

    //Evaluate PQR = false, true, false, ftf

    for(int i=0;i<x.length();i++)
    {
        char ch = x.charAt(i);
        char t = 'T';
        char f = 'F';

        if (ch=='R' || ch=='P')
        {
            S7.push(f);
            ftf+=f;  
        }
        if (ch == 'Q')
        {
            S7.push(t);
            ftf+=t;
        }

        else if(ch=='>')
        {
            if(S7.peek() == 'T' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(t);}
            else if(S7.peek() == 'T' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(t);}
            else if(S7.peek() == 'F' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(f);}
            else if (S7.peek() == 'F' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(t);}

            ftf+=ch;
        }
        else if(ch=='<')
        {
            if(S7.peek() == 'T' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(t);}
            else if(S7.peek() == 'T' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(f);}
            else if(S7.peek() == 'F' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(f);}
            else if (S7.peek() == 'F' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(t);}

            ftf+=ch;
        }
        else if(ch=='&')
        {   
            if(S7.peek() == 'T' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(t);}
            else if(S7.peek() == 'T' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(f);}
            else if(S7.peek() == 'F' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(f);}
            else if (S7.peek() == 'F' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(f);}

            ftf+=ch;
        }
        else if(ch=='v')
        {    
            if(S7.peek() == 'T' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(t);}
            else if(S7.peek() == 'T' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(t);}
            else if(S7.peek() == 'F' && S7.prevPeek() == 'T'){
                S7.pop();
                S7.pop();
                S7.push(t);}
            else if (S7.peek() == 'F' && S7.prevPeek() == 'F'){
                S7.pop();
                S7.pop();
                S7.push(f);}

            ftf+=ch;
        }
        else if(ch=='~')
        {
            if(S7.peek() == 'T'){
                S7.pop();
                S7.push(f);}
            else{
                S7.pop();
                S7.push(t);}

            ftf+=ch;
        }
    }

如果需要,我可以发布更多代码,如果不清楚,我可以解释目的。

我想做类似的事情:

System.out.println("FTF = " + S7.pop());

得到正确的值。

后缀方法的中缀

public void postFix(String x)
{

    Operations newList = new Operations();
    output = "";
    for(int i=0; i<x.length(); i++)
    {
        char ch = x.charAt(i);

        if(ch =='>' || ch=='<' || ch=='v' || ch=='&' || ch=='~' )
        {

            while(!newList.empty() && (priority(newList.peek()) >= priority(ch)))
            {
                output+=newList.peek();
                newList.pop();
            }
            newList.push(ch);
        }
        else if(ch=='(')
        {
            newList.push(ch);
        }
        else if(ch==')')
        {
            while (!newList.peek().equals('('))
                output+=newList.pop();
            newList.pop();
        }
        else

            output+=ch;

    }

    while(!newList.empty())
    {
        output+=newList.pop();
    }
    System.out.println("THE INFIX = "+x);
    System.out.println("THE POSTFIX = "+output);

}
4

0 回答 0