離散數學實踐作業,java輸出真值表(轉)

下輩子變形蟲發表於2020-12-06

https://blog.csdn.net/diaotai/article/details/52565646?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.not_use_machine_learn_pai&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.not_use_machine_learn_pai
跟著老哥思路的。。。邊抄邊寫,感覺學到很多。。
下面展示一些 內聯程式碼片

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class math {
    //字串形式輸入公式
    StringBuilder inputStr;
    String inputStr2;
    //用於統計最初輸入的表示式有幾個括號
    int count = 0;
    //存放真值表
    int[][] table = new int[16][5];
    //用於存放P,Q,R,S
    String[] prqs = new String[]{"P", "Q", "R", "S"};
    //flag代表表示式的結果
    int flag = 0;
    //用於控制遞迴次數
    int COUNT;
    //用於表示是否含有括號
    boolean ifHas = false;

    public void input(StringBuilder string) {
        inputStr = string;
        inputStr2 = string.toString();
    }

    public void restart() {
        inputStr = new StringBuilder(inputStr2);
    }

    //處理非
    public String math1(int x) {
        if (x == 0) {
            return "1";
        } else return "0";
    }

    //處理析取V(或)
    public int hequ (int x, int y) {
        return (x & y);
    }
    //用於處理析取運算子
    public int xiqut(int x,int y)
    {
        return (x|y);
    }
    //運算不同情況下的表達值,P——X,Q-Y,R-Z,S-G
    public void Calculate(int x, int y, int z, int g) {
        if (count == 0 && ifHas == false) {
            for (int i = 0; i < inputStr.length(); i++) {
                if (inputStr.charAt(i) == ')') {
                    count++;
                    ifHas = true;
                }
                COUNT = count;
            }
        }

        if (COUNT != 0) {
            COUNT--;
            for (int i = 0; i < inputStr.length(); i++) {
                if (inputStr.charAt(i) == ')') {
                    for (int j = i; ; j--) {

                        if (inputStr.charAt(j) == '(') {
                            int result;
                            String[] strs = new String[3];
                            strs[0] = inputStr.substring(0, j);
                            strs[1] = inputStr.substring(j + 1, i);
                            strs[2] = inputStr.substring(i + 1);

                            StringBuilder s = new StringBuilder(strs[1]);
                            result = computeSim(s, x, y, z, g);
                            String string = strs[0] + result + strs[2];
                            inputStr = new StringBuilder(string);
                            //System.out.println("LAST\t"+inputStr);
                            break;
                        }
                    }
                    break;
                }
            }
            if ((COUNT != 0)) {
                Calculate(x, y, z, g);
            }
            if (COUNT == 0) {
                flag = computeSim(inputStr, x, y, z, g);
            }
        }
    }




    //計算括號內的值
   public int computeSim(StringBuilder sb, int x, int y, int z, int g)
    {
        int been = 0;
        for (int i = 0; i < sb.length(); i++)
        {
            if (sb.charAt(i) == '!') {
                if (sb.charAt(i + 1) == 'P')
                    sb.replace(i, i + 2, math1(x));
                else if (sb.charAt(i + 1) == 'Q')
                    sb.replace(i, i + 2, math1(y));
                else if (sb.charAt(i + 1) == 'R')
                    sb.replace(i, i + 2, math1(z));
                else if (sb.charAt(i + 1) == 'S')
                    sb.replace(i, i + 2, math1(g));
                else if (sb.charAt(i + 1) == '1')
                    sb.replace(i, i + 2, "0");
                else if (sb.charAt(i + 1) == '0')
                    sb.replace(i, i + 2, "1");
            }
        }
        for (int i = 0; i < sb.length(); i++)
        {
            //以下程式碼用於初始化been
            if (i == 0) {
                if (sb.charAt(i) == 'P')
                    been = x;
                else if (sb.charAt(i) == 'Q')
                    been = y;
                else if (sb.charAt(i) == 'R')
                    been = z;
                else if (sb.charAt(i) == '1')
                    been = 1;
                else if (sb.charAt(i) == '0')
                    been = 0;
            }
            switch (sb.charAt(i))
            {
                case '!':
                    break;
                case '|':
                    if (sb.charAt(i + 1) == 'P')
                        been = xiqut(been, x);
                    if (sb.charAt(i + 1) == 'Q')
                        been = xiqut(been, y);
                    if (sb.charAt(i + 1) == 'R')
                        been = xiqut(been, z);
                    if (sb.charAt(i + 1) == 'S')
                        been = xiqut(been, g);
                    if (sb.charAt(i + 1) == '1')
                        been = xiqut(been, 1);
                    if (sb.charAt(i + 1) == '0')
                        been = xiqut(been, 0);
                    break;
                case '&':
                    if (sb.charAt(i + 1) == 'P')
                        been = hequ(been, x);
                    if (sb.charAt(i + 1) == 'Q')
                        been = hequ(been, y);
                    if (sb.charAt(i + 1) == 'R')
                        been = hequ(been, z);
                    if (sb.charAt(i + 1) == 'S')
                        been = hequ(been, g);
                    if (sb.charAt(i + 1) == '0')
                        been = hequ(been, 0);
                    if (sb.charAt(i + 1) == '1')
                        been = hequ(been, 1);
                    break;
                default:
                    break;
            }
        }
        return been;
    }
    public void init() {
        int n = 0;
        for (int i = 1; i >= 0; i--) {
            for (int j = 1; j >= 0; j--) {
                for (int k = 1; k >= 0; k--) {
                    for (int g = 1; g >= 0; g--) {
                        Calculate(i, j, k, g);
                        table[n][0] = i;
                        table[n][1] = j;
                        table[n][2] = k;
                        table[n][3] = flag;
                        n++;
                        restart();
                        COUNT = count;
                        //	System.out.println(inputStr);
                    }
                }
            }
        }
    }
    //用於把1和0轉換為T和F
    public String replace(int x)
    {
        if(x==1)
            return "T";
        return "F";
    }
    public void print()
    {
        System.out.println("P\tQ\tR\tS\t"+inputStr);
        for(int i=0;i<16;i++)
        {
            for(int j=0;j<5;j++)
                System.out.print(replace(table[i][j])+"\t");
            System.out.println();
        }
    }
    public static void main(String []args)throws Exception
    {
        math l=new math();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請輸入表示式,三個命題分別為P,Q,R,S非號用!代替,析取號用|代替,合取號用&代替");
        System.out.println("輸入不當可能導致未知錯誤,請注意輸入格式");

        String s=null;
        s=br.readLine();
        StringBuilder sb=new StringBuilder(s);
        l.input(sb);
        l.init();
        l.print();
    }
}

相關文章