计算与软件工程作业三

计算与软件工程作业三

作业要求 https://edu.cnblogs.com/campus/jssf/infor_computation17-31/homework/10454
课程目标 深刻理解软件工程,提高编程能力,以及熟悉各个软件的操作能力
参考文献 https://www.cnblogs.com/SivilTaram/p/software_pretraining_cpp.html https://blog.csdn.net/lovehaihong5401/article/details/77608959?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task https://www.cnblogs.com/SivilTaram/p/software_pretraining_cpp.html
此作业在哪个具体方面帮我实现目标 掌握如何设计单元测试、运行单元测试
作业正文 https://www.cnblogs.com/zxy123456/p/12449427.html

要求:数组中最大子数组的和
用类/函数来实现
需求:希望返回 3 种信息
最大子数组的和
最大子数组开始的下标
最大子数组结束的下标
从文本文件中读输入的数据,熟悉文件操作, 文件有两种数据
第一个数字:这次测试中有多少个数据, 数字后面是冒号。
后续数字: 每个数据的值,用逗号隔开
比如
文件内容:
17: -32, -10, 33, -23, 32, -12, 41, -12, 1, 3, 5, -98, 70, -21, 10, -9, 61
输出
sum = 71

程序代码

//FMax.cpp
#include<iostream>
#include<cstdlib>
using namespace std;
int getmax(int array[],int length)
{
    int sum = 0;    
    int max = 0;   
    int startIndex = 0; 
    int endIndex = 0;   
    int newStartIndex = 0;  
    for (int i = 0; i < length; i++)    //遍历整个数组
    {
        if (max < 0)  
        {
            max = array[i];    
            newStartIndex = i;  
        }
        else
        {
            max += array[i];   
        }
        if (sum < max) 
        {
            sum = max; 
            startIndex = newStartIndex;
            endIndex = i;  
        }
    }
    return max;
}

int getstartIndex(int array[],int length)
{
    int sum = 0;    
    int max = 0;   
    int startIndex = 0; 
    int endIndex = 0;   
    int newStartIndex = 0;  
    for (int i = 0; i < length; i++)    
    {
        if (max < 0)   
        {
            max = array[i];    
            newStartIndex = i;  
        }
        else
        {
            max += array[i];   
}
        if (sum < max) 
        {
            sum = max; 
            startIndex = newStartIndex; 
            endIndex = i;   
        }
    }
    return startIndex;
}
int getendIndex(int array[],int length)
{
    int sum = 0;    
    int max = 0;   
    int startIndex = 0; 
    int endIndex = 0;   
    int newStartIndex = 0;  
    for (int i = 0; i < length; i++)    
    {
        if (max < 0)   
        {
            max = array[i];    
            newStartIndex = i;  
        }
        else
        {
            max += array[i];   
        }
        if (sum < max) 
        {
            sum = max; 
            startIndex = newStartIndex; 
            endIndex = i;   
        }
    }
    return endIndex;
}

int main()
{
    int length,i=0;
    cout<<"输入数组个数:";
    cin>>length;
    cout<<"输入数组:";
    int array[1000]={0};
    for(i=0;i<length;i++)
    {
        cin>>array[i];
    }
    cout<<"最大子数组的和:"<<getmax(array,length)<<endl;
    cout<<"最大子数组开始的下标:"<<getstartIndex(array,length)<<endl;
    cout<<"最大子数组结束的下标:"<<getendIndex(array,length)<<endl;
    system("pause");
    return 0;
}

运行截图

单元测试代码

//unittest1.cpp
#include "stdafx.h"
#include "CppUnitTest.h"
#include "Fmax.cpp"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{		
	TEST_CLASS(UnitTest1)
	{
	public:
		
		TEST_METHOD(TestMethod1)
		{
			int a[]={3,4,2};
			Assert::AreEqual(9,getmax(a,3));
			// TODO: 在此输入测试代码
		}

	};
}

测试截图

小结
进行单元测试时,单元测试文件unittest1.cpp无法打开文件的问题,之后更改他的属性VC++包含目录解决。

目前对于软件的认识尚浅,很多编程语言运用并不熟练,希望尽人事。相对来说,个人认为Java相对c语言简单。希望在今后的学习中,能够尽量补缺自己的短板。增加就业优势。
码云链接:https://gitee.com/if_evening/fmax2

原文地址:https://www.cnblogs.com/zxy123456/p/12449427.html