我希望你可以帮助一点头脑.
我写了一个模板类来计算标准偏差:
template <class T>
double GetStandardDeviation(T* valueArray, int populationSize)
{
double average;
T cumulativeValue = 0;
double cumulativeSquaredDeviation = 0;
// calculate average
for (int i = 0; i < populationSize; i++)
{
cumulativeValue += valueArray[i];
}
average = (double)cumulativeValue / (double)populationSize;
// calculate S.D.
for (int i = 0; i < populationSize; i++)
{
double difference = average - (double)valueArray[i];
double squaredDifference = difference * difference;
cumulativeSquaredDeviation += squaredDifference;
}
return cumulativeSquaredDeviation / (double)populationSize;
}
Run Code Online (Sandbox Code Playgroud)
这似乎正在做正确的事情,除了它将结果返回到只有5位小数.任何人都可以提出这个理由吗?我很难过!
我已经写了一个TopShelf服务/控制台应用程序,它似乎按预期运行,除了我希望它在启动时运行一次,然后禁用自己直到下次启动/重启.
我希望这会奏效:
class MyServiceClass
{
public void Start()
{
// do the things that need doing
this.Stop();
}
public void Stop()
{
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,大概是因为this.Stop()命令用于清理,而不是导致服务停止.
我的Program.cs看起来像这样:
// Uses Topshelf: http://topshelf-project.com/
// Guided by: http://www.ordina.nl/nl-nl/blogs/2013/maart/building-windows-services-with-c-and-topshelf/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Topshelf;
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(hostConfigurator =>
{
hostConfigurator.Service<MyServiceClass>(serviceConfigurator =>
{
serviceConfigurator.ConstructUsing(() => new MyServiceClass());
serviceConfigurator.WhenStarted(myServiceClass => myServiceClass.Start());
serviceConfigurator.WhenStopped(myServiceClass => myServiceClass.Stop());
});
hostConfigurator.RunAsLocalSystem();
hostConfigurator.SetDisplayName("MyService");
hostConfigurator.SetDescription("Does stuff.");
hostConfigurator.SetServiceName("MyService"); …Run Code Online (Sandbox Code Playgroud) 我是第一次尝试用C++进行单元测试,多年来我没有使用过C++(目前我主要是C#编码器).看起来我正在做一个正确的猪耳朵 - 我希望有人能引导我回到正义的道路上.我刚开始在这里开始,并且真的希望使用最佳实践来实现这些测试,所以欢迎任何和所有评论,即使我目前最关心的是我的链接器错误.
所以,我有一个整体解决方案"Technorabble",子项目"CalibrationTool"和"CalibrationToolUnitTests".
CalibrationTool有一个MathUtils.h文件:
#ifndef __math_utils__
#define __math_utils__
#include "stdafx.h"
#include <vector>
namespace Technorabble
{
namespace CalibrationTool
{
double GetDoubleVectorAverage(std::vector<double> v)
{
double cumulativeValue = 0;
for(std::vector<double>::iterator iter = v.begin(); iter != v.end(); ++iter)
{
cumulativeValue += *iter;
}
return cumulativeValue / v.size();
}
}; // end namespace CalibrationTool
}; // end namespace Technorabble
#endif // !__math_utils__
Run Code Online (Sandbox Code Playgroud)
(但是没有.cpp文件,因为我有各种(有些类似的)问题让我的模板功能正常工作 - 所以我最终定义了内联).
继续单元测试项目,我有一个main.cpp:
#include "MathUtilsTest.h"
void RunMathUtilsTests();
int main()
{
RunMathUtilsTests();
// Other class tests will go here when I have …Run Code Online (Sandbox Code Playgroud) 我在一个类中有一个向量数组:
class MeasurementData
{
private:
std::vector<double> m_measuredStrengths[3];
}
Run Code Online (Sandbox Code Playgroud)
我想要另一个类的函数来检查它并根据分析传回一个整数,例如
int CStrengthAnalyser::GetBestFit(std::vector<double> measuredStrengths[3])
{
int bestFit = -1;
// do stuff
return bestFit;
}
Run Code Online (Sandbox Code Playgroud)
我对传递此类对象的最佳做法感到有些困惑,另外还设置了我的接收函数以保证不会更改原始数据.
我的函数声明是否正常,或者我是否需要添加一些最佳实践调整?
我有一个XML文件,例如
<Bars1>
<Bar name='0'>245</Bar>
<Bar name='1'>180</Bar>
<Bar name='2'>120</Bar>
<Bar name='3'>60</Bar>
<Bar name='4'>0</Bar>
</Bars1>
<Bars2>
<Bar name='0'>25</Bar>
<Bar name='1'>10</Bar>
<Bar name='2'>10</Bar>
<Bar name='3'>6</Bar>
<Bar name='4'>0</Bar>
</Bars2>
<Gubbins3>
<Bar name='0'>45</Bar>
<Bar name='1'>18</Bar>
<Bar name='2'>12</Bar>
<Bar name='3'>4</Bar>
<Bar name='4'>0</Bar>
</Gubbins3>
Run Code Online (Sandbox Code Playgroud)
和一个List<int>notNeededBarNames,包含例如{1,3}
我已经将XML文件加载到XmlDocumentxmlDoc中,并且想要删除任何"Bar"元素,其中属性"name"是我列表中的整数之一,无论它在XML中可能存在于何处.我的例子很小,但实际上文档和列表可能非常大.
这样做有一个很好的方法吗?我可以"蛮力"它,但我不禁觉得可能有更好的方法.
希望你能帮忙!