如何计算在Java中出现的“+”和“++”的频率?

huangapple 未分类评论55阅读模式
英文:

How to compute the frequency of occurrence of '+' and '++' in Java?

问题

我想计算输入文本文件中所有运算符出现的频率。文件中包含运算符+++。如何区分它们各自的频率,因为我的程序将++视为两个不同的+运算符,而不是一个++

以下是我的代码(input7.txt是一个测试文件):

public static void main(String[] args) throws IOException {
		
    String string = new String(Files.readAllBytes(Paths.get("input7.txt"))); //待计数的字符串
		
	int frequencyArray[] = new int[string.length()]; 
    int frequencyArray2[] = new int[string.length()];
        
    char stringArray[] = string.toCharArray(); //字符数组
    int i, j;
		
	//统计字符出现频率
		
	for (i = 0; i < string.length(); i++) {
			
		frequencyArray[i] = 1;
        //frequencyArray2[i] = 1;
            
        for(j = i + 1; j < string.length(); j++) 
        {
            if(stringArray[i] == stringArray[j]) 
            {
					
				frequencyArray[i]++;	
				stringArray[j] = '0'; //避免重复访问字符
				
			}	
		}	
	}
		
		
	//显示结果
		
	System.out.println("字符及其对应的频率");
		
	for (i = 0; i < frequencyArray.length; i++) {
			
		if (stringArray[i] != ' ' && stringArray[i] != '0') {
				
			System.out.println(stringArray[i] + "-" + frequencyArray[i]); 
				
		}
	}
}
英文:

I want to calculate the frequency of the occurrence of all the operators from an input text file. The file contains the operators + and ++. How can I distinguish their respective frequency, as my program treats ++ as 2 distinct + operators rather than 1 ++?

Here is my code (input7.txt is a test file):

public static void main(String[] args) throws IOException {
		
    String string = new String(Files.readAllBytes(Paths.get(&quot;input7.txt&quot;))); //String to be counted 
		
	int frequencyArray[] = new int[string.length()]; 
    int frequencyArray2[] = new int[string.length()];
        
    char stringArray[] = string.toCharArray(); //Array of characters
    int i, j;
		
	//Count characters
		
	for (i = 0; i &lt; string.length(); i++) {
			
		frequencyArray[i] = 1;
        //frequencyArray2[i] = 1;
            
        for(j = i + 1; j &lt; string.length(); j++) 
        {
            if(stringArray[i] == stringArray[j]) 
            {
					
				frequencyArray[i]++;	
				stringArray[j] = &#39;0&#39;; //To avoid revisiting a character
				
			}	
		}	
	}
		
		
	//Display results
		
	System.out.println(&quot;Characters and their corresponding frequencies&quot;);
		
	for (i = 0; i &lt; frequencyArray.length; i++) {
			
		if (stringArray[i] != &#39; &#39; &amp;&amp; stringArray[i] != &#39;0&#39;) {
				
			System.out.println(stringArray[i] +&quot;-&quot; + frequencyArray[i]); 
				
		}
	}
}

</details>


# 答案1
**得分**: 0

这对我有效:

```java
String s = "sdfasd++ sdfadsf+asdf sdf++sadfasdf++sadfsdf+asdfasdf++";
// 创建包含所有不同字符的集合
Set<Character> chars = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
  chars.add(s.charAt(i));
}
// 计算不同字符数量并将结果放入哈希映射中
Map<Character, Integer> counts = new HashMap<Character, Integer>();
for (Character c : chars) {
  int count = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == c)
      count++;
  }
  counts.put(c, count);
}
// 计算双字符运算符数量
int countPlusPlus = 0;
for (int i = 0; i < s.length() - 1; i++) {
  if (s.substring(i, i + 2).equals("++"))
    countPlusPlus++;
}
// 计算总数
int singleplusTotal = counts.get('+');
System.out.println("单个加号总数:" + singleplusTotal);
System.out.println("双加号总数:" + countPlusPlus);
System.out.println("仅单个加号:" + (singleplusTotal - countPlusPlus * 2));
英文:

This works for me:

  String s = &quot;sdfasd++ sdfadsf+asdf sdf++sadfasdf++sadfsdf+asdfasdf++&quot;;
            // create Set with all distinct characters 
Set&lt;Character&gt; chars = new HashSet&lt;Character&gt;();
            for (int i = 0; i &lt; s.length(); i++) {
              chars.add(s.charAt(i));
            }
// count distinct characters and put Results in HashMap
            Map&lt;Character, Integer&gt; counts = new HashMap&lt;Character, Integer&gt;();
            for (Character c : chars) {
              int count = 0;
              for (int i = 0; i &lt; s.length(); i++) {
                if (s.charAt(i) == c)
                  count++;
              }
              counts.put(c, count);
            }
// Count double-Character-Operators like this
            int countPlusPlus = 0;
            for (int i = 0; i &lt; s.length() - 1; i++) {
              if (s.substring(i, i + 2).equals(&quot;++&quot;))
                countPlusPlus++;
            }
        // Calculate totals like this
            int singleplusTotal = counts.get(&#39;+&#39;);
            System.out.println(&quot;Single Plus total&quot; + singleplusTotal);
            System.out.println(&quot;Double Plus total&quot; + countPlusPlus);
            System.out.println(&quot;Only single Plus&quot; + (singleplusTotal - countPlusPlus * 2));

huangapple
  • 本文由 发表于 2020年4月9日 12:53:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61114227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定