软件工程-个人项目作业

    • 个人项目作业

      1.Github项目地址

      https://github.com/chenchuangzhi/untitled

    • 2.实现程序前,模块开发预计时间

      PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
      Planning 计划 30
      Estimate 估计这个任务需要多少时间 30
      Development 开发 690
      Analysis 需求分析(包括学习新技术) 180
      Design Spec 生成设计文档 30
      Design Review 设计复审(和同事审核设计文档) 0
      Coding Standard 代码规范(为目前的开发置顶合适的规范) 30
      Design 具体设计 90
      Coding 具体编码 180
      Code Review 代码复审 90
      Test 测试(自我测试,修改代码,提交修改) 90
      Reporting 报告 150
      Test Report 测试报告 60
      Size Measurement 计算工作量 60
      Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 30
      Total 合计 870
    • 3.解题思路

      编程语言的选择

      使用java语言开发

      需要用到的新知识

      java的图形界面,关于java  string字符串的各个api,正则表达式,文件读取等知识。

      结构的分析

      为了操作的方便,运行代码后会出现一个图形界面。图形界面共有五个按钮,使用时根据自己的需要点击相应的按钮实现即可。

      遇到的困难

      实现各种功能时,因为要考虑到统计的各个情况,所以写出的功能多次统计错误。之后充分考虑各种情况后,解决了较多的统计错误。因为储备的知识不足,如何用图形界面实现功能,也是个问题,为实现这些功能,初步学习了swing与时间处理中的鼠标事件。如何读取文件等都花了较多的时间。

    • 4.设计实现过程

      设计实现主要有一个主函数,里面构造了图形界面,并设置了五个有鼠标事件的按钮。之后有四个函数,分别为-c,-l,-w,-a。在wc.主程序操作分别可实现这些功能。

    • 5.代码说明

      1. wc主程序。
         public static void main(String[] args) {
                //设置主界面
                JFrame mainFrame= new JFrame("WC.exe");
                mainFrame.setSize(400,400);
                mainFrame.setLayout(new GridLayout(3, 1));
                mainFrame.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent windowEvent){
                        System.exit(0);
                    }
                });
                //设置按钮
                JPanel controlPanel=new JPanel();
                JButton zifu= new JButton("wc.exe -c(字符数)");
        
                JButton danci = new JButton("wc.exe -w(单词数) ");
                JButton hangshu = new JButton("wc.exe -l(行数)");
                JButton a = new JButton("-a(代码行,注释行,空行)");
                JButton wenjian= new JButton("选取文件(不使用该功能则默认统计项目内file.c)");
                controlPanel.add(zifu);
                controlPanel.add(hangshu);
                controlPanel.add(danci);
                controlPanel.add(a);
                controlPanel.add(wenjian);
                //将按钮放入主界面
                mainFrame.add(controlPanel);
                mainFrame.setVisible(true);
        
                //统计文件的行数
                hangshu.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        int  k=line(lujing);
                        JOptionPane.showMessageDialog(null, "文件里面的行数为"+k);
        
                    }});
        
                //统计文件的字符数
                zifu.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        int  k=zifu(lujing);
                        JOptionPane.showMessageDialog(null, "文件里面的字符数为"+k+"(包括空格不包括回车)");
        
                    }});
        
                //统计文件的单词数
                danci.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        int  k=word(lujing);
                        JOptionPane.showMessageDialog(null, "文件里面的单词数为"+k);
        
                    }});
        
         //统计文件里的空白行,注释行和代码行
                a.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        a(lujing);
        
        
                    }});
        
                wenjian.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser chooser = new JFileChooser(); //设置选择器
                        chooser.setMultiSelectionEnabled(true); //设为多选
                        int returnVal = chooser.showOpenDialog(wenjian); //是否打开文件选择框
                        System.out.println("returnVal="+returnVal);
                        if (returnVal == JFileChooser.APPROVE_OPTION) { //如果符合文件类型
        
                           lujing =  chooser.getSelectedFile().getAbsolutePath();
                        }
        
                    }});
        
            }
        
      2. -c(统计文件中的字符数)
        //统计文件的字符数
        private static int zifu(String filename) {
        
            File file=new File(filename);
            BufferedReader reader=null;
            int character=0;
            try{
                reader = new BufferedReader(new FileReader(file));
                String tempchar;
                while ((tempchar=reader.readLine()) != null) {
                    character +=  tempchar.length();
                }
        
                reader.close();
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, "不存在该文件");
            }
            return character;
        }
        

          

          3.-l(统计文件中的行数)

        //统计file文档的行数
            public static int line(String fileName) {
                File file = new File(fileName);
                int line = 0;
                BufferedReader reader = null;
                String tempString = null;
                try {
        
                    reader = new BufferedReader(new FileReader(file));
        
        
                    // 一次读入一行,直到读入null为文件结束
                    while ((tempString = reader.readLine()) != null) {
        
                        line++;
                    }
                    reader.close();
                } catch (IOException l) {
                    l.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e1) {
                        }
                    }
                }
                // 返回行数
                return line;
        
            }
        

        4.-w(统计文档的单词数) 

        //统计文件的单词数
            public static int word(String fileName) {
                File file = new File(fileName);
                int word = 0;
                BufferedReader reader = null;
        
                try {
        
                    reader = new BufferedReader(new FileReader(file));
        
                    Pattern Pa =Pattern.compile("[a-zA-Z]+\\b");//判定为单词的正则表达式条件
        
                    String w=null;
                    while ((w = reader.readLine()) != null) {
                        Matcher Ma =Pa.matcher(w);
                        while(Ma.find()) //当找到单词时,单词数+1
                            word++;
                    }
                    reader.close();
                } catch (IOException k) {
                    k.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e1) {
                        }
                    }
                }
                // 返回单词数
        
                return word;
            }
        

           5.-a(统计文档的代码行数,注释行数与空白行数)

        //-a扩展程序,统计代码行数,注释行数与空白行数)    
        public static void a(String fileName) {
                File file = new File(fileName);
                int blankcount = 0;   //空白行
                int notescount = 0;   //注释行
                int codecount = 0;    //代码行
                BufferedReader reader = null;
                String a = null;
                int state = 0;//判断是否有注释标记
                try {
        
                    reader = new BufferedReader(new FileReader(file));
        
        
                    // 一次读入一行,直到读入null为文件结束
                    while ((a = reader.readLine()) != null) {
                        a=remove(a,'{');
                        a=remove(a,'}');   //去除干扰判断的{和}符号
                        if(state == 0){        //注释没有标记时
        
                            if(a.trim().indexOf("//") == 0 || a.trim().indexOf("/*") == 0)
                                notescount++;
        
                            else if(a.trim().length() > 1) 	codecount++;
                            else blankcount++;
                            if(a.contains("/*"))   state = 1;  //如果有/*,则注释标记
        
                        }
                        else
                        {
        
                            if(a.contains("*/")) {    //取消注释标记
                                state = 0;
        
                            }
                            notescount++;
        
                        }
                    }
        
                    reader.close();
                } catch (IOException l) {
                    l.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e1) {
                        }
                    }
                }
                JOptionPane.showMessageDialog(null, "文件里面的空白行为"+blankcount+"\n代码行为"+codecount+"\n注释行为"+notescount);
        
            }
            //去除字符串中指定字符
            public static String remove(String sourceString, char chElemData) {
                String deleteString = "";
                for (int i = 0; i < sourceString.length(); i++) {
                    if (sourceString.charAt(i) != chElemData) {
                        deleteString += sourceString.charAt(i);
                    }
                }
                return deleteString;
            }
        

          

6.测试运行

程序实现功能

本程序实现了高级功能的支持通过图形界面选取文件;-c、-l、-w指令以及-a的全部指令,
支持各种文本文档文件,
还实现了直接点击实现相应功能

测试使用样例

#include <stdio.h>
int main()
    {   //hello  world

    printf(“ Hello,world!”);
    /*shdh
    sd
    */
    return0;
    }

测试过程

        1. 打开编译器,编译代码,弹出窗口
        2. 点击最下面按钮,选择要进行计数的文件。

        3. 如图为各功能实现结果,符合预计要求

          7.实现完程序后,实际花费时间

 

 
PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划 30  24
Estimate 估计这个任务需要多少时间 30  24
Development 开发 690  630
Analysis 需求分析(包括学习新技术) 180  150
Design Spec 生成设计文档 30 30 
Design Review 设计复审(和同事审核设计文档) 0  0
Coding Standard 代码规范(为目前的开发置顶合适的规范) 30  30
Design 具体设计 90  105
Coding 具体编码 180  150
Code Review 代码复审 90  75
Test 测试(自我测试,修改代码,提交修改) 90  90
Reporting 报告 150  120
Test Report 测试报告 60  45
Size Measurement 计算工作量 60  45
Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 30  30
Total 合计 870  774

8.小结

过去的开发都是直接写代码,而无任何开发规范,经过这次项目,使得我懂的了规划,同时也为了本次开发,学习了许多新的东西。

而且这次仍有许多不足,有许多功能未能开发完毕,自己做出的程序也不是特别好用,例如界面太难看,操作还不够方便等问题。

 
 

 

 

posted @ 2020-03-13 20:11  Edward爱德华  阅读(300)  评论(0编辑  收藏  举报