The task is this: there is a line - "12 * 3-4 + 342 / 2.54".

You need to match the last number after the sign, i.e. 2.54, and instead of "/" there can be both "+" and "-" and "*". Tell me if it's easy. Tried like this:

function getLastNumber() { if (lastSymbol == (0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || .)) { lastNumber += lastSymbol; inverse(lastNumber); } getLastNumber() } 

But it does not work, throws it out of the application when the function is called. Apparently, something is wrong with recursion. I apologize in advance for the pseudocode ... (:

  • well and a method .... read about the reverse Polish record - it solves such problems - Gorets
  • The HMO solves slightly different tasks, and I do not need to break the example, I just need the last number, and writing a whole algorithm for the sake of it somehow does not seem to me right. - andrewshka
  • Well, if you need the last 4 characters - you can take them by function - Gorets
  • 2
    If we are sure that there is a number there, then why not just in a loop not move character by character from the end of the line while the digit or dot character is? And then read the number. - avp
  • one
    That's when the android label appears in the question in which nothing is connected with the android, you can immediately understand that they decided to jump over the study of java. "pseudocode" is generally tin, and you spoke about crutches to the user @Barmaley - rasmisha

4 answers 4

A generalized algorithm for solving such a problem is (without delving into the details of the syntactic analysis, the Polish inverse entry, etc.):

  1. Breakdown of expression into tokens. By a token we mean in this context a breakdown into lexemes, that is, where are the characters, where are the numbers, where are the operations. In this case:

    12 * 3-4 + 342 / 2.54

    9 tokens are obtained: {'12', '*', '3', '-', '4', '+', '342', '/', '2.54'}

  2. Further parsing of tokens - we define who is who, where operation, where symbol, where variable, and where constant

  3. Depending on the given grammar, the alignment of the decision tree (in the particular case of the Polish inverse) - the output will be something like: {'342', '2.54', '/', '12', '3', '*', '4', '-', '+'}
  4. We put a tree in the solver

In relation to the question: in stage 2 you get your 2.54 easily and naturally.

    Little bydlovato)) But it is possible through the regular season

      StringTokenizer tokenizer = new StringTokenizer("12*3-4+342/2.54", "+-*"); String result=""; while(tokenizer.hasMoreTokens()){ result = tokenizer.nextToken(); } return result; 
    • one
      No need to use StringTokenizer, StringTokenizer is for example. It is recommended that anyone use this split method of the string or the java.util.regex package instead. - Anton Feoktistov
     String str = "12*3-4+342/2.54"; String[] arr = str.split("(\\*|-|\\+|/)"); 

    Here, in general, and all ...

      I don’t know how much a normal java differs from Androyd, but then regular expressions come to be

        String s= "12*3-4+342/2.54"; Pattern pattern= Pattern.compile("(/|\\*|\\+|-)([\\d.]*)$"); Matcher m=pattern.matcher(s); if(m.find()){ System.out.println(m.group(2)); }