📄 point24.java
字号:
class point24
{
int []number=new int [4]; //存放输入的四个数
char []four={'+','-','*','/'}; //存放四种操作符号
int [][]allnum=new int[24][4]; //存放四个数的24种排列形式
char [][]out=new char[500][11]; //存放所用找到的符合条件的表达式
int []type=new int[500]; //指明out数组中表达式是五种形式中的哪一种
public boolean []outmark=new boolean [500]; //标记out数组中的表达式是否输出
char [][]tempout=new char[500][11]; //存放所得表达式的后序形式
int index=0;
public int howlong=0; //out数组的游标
char []houzhui=new char[11];
boolean []already=new boolean[24];
public void update(int a[])
{
number[0]=a[0];
number[1]=a[1];
number[2]=a[2];
number[3]=a[3];
} //存放一个后缀表达式
public point24()
{
number[0]=1;
number[1]=1;
number[2]=1;
number[3]=1;
} //构造函数
public void quanpailie()
{
allnum[0]=new int[]{number[0],number[1],number[2],number[3]};
allnum[1]=new int[]{number[0],number[1],number[3],number[2]};
allnum[2]=new int[]{number[0],number[2],number[1],number[3]};
allnum[3]=new int[]{number[0],number[2],number[3],number[1]};
allnum[4]=new int[]{number[0],number[3],number[2],number[1]};
allnum[5]=new int[]{number[0],number[3],number[1],number[2]};
allnum[6]=new int[]{number[1],number[0],number[2],number[3]};
allnum[7]=new int[]{number[1],number[0],number[3],number[2]};
allnum[8]=new int[]{number[1],number[2],number[0],number[3]};
allnum[9]=new int[]{number[1],number[2],number[3],number[0]};
allnum[10]=new int[]{number[1],number[3],number[2],number[0]};
allnum[11]=new int[]{number[1],number[3],number[0],number[2]};
allnum[12]=new int[]{number[2],number[0],number[1],number[3]};
allnum[13]=new int[]{number[2],number[0],number[3],number[1]};
allnum[14]=new int[]{number[2],number[1],number[0],number[3]};
allnum[15]=new int[]{number[2],number[1],number[3],number[0]};
allnum[16]=new int[]{number[2],number[3],number[0],number[1]};
allnum[17]=new int[]{number[2],number[3],number[1],number[0]};
allnum[18]=new int[]{number[3],number[0],number[2],number[1]};
allnum[19]=new int[]{number[3],number[0],number[1],number[2]};
allnum[20]=new int[]{number[3],number[1],number[2],number[0]};
allnum[21]=new int[]{number[3],number[1],number[0],number[2]};
allnum[22]=new int[]{number[3],number[2],number[0],number[1]};
allnum[23]=new int[]{number[3],number[2],number[1],number[0]};
for(int i=0;i<24;i++)
for(int j=0;j<i;j++)
if(allnum[i][0]==allnum[j][0] && allnum[i][1]==allnum[j][1]&&
allnum[i][2]==allnum[j][2]&&allnum[i][3]==allnum[j][3])
already[i]=true;
} //得到全排列
public int first(char a)
{
if(a=='*'||a=='/')
return 3;
else if(a=='+'||a=='-')
return 1;
else
{
System.out.println("this is wrong!");
return -1;
}
} //设定操作符的优先级
public void makesmall(int lenth)
{
for(int count =0;count<lenth;count++)
{
for(int i=0;i<11;i++)
if(out[count][i]=='('||out[count][i]==')')
for(int j=i;j<10;j++)
out[count][j]=out[count][j+1];
for(int i=0;i<11;i++)
if(out[count][i]=='('||out[count][i]==')')
for(int j=i;j<10;j++)
out[count][j]=out[count][j+1];
out[count][7]='\0';out[count][8]='\0';
out[count][9]='\0';out[count][10]='\0';
}
// System.out.println(temp);
} //表达式去括号
public boolean isop(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/')
return true;
else
return false;
} //判断是否为操作符
public void insertleft(int a,int count)
{
int temp=0;
if(a==0)
{
for(int i=9;i>=0;i--)
out[count][i+1]=out[count][i];
out[count][0]='(';
}
else
{
temp=0;
while(!isop(out[count][temp])||a>1)
{
if(isop(out[count][temp]))
a--;
temp++;
}
for(int j=8;j>=temp;j--)
out[count][j+2]=out[count][j+1];
out[count][temp+1]='(';
}
} //在当前位置添加'(’
public void insertright(int b,int count)
{
int temp=0;
if(b==4)
{
temp=0;
while(out[count][temp]!='\0')
temp++;
out[count][temp]=')';
}
else
{
temp=0;
while(!isop(out[count][temp])||b>1)
{
if(isop(out[count][temp]))
b--;
temp++;
}
for(int j=9;j>=temp;j--)
out[count][j+1]=out[count][j];
out[count][temp]=')';
}
} //在当前位置添加')’
public void addthis(int index,int count)
{
switch(type[count])
{
case 1: //((a+b)+c)+d
if(index==1)
{insertleft(0,count);insertright(2,count);}
else
{insertleft(0,count);insertright(3,count);}
break;
case 2: //(a+(b+c))+d
if(index==1)
{insertleft(1,count);insertright(3,count);}
else
{insertleft(0,count);insertright(3,count);}
break;
case 3: //(a+b)+(c+d)
if(index==1)
{insertleft(0,count);insertright(2,count);}
else
{insertleft(2,count);insertright(4,count);}
break;
case 4: //a+((b+c)+d)
if(index==1)
{insertleft(1,count);insertright(3,count);}
else
{insertleft(1,count);insertright(4,count);}
break;
default: //a+(b+(c+d))
if(index==1)
{insertleft(2,count);insertright(4,count);}
else
{insertleft(1,count);insertright(4,count);}
}
} //指出括号添加的位置
public void makegood(int lenth)
{
char []tempop=new char[3];
int count=0;
while(count<lenth)
{
int tempopindex=0;
int tempoutindex=0;
while(tempoutindex<11)
{
if(isop(tempout[count][tempoutindex]))
{
tempop[tempopindex]=tempout[count][tempoutindex];
tempopindex++;
}
tempoutindex++;
}
tempopindex=0;
switch(type[count])
{
case 1: //((a+b)+c)+d
if(first(tempop[0])<first(tempop[1]))
addthis(1,count);
if(first(tempop[1])<first(tempop[2]))
{
addthis(2,count);
}
break;
case 2: //(a+(b+c))+d
if(first(tempop[0])<first(tempop[1])||first(tempop[0])==first(tempop[1])&&(tempop[1]=='-'||tempop[1]=='/'))
addthis(1,count);
if(first(tempop[1])<first(tempop[2]))
addthis(2,count);
break;
case 3: //(a+b)+(c+d)
if(first(tempop[0])<first(tempop[2]))
addthis(1,count);
if(first(tempop[1])<first(tempop[2])||first(tempop[1])==first(tempop[2])&&(tempop[2]=='-'||tempop[2]=='/'))
addthis(2,count);
break;
case 4: //a+((b+c)+d)
if(first(tempop[0])<first(tempop[1]))
addthis(1,count);
if(first(tempop[1])<first(tempop[2])||first(tempop[1])==first(tempop[2])&&(tempop[2]=='-'||tempop[2]=='/'))
addthis(2,count);
break;
case 5: //a+(b+(c+d))
if(first(tempop[0])<first(tempop[1])||first(tempop[0])==first(tempop[1])&&(tempop[1]=='-'||tempop[1]=='/'))
addthis(1,count);
if(first(tempop[1])<first(tempop[2])||first(tempop[1])==first(tempop[2])&&(tempop[2]=='-'||tempop[2]=='/'))
addthis(2,count);
break;
}
count++;
}
} //为没有括号的表达式添加括号,使表达式正确
public int length(char [] c)
{
int result=0;
for(int i=0;i<11&&c[i]!='\0';i++)
result++;
return result;
} //获得数组长度
public boolean partsame(char []one,char[] two)
{
int len=length(one);
if(len!=length(two))
return false;
if(len==1)
if(one[0]==two[0])
return true;
else return false;
else if(len==3)
{
if(one[2]!=two[2])
return false;
if(one[2]=='+'||one[2]=='*')
{
if(one[0]==two[0]&&one[1]==two[1]||one[0]==two[1]&&one[1]==two[0])
return true;
else return false;
}
else if(one[2]=='-'||one[2]=='/')
if(one[0]==two[0]&&one[1]==two[1])
return true;
else return false;
else
{
System.out.println("A op should be here!3333333333"+one[0]+one[1]+one[2]);
return false;
}
}
else if(len==5)
{
if(one[4]!=two[4])
return false;
if(one[4]=='+'||one[4]=='*')
{
if(isop(one[3])&&isop(two[3]))
{
if(one[0]!=two[0])
return false;
else
{
char[] list1 = {one[1],one[2],one[3],'\0'};
char[] list2 = {two[1],two[2],two[3],'\0'};
return(partsame(list1,list2));
}
}
else if(!isop(one[3])&&!isop(two[3]))
{
if(one[3]!=two[3])
return false;
else
{
char[] list1 = {one[0],one[1],one[2],'\0'};
char[] list2 = {two[0],two[1],two[2],'\0'};
return(partsame(list1,list2));
}
}
else if(!isop(one[3])&&isop(two[3]))
{
if(one[3]!=two[0])
return false;
else
{
char[] list1 = {one[0],one[1],one[2],'\0'};
char[] list2 = {two[1],two[2],two[3],'\0'};
return(partsame(list1,list2));
}
}
else
{
if(one[0]!=two[3])
return false;
else
{
char[] list1 = {one[1],one[2],one[3],'\0'};
char[] list2 = {two[0],two[1],two[2],'\0'};
return(partsame(list1,list2));
}
}
}
else if(one[4]=='-'||one[4]=='/')
{
if(isop(one[3])&&isop(two[3]))
{
if(one[0]!=two[0])
return false;
else
{
char[] list1 = {one[1],one[2],one[3],'\0'};
char[] list2 = {two[1],two[2],two[3],'\0'};
return(partsame(list1,list2));
}
}
else if(!isop(one[3])&&!isop(two[3]))
{
if(one[3]!=two[3])
return false;
else
{
char[] list1 = {one[0],one[1],one[2],'\0'};
char[] list2 = {two[0],two[1],two[2],'\0'};
return(partsame(list1,list2));
}
}
else return false;
}
else
{
System.out.println("A op should be here!5555555555");
return false;
}
}
else
{
System.out.println("this is not correct len!");
return false;
}
}//比较两个子串是否相等
public boolean isone(int count)
{
char first,second;
int i=0;
while(!isop(tempout[count][i]))
i++;
first=tempout[count][i++];
while(!isop(tempout[count][i]))
i++;
second=tempout[count][i];
if(first==second&&first==tempout[count][6]&&(first=='+'||first=='*'))
return true;
else
return false;
}
public int istwo(int count)
{
char first,second;
int i=0;
while(!isop(tempout[count][i]))
i++;
first=tempout[count][i++];
while(!isop(tempout[count][i]))
i++;
second=tempout[count][i];
if((first==second||second==tempout[count][6])&&type[count]!=3&&(second=='+'||second=='*'))
{
if(first==second)
return 3;
else
return 2;
}
if((first==tempout[count][6]||second==tempout[count][6])&&type[count]==3&&(tempout[count][6]=='+'||tempout[count][6]=='*'))
{
return 2;
}
return 0;
}
public boolean issame(int a,int b)
{
boolean result=true;
char []one=new char[6];
char []two=new char[6];
int lenone=length(out[a]);
int lentwo=length(out[b]);
if(lenone!=lentwo)
return false;
for(int i=0;i<11;i++)
{
if(out[a][i]!=out[b][i])
{result = false;break;}
}
if(result)
return true;
if(isone(a)&&isone(b))
{
return true;
}
if(istwo(a)!=0&&istwo(b)!=0&&tempout[a][6]==tempout[b][6])
{
if(istwo(a)!=istwo(b))
return false;
if(istwo(a)==3)
{
char ma,mb;
if(isop(tempout[a][5]))
ma=tempout[a][0];
else ma=tempout[a][5];
if(isop(tempout[b][5]))
mb=tempout[b][0];
else mb=tempout[b][5];
if(ma==mb)
return true;
else return false;
}
else
{
char a1,a2,b1,b2;
if(type[a]!=3)
{
if(isop(tempout[a][5]))
{
a1=tempout[a][0];
if(isop(tempout[a][4]))
a2=tempout[a][1];
else a2=tempout[a][4];
}
else
{
a1=tempout[a][5];
if(isop(tempout[a][3]))
a2=tempout[a][0];
else a2=tempout[a][3];
}
}
else
{
if(tempout[a][2]==tempout[a][6])
{
a1=tempout[a][0];
a2=tempout[a][1];
}
else
{
a1=tempout[a][3];
a2=tempout[a][4];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -