算24的程序

来源:互联网 发布:淘宝旺铺模板 编辑:程序博客网 时间:2024/04/30 02:33

运算类

Caculate24.java

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class Caculate24 {
    
//保存高优先级操作符*/
    static Set<Character> hOpr = new HashSet<Character>();
    
//保存低优先级操作符+-
    static Set<Character> lOpr = new HashSet<Character>();
    
//保存所有运算符
    static Set<Character> OPR = new HashSet<Character>();
    
static {
        hOpr.add(
'*');
        hOpr.add(
'/');
        lOpr.add(
'+');
        lOpr.add(
'-');
        OPR.addAll(hOpr);
        OPR.addAll(lOpr);
    }

    
//产生所给出的四个数字的全排列
    String[] generateNums(String base){
        String[] nums 
= new String[24];
        
int m = 0;
        
for(int i=0;i<base.length();i++)
            
for(int j=0;j<base.length();j++)
                
for(int k=0;k<base.length();k++)
                    
for(int l=0;l<base.length();l++){
                        
if((i!=j) && i!=&& i!=&& j!=&& j!=&& k!=l){
                            nums[m
++= new String(
                                            
new char[]{base.charAt(i)
                                                    ,base.charAt(j)
                                                    ,base.charAt(k)
                                                    ,base.charAt(l)}
);
                        }

                    }

        
return nums;
    }

    
//产生运算符的全排列
     String[] gerenteOprs(String base) {
        String[] ops 
= new String[64];
        
int l = 0;
        
for (int i = 0; i < base.length(); i++)
            
for (int j = 0; j < base.length(); j++)
                
for (int k = 0; k < base.length(); k++)
                    ops[l
++= new String(new char[] { base.charAt(i),
                            base.charAt(j), base.charAt(k) }
);
        
return ops;
    }

    
//打印产生的表达式
    void printExpression(String numStr,String oprStr) {
        Stack
<Character> nums = new Stack<Character>();
        
for(int i=0;i<numStr.length();i++){
            nums.push(numStr.charAt(i));
        }
    
        
//trace(nums);
        Set<Stack> oprSta = new HashSet<Stack>();
        initiStack(oprStr, oprSta);
        
for (Stack opr : oprSta) {
            trace(generateExpression(nums, opr));
        }

    }

    
    
/**
     *  产生表达式
     *  
     
*/

    String generateExpression(Stack
<Character> nums, Stack<Character> oprs) {
        StringBuffer exp 
= new StringBuffer();
        Stack
<Character> topr = new Stack<Character>();
        topr.addAll(oprs);
        Stack
<Character> num = new Stack<Character>();
        num.addAll(nums);
        out: 
for (;;) {
            
boolean hasNum = !num.empty();
            
boolean hasOpr = !topr.empty();
            
if (!hasOpr) {
                
if (!hasNum)
                    
break;
                
else
                    exp.append(num.pop());
            }

            
if (hasOpr && topr.peek().equals('#')) {
                topr.pop();
                
if (topr.peek().equals(')'))
                    
continue out;
                exp.append(num.pop());
            }

            
if (hasOpr && topr.peek().equals('(')) {
                exp.append(topr.pop());
                
continue out;
            }

            
if (hasOpr && OPR.contains(topr.peek())) {
                Character p 
= topr.pop();
                hasOpr 
= !topr.empty();
                
if (hasOpr && topr.peek().equals(')')) {
                    exp.append(p);
                }
 else {
                    exp.append(p);
                    
if (hasNum)
                        exp.append(num.pop());
                }

                
continue out;
            }

            
if (hasOpr && topr.peek().equals(')')) {
                exp.append(topr.pop());
                
if (hasNum)
                    exp.append(num.pop());
                
continue out;
            }


        }


        
return exp.reverse().toString();
    }

    
//根据产生的表达式,计算表达式的值
    int caculateValue(String exp) {
        
int r = 0;
        
// 对表达式求值,r保存所求的值
        return r;
    }

    
//产生所有可能的符号组合,保存在一个Stack的Set里
    void initiStack(String oprs, Set oprset) {
        Stack
<Character> sta = new Stack<Character>();
        sta.clear();
        
for (Character c : oprs.toCharArray())
            sta.push(c);
        sta.push(
'#');
        oprset.add(sta);
        
int hCount = statHight(oprs);
        
if (hCount == 1{
            
int hPoision = highPoision(oprs);
            
switch (hPoision) {
            
case 0// *+-
                sta = new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(
'(');
                sta.push(oprs.charAt(
1));
                sta.push(
')');
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(
'(');
                sta.push(oprs.charAt(
1));
                sta.push(oprs.charAt(
2));
                sta.push(
')');
                sta.push(
'#');
                oprset.add(sta);
                
break;
            
case 1:// +*-
                sta = new Stack<Character>();
                sta.clear();
                sta.push(
'(');
                sta.push(oprs.charAt(
0));
                sta.push(
')');
                sta.push(oprs.charAt(
1));
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(
'(');
                sta.push(oprs.charAt(
0));
                sta.push(
')');
                sta.push(oprs.charAt(
1));
                sta.push(
'(');
                sta.push(oprs.charAt(
2));
                sta.push(
')');
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(oprs.charAt(
1));
                sta.push(
'(');
                sta.push(oprs.charAt(
2));
                sta.push(
')');
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                
break;
            
case 2:
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(
'(');
                sta.push(oprs.charAt(
1));
                sta.push(
')');
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(
'(');
                sta.push(oprs.charAt(
0));
                sta.push(oprs.charAt(
1));
                sta.push(
')');
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
            
default:
                
// trace(sta);
                break;
            }

        }
 else if (hCount == 2{
            
int lPoision = lowPoision(oprs);
            
switch (lPoision) {
            
case 0:
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(
'(');
                sta.push(oprs.charAt(
0));
                sta.push(
')');
                sta.push(oprs.charAt(
1));
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(
'(');
                sta.push(oprs.charAt(
0));
                sta.push(oprs.charAt(
1));
                sta.push(
')');
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                
break;
            
case 1:
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(
'(');
                sta.push(oprs.charAt(
1));
                sta.push(
')');
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(
'(');
                sta.push(oprs.charAt(
0));
                sta.push(oprs.charAt(
1));
                sta.push(
')');
                sta.push(oprs.charAt(
2));
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(
'(');
                sta.push(oprs.charAt(
1));
                sta.push(oprs.charAt(
2));
                sta.push(
')');
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                
break;
            
case 2:
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(oprs.charAt(
1));
                sta.push(
'(');
                sta.push(oprs.charAt(
2));
                sta.push(
')');
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                sta 
= new Stack<Character>();
                sta.clear();
                sta.push(oprs.charAt(
0));
                sta.push(
'(');
                sta.push(oprs.charAt(
1));
                sta.push(oprs.charAt(
2));
                sta.push(
')');
                
// trace(sta);
                sta.push('#');
                oprset.add(sta);
                
break;
            
default:
                
// trace(sta);
                break;
            }

        }

    }

    
//由于只有高低运算符混合的时候才可能插入括号调整优先级
    
//所以要计算高优先级运算符的位置
    int highPoision(String oprs) {
        
int p = -1;
        
for (int j = 0; j < oprs.length(); j++{
            
if (hOpr.contains(oprs.charAt(j))) {
                p 
= j;
                
break;
            }

        }

        
return p;
    }

    
//统计高优先级运算符的数量
    int statHight(String oprs) {
        
int i = 0;
        
for (Character c : oprs.toCharArray()) {
            
if (hOpr.contains(c)) {

                i
++;
            }

        }

        
return i;
    }

    
//由于只有高低运算符混合的时候才可能插入括号调整优先级
    
//所以要计算低优先级运算符的位置
    int lowPoision(String oprs) {
        
int p = -1;
        
for (int j = 0; j < oprs.length(); j++{
            
if (lOpr.contains(oprs.charAt(j))) {
                p 
= j;
                
break;
            }

        }

        
return p;
    }

    
//输出一个对象的字符串表示
    void trace(Object o) {
        System.out.println(
"跟踪: " + o.toString());
    }

}

 主类

Gate.java

public class Gate {
  
public static void main(String[] args) {
        Caculate24 ca 
= new Caculate24();
        String[] nums 
= ca.generateNums("5678");
        String[] oprs 
= ca.gerenteOprs("+-*/");    
        
for(String num:nums)
            
for(String opr:oprs){
                ca.printExpression(num, opr);
            }
        
    }

}

 

老师,您好,回去后我好好想了想,把自己的思路整理了,重新设计了这个算法,诚恳的请老师们再批评,指正..

.................

原创粉丝点击