2013-11-26 40 views
1

我有一个字符串数组,它看起来像这样:的Java打印某些字符从一个数组

[67, +, 12, -, 45] 

我想打印出来,所以它看起来是这样的:

67 12 + 45 - 

下面的代码我试图用它来做到这一点。

String[] temp = line.split(" "); 
      String tmp = line.replaceAll("\\s+",""); 

      for(int i = 0; i < temp.length; i++) 
      { 
       if(isInt(temp[i]) == false) 
       { 
        expression = temp[i]; 
        firstExp = true; 
       } 
       else if(isInt(temp[i]) == false && firstExp == true && secondExp == false) 
       { 
        System.out.print(expression); 
        secondExp = true; 
       } 
       else if(isInt(temp[i]) == false && firstExp == true && secondExp == true) 
       { 
        System.out.print(expression); 
        firstExp = false; 
        secondExp = false; 
       } 
       else 
       { 
        System.out.print(temp[i]); 
       } 
      } 

firstExp和secondExp是检查应出现在阵列中的表达式布尔。 isInt()只是一个用来确定字符串是否是数字的方法。现在,所有这些代码并输出这样的:

671245 
+0

有一些规则来得到这个结果'67 12 + 45 -' –

+0

基本规则是,它需要打印所有涉及到的数字首先一个表达式,然后打印表达式本身。 – Generalkidd

+0

那么你有如下阵列:65,23,+,36, - ,45?输出是什么? –

回答

1
public static void main (String[] args) throws java.lang.Exception 
    { 
     String[] expr = new String[]{"67", "+", "45", "-", "12", "*", "5", "/", "78"}; 
     int current = 0; 
     StringBuilder postfix = new StringBuilder(); 

     // handle first three 
     postfix.append(expr[current]).append(" "); 
     postfix.append(expr[current+2]).append(" "); 
     postfix.append(expr[current+1]).append(" "); 
     current += 3; 

     // handle rest 
     while(current <= expr.length-2){ 
      postfix.append(expr[current+1]).append(" "); 
      postfix.append(expr[current]).append(" "); 
      current += 2; 
     } 

     System.out.println(postfix.toString()); 
    } 

输出:

67 45 + 12 - 5×78/

您可以运行/编辑这个在:http://ideone.com/zcdlEq

1

我想你要做的是将中缀表达式转换为pos t修复。有时候我写了下面的代码:

public class InfixToPostfix { 
    private Stack stack; 
    private String input; 
    private String output = ""; 
    public InfixToPostfix(String in) { 
     input = in; 
     int stackSize = input.length(); 
     stack = new Stack(stackSize); 
    } 
    public String translate() { 
     for (int j = 0; j < input.length(); j++) { 
     char ch = input.charAt(j); 
     switch (ch) { 
      case '+': 
      case '-': 
      hastOperator(ch, 1); 
      break; 
      case '*': 
      case '/': 
      hastOperator(ch, 2); 
      break; 
      case '(': 
      stack.push(ch); 
      break; 
      case ')': 
      hasSuperior(ch); 
      break; 
      default: 
      output = output + ch; 
      break; 
     } 
     } 
     while (!stack.isEmpty()) { 
     output = output + stack.pop(); 
     } 
     System.out.println(output); 
     return output; 
    } 
    public void hastOperator(char op, int precedence) { 
     while (!stack.isEmpty()) { 
     char opTop = stack.pop(); 
     if (opTop == '(') { 
      stack.push(opTop); 
      break; 
     } 
     else { 
      int prec2; 
      if (opTop == '+' || opTop == '-') 
      prec2 = 1; 
      else 
      prec2 = 2; 
      if (prec2 < precedence) { 
       stack.push(opTop); 
       break; 
      } 
      else 
      output = output + opTop; 
     } 
     } 
     stack.push(op); 
    } 
    public void hasSuperior(char ch){ 
     while (!stack.isEmpty()) { 
     char chx = stack.pop(); 
     if (chx == '(') 
     break; 
     else 
     output = output + chx; 
     } 
    } 
    public static void main(String[] args) 
    throws IOException { 
     String input = "67 + 12 - 45"; 
     String output; 
     InfixToPostfix theTrans = new InfixToPostfix(input); 
     output = theTrans.translate(); 
     System.out.println("Postfix is " + output + '\n'); 
    } 
    class Stack { 
     private int maxSize; 
     private char[] stackArray; 
     private int top; 
     public Stack(int max) { 
     maxSize = max; 
     stackArray = new char[maxSize]; 
     top = -1; 
     } 
     public void push(char j) { 
     stackArray[++top] = j; 
     } 
     public char pop() { 
     return stackArray[top--]; 
     } 
     public char peek() { 
     return stackArray[top]; 
     } 
     public boolean isEmpty() { 
     return (top == -1); 
    } 
    } 
    } 

您可能需要修改此程序以从数组中读取,但这非常简单。

1

这里是你如何在一行做到这一点:

System.out.println(Arrays.toString(temp).replaceAll("[^\\d +*/-]", "").replaceAll("[+*/-]) (\\d+)", "$2 $1"));