2017-07-27 13 views
1

私は文字列内のすべての数字を見つけて、それらと簡単な算術をする必要があります。 2つの数字の間の記号の数が偶数の場合、演算子は'+'、奇数の場合演算子は' - 'です。文字列から数値を抽出し、それらの間の要素を数えるにはどうすればよいですか?

入力:10plus5 - 出力:15; (10 + 5);


入力:10i5can3do2it6 - 出力:10; (10-5-3 + 2 + 6);


入力:10i5can3do2it - 出力:4。 (10-5-3 + 2)である。

Iは、第一の例の解を見つけることができる:

public static void main(String[] args) throws IOException { 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    String input = br.readLine(); 
    int result = 0; 
    int count = 0; 
    Pattern pat = Pattern.compile("([\\d]+)([\\D]+)([0-9]+)"); 

    Matcher match = pat.matcher(input); 

    while(match.find()){ 
     char[] array = match.group(2).toCharArray(); 
     for (int i = 0; i < array.length; i++) { 
      int firstNumber = Integer.parseInt(match.group(1)); 
      int secondNumber = Integer.parseInt(match.group(3)); 
      count++; 
      if(count % 2 == 0){ 
       result = firstNumber + secondNumber ; 
      }else{ 
       result = firstNumber - secondNumber; 
      } 
     } 

    } 
    System.out.println(result); 
} 
+1

だけ。また、 'カウント%2 == 0 'の比較が起こることを必要 –

+0

最後の計算を得るようにするには、ループ上result'たびに'の値を上書きループの外側にあります。実際には、ループは必要ありません。 'if(array.length%2 == 0)'は比較対象となります –

+0

Thaks私はこれを修正しますが、私の問題は他の例です。 –

答えて

1

パターンは、おそらく単純なアプローチは、例えば、良好で、非常に簡単である:

private static int compute(String input, int index) { 
    int result = 0; 
    int i = index; 
    while (i < input.length() && !Character.isDigit(input.charAt(i))) { 
     i++; 
    } 
    if (i < input.length()) { 
     int j = i; 
     int value = 0; 
     while (j < input.length() && Character.isDigit(input.charAt(j))) { 
      value = value * 10 + (input.charAt(j) - '0'); 
      j++; 
     } 
     if (j < input.length()){ 
      result = compute(input, j); 
     } 
     if ((index - i) % 2 == 0){ 
      result += value; 
     } else { 
      result -= value; 
     } 
    } 
    return result; 
} 

public static void main(String[] args) throws IOException { 
    String input = "10i5can3do2it6"; 
    System.out.println(compute(input, 0)); 
} 
4

以下溶液input文字列が数字で始まり、一連の単語番号の組み合わせ(数字で終わる必要はありません)で始まる場合にのみ機能します。これの検証は含まれていません。

String input = "10i5can3do2it";  
String[] parts = input.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); 

int result = Integer.valueOf(parts[0]); 

for (int i = 2; i < parts.length; i+=2){ 
    if (parts[i-1].length() % 2 == 1) { 
     result -= Integer.valueOf(parts[i]); 
    } else { 
     result += Integer.valueOf(parts[i]); 
    } 
} 

System.out.println(result); 

プリント4

hereのように、正規表現は文字と数字の間で分割されます。残りはかなり自明です。

3

コメントに記載されているように、最後の2つの数字の結果のみを計算しています。

計算を行う必要があります。

たとえば、 (すべての3つの与えられた入力を検証済み)

public static void main(String[] args) throws IOException { 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String input = br.readLine(); 

    Pattern pat = Pattern.compile("([\\d]+|)([\\D]+)([0-9]+)"); 
    Matcher match = pat.matcher(input); 

    int result = 0; 

    while (match.find()) { 
     // Pre-capture the groups 
     String[] matches = new String[3]; 
     for (int i = 0; i < 3; i++) { 
      matches[i] = match.group(i+1); 
     } 

     // Handle first number, if exists, else 0 
     int firstNumber = 0; 
     try { 
      firstNumber = Integer.parseInt(matches[0]); 
     } catch (NumberFormatException e) {} 
     result+=firstNumber; 

     // if second number doesn't exist 
     if (matches[2] == null) { 
      break; 
     } 

     // when second number exists, do the logic 
     int secondNumber = Integer.parseInt(matches[2]); 
     if (matches[1].length() % 2 == 0) { 
      result += secondNumber; 
     } else { 
      result -= secondNumber; 
     } 
    } 
    System.out.println(result); 
} 
関連する問題