1

I have an arithmetic expression(a string) with unary operator, and I want to put each element in an array. For example: -3+4.2*5==>output should be: -3, +, 4.2 ,* , 5(not -,3,+,4.2, *, 5) 3+-5 ==> output should be: 3,+,-5(with the unary operator) (3/(5-8)+18)2==>output should be: (,3,/,(,5,-,8,),+,18,),,2

Here is the code I tried so far, and the output is 3,+,-,5, which didn't put the unary operator in the front of a digit.

My question is how to put each element properly in an array.

     public class Test2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    System.out.println("Input:");
    Scanner in = new Scanner(System.in);
    String input = in.nextLine();
    String[]  arr1= splitInfixExpression(input);
    for(int i=0;i<arr1.length;i++)
    {
        System.out.println(arr1[i]);
    }

    }
  priva te static boolean isOperandChar(final char c) {
    return Character.isDigit(c) || c == '.';
   }
  private static boolean isParenthesis(final char c) {
return c=='('||c==')';
}




    private static String[] splitInfixExpression(final String input) {
    final List<String> postfixExpression = new ArrayList<>();
    boolean encounteredOperandStart = false;
    String currentOperand = "";
    for (final char c : input.toCharArray()) {
        if (encounteredOperandStart) {
            if (isOperandChar(c)) {
                currentOperand += c;
            } 


                postfixExpression.add(currentOperand);
                postfixExpression.add(String.valueOf(c));
                currentOperand = "";
                encounteredOperandStart = false;

        } else {
            if (isOperandChar(c)) {
                encounteredOperandStart = true;
                currentOperand += c;
            }

            else if(isParenthesis(c)) {
            postfixExpression.add(String.valueOf(c));
            //currentOperand = "";
           encounteredOperandStart=false;
        }  
            else{
           postfixExpression.add(String.valueOf(c));
            //currentOperand = "";
           encounteredOperandStart=false;
       }                

        }
    }
    if (!currentOperand.isEmpty()) {
        postfixExpression.add(currentOperand);
    }
    return postfixExpression.toArray(new String[postfixExpression.size()]);
}}
Sophie
  • 69
  • 1
  • 9
  • 2
    This is a pretty intense problem for logic. I wouldn't even want to try it, to be honest. I would use regular expressions. – aliteralmind Mar 30 '14 at 23:02
  • Also see http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – aliteralmind Mar 30 '14 at 23:03
  • aliteralmind, this is not a me thematic evaluation question, it is how to put a string into an array. – Sophie Mar 30 '14 at 23:44
  • 1
    Of course. The first step in these linked questions is obviously getting the formula-string pieces into an array. Solving is the easy(er) part. – aliteralmind Mar 30 '14 at 23:58
  • How is this a duplicate? – Dawood ibn Kareem Mar 31 '14 at 02:12
  • @Sophie You may not realize it, but the question is about parsing arithmetic expressions. – user207421 Mar 31 '14 at 02:15
  • @DavidWallace Because you can't get the unary operator into the right place without doing a proper parse, and for that you need a correct arithmetic expression parsing technique, such as recursive descent, or the Dijkstra shunting-yard algorithm. These algorithms have been known for many decades. *Ad hoc* techniques do not work. – user207421 Mar 31 '14 at 02:19
  • Whether or not that's true, that doesn't mean that "how do I break this string into an array" is automatically a duplicate of "how do I calculate a numeric value for this expression". These two questions have a lot of common ground, but they're not duplicates of each other. Voting to reopen. – Dawood ibn Kareem Mar 31 '14 at 02:22

1 Answers1

0

I think this is want you want? :)

public static List<String> splitInfixExpression(String someString){
    List<String> someList = new ArrayList<String>();
    String tempString = "";
    for (int i = 0; i < someString.length(); i++){
        if (Character.isDigit(someString.charAt(i)) || (someString.charAt(i) == '-' && someString.length() == 0) || someString.charAt(i) == '.'){
            tempString += String.valueOf(someString.charAt(i));
            tempString = tempString.trim();
        }
        else{
            if (tempString.length() > 0){
                someList.add(tempString);
            }
            tempString = String.valueOf(someString.charAt(i));
            if (someList.size() > 0 && Character.isDigit(someList.get(someList.size() - 1).charAt(someList.get(someList.size() - 1).length() - 1))){
                someList.add(tempString);
                tempString = "";
            }
            else if (tempString.trim().length() > 0 && ((!tempString.equals("-"))) && ((!tempString.equals("+")))){
                someList.add(tempString);
                tempString = "";
            }
        }
    }
    someList.add(tempString);

    return someList;
}
Stephen Buttolph
  • 643
  • 8
  • 16