wordcount(代码片段)

yzynbone yzynbone     2023-01-02     533

关键词:

gitee地址:

https://gitee.com/yzyindex/wordcount

需求分析

通过程序设计,编写一个可执行文件exe能够对源程序文件进行统计字符数、单词数、行数,统计结果可以以指定的格式输出到默认文件中。

可执行程序命名为:wc.exe,该程序处理用户需求的模式为:

wc.exe [parameter] [input_file_name]

基本功能:

wc.exe -c file.c 对字符数的统计

wc.exe -w file.c 对单词数的统计

wc.exe -l file.c 对行数的统计

wc.exe -o result.txt 将结果输出到指定文件result.txt

psp

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 30

 20

· Estimate

· 估计这个任务需要多少时间

 100

 70

Development

开发

 700

 1000

· Analysis

· 需求分析 (包括学习新技术)

 200

 90

· Design Spec

· 生成设计文档

 32  

 50

· Design Review

· 设计复审 (和同事审核设计文档)

 100

 60

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 100

 60

· Design

· 具体设计

 100

 300

· Coding

· 具体编码

 500

 600

· Code Review

· 代码复审

 60

 100

· Test

· 测试(自我测试,修改代码,提交修改)

 140

 150

Reporting

报告

 100

 100

· Test Report

· 测试报告

 50

 50

· Size Measurement

· 计算工作量

 30

 50

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 20

 30

 

合计

 2360

 2800

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 --

 

 

 

 

 

 

 

解题思路

--

为了用两个数组分别存取文件名和操作命令,根据是否带有-0指令来采取两种不同的方式输出结果到指定文件和默认文件,每种方式采用循环+switch的方式得到输出信息的字符串str

最终将str输入到指定的文件

代码说明

--

将命令和文件名分离

List<String> operations = new ArrayList<String>();
        List<String> files = new ArrayList<String>();
        String outPutFile ="result.txt";
        for(int i=0 ;i<args.length;i++)
        
            if(args[i].startsWith("-"))
            operations.add(args[i]);
            else
                files.add(args[i]);
                
        

 

不带-o命令的操作 通过for+switch取得输出信息str,最终输入到指定文件。

 if(!operations.contains("-o"))
            
                for (int j= 0; j < files.size(); j++) 
                    switch (operations.get(i)) 
                    case "-c":
                        str+=" "+basicFunction.getCharNum(files.get(j));
                        break;
                    case "-l":
                        str+=" "+basicFunction.getLineNum(files.get(j));
                        break;
                    case "-w":
                        str+=" "+basicFunction.getWordNum(files.get(j));
                        break;
                    default:
                        System.out.println("false");
                        break;
                    
                
                outputfile(str,outPutFile);
              
            

带有-o的操作

else 
                outPutFile=files.get(files.size()-1);
                for (int j= 0; j< files.size()-1; j++) 
                    switch (operations.get(i)) 
                    case "-c":
                        str+=" "+basicFunction.getCharNum(files.get(j));
                        break;
                    case "-l":
                        str+=" "+basicFunction.getLineNum(files.get(j));
                        break;
                    case "-w":
                        str+=" "+basicFunction.getWordNum(files.get(j));
                        break;
                    case "-o" : break;
                    default:
                        System.out.println("false");
                        break;
                    
                
                outputfile(str,outPutFile);
            

获取字符数量,单词数量,行数的函数

public static String getCharNum(String string) throws IOException 
    
         
          
            BufferedReader buf =Util.getBufferedReader(string);
            String line;
            int charNum = 0;
               while((line = buf.readLine()) != null)
                    char[] ch = line.toCharArray();
                    for(int i=0; i < ch.length; i++) 
                        if(!Character.isWhitespace(ch[i])) 
                            charNum++;
                        
                    
                
                System.out.println("字符数: " + charNum);
                String str="字符数: " + charNum;
                buf.close();
                return str;
            
    
    public static String getWordNum(String file) throws IOException
    
    
         int wordNum=0;
          
            int x=-1;
            Reader Reader=Util.getFileReader(file);
            while((x=Reader.read())!=-1)
            
                char a=(char)x;
                if(a==‘ ‘||a==‘,‘||a==‘.‘)
                
                    wordNum++;
                
            
            Reader.close();
            String str="单词数:"+wordNum;
            System.out.println("单词数:"+wordNum);
            return str;
    
    public static String getLineNum(String file) throws IOException
    
             int lineNum=0;
            BufferedReader buf=Util.getBufferedReader(file);
                while(buf.readLine()!=null)
                
                    lineNum++;
                
                buf.close();
            String str="行数:"+lineNum;
            
            System.out.println("行数:"+lineNum);
            return str;
    

工具util类 提供各种的文件操作流

public static BufferedReader getBufferedReader(String file) throws FileNotFoundException 
        File file1 = new File(file);
        BufferedReader buf=null;
        buf = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
        return buf;
    
    
    public static InputStream  getInputStream(String file) throws FileNotFoundException 
        File file1 = new File(file);
        InputStream in=null;
        in= new FileInputStream(file1);
        return in;
        
    
    public static Reader getFileReader(String file) throws FileNotFoundException
    
        File file1 = new File(file);
        Reader rd= null;
        rd =new FileReader(file1);
        return rd;
    
    public static BufferedWriter getBufferedWriter(File file) throws FileNotFoundException
    

        BufferedWriter buf = null;
        buf= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        return buf;
    

测试输入:

wc.exe -c fix.txt

wc.exe -c -l fix.txt

wc.exe -c -w -l test.txt

wc.exe -l -c test.txt

wc.exe -c -w test.xt
wc.exe  -c -w -l test.txt

wc.exe  -c --w -c test.txt

wc.exe -l --ww -c test.txt

部分测试截图:

技术分享图片

技术分享图片

 技术分享图片

 

 

技术分享图片

技术分享图片

技术分享图片

 

 

 

经上述测试 程序都能排除错误,但由于时间原因我并没有编写当用户输入错误提示用户重新输入的代码,导致程序存在一些逻辑问题

心得体会:我自以为学了java的很多知识,但是当用的时候才发现,学了当没学!!!也特别是对io的操作,简直是重新学了一遍,本来没有给编码留太多的时间,最后到写时编码花了太多的时间,不过能重新学到新的知识,也算是有收获,不过这也让我下定决心再学一遍代码,边学边敲! 除此之外,这次试验最大的收获便是git了,简直太好用了,我花了一下午+一晚上的时间,掌握了许多命令操作,算是被编码打击后的一些安慰吧。希望老师多出点个人项目,我想多实战一下!!!

有关博客的使用和排版,:http://www.cnblogs.com/math/p/se-tools-001.html。 

    有关Git的使用,廖雪峰的官方网站。 

 


wordcount(代码片段)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;namespaceWordCountclassProgramstaticvoidMain(string[]args)Typetype=newType() 查看详情

wordcount(代码片段)

1.Github项目地址https://github.com/chaindreamjet/WordCount2.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 20 30·Estimate·估计这个任务需要多少时间 20 30Development开发 700 940·Analysis·需求分析(包括 查看详情

mapreduce编写wordcount程序代码实现(代码片段)

MapReduce经典案例代码(wordcount)以经典的wordcount为例,通过自定义的mapper和reducer来实现单词计数packagecom.fwmagic.mapreduce;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io. 查看详情

分享知识-快乐自己:运行(wordcount)案例(代码片段)

运行wordcount案例:一):大数据(hadoop)初始化环境搭建二):大数据(hadoop)环境搭建三):运行wordcount案例四):揭秘HDFS五):揭秘MapReduce六):揭秘HBase七):HBase编程-----------------------------------------------------------------Hadoo... 查看详情

wordcount(代码片段)

一.码云地址https://i.cnblogs.com/EditPosts.aspx?opt=1https://gitee.com/lttzy/wordcount二.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 20·Estimate·估计这个任务需要多少时间 20 10Development开发 45 查看详情

wordcount(代码片段)

一.github:https://github.com/JiejieCM/WordCount二.pspPSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 30·Estimate·估计这个任务需要多少时间3030Development开发 3天 5天·Analysis·需求分析(包括学习新技术) 2天&nbs... 查看详情

wordcount(代码片段)

WordCount一.个人Gitee地址:https://gitee.com/godcoder979/(该项目完整代码在这里) 二.项目简介:该项目是一个统计文件字符、单词、行数等数目的应用程序,通过输入命令来执行你想要的操作。所用语言:java命令格式:wc.exe[para]&... 查看详情

wordcount(代码片段)

一、Gitee项目地址https://gitee.com/YSS_MYGit/WordCount二、基本思路 我看到项目后,首先想到的是使用C#语言编写,因为我对C#语言比较熟悉些。此项目有两个类,一个是Program类,该类是程序的入口,主要用于传参,另一个是WC类,... 查看详情

wordcount(代码片段)

一.Gitee地址:https://gitee.com/zjgss99/WordCount 二.项目分析:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。命令格式:wc.exe[para] ... 查看详情

个人项目wordcount(代码片段)

1.项目GitHub地址:https://github.com/FPXBao/wordcount2.解题思路:分析程序基本需求,将其功能分为三个函数调用:主函数intmain();功能函数Ccount();Wcount();Lcount();并进行相关知识学习。3.代码说明:主函数:#include<stdio.h>#include<stdlib.... 查看详情

wordcount(代码片段)

   码云地址:https://gitee.com/qaqxx88/wc1WordCount需求说明 (1)对源文件进行字符数,行数,单词数的统计并且放在与wc.exe相同目录下的result.txt中  (2)基本功能  wc.exe -c file.c   &nbs 查看详情

wordcount基础功能(代码片段)

Gitte地址:https://gitee.com/gyuyue/WordCount PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 15 20·Estimate·估计这个任务需要多少时间 30 30Development开发 60 80·Analysis·需求分析(包括学习新技术)... 查看详情

wordcount代码实现及测试(代码片段)

...者:201631062515201631062415码云地址:https://gitee.com/heshuxiang/WordCount/tree/master2.项目需求  对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多... 查看详情

wordcount基本功能(代码片段)

项目的gitee地址:https://gitee.com/ITtoto/WordCount 开发语言:C语言PSP2.1表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划20 16·Estimate·估计这个任务需要多少时间 30 27Development开发 120 160·Analysis·需... 查看详情

wordcount(代码片段)

   Gitee地址:https://gitee.com/dyh1621838953/WordCount1此项目我只实现了基本功能,即字符、行数、单词数的计算以及将结果输出到文件中。该项目用C#语言进行实现。解题思路:   根据题目要求所知,需要读取一... 查看详情

[mapreduce_1]运行wordcount示例程序(代码片段)

 0.说明  MapReduce实现WordCount示意图&& WordCount代码编写    1. MapReduce实现WordCount示意图     1.Map:预处理阶段,将原始数据映射成每个K-V,发送给reduce  2.Shuffle:混洗(分类),将相同的Key... 查看详情

wordcount(代码片段)

gitee地址:https://gitee.com/yzyindex/wordcount需求分析通过程序设计,编写一个可执行文件exe能够对源程序文件进行统计字符数、单词数、行数,统计结果可以以指定的格式输出到默认文件中。可执行程序命名为:wc.exe,该程序处理用... 查看详情

wordcount-软测作业(代码片段)

1.github地址 https://github.com/zgwe/wordCount2.PSP2.1PSP阶段预估耗时实际耗时(分钟)(分钟)Planning计划 10 10·Estimate·估计这个任务需要多少时间 20 10Development开发 40 60·Analysis·需求分析(包括学习新技术)  查看详情