今天偶然在网上看到以前的一道笔试题目,大概是这样的:从文件text.in读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典编辑顺序将单词及它的出现次数输出到正文文件word.out中。
该题目用c实现,主要思路是:用一棵有序二叉树存储这些单词及其出现的次数,一边读入一边建立.然后中序遍历该二叉树,将遍历经过的二叉树上的节点的内容输出即可。
c代码如下:
1 // asef.cpp : 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include
6 #include
7 #include
8 #include <string.h>
9
10 #define SOURCE_FILE "text.in"
11 #define OUTPUT_FILE "word.out"
12 #define MAX_WORD_LEN 128
13
14 //树节点包括单词大小&#xff0c;出现次数
15 typedef struct treenode
16 {
17 char szWord[MAX_WORD_LEN]; //大小&#xff0c;最长为128
18 int nCount; //个数
19 struct treenode* pLeft;
20 struct treenode* pRight;
21 }BNODE;
22
23 int getword(FILE* pFile,char* pasWordBuffer,int nBufferLen)
24 {
25 int result &#61; 0;
26 result &#61; fscanf(pFile, "%s", pasWordBuffer); //从流中格式化输入&#xff0c;若有&#xff0c;则返回1&#xff0c;若结束则返回0
27 if(EOF &#61;&#61; result || 0 &#61;&#61; result) //文件结尾
28 result &#61; 0;
29 else
30 result &#61; 1;
31
32 return result;
33 }
34
35 void binary_tree(BNODE** ppNode,char* pszWord)
36 {
37 BNODE* pCurrentNode &#61; NULL; //
38 BNODE* pMemoNode &#61; NULL; //
39 int nStrCmpRes &#61; 0; //
40
41 if(ppNode !&#61; NULL && pszWord !&#61; NULL)
42 {
43 // BNODE* pCurrentNode &#61; NULL;
44 // BNODE* pMemoNode &#61; NULL;
45 // int nStrCmpRes&#61;0;
46
47 pCurrentNode &#61; *ppNode; //____(1)_____
48
49 while(pCurrentNode)
50 {
51 /*寻找插入位置*/
52 nStrCmpRes &#61; strcmp(pszWord, pCurrentNode->szWord); //比较字符串
53
54 if(!nStrCmpRes)
55 {
56 pCurrentNode->nCount&#43;&#43;; //___(3)___
57
58 return;
59 }
60 else
61 {
62 pMemoNode&#61;pCurrentNode;// ___(4)___
63 pCurrentNode &#61; nStrCmpRes>0? pCurrentNode->pRight : pCurrentNode->pLeft;
64 }
65 }
66 }
67
68 pCurrentNode &#61; new BNODE;
69
70 if(pCurrentNode !&#61; NULL)
71 {
72 memset(pCurrentNode,0,sizeof(BNODE));
73 strncpy(pCurrentNode->szWord,pszWord,MAX_WORD_LEN-1);
74 pCurrentNode->nCount&#61;1;
75 }
76
77 if(pMemoNode&#61;&#61;NULL)
78 {
79 *ppNode&#61; pCurrentNode; // ___(5)___
80 }
81 else if(nStrCmpRes>0)
82 {
83 pMemoNode->pRight&#61;pCurrentNode;
84 }
85 else
86 {
87 pMemoNode->pLeft&#61;pCurrentNode;
88 }
89 }
90
91 void midorder(FILE* pFile,BNODE* pNode)
92 {
93 if(!pNode||!pFile) return; //___(6)___
94
95 midorder(pFile,pNode->pLeft);
96 fprintf(pFile,"%s %d\n",pNode->szWord,pNode->nCount);
97 midorder(pFile,pNode->pRight);
98 }
99
100 void main()
101 {
102 FILE* pFile&#61;NULL;
103 BNODE* pRootNode&#61;NULL;
104 char szWord[MAX_WORD_LEN]&#61;{0};
105
106 pFile&#61;fopen(SOURCE_FILE,"r");
107
108 if(pFile&#61;&#61;NULL)
109 {
110 printf("Can&#39;t open file %s\n",SOURCE_FILE);
111 return;
112 }
113
114 while(getword(pFile,szWord,MAX_WORD_LEN) &#61;&#61; 1)
115 {
116 binary_tree(&pRootNode,szWord); // 生成二叉树
117 }
118
119 fclose(pFile);
120
121 pFile&#61;fopen(OUTPUT_FILE,"w");
122 midorder(pFile,pRootNode);
123 fclose(pFile);
124 }
我用java实现&#xff0c;则同样需要读写文件&#xff0c;然后利用Map,单词为key,次数为value。。。map很方便啊&#xff0c;
其中的关键点&#xff1a;
1&#xff1a;读&#xff0c;写文件&#xff1b;
2&#xff1a;修改Map&#xff1b;
现附上Java源码&#xff1a;
1 package cn.com.test;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.PrintWriter;
6 import java.util.Iterator;
7 import java.util.Map;
8 import java.util.Scanner;
9 import java.util.Set;
10 import java.util.TreeMap;
11
12 public class test {
13
14 /**
15 * &#64;param args
16 * &#64;throws FileNotFoundException
17 */
18 public static void main(String[] args) throws FileNotFoundException {
19 // TODO Auto-generated method stub
20 //in 可以读取文件
21 Scanner in &#61; new Scanner(new File("text.in"));
22 //构造map
23 Map map &#61; new TreeMap();
24
25 while(in.hasNext())
26 {
27 //获取map的entrySet
28 Set entrySet &#61; map.entrySet();
29 //获取set的迭代器
30 Iterator i &#61; entrySet.iterator();
31 String temp &#61; in.next().toString();
32 boolean sign &#61; false;
33 while(i.hasNext())
34 {
35 Map.Entry entry &#61; (Map.Entry) i.next();
36 if(temp.equals(entry.getKey()))
37 {
38 int value &#61; Integer.parseInt(entry.getValue().toString())&#43;1;
39 entry.setValue(value);
40 sign &#61; true;
41 }
42 }
43 if(!sign)
44 {
45 map.put(temp, 1);
46 }
47 }
48 //关闭流
49 in.close();
50 Set entrySet &#61; map.entrySet();
51 Iterator i &#61; entrySet.iterator();
52 PrintWriter out &#61; new PrintWriter("word.out");
53 while(i.hasNext())
54 {
55 Map.Entry entry &#61; (Map.Entry)i.next();
56 System.out.println(entry.getKey()&#43;"&#61;&#61;&#61;&#61;&#61;&#61;&#61;"&#43;entry.getValue());
57 out.printf("%s %d\n",entry.getKey(),entry.getValue());
58 }
59 out.close();
60
61 }
62
63 }
这样&#xff0c;就用Java实现了该题目&#xff0c;欢迎各位同行指导&#xff0c;转载。