今天偶然在網上看到以前的一道筆試題目,大概是這樣的:從文件text.in讀入一篇英文短文,統計該短文中不同單詞和它的出現次數,並按詞典編輯順序將單詞及它的出現次數輸出到正文文件word.out中。
該題目用c實現,主要思路是:用一棵有序二叉樹存儲這些單詞及其出現的次數,一邊讀入一邊建立.然后中序遍歷該二叉樹,將遍歷經過的二叉樹上的節點的內容輸出即可。
c代碼如下:

1 // asef.cpp : 定義控制台應用程序的入口點。
2 //
3
4 #include "stdafx.h"
5 #include <stdio.h>
6 #include <malloc.h>
7 #include <ctype.h>
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 //樹節點包括單詞大小,出現次數
15 typedef struct treenode
16 {
17 char szWord[MAX_WORD_LEN]; //大小,最長為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 = 0;
26 result = fscanf(pFile, "%s", pasWordBuffer); //從流中格式化輸入,若有,則返回1,若結束則返回0
27 if(EOF == result || 0 == result) //文件結尾
28 result = 0;
29 else
30 result = 1;
31
32 return result;
33 }
34
35 void binary_tree(BNODE** ppNode,char* pszWord)
36 {
37 BNODE* pCurrentNode = NULL; //
38 BNODE* pMemoNode = NULL; //
39 int nStrCmpRes = 0; //
40
41 if(ppNode != NULL && pszWord != NULL)
42 {
43 // BNODE* pCurrentNode = NULL;
44 // BNODE* pMemoNode = NULL;
45 // int nStrCmpRes=0;
46
47 pCurrentNode = *ppNode; //____(1)_____
48
49 while(pCurrentNode)
50 {
51 /*尋找插入位置*/
52 nStrCmpRes = strcmp(pszWord, pCurrentNode->szWord); //比較字符串
53
54 if(!nStrCmpRes)
55 {
56 pCurrentNode->nCount++; //___(3)___
57
58 return;
59 }
60 else
61 {
62 pMemoNode=pCurrentNode;// ___(4)___
63 pCurrentNode = nStrCmpRes>0? pCurrentNode->pRight : pCurrentNode->pLeft;
64 }
65 }
66 }
67
68 pCurrentNode = new BNODE;
69
70 if(pCurrentNode != NULL)
71 {
72 memset(pCurrentNode,0,sizeof(BNODE));
73 strncpy(pCurrentNode->szWord,pszWord,MAX_WORD_LEN-1);
74 pCurrentNode->nCount=1;
75 }
76
77 if(pMemoNode==NULL)
78 {
79 *ppNode= pCurrentNode; // ___(5)___
80 }
81 else if(nStrCmpRes>0)
82 {
83 pMemoNode->pRight=pCurrentNode;
84 }
85 else
86 {
87 pMemoNode->pLeft=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=NULL;
103 BNODE* pRootNode=NULL;
104 char szWord[MAX_WORD_LEN]={0};
105
106 pFile=fopen(SOURCE_FILE,"r");
107
108 if(pFile==NULL)
109 {
110 printf("Can't open file %s\n",SOURCE_FILE);
111 return;
112 }
113
114 while(getword(pFile,szWord,MAX_WORD_LEN) == 1)
115 {
116 binary_tree(&pRootNode,szWord); // 生成二叉樹
117 }
118
119 fclose(pFile);
120
121 pFile=fopen(OUTPUT_FILE,"w");
122 midorder(pFile,pRootNode);
123 fclose(pFile);
124 }
我用java實現,則同樣需要讀寫文件,然后利用Map,單詞為key,次數為value。。。map很方便啊,
其中的關鍵點:
1:讀,寫文件;
2:修改Map;
現附上Java源碼:

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 * @param args
16 * @throws FileNotFoundException
17 */
18 public static void main(String[] args) throws FileNotFoundException {
19 // TODO Auto-generated method stub
20 //in 可以讀取文件
21 Scanner in = new Scanner(new File("text.in"));
22 //構造map
23 Map map = new TreeMap();
24
25 while(in.hasNext())
26 {
27 //獲取map的entrySet
28 Set entrySet = map.entrySet();
29 //獲取set的迭代器
30 Iterator i = entrySet.iterator();
31 String temp = in.next().toString();
32 boolean sign = false;
33 while(i.hasNext())
34 {
35 Map.Entry entry = (Map.Entry) i.next();
36 if(temp.equals(entry.getKey()))
37 {
38 int value = Integer.parseInt(entry.getValue().toString())+1;
39 entry.setValue(value);
40 sign = true;
41 }
42 }
43 if(!sign)
44 {
45 map.put(temp, 1);
46 }
47 }
48 //關閉流
49 in.close();
50 Set entrySet = map.entrySet();
51 Iterator i = entrySet.iterator();
52 PrintWriter out = new PrintWriter("word.out");
53 while(i.hasNext())
54 {
55 Map.Entry entry = (Map.Entry)i.next();
56 System.out.println(entry.getKey()+"======="+entry.getValue());
57 out.printf("%s %d\n",entry.getKey(),entry.getValue());
58 }
59 out.close();
60
61 }
62
63 }
這樣,就用Java實現了該題目,歡迎各位同行指導,轉載。