Java真值表堆栈 - 仅返回最后计算的值?

时间:2015-03-22 15:54:23

标签: java stack truthtable

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

这是我在参加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());

获取正确的值。

中缀到Postfix方法

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);

}

0 个答案:

没有答案