java实现真值表代码(不完善)恳求大佬指导

今天离散数学老师让我们尝试用编程来实现真值表,自己做了一个但是限制有点多,希望有大佬能指点一下。

Test类

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("规则: 1,每个步骤必须有括号 2,输入的字母必须从a开始(例a,b,c,d)");
        System.out.println("输入命题公式吧");
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();  //获取字符串
        char[] arr = new char[20];
        int[] arr2 = new int[30];
        int count = 0; //计数器
        int j = 0;
        for (int i = 0; i < str.length(); i++) {
            char a = str.charAt(i);
            if (Character.isLetter(a)){
                arr[j] = a;
                j++;
            }
        }
        for (int i = 0; i < j; i++) {
            char w = arr[i];
            arr2[w-'a']+=1;
        }
        for (int i = 0; i < arr2.length; i++) {
            if(arr2[i]!=0){
                count++;
                arr2[i]=0;
            }
        }
        System.out.println(count);
        boolean[] Boolean = new boolean[count];
        TruthTable truthTable = new TruthTable();
        truthTable.setArr(str);
        truthTable.create(Boolean,0);

    }


}

TruthTable

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;

public class TruthTable {
    public String getArr() {
        return str;
    }

    public void setArr(String arr) {
        this.str = arr;
    }
    private String str;

    public void create(boolean[] Boolean,int index){
        if(Boolean.length==index){
            display(Boolean);
            Judge(Boolean,str);//这里--------
            System.out.println();
            //未打印
            return;
        }

        Boolean[index] = false;
        create(Boolean,index+1);

        Boolean[index] = true;
        create(Boolean,index+1);
    }

    public void display(boolean[] Boolean){
        for (int i = 0; i < Boolean.length; i++) {
            if(Boolean[i] == true){
                System.out.print(1 + " ");
            }
            else {
                System.out.print(0 + " ");
            }
        }
    }

    Stack<Character> stack = new Stack<>();//括号
    Stack<Boolean> stack2 = new Stack<>();//010101
    //∨ ∧ ↔ → ¬
    //char az =
    public void Judge(boolean[] Boolean,String str){
        //((p→((¬p→q)∧r))∨q)
        stack.clear();
        stack2.clear();
        boolean w=false;
        boolean a=false;
        boolean b=false;
        int j = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if(c=='('){
                stack.push(c);
            }
            if(Character.isLetter(c)){
                if(c=='a'){
                    stack2.push(Boolean[0]);
                }
                else if(c=='b'){
                    stack2.push(Boolean[1]);
                } else if (c=='c') {
                    stack2.push(Boolean[2]);
                } else if (c=='d') {
                    stack2.push(Boolean[3]);
                }else if (c=='e') {
                    stack2.push(Boolean[4]);
                } else if (c=='f') {
                    stack2.push(Boolean[5]);
                }
                else if (c=='g') {
                    stack2.push(Boolean[6]);
                }
                else if (c=='h') {
                    stack2.push(Boolean[7]);
                }
                else if (c=='i') {
                    stack2.push(Boolean[8]);
                } else if (c=='j') {
                    stack2.push(Boolean[9]);
                }
                //_________________________________________________这块
            }
            if(c=='∨' || c=='∧' || c=='↔' || c=='→' || c=='¬'){
                stack.push(c);
            }
            if(c==')'){
                while (stack.peek()!='('){
                    if (!stack2.isEmpty()){
                        a = stack2.pop();//先出的在右边
                    }
                    if (!stack2.isEmpty()){
                        b = stack2.pop();
                    }
                    if(stack.peek()=='∧'){
                        if(a==true && b==true){
                            System.out.print("    " + 1);
                            w = true;
                        } else{
                            System.out.print("    " + 0);
                            w = false;
                        }
                        stack2.push(w);
                        stack.pop();
                    }
                    if (stack.peek()=='∨'){
                        if(a==true || b==true){
                            System.out.print("    " + 1);
                            w = true;
                        } else{
                            System.out.print("    " + 0);
                            w = false;
                        }
                        stack2.push(w);
                        stack.pop();
                    }
                    if (stack.peek()=='↔'){
                        if(a==b){
                            System.out.print("    " + 1);
                            w = true;
                        } else{
                            System.out.print("    " + 0);
                            w= false;
                        }
                        stack2.push(w);
                        stack.pop();
                    }
                    if(stack.peek()=='→'){
                        if(b==false){
                            System.out.print("    " + 1);
                            w = true;
                        }
                        else if(b==true && a==true){
                            System.out.print("    " + 1);
                            w = true;
                        }
                        else if(b==true && a==false){
                            System.out.print("    " + 0);
                            w = false;
                        }
                        //System.out.print(b + " " + a);
                        stack2.push(w);
                        stack.pop();
                    }
                    if(stack.peek()=='¬'){
                        stack2.push(b);
                        boolean t = !a;
                        System.out.print("    " + (t ? 1 : 0));
                        stack2.push(t);
                        stack.pop();
                    }
                }
                stack.pop();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值