第一次个人编程作业

第一次个人编程作业

一.github的链接

github

二.估计将在程序的各个模块的开发上耗费的时间

PSP2.1 Personal Software Process Stages 预估耗时(分钟)
Planning 计划 30
stimate 估计这个任务需要多少时间 15
Development 开发 500
Analysis 需求分析 (包括学习新技术) 300
Design Spec 生成设计文档 30
Design Review 设计复审 30
Coding Standard 代码规范 (为目前的开发制定合适的规范) 30
Design 具体设计 60
Coding 具体编码 240
Code Review 代码复审 45
Test 测试(自我测试,修改代码,提交修改) 180
Reporting 报告 100
Test Repor 测试报告 60
Size Measurement 计算工作量 20
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 50
合计 1690

三.计算模块接口的设计与实现过程

求出两个字符串中最长公共子串长度:

比如:str=acbcbcef,str2=abcbced,则str和str2的最长公共子串为bcbce,最长公共子串长度为5。

LCS算法思路:

  • 1、把两个字符串分别以行和列组成一个二维矩阵。
  • 2、比较二维矩阵中每个点对应行列字符中否相等,相等的话值设置为1,否则设置为0。
  • 3、通过查找出值为1的最长对角线就能找到最长公共子串。
针对于上面的两个字符串我们可以得到的二维矩阵如下:

从上图可以看到,str1和str2共有5个公共子串,但最长的公共子串长度为5。
为了进一步优化算法的效率,我们可以再计算某个二维矩阵的值的时候顺便计算出来当前最长的公共子串的长度,即某个二维矩阵元素的值由record[i][j]=1演变为record[i][j]=1 +record[i-1][j-1],这样就避免了后续查找对角线长度的操作了。修改后的二维矩阵如下:

代码如下

public class Simicalcu_2{
    public static void main(String[] args) throws IOException{
    	String str1="";
        File file1=new File(args[0]);
        FileInputStream in1= null;
        in1 = new FileInputStream(file1);
        // size  为字串的长度 ,这里一次性读完
        int size1=in1.available();
        byte[] buffer1=new byte[size1];
        in1.read(buffer1);
        in1.close();
        str1=new String(buffer1,"utf8");
        String str2="";
        File file2=new File(args[1]);
        FileInputStream in2=new FileInputStream(file2);
        // size  为字串的长度 ,这里一次性读完
        int size2=in2.available();
        byte[] buffer2=new byte[size2];
        in2.read(buffer2);
        in2.close();
        str2=new String(buffer2,"utf8");
        DecimalFormat df= new DecimalFormat("######0.00");
        String result = df.format(Computeclass.SimilarDegree(str1, str2));
        System.out.print("重复率:"+result);        
        String str3 = args[2];
        FileOutputStream  ou = new FileOutputStream(str3);        
        ou.write(result.getBytes());
        ou.close();       
    }
}
public class Computeclass {
    /*
     * 计算相似度
     * */
    public static double SimilarDegree(String strA, String strB){
        String newStrA = removeSign(strA);
        String newStrB = removeSign(strB);
        //用较大的字符串长度作为分母,相似子串作为分子计算出字串相似度
        int temp = Math.max(newStrA.length(), newStrB.length());
        int temp2 = longestCommonSubstring(newStrA, newStrB);
        return temp2 * 1.0 / temp;
    }
    /*
     * 将字符串的所有数据依次写成一行
     * */
    public static String removeSign(String str) {
        StringBuffer sb = new StringBuffer();
        //遍历字符串str,如果是汉字数字或字母,则追加到ab上面
        for (char item : str.toCharArray())
            if (charReg(item)){
                sb.append(item);
            }
        return sb.toString();
    }
    /*
     * 判断字符是否为汉字,数字和字母,
     * 因为对符号进行相似度比较没有实际意义,故符号不加入考虑范围。
     * */
    public static boolean charReg(char charValue) {
        return (charValue >= 0x4E00 && charValue <= 0X9FA5) || (charValue >= 'a' && charValue <= 'z')
                || (charValue >= 'A' && charValue <= 'Z')  || (charValue >= '0' && charValue <= '9');
    }
    /*
     * 求公共子串,采用动态规划算法。
     * 其不要求所求得的字符在所给的字符串中是连续的。
     *
     * */
    public static int longestCommonSubstring(String str1, String str2) {
        char[] chars_strA = strA.toCharArray();
        char[] chars_strB = strB.toCharArray(); 
        int m = chars_strA.length;   
        int n = chars_strB.length;         
        /*
         * 初始化矩阵数据,matrix[0][0]的值为0,
         * 如果字符数组chars_strA和chars_strB的对应位相同,则matrix[i][j]的值为左上角的值加1,
         * 否则,matrix[i][j]的值等于左上方最近两个位置的较大值,
         * 矩阵中其余各点的值为0.
        */
        int[][] matrix = new int[m + 1][n + 1];   
        for (int i = 1; i <= m; i++) {  
            for (int j = 1; j <= n; j++) {    
                if (chars_strA[i - 1] == chars_strB[j - 1])   
                    matrix[i][j] = matrix[i - 1][j - 1] + 1;    
                else   
                    matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]);   
            }   
        }
        /*
         * 矩阵中,如果matrix[m][n]的值不等于matrix[m-1][n]的值也不等于matrix[m][n-1]的值,
         * 则matrix[m][n]对应的字符为相似字符元,并将其存入result数组中。
         * 
         */
        char[] result = new char[matrix[m][n]];    
        int currentIndex = result.length - 1;   
        while (matrix[m][n] != 0) {   
            if (matrix[n] == matrix[n - 1])  
                n--;   
            else if (matrix[m][n] == matrix[m - 1][n])    
                m--;   
            else {   
                result[currentIndex] = chars_strA[m - 1];   
                currentIndex--;  
                n--;   
                m--;  
            }  
        }    
       return new String(result);  
    }
}

输出答案⬇

四.计算模块接口部分的性能改进



有九百多M。。。

去搜索了动态规划的性能优化,改了一部分代码

 public static int longestCommonSubstring(String str1, String str2) {
        int length1 = (short) str1.length();
        int length2 = (short) str2.length();
        int[][] result = new int[2][length2 + 1];   //滚动数组,节省空间只依赖于前面的两个解
        for (int i = 1; i <= length1; i++) {
            for (int j = 1; j <= length2; j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1))
                    result[i % 2][j] = (short) (result[(i - 1) % 2][j - 1] + 1);
                else
                    result[i % 2][j] = result[(i - 1) % 2][j] > result[i % 2][j - 1] ? result[(i - 1) % 2][j] : result[i % 2][j - 1];
            }
        }
        return result[length1 % 2][length2];
    }

占用内存一下子就小了!

五.计算模块部分单元测试展示

####测试了给出的各种情况,还有两个文件相同时的情况
public class JUnitTest {

	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void testSimilar_same() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig.txt"));
	}
	@Test
	public void testSimilar_add() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_add.txt"));
	}
	@Test
	public void testSimilar_del() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_del.txt"));
	}
	@Test
	public void testSimilar_dis_1() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_dis_1.txt"));
	}
	@Test
	public void testSimilar_dis_3() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_dis_3.txt"));
	}
	@Test
	public void testSimilar_dis_7() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_dis_7.txt"));
	}
	@Test
	public void testSimilar_dis_10() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_dis_10.txt"));
	}
	@Test
	public void testSimila_dis_15() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_dis_15.txt"));
	}
	@Test
	public void testSimilar_mix() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_mix.txt"));
	}
	@Test
	public void testSimilar_rep() throws IOException {
		System.out.print(Simicalcu_2.similar("E:\\rjgc\\orig.txt","E:\\rjgc\\orig_0.8_rep.txt"));
	}
}

正确了⬇

覆盖率⬇

六.计算模块部分异常处理说明

如果输入为空,报错“错误!输入为空”
if(str1==null||str2==null)
{
	try{
		throw new emptyException("错误!输入为空");
	}catch(emptyException e){
		e.printStackTrace();
	}
}
public class emptyException extends Exception{
	public emptyException(){    
	}
	public emptyException(String message){
	    super(message);
	}
	public emptyException(Throwable cause){
	    super(cause);
	}
}

七.PSP表格记录下在程序的各个模块上估计实际花费的时间

PSP2.1 Personal Software Process Stages 实际耗时(分钟)
Planning 计划 30
stimate 估计这个任务需要多少时间 20
Development 开发 480
Analysis 需求分析 (包括学习新技术) 330
Design Spec 生成设计文档 30
Design Review 设计复审 30
Coding Standard 代码规范 (为目前的开发制定合适的规范) 30
Design 具体设计 50
Coding 具体编码 300
Code Review 代码复审 40
Test 测试(自我测试,修改代码,提交修改) 120
Reporting 报告 90
Test Repor 测试报告 60
Size Measurement 计算工作量 30
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 60
合计 1700

八.心得

通过这次作业,我受益匪浅。在作业中有许多要求都是我不曾接触到的,所以我查阅了许多资料,也学习了许多,像自定义异常,单元测试,git的使用,打包jar包,命令行参数args等等(头秃)。以前的学习比较注重理论,所以动手能力不太强,但在不懈的努力下还是完成了,十分有成就感。以后要多多学习理论与实践相结合。

posted @ 2020-09-17 17:26  呱506  阅读(219)  评论(0)    收藏  举报