二维数组的最大子数组之和,并对其进行单元测试/代码覆盖率
一、程序分析
(1).要求
- 以指定格式的文本文件形式输入数组。
- 数组由一维变为二维。
- 使用git常用命令将作业签入代码版本控制平台。
- 给出单元测试/代码覆盖率的最终覆盖率的报告
(2).给出分析
- 先编写一个读取文本文档的算法的函数,分别取出每一行的内容,再通过“ ”分隔符取出每一个元素存在二维数组里面
- 求出每一个子数组的和
- 再将每一个子数组存在一个数组里面
- 最后在用上一篇博客的算法求出,数组里面的最大子元素的和
- 最后在用单元测试对其进行测试
- 再测试代码的覆盖率
二、编写代码
这里我用的是idea集成开发工具,使用的java编程语言编写的这个代码
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TwoDimensionalArray_sonmax { public static void main(String[] args) throws IOException { Integer[][] c = new Integer[100][]; c = ReadFile("C:\\Users\\GK丶taptap\\Desktop\\input.txt"); for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { System.out.print(c[i][j] + " "); } System.out.println(); } System.out.println("max:" + GetMaxSUM(c)); } public static Integer[][] ReadFile(String str) { FileReader file = null; try { file = new FileReader(str); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("文件测试数据如下:"); BufferedReader br = new BufferedReader(file);// 读取文件 try { String line = br.readLine();// 读取一行数据 int lenx = Integer.parseInt(line);// 将数据转化为int类型 line = br.readLine();// 读取一行数据 int leny = Integer.parseInt(line);// 将数据转化为int类型 // System.out.println(lines); String[] sp = null; String[][] c = new String[lenx][leny]; Integer[][] cc = new Integer[lenx][leny]; int count = 0; while ((line = br.readLine()) != null) {// 按行读取 sp = line.split(" ");// 按空格进行分割 for (int i = 0; i < sp.length; i++) { c[count][i] = sp[i]; } count++; } for (int i = 0; i < lenx; i++) { for (int j = 0; j < leny; j++) { cc[i][j] = Integer.parseInt(c[i][j]); } } return cc; } catch (IOException e) { e.printStackTrace(); } return null; } public static int GetMaxSUM(Integer[][] a) { int n = a.length; int m = a[0].length; // 分块 Integer[] Max = new Integer[100]; Integer[] Begin = new Integer[100]; Integer[] End = new Integer[100]; Integer[] b = new Integer[100]; for (int i = 0; i < n; i++) { // 按行分组 for (int j = 0; j < m; j++) { b[j] = a[i][j]; } MaxIntArray(b, Max, Begin, End, m, i); } int max = Max[0]; for (int i = 0; i < n - 1; i++) { if ((Begin[i] <= End[i + 1] && Begin[i] >= Begin[i + 1]) || (End[i] <= End[i + 1] && End[i] >= Begin[i + 1])) { max = Max[i + 1] + max; } else { // 如果不能直接连通,判断代价是否合适 if (Begin[i] > End[i + 1]) { int t = Begin[i] - End[i + 1]; int s = Begin[i]; int temp = 0; for (int k = 0; k < t; k++) { temp += a[i + 1][s - k]; } if (temp + Max[i + 1] > 0) max = temp + Max[i + 1]; } if (End[i] < Begin[i + 1]) { int t = Begin[i + 1] - End[i]; int s = End[i]; int temp = 0; for (int k = 0; k < t; k++) { temp += a[i + 1][s + k]; } if (temp + Max[i + 1] > 0) max = temp + Max[i + 1]; } } } return max; } public static void MaxIntArray(Integer[] a, Integer[] max, Integer[] begin, Integer[] end, int n, int index) { Integer[] Max = new Integer[100]; Max[0] = 0; int i = 0;// 数组下标 int j = 0;// 最大值数组下标 int temp = 0;// 中间变量 // 记录子数组的起始位置和末位 // Integer[] Bg=new Integer[100]; Integer[] Bg = { -1, -1, -1, -1, -1 }; Integer[] Ed = new Integer[100]; while (i < n) { if (temp + a[i] >= Max[j]) { temp = temp + a[i]; Max[j] = temp; if (Bg[j] == -1) Bg[j] = i; Ed[j] = i; i++; } else if (temp + a[i] < Max[j] && temp + a[i] > 0) { temp = temp + a[i]; i++; } else if (temp + a[i] <= 0) { i++; j++; Max[j] = 0; temp = 0; } } max[index] = Max[0]; int q = 0; for (int k = 0; k <= j; k++) { if (Max[k] > max[index]) { max[index] = Max[k]; q = k; } } begin[index] = Bg[q]; end[index] = Ed[q]; } }
三、对程序进行单元测试
具体的测试过程这里就不详细写了,可参见我的上一篇博客:https://www.cnblogs.com/z-m-t/p/14530201.html
(1).单元测试代码如下:
import org.junit.Test;
public class Test2 {
Funcation1 funcation1 = new Funcation1();//创建函数对象
@Test
public void test1() {
String str="C:\\Users\\GK丶taptap\\Desktop\\input.txt";
Integer[][] c= funcation1.ReadFile(str);
System.out.println("MaxSUM:"+ funcation1.GetMaxSUM(c));
}
@Test
public void test2() {
String str="C:\\Users\\GK丶taptap\\Desktop\\input1.txt";
Integer[][] c= funcation1.ReadFile(str);
System.out.println("MaxSUM:"+ funcation1.GetMaxSUM(c));
}
@Test
public void test3() {
String str="C:\\Users\\GK丶taptap\\Desktop\\input2.txt";
Integer[][] c= funcation1.ReadFile(str);
System.out.println("MaxSUM:"+ funcation1.GetMaxSUM(c));
}
}
(2).单元测试结果

四、测试覆盖率
(1).使用IDEA,测试覆盖率很简单,测试过程如下
- 点击一下Run'Test2'with Coverage就可以进行测试代码覆盖率了

(2).测试结果


由上图可知覆盖率为77%
(3).被测试的代码
- 绿色部分为已测代码
- 红色部分为没有测的代码


浙公网安备 33010602011771号