软件工程作业3
作者:互联网
1.题目概述
给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。当所给的整数均为负数时定义子段和为0,依此定义,所求的最优值为: Max{0,a[i]+a[i+1]+…+a[j]},1<=i<=j<=n例如,当(a[1],a[2],a[3],a[4],a[5],a[6])=(-2,11,-4,13,-5,-2)时,最大子段和为20。-- 引用自《百度百科》
2.具体要求
(1)请从上述两个题目中根据个人实力任选一题,要求写出可运行的完整代码提交至GitHub或者Coding.net系统中,并将代码地址附到博客内
(2)请从语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、条件组合覆盖五个覆盖标准中(条件组合覆盖难度较大,鼓励尝试,但请谨慎选择),任选一个标准设计测试用例
(3)请利用自动测试工具对程序进行测试
(4)请将程序运行结果和自动测试分析结果截图附到博客中
3.代码设计
(1)主程序
···#include <stdio.h>
#include <zdh.h>
int main()
{
int n, a[99],i,max;
scanf_s("%d", &n);
for(i=0;i<n;i++)
{
scanf_s("%d", &a[i]);
}
max = zdh(a, n);
printf("%d", max);
return 0;
}···
(2)头文件
···#pragma once
int zdh(int a[], int n)
{
int i = 0;
int thisSum = 0, max = 0;
for (i = 0; i < n; i++) {
thisSum += a[i];
if (thisSum > max) {
max = thisSum;
}
else if (thisSum <= 0) {
thisSum = 0;
}
}
return max;
}···
(3)程序流程图
4.测试设计
判定覆盖
由流程图可知,需要测试以下数据
thissum>sum
thissum<=sum
thissum>0
thissum<=0
使用单元测试代码测试程序,具体代码如下
#include "stdafx.h"
#include "CppUnitTest.h"
#include "..\3\zdh.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
extern int MaxSubseqSum4(int A[], int N);
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
int A[] = { 10,20,30,40 };
Assert::AreEqual(MaxSubseqSum4(A, 4),100);
}
TEST_METHOD(TestMethod2)
{
int A[] = { -10,-20,-30 };
Assert::AreEqual(MaxSubseqSum4(A, 3), 0);
}
TEST_METHOD(TestMethod3)
{
int A[] = { -10,-20,20,30,40,-20,30,-5};
Assert::AreEqual(MaxSubseqSum4(A, 8), 100);
}
TEST_METHOD(TestMethod4)
{
int *A = 0;
Assert::AreEqual(MaxSubseqSum4(A, 0), 0);
}
};
}
五、测试结果
标签:20,MaxSubseqSum4,int,max,作业,软件工程,thisSum,include 来源: https://www.cnblogs.com/xyz440229/p/10740359.html