就是我想弄个代码,求命题的真值表,但是不知道为啥,一直就是有异常,关于栈的
package 大作业;
import java.util.*;
public class LiSanMath {
public static void main(String[] args){
System.out.println("输入命题公式:");
Scanner sc=new Scanner(System.in);
String proposition=sc.nextLine();
CreateTable(proposition);
}
public static void CreateTable(String proposition){
boolean[][] table1=table(proposition);
boolean[] result=Calculate(proposition);
HashSet<String> hs=Bianyuan(proposition);
for(String s:hs){
System.out.print(s+"\t");
}
System.out.print(proposition);
System.out.print("\n");
for (int i = 0; i < (int)Math.pow(2,count(proposition)); i++) {
for (int j = 0; j < count(proposition); j++) {
if(table1[i][j]==false) System.out.print("0");
else System.out.print("1");
System.out.print("\t");
}
if(result[i]==false) System.out.print("0");
else System.out.print("1");
System.out.print("\n");
}
}
public static int count(String proposition){
int count = 0;
for (int i = 0; i < proposition.length(); i++) {
char ch=proposition.charAt(i);
if(ch<='Z'&&ch>='A')count++;
}
return count;
}
public static HashSet<String> Bianyuan(String proposition){
HashSet<String> hs=new HashSet<>();
for (int i = 0; i < proposition.length(); i++) {
char ch=proposition.charAt(i);
if(ch<='Z'&&ch>='A')hs.add(String.valueOf(ch));
}
return hs;
}
public static boolean[][] table(String proposition){
int col=count(proposition);
int row= (int) Math.pow(2,col);
boolean[][] table=new boolean[row][col];
for (int i = 0; i < row; i++) {
int[] value=new int[col];
for (int j = col-1; j>=0; j--) {
int x=(int)Math.pow(2,j);
value[col-j-1]=i/x%2;
}
for (int j = 0; j < col; j++) {
table[i][j]=value[j]==1;
}
}
return table;
}
public static boolean[] Calculate(String proposition){
//运算符的优先级
HashMap<String,Integer> ch=new HashMap<>();
ch.put("&",1);
ch.put("|",1);
ch.put("!",0);
ch.put("-",2);
ch.put("+",3);
ch.put("@",1000);
boolean[] result=new boolean[(int) Math.pow(2,count(proposition))];
boolean[][] table=table(proposition);
int index_1=0;
while(index_1<(int)Math.pow(2,(count(proposition)))){
boolean[] table_1=new boolean[count(proposition)];
HashMap<String,Boolean> hm=new HashMap<>();
HashSet<String> bianyuan=Bianyuan(proposition);
for (int i = 0; i < count(proposition); i++) {
table_1[i]=table[index_1][i];
}
int x=0;
for(String s:bianyuan){
hm.put(s,table_1[x]);
x++;
}
Stack<Boolean> stack_1=new Stack<>();
Stack<String> stack_2=new Stack<>();
stack_2.push("@");
stack_1.push(false);
int index_2=0;
while (index_2<proposition.length()){
char cha=proposition.charAt(index_2);
if(cha>='A'&&cha<='Z'){
stack_1.push(hm.get(cha));
}
else {
if(stack_2.empty()==true)stack_2.push(String.valueOf(cha));
else {
if(cha=='('){
stack_2.push(String.valueOf(cha));
}
else if(cha!=')'&&cha!='('){
String s1=stack_2.peek();
String s2= String.valueOf(cha);
if(ch.get(s1)>ch.get(s2)){
stack_2.push(s2);
}
else {
boolean b1=stack_1.pop();
boolean b2=stack_1.pop();
boolean b3=Calcuate_1(s2,b1,b2);
if(s1.equals("!")){
stack_1.push(b1);
stack_1.push(b3);
}
else stack_1.push(b3);
}
}
else if (cha==')') {
while (stack_2.peek()!="("){
boolean b1=stack_1.pop();
boolean b2=stack_1.pop();
String s1=stack_2.pop();
boolean b3=Calcuate_1(s1,b1,b2);
if(s1.equals("!")){
stack_1.push(b1);
stack_1.push(b3);
}
else stack_1.push(b3);
if (stack_2.peek()=="(")stack_2.pop();
}
}
}
}
index_2++;
}
while (stack_2.peek()!="@"){
boolean b1=stack_1.pop();
boolean b2=stack_1.pop();
String s1=stack_2.pop();
boolean b3=Calcuate_1(s1,b1,b2);
if(s1.equals("!")==true){
stack_1.push(b1);
stack_1.push(b3);
}
else stack_1.push(b3);
}
result[index_1]=stack_1.pop();
index_1++;
}
return result;
}
public static boolean Calcuate_1(String fuhao,boolean b1,boolean b2){
boolean result = false;
switch (fuhao){
case "&":result=b1&&b2;break;
case "|":result=b1||b2;break;
case "-":{
if(b1==true&&b2==false) result=false;
else result=true;
}break;
case "+":{
if (b1==b2)result=true;
else result=false;
}break;
case "!":result=!b2;
}
return result;
}
}
ch里没有左括号和右括号,109行 if(ch.get(s1)>ch.get(s2)) 当s1为"("时,ch.get(s1) 为null ,null不能用> 比较大小
class Bean{
private static final Integer INT_VALUE = 100;
}