軟件工程——第三次做業

[toc]ios

本項目發佈在在騰訊雲代碼託管平臺(即Coding.net)git

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. 設計程序

使用C ++語言編寫所須要的程序測試

首先編寫主程序Max SubSequence Sum.cppthis

// Max SubSequence Sum.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//

#include "pch.h"
#include <iostream>
#include "sumlib.h"

using namespace std;

int main()
{
	const int aArray[] = { -2,-11,-4,-13,-5,-2 };
	int iLength = sizeof(aArray) / sizeof(aArray[0]);
	int maxSSSum = MaxSubSequenceSum(aArray, iLength);
	cout << "The Max Sub-sequence summary of this array is: " << maxSSSum << endl;
	system("pause");
	return 0;
}

再編寫頭文件sumlib.hspa

#pragma once
#include<iostream>
using namespace std;

int MaxSubSequenceSum(const int *array, int cntLength)
{
	if (array == NULL || cntLength <= 0)
	{
		return 0;
	}
	int tempSum, maxSum;
	maxSum = 0;
	for (int cntI = 0; cntI < cntLength; cntI++)
	{
		tempSum = 0;
		for (int cntJ = cntI; cntJ < cntLength; cntJ++)
		{
			tempSum += array[cntJ];
			if (tempSum > maxSum)
			{
				maxSum = tempSum;
			}
		}
	}
	return maxSum;
}

程序運行的結果如圖所示 avator.net

#3. 選擇覆蓋標準並設計測試樣例 咱們選擇斷定/條件覆蓋來設計測試樣例設計

所設計的程序中求最大子段和的函數code

int MaxSbuSequence(const int *array, int cntLength)

的流程圖爲 avator

根據流程圖,咱們能夠看出,若想知足斷定/條件覆蓋,則需知足下列條件

  • 數組爲空 或 長度≤0 ?
    • 數組爲空,長度 ≤ 0,T1斷定爲True
    • 數組爲空,長度 > 0, T1斷定爲True
    • 數組不爲空,長度 ≤ 0, T1斷定爲True
    • 數組不爲空,長度 > 0, T1斷定爲False
  • tempSum > maxSum ?
    • tempSum > maxSum,T2斷定爲True
    • tempSum < maxSum,T2斷定爲Flase
  • cntJ < cntLength ?
    • cntJ < cntLength,T3斷定爲True
    • cntJ > cntLength,T3斷定爲Flase
  • cntI < cntLength ?
    • cntI < cntLength,T4斷定爲True
    • cntI > cntLength,T4斷定爲Flase

斷定3和斷定4爲循環控制過程當中的判斷,每次執行程序均會經歷T = true和T = fasle,所以無需對這兩個斷定設計測試樣例。

根據以上條件咱們能夠得出以下的測試樣例:

<table border=0 cellpadding=0 cellspacing=0 width=959 style='border-collapse: collapse;table-layout:fixed;width:719pt'> <col class=xl66 width=64 span=3 style='width:48pt'> <col class=xl66 width=244 style='mso-width-source:userset;mso-width-alt:8675; width:183pt'> <col class=xl66 width=267 style='mso-width-source:userset;mso-width-alt:9500; width:200pt'> <col class=xl66 width=128 style='mso-width-source:userset;mso-width-alt:4551; width:96pt'> <col class=xl66 width=64 span=2 style='width:48pt'> <tr height=19 style='height:14.4pt'> <td rowspan=2 height=38 class=xl67 width=64 style='height:28.8pt;width:48pt'><font class="font9">序號</font></td> <td colspan=2 class=xl67 width=128 style='border-left:none;width:96pt'>T1</td> <td class=xl67 width=244 style='border-left:none;width:183pt'>T2</td> <td rowspan=2 class=xl67 width=267 style='width:200pt'><font class="font9">路徑</font></td> <td colspan=2 class=xl67 width=192 style='border-left:none;width:144pt'><font class="font9">樣例</font></td> <td rowspan=2 class=xl67 width=64 style='width:48pt'><font class="font9">指望值</font></td> </tr> <tr height=19 style='height:14.4pt'> <td height=19 class=xl67 style='height:14.4pt;border-top:none;border-left: none'><font class="font9">數組爲空</font></td> <td class=xl67 style='border-top:none;border-left:none'><font class="font9">長度≤</font><font class="font8">0</font></td> <td class=xl67 style='border-top:none;border-left:none'>tempSum&gt;maxSum</td> <td class=xl67 style='border-top:none;border-left:none'><font class="font9">數組</font></td> <td class=xl67 style='border-top:none;border-left:none'><font class="font9">長度</font></td> </tr> <tr height=19 style='height:14.4pt'> <td height=19 class=xl65 style='height:14.4pt;border-top:none'>1</td> <td class=xl65 style='border-top:none;border-left:none'>T</td> <td class=xl65 style='border-top:none;border-left:none'>T</td> <td class=xl65 style='border-top:none;border-left:none'>T1<font class="font6">爲</font><font class="font7">T</font><font class="font6">,則路徑不通過</font><font class="font7">T2</font></td> <td class=xl65 style='border-top:none;border-left:none'>START<font class="font6">→</font><font class="font7">B</font><font class="font6">→</font><font class="font7">END</font></td> <td class=xl65 style='border-top:none;border-left:none'>{ }</td> <td class=xl65 style='border-top:none;border-left:none'>0</td> <td class=xl65 style='border-top:none;border-left:none'>0</td> </tr> <tr height=18 style='height:13.8pt'> <td height=18 class=xl65 style='height:13.8pt;border-top:none'>2</td> <td class=xl65 style='border-top:none;border-left:none'>F</td> <td class=xl65 style='border-top:none;border-left:none'>F</td> <td class=xl65 style='border-top:none;border-left:none'>F</td> <td class=xl65 style='border-top:none;border-left:none'>START→A→C<font class="font10">→</font><font class="font7">D</font><font class="font10">→</font><font class="font7">……→END</font></td> <td class=xl65 style='border-top:none;border-left:none'>{ -5,-6,-1,-3 }</td> <td class=xl65 style='border-top:none;border-left:none'>4</td> <td class=xl65 style='border-top:none;border-left:none'>0</td> </tr> <tr height=18 style='height:13.8pt'> <td height=18 class=xl65 style='height:13.8pt;border-top:none'>3</td> <td class=xl65 style='border-top:none;border-left:none'>F</td> <td class=xl65 style='border-top:none;border-left:none'>F</td> <td class=xl65 style='border-top:none;border-left:none'>T</td> <td class=xl65 style='border-top:none;border-left:none'>START→A<font class="font10">→</font><font class="font7">D</font><font class="font10">→</font><font class="font7">……</font><font class="font10">→</font><font class="font7">END</font></td> <td class=xl65 style='border-top:none;border-left:none'>{<span style='mso-spacerun:yes'>&nbsp; </span>3,7,2,27,3 }</td> <td class=xl65 style='border-top:none;border-left:none'>5</td> <td class=xl65 style='border-top:none;border-left:none'>42</td> </tr> <tr height=19 style='height:14.4pt'> <td height=19 class=xl65 style='height:14.4pt;border-top:none'>4</td> <td class=xl65 style='border-top:none;border-left:none'>F</td> <td class=xl65 style='border-top:none;border-left:none'>F</td> <td class=xl65 style='border-top:none;border-left:none'><font class="font6">在循環過程當中經歷了</font><font class="font7">T</font><font class="font6">和</font><font class="font7">F</font><font class="font6">兩種斷定</font></td> <td class=xl65 style='border-top:none;border-left:none'>START→A<font class="font10">→</font><font class="font7">C</font><font class="font10">→</font><font class="font7">D</font><font class="font10">→</font><font class="font7">……</font><font class="font10">→</font><font class="font7">A</font><font class="font10">→</font><font class="font7">D</font><font class="font10">→</font><font class="font7">END</font></td> <td class=xl65 style='border-top:none;border-left:none'>{ -2,11,-4,13,-5,-2 }</td> <td class=xl65 style='border-top:none;border-left:none'>6</td> <td class=xl65 style='border-top:none;border-left:none'>20</td> </tr> <![if supportMisalignedColumns]> <tr height=0 style='display:none'> <td width=64 style='width:48pt'></td> <td width=64 style='width:48pt'></td> <td width=64 style='width:48pt'></td> <td width=244 style='width:183pt'></td> <td width=267 style='width:200pt'></td> <td width=128 style='width:96pt'></td> <td width=64 style='width:48pt'></td> <td width=64 style='width:48pt'></td> </tr> <![endif]> </table>

4. 測試和結果

設計測試程序的代碼

#include "stdafx.h"
#include "CppUnitTest.h"
#include "..\Max SubSequence Sum\sumlib.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{		
	TEST_CLASS(UnitTest1)
	{
	public:
		
		TEST_METHOD(TestMethod1)
		{
			const int *aArray = {};
			int iLength = sizeof(aArray) / sizeof(aArray[0]);
			Assert::AreEqual(MaxSubSequenceSum(aArray, iLength), 0);
		}

		TEST_METHOD(TestMethod2)
		{
			const int aArray[] = { -5,-6,-1,-3 };
			int iLength = sizeof(aArray) / sizeof(aArray[0]);
			Assert::AreEqual(MaxSubSequenceSum(aArray, iLength), 0);
		}

		TEST_METHOD(TestMethod3)
		{
			const int aArray[] = { 3,7,2,27,3 };
			int iLength = sizeof(aArray) / sizeof(aArray[0]);
			Assert::AreEqual(MaxSubSequenceSum(aArray, iLength), 42);
		}

		TEST_METHOD(TestMethod4)
		{
			const int aArray[] = { -2,11,-4,13,-5,-2 };
			int iLength = sizeof(aArray) / sizeof(aArray[0]);
			Assert::AreEqual(MaxSubSequenceSum(aArray, iLength), 20);
		}

	};
}

測試結果如圖所示 avator

相關文章
相關標籤/搜索