软件工程个人项目二:WC项目

1. 代码来源:

   博客园http://www.cnblogs.com/duasonir/p/5297338.html

2. Platform:Eclipse

    Language:Java

3. Bug:暂时还没有bug

4. Function improvement:所找的代码的各个扩展已经很全面,主函数用string字符串接受用户输入,并分解成参数数组和文件地址。编写BaseCount()函数实现对文件的读操作,逐行统计统计字符数,并记录行数,同时使用StringBuffer类记录文件中所有的信息。最后一起统计词数,避免在逐行计词时,把标点计为一词。编写Response()函数根据用户输入的参数输出信息,这里程序不管用户输入的参数是什么,都在读取文件的时候把所有信息都记录下来并保存,用户输入的参数里有什么输出什么。

5. Implementation:可实现

6.代码如下:

package cWordCount;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class wcXiangMu {
	private static String shuru = "";
	 private static String[] strLong;
	 private static String File;
	 private static int linecount = 0;
	 private static int wordcount = 0;
	 private static int charcount = 0;
	 private static int nullLinecount = 0;
	 private static int codeLinecount = 0;
	 private static int noteLinecount = 0;
	
	public static void main(String[] args) {
		while(true){
			System.out.print("wc.exe ");
			 Scanner s = new Scanner(System.in);
			 shuru = s.nextLine();
			 String[] arrMessSplit = shuru.split(" ");
			 int iMessLength = arrMessSplit.length;
			 strLong = new String[iMessLength - 1];
			 File = arrMessSplit[iMessLength - 1];
			 CountNumber();
			 for (int i = 0; i < strLong.length; i++) {
				 if (arrMessSplit[i].equals("-w")){
					 System.out.println("单词数:"+wordcount);
				 }
				 if (arrMessSplit[i].equals("-l")){
					 System.out.println("文件总行数:"+linecount);
				 }
				 if (arrMessSplit[i].equals("-c")){
					 System.out.println("字符数:"+charcount);
				 }
				 if (arrMessSplit[i].equals("-a")){
					 System.out.println("空行数:"+nullLinecount);
				 System.out.println("代码行数:"+codeLinecount);
				 System.out.println("注释行数:"+noteLinecount);
				 }
			 }
		}
	}
	private static void CountNumber() {
        linecount = 0;
        wordcount = 0;
        charcount = 0;
        nullLinecount = 0;
        codeLinecount = 0;
        noteLinecount = 0;
        File file = new File(File);
        if (file.exists()) {
            try {
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
                BufferedReader br = new BufferedReader(isr);
                String line = "";
                StringBuffer sb = new StringBuffer();
                while ((line = br.readLine()) != null) {
                    linecount++;
                    sb.append(line);
                    charcount += line.length();
                    line = line.trim();
                   
                    if (line == "" || line.length() <= 1) {
                        nullLinecount++;
                        continue;
                    }
                    
                    int a = line.indexOf("/");
                    int b = line.substring(a + 1).indexOf("/");
                    if (b == 0) {
                        noteLinecount++;
                        continue;
                    }
                    
                    codeLinecount++;
                }
                wordcount = sb.toString().split("\\s+").length;
                br.close();
                isr.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("path error");
        }
    }
}

 

7. Github:https://github.com/I-December/WC-project

转载于:https://www.cnblogs.com/Z--Y/p/7590463.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值