2012-02-13 68 views
1

嗨我的代码中有一个问题,我必须计算表达式中使用的变量,当两个或多个变量相同时,它应该计为1.例如,a + ab =使用可变的总数:2的问题是,当我输入使用可变的A + A =总数:2. 这是我的代码:在字符串中计算字符数Java

public void simplify(String strexp){ 
    int ex =strexp.length(); 

    for (int a=0;a<=ex-1;a++){ 
     if(a==0) 
     { 
     if (Character.isLetter(strexp.charAt(a))) 
     { 
     b++; 
     } 


     } 
     else{ 
     for(int c=0;c<=a-1;c++){ 
       if (Character.isLetter(strexp.charAt(c))==Character.isLetter(strexp.charAt(a))) 
       { 

       System.out.println("equal"); 
       break; 
       } 
       else if (Character.isLetter(strexp.charAt(c))!=Character.isLetter(strexp.charAt(a))) 
       { 
      //if(c==a-1) 
       //{ 
       b++; 
       System.out.println("nomatch"); 
      // } 
       } 
     } 
     } 
     } 
    JOptionPane.showMessageDialog(null, b); 
    } 
+0

您可以使用正则表达式查找所有的变量,然后通过一些独特的阵列功能或别的什么东西。 – designerrr 2012-02-13 11:46:22

+0

“ab”和“AB”是否相等? – wmz 2012-02-13 12:23:06

+0

你想要计算哪一个:变量的数量或字符的数量?我可以用一个或多个字符命名变量;例如:a,variable,ab,a1 – ecle 2012-02-13 14:35:39

回答

0

使用列表来算的变量

public static int simplify(String strexp) { 
    int length = strexp.length(); 

    List<Character> usedVariables = new ArrayList<Character>(); 
    for (int i = 0; i < length; i++) { 
     char c = strexp.charAt(i); 
     if (Character.isLetter(c) && !usedVariables.contains(c)) { 
      usedVariables.add(c); 
     } 
    } 

    return usedVariables.size(); 
} 

public static void main(String[] args) { 
    System.out.println(simplify("x + b = d")); 
    System.out.println(simplify("x + x = a")); 
    System.out.println(simplify("x + x = x/2")); 
} 

总量此打印出

3 
2 
1 
+0

非常感谢您的帮助,名单是一个很好的帮助 – 2012-02-13 12:45:28

+0

@AlvinPulido,快乐,如果你喜欢答案,你也可以投票。 :) – epoch 2012-02-13 12:47:18

0

转换的字符串字符数组,它们加入一个映射,其中键是char,值是其变量计数。

import java.util.*; 

    class Main 

{ 
    public static void main (String[] args) 
    { 
     String yourString = "UNDERSTAND"; 

     Map<Character, Integer> count = new TreeMap<Character, Integer>(); 
     for (char c : yourString.toCharArray()) { 
      if (count.containsKey(c)) { 
       count.put(c, (int) count.get(c) + 1); 
      } else { 
       count.put(c, 1); 
      } 
     } 

     System.out.println(count); 

    } 
} 
+0

对于他的情况,“Map”是不必要的,为了计算有多少个唯一字符,不需要计数值。 – Dervall 2012-02-13 11:50:00

0

您需要跟踪您所看到的变量。一种方法可能是使用Set<char>。无论何时你看到一个角色,都将其添加到该组中。唯一运算符的数量将是集合中的字符数。

0

这是很难跟随在如此低的水平。为什么不使用正则表达式来查找所有匹配,然后构建一组独特的匹配项?

标题有误导性。这个任务更像是词汇记号计数。它比简单的人物高出一步。

0

单行

String complexExpression = "a + a*2 (2*a + b - 34)/2 + 3*c"; 

System.out.println(Arrays.toString(complexExpression.split("[+*()-/\\d\\s&&[^a-z]]+"))); 

System.out.println(new HashSet<String>(Arrays.asList(complexExpression.split("[+*()-/\\d\\s&&[^a-z]]+"))).size()); 

上面的代码打印下面对我来说:

[a, a, a, b, c] 
3 

当然,我不建议split操作多次使用相同的模式做。

+0

你的回答是正确的,因为它考虑了一个或多个字符大小的变量:例如表达式“ab + ab * 2(2 * ab + ba - 34)/ 2 + 3 * c”'给出一个[ab,ab,ab,ba,c]'的数组,其唯一变量计数为3。 – ecle 2012-02-13 15:06:21

0

这里的东西:

public class DistinctCharCount { 
    public static void main(String[] args) { 
     String s = "a+ab+abc+abcd"; 

     HashSet<Character> charSet = new HashSet<Character>();   
     for(int i=0;i<s.length();i++) { 
      charSet.add(s.charAt(i)); 
     } 

     System.out.println("Distinct Char Count : " + charSet.size()); 
    } 
}