编程题
请编写一个程序,从文件src.txt读入一篇英文短文,统计并输出该短文中不同单词出现的次数,然后输出统计结果。
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.regex.Pattern; /** * 请编写一个程序,从文件src.txt读入一篇英文短文,统计并输出该短文中不同单词出现的次数,然后输出统计结果。 * @author qiuqi * */ public class CountNumber { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file=new File("sc.txt"); BufferedReader br=new BufferedReader(new FileReader(file)); //String testStr=" In the nineteenth century: there was? a huge increase in the spread of English because of the development of new technology and new methods of mass production, transportation and communication. This spread has accelerated into the tweniteth and twenty-first century. Steamships and railways were developed in the first half of the nineteenth century, which brought people into closer contact. Railways carried newspapers across countires and in the second half of the nineteenth century, the telephone and telegraph were invented, making it even easier for people to communicate quickly and across vast distances."; Map<String,Integer> map=new HashMap<String,Integer>(); String regex="\t|\r|\n|\"|\\,|\\.|\\:|\\?"; String result=null; while((result=br.readLine())!=null){ String s=result.replaceAll(regex, " "); System.out.println(s); String[] arr=s.split(" "); for(int i=0;i<arr.length;i++) { if(arr[i].equals("")) continue; if(map.containsKey(arr[i])) { map.put(arr[i], map.get(arr[i])+1); }else { map.put(arr[i], 1); } } } Set<Entry<String, Integer>> entrySet = map.entrySet(); for(Entry<String, Integer> entry : entrySet){ System.out.println(entry.getKey() +":" +entry.getValue()); } } }
#include<iostream> #include<vector> #include<fstream> #include<string> #include <sstream> #include <algorithm> using namespace std; int main(){ ifstream infile; infile.open("read.txt"); string readline; vector<string> vec; vector<string> word; vector<string>word2; int num = 0; //计数单词个数 //循环读取一行 while (getline(infile, readline)) { vec.push_back(readline);//将每一行插入容器中 } for (auto it = vec.begin(); it != vec.end(); it++) { istringstream is(*it); string s; while (is >> s) { word.push_back(s); } } //打印出所有单词 for (auto it = word.begin(); it != word.end(); it++) { if (find(word2.begin(), word2.end(), *it) != word2.end())//找到相同的单词了 { continue; } word2.push_back(*it); num++; } for (auto it = word2.begin(); it != word2.end(); it++) { cout << (*it) << endl; } cout << "一共有" << num << "个单词" << endl; return 0; }
package com.hh.testPrivateStatic; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; // 2015 - 六 22题 /* 编程题:请编写一个程序,从文件src.txt读入一篇英文短文,统计并输出该短文中不同单词出现的次数,然后输出统计结果。 算法思路大致如下: (英语短文,它应该是连续的,然后有除大小写英文字母外其他的特殊字符如逗号、句号、双引号等) 把所有内容读取一个string,都小写。 把遍历string,把非字母字符都换成空格,这样可能会多出单个空格或者多个连续的空格。 循环去多空格 单空格split, 这样可以获得单词数组, 将数组元素放入map,(如果map中已存在则对应value++,不存在默认为1) 最后遍历输出结果。 */ public class Six_2015_22th { public static void main(String[] args) throws IOException { // String s = "123 456 789 0"; // for (String s1 : s.split(" ")) { // if (s1.equals("")){ // 空字符串 // System.out.println(s1 + "length:" + s1.length()); // } // } FileInputStream input = new FileInputStream("E:\\IdeaProjects\\try-algorithm\\src\\main\\java\\com\\hh\\article"); InputStreamReader inputStreamReader = new InputStreamReader(input); BufferedReader reader = new BufferedReader(inputStreamReader); String[] letters; // 所有字母(包括重复) Map<String, Integer> map; // 计算 单词出现次数map String s = reader.readLine(); if (s != null){ s = s.toLowerCase(); StringBuilder builder = new StringBuilder(s); // 非字母替换成空格 for (int i = 0; i < s.length(); i++){ if (!isLetter(s.charAt(i))){ // 如果不是 字母 则替换成空格 builder.setCharAt(i, ' '); } } //多空格化成单空格 去首尾空格 并提取单词 s = builder.toString(); s = s.trim(); while (s.contains(" ")) s = s.replace(" ", " "); letters = s.split(" "); //计算单词出现次数 map = new HashMap<>(); for (String letter : letters) { if (map.containsKey(letter)) map.put(letter, map.get(letter)+1); else map.put(letter, 1); } // 循环遍历map 输出计算结果 for (Map.Entry<String, Integer> entry : map.entrySet()){ System.out.println(entry.getKey() + " :" + entry.getValue()); } } // String s = "1 2 3 4 5"; //// s = s.replace(" ", " "); //1 2 3 4 5 // while (s.contains(" ")){ // s = s.replace(" ", " "); // } // System.out.println(s); } public static boolean isLetter(char ch){ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); } }
解题思路:
import java.io.*; import java.util.*; public class Test { public static void main(String[] args) throws Exception { String file = "src.txt"; BufferedReader br = new BufferedReader(new FileReader(file)); StringBuffer sBuff = new StringBuffer(); String s = null; while((s=br.readLine())!=null){ sBuff.append(s+" "); } String str = sBuff.toString(); str = str.replaceAll("[,.?:\"]"," ").replace("'s"," is").replace("'t","not"); String[] arr = str.split(" "); HashMap<String,Integer> strMap = new HashMap(); for(String ss : arr){ if(!"".equals(ss) && ss != null){ if (!strMap.containsKey(ss)){ strMap.put(ss,1); }else{ strMap.put(ss,strMap.get(ss)+1); } } } Set<Map.Entry<String,Integer>> entrySet = strMap.entrySet(); for(Map.Entry<String,Integer> entry : entrySet){ System.out.println("key: "+entry.getKey()+" value:"+entry.getValue()); } } }
#include<iostream> #include<map> #include<fstream> #include<string> using namespace std; int main(){ fstream infile; infile.open("read.txt"); map<string,int> mp; map<string,int>::iterator it; char ch; string word ; while(!infile.eof()){ infile >> ch; //这里的一个错误是inflie>>读取文件自动跳过空格... if(ch >='a' && ch<='z'){ word += ch; }else{ it = mp.find(word); if(it!=mp.end()){ it->second ++; }else{ mp.insert(pair<string,int>(word,1)); } word.clear(); // word = ""; } } for(it = mp.begin();it!=mp.end();++it){ cout<<it->first <<" "<<it->second<<endl; } system("pause"); return 0; } /* (1)读文件 (2)识别出单词 (3)单词计数——交给map吧 key为单词,value来进行计数 (4)顺序遍历map容器输出结果即可 思路:先从文件中读出指定长度数据,根据对每个字节判断是否在’a’~’z’或是’A’~’Z’,来判断是否为英文字母, 当判断到不是英文字母时,将之前字母的组合认为是一个单词,然后将单词保存到map容器中,在加入map容器前 先判断这个单词是否存在?如果存在,将记数加1,如果不存在,将单词加入map,记数为1,可能有存在读一次, 单词读了一半的情况,这个需要保留下来,下一次处理,再循环到最前面,直到全部读完为止。打印输出的时候, 只需要循环map容器即可。 c++ 读取文件 fstream */
public static void getWrite(String path) throws IOException { File file=new File(path); Reader reader =null; String str=new String(); reader =new FileReader(file); int a=0; while((a=reader.read())!=-1){ char c =(char) a; str+=c; } String[] str2=str.split("[ ,. ]"); Map map=new HashMap(); for (String str1:str2){ if(map.containsKey(str1)){ map.put(str1,(int)map.get(str1)+1); }else { map.put(str1,1); } } for(Object str3:map.keySet()){ System.out.println(str3.toString()+" "+map.get(str3)); } reader.close(); } public static void main(String[] args) throws IOException { String path="F:\\test.txt"; getWrite(path); }
package com.objectdemotest.hundsun; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class SolutionWordCount { public static void main(String[] args) { try { wordCount(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void wordCount() throws IOException { FileInputStream fileInputStream = new FileInputStream("C:\\Users\\NoCortY\\Desktop\\src.txt"); byte[] buffer = new byte[fileInputStream.available()]; fileInputStream.read(buffer); String content = new String(buffer).replace(".", " ").replace(",", " ").replace("!", " ").replace("(", " ") .replace(")", " "); String[] words = content.split(" "); Map<String,Integer> wordMap = new HashMap<String,Integer>(); for(String word:words) { Integer count = wordMap.get(word); if(count==null) { wordMap.put(word, 1); }else { wordMap.put(word, ++count); } } for(String key:wordMap.keySet()) { System.out.println(key+":"+wordMap.get(key)); } } }
public static void readFile(){ ArrayList<String> words = new ArrayList<>(); if(FileOperation.readValue("src.txt",words)){ TreeMap<String,Integer> map = new TreeMap<>(); for(String word:words){ if(map.containsKey(word)){ map.put(word,map.get(word)+1); }else{ map.put(word,1); } } System.out.print("Total different words: " + map.size()); } }
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class TestYY { public static void main(String[] args) throws IOException { File file = new File("src.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); Map<String, Integer> map = new HashMap<String, Integer>(); String result = null; while((result = br.readLine()) != null) { String[] split = result.split(" "); for(int i = 0; i < split.length; i++) { System.out.print(split[i]); if(map.containsKey(split[i])) { Integer num = map.get(split[i]); map.put(split[i], num+1); } else { map.put(split[i], 1); } } System.out.println(); } Set<Entry<String, Integer>> entrySet = map.entrySet(); for(Entry<String, Integer> entry : entrySet){ System.out.println(entry.getKey() +":" +entry.getValue()); } } }
#include <stdio.h> #include <malloc.h> #include <ctype.h> #include <string.h> #define INF "text.in" #define OUTF "wotd.out" typedef struct treenode char *word; int count; struct treenode *left,*right; BNODE int getword (FILE *fpt,char *word) char c; c=fgetc (fpt); if ( c=EOF) return 0; while(!(tolower(c)>=’a’ && tolower(c)<=’z’)) c=fgetc (fpt); if ( c==EOF) return 0; /*跳过单词间的所有非字母字符*/ while (tolower (c)>=’a’ && tolower (c)<=’z’) *word++=c; c=fgetc (fpt); *word=’\0’; return 1; void binary_tree(BNODE **t,char *word) BNODE *ptr,*p;int compres; P=NULL; ptr=*t ; while (ptr) /*寻找插入位置*/ compres=strcmp (word, ptr->word );/*保存当前比较结果*/ if (!compres) ptr->count++ ;return; else p=ptr ; ptr=compres>0 ptr->right:ptr->left; ptr= (BNODE*) malloc (sizeof (BNODE)) ; ptr->left = ptr->right = NULL; ptr->word= (char*) malloc (strlen (word) +1) ; strcpy (ptr->word, word); ptr->count - 1; if (p==NULL) *t=ptr ; else if (compres > 0) p->right = ptr; else p->left = ptr; void midorder (FILE **fpt, BNODE *t) if (t==NULL) return; midorder (fpt, t->left); fprintf (fpt, "%s %d\n", t->word, t->count) midorder (fpt, t->right); void main() FILE *fpt; char word[40]; BNODE *root=NULL; if ((fpt=fopen (INF,"r")) ==NULL) printf ("Can’t open file %s\n", INF ) return; while (getword (fpt, word) ==1 ) binary_tree (&root, word ); fclose (fpt); fpt = fopen (OUTF, "w"); if (fpt==NULL) printf ("Can’t open file %s\n", OUTF) return; midorder (fpt, root); fclose(fpt);