高手帮忙看看我的程序 调试下
(可能还有逻辑错误 ╭(╯^╰)╮
希望把改过的地方用不同颜色标注下 急!!!
先谢啦~~
Calculator.java
import javax.swing.JOptionPane;
import java.lang.Math;
//import Stack.java
public class Calculator {
//public Stack optr = new Stack();
//public Stack opnd = new Stack();
protected boolean IsOptr(char ch){
if(ch=='='||ch=='('||ch==')'||ch=='~'||ch=='→'||ch=='∧'||ch=='∨')return true;
else return false;
}
protected int IntOperPrior(char op){
if (op=='→') return 0;
if(op=='∨') return 1;
if(op=='∧') return 2;
if(op=='~') return 3;
else {
System.out.println("Errors:unexpected operator");
return 10;}
}
protected int OutOperPrior(char op){
if (op=='→') return 4;
if(op=='∨') return 5;
if(op=='∧') return 6;
if(op=='~') return 7;
else {
System.out.println("Errors:unexpected operator");
return 10;}
}
protected int Operate(int d1,char op,int d2){
if(op=='~'){
if(d1==1)return 0;
else return 1;
}
else if(op=='∧')return d1*d2;
else if(op=='∨')
{
if(d1==1||d2==1)return 1;
else return 0;
}
else if (op=='→')
{
if(d1==0)return 0;
else return d1*d2;
}
else {
System.out.print("Unexperient operator!");
return 3;
}
public void Run(){
stack optr,opnd;
int iCount=0,flag=0,operand=0,x=0,right1=0,left1=0;
char priorChar='=',right='0',left='0',op;
char[] data=new char[81];
char[] data2=new char[81];
char[] data3=new char[81];
String str = JOptionPane.showInputDialog(null, "Enter a logical expression ended with '='", "Calculater", JOptionPane.QUESTION_MESSAGE);
int length=str.length();
data = str.toCharArray();
System.out.println("The corresponding truth table is:");
for(int i=0;i<length;i++){
if(data[i]>='A'&&data[i]<='Z'||data[i]>='a'&&data[i]<='z'){
if(i==0){
iCount=1;
data2[0]=data[0];
}
else{
for(int j=i-1;j==0;j--){
if(data[i]!=data[j]){
data2[iCount-1]=data[i];
iCount++;
System.out.print(data2[iCount-1]+" ");
}
}
}
}
}
System.out.println("result");
for(int k=0;k<Math.pow(2,iCount);k++){
for(int m=0;m<iCount;m++){
data3[m]=data2[m];
data3[m]=(char)(k%2);
k=k/2;
System.out.print(data3[m]+" ");}
System.out.println(operand);
while(optr.Top()!='='||data[x]!='=')
{
char ch=data[x];
if(!IsOptr(ch))
{
opnd.Push(ch);
priorChar='0';
}
else{
if(ch=='('){
optr.Push(ch);
flag=1;
priorChar='(';
}
else if(ch==')'){
optr.Push(ch);
flag=0;
priorChar=')';
}
else if((priorChar=='='||priorChar=='('||priorChar==')')&&ch=='~'){
opnd.Push('0');
priorChar='0';
}
else if(optr.Top()==')'&&ch=='('||optr.Top()=='('&&ch=='='||optr.Top()=='='&&ch==')'||
(optr.Top()=='('||ch==')')&&(ch=='∧'||ch=='∨'||ch=='→'))
System.out.println("Errors:unexpected operator");
else if(optr.Top()=='('&&ch==')') {
ch=data[x+1];
}
if(flag==0){
if(OutOperPrior(optr.Top())<OutOperPrior(ch)){
optr.Push(ch);
ch=data[x+1];
}
else{
optr.Pop(op);
opnd.Pop((char)right);
opnd.Pop(left);
for(int y=0;y<iCount;y++){
if(right==data2[y])right1=data3[y];
else if(left==data2[y])left1=data3[y];
}
opnd.Push((char)(Operate(left1,op,right1)));
}
}
}
if(flag==1){
if(IntOperPrior(optr.Top())<IntOperPrior(ch)){
optr.Push(ch);
ch=data[x+1];
}
else{
optr.Pop(op);
opnd.Pop(right);
opnd.Pop(left);
for(int y=0;y<iCount;y++){
if(right==data2[y])right1=data3[y];
else if(left==data2[y])left1=data3[y];
}
opnd.Push((char)(Operate(left1,op,right1)));
}
}
}
}
}
}
stack.java
import javax.swing.JOptionPane;
public class stack {
private char[] stack = new char[100];
private int i = 0;
public int empty(){
if(i==0)
return 0;
else
return 1;
}
public char Top(){
if(empty()==0){
JOptionPane.showMessageDialog(null, "Can't pop the word!");
System.exit(0);
return 'w';
}
else{
return stack[i-1];
}
}
public void Push(char C){
if(i<100){
stack[i] = C;
i++;
}
else{
JOptionPane.showMessageDialog(null, "Can't push a new word!");
System.exit(0);
}
}
public void Pop(char e){
if(i>=-1){
i--;
e=stack[i];
}
else{
JOptionPane.showMessageDialog(null, "Can't pop a word!");
System.exit(0);
}
}
}
test.java
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator calculator = new Calculator();
calculator.Run();
}
}