1. 程式人生 > >軟件工程個人項目二:WC項目

軟件工程個人項目二:WC項目

utf ror pen red pac [] csharp platform -a

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

軟件工程個人項目二:WC項目