简体   繁体   English

如何使用一元运算符输出算术表达式?

[英]How to output an arithmetic expression with unary operator?

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 例如:-3 + 4.2 * 5 ==>输出应为:-3,+,4.2,*,5(不是-,3,+,4.2,*,5)3 + -5 ==>输出应为:3,+,-5(使用一元运算符)(3 /(5-8)+18) 2 ==>输出应为:(,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. 这是到目前为止我尝试过的代码,其输出为3,+,-,5,这没有将一元运算符放在数字的前面。

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()]);
}}

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM