zoukankan      html  css  js  c++  java
  • 用java实现一道c笔试题

      今天偶然在网上看到以前的一道笔试题目,大概是这样的:从文件text.in读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典编辑顺序将单词及它的出现次数输出到正文文件word.out中。

      该题目用c实现,主要思路是用一棵有序二叉树存储这些单词及其出现的次数,一边读入一边建立.然后中序遍历该二叉树,将遍历经过的二叉树上的节点的内容输出即可。

      c代码如下:

    View Code
      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源码:

      

    View Code
     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实现了该题目,欢迎各位同行指导,转载。

        

  • 相关阅读:
    vue:自定义指令
    vue去掉严格开发,即去掉vue-cli安装时的eslint
    vue路由6:导航钩子
    vue路由5:命名视图
    vue路由3:子路由
    vue组件通信之任意级组件之间的通信
    vue中组件通信之子父通信
    令狐冲也是个情场高手啊
    Java —— 时区(夏令时)问题
    PL/SQL 如何导出INSERT语句
  • 原文地址:https://www.cnblogs.com/benshan/p/2400864.html
Copyright © 2011-2022 走看看