Open alexey-malov opened 8 years ago
void DoubleOddDecreaseEven(vector<double>& inputVector)
{
double sumPositiveValues = CalculatePositiveElementSum(inputVector);
int index = 0;
for_each(inputVector.begin(), inputVector.end(),
[&index, &sumPositiveValues](double& value) {
value = (index % 2) == 0 ? value * 2 : value - sumPositiveValues;
++index;
}
);
}
bool isEven = true;
transform(inputVector.begin(), inputVector.end(), inputVector.begin(),
[&](double value) {
isEven = !isEven;
return isEven ? value * 2 : value - sumPositiveValues;
}
);
double CalculatePositiveElementSum(const vector<double>& inputVector)
{
double sumPositiveValues = 0;
for_each(inputVector.begin(), inputVector.end(),
[&sumPositiveValues](double element) {
if (element >= 0)
{
sumPositiveValues += element;
}
}
);
return sumPositiveValues;
}
1>c:\vs2015\vc\include\numeric(20): warning C4244: '=': conversion from 'double' to 'int', possible loss of data
1> c:\vs2015\vc\include\numeric(30): note: see reference to function template instantiation '_Ty std::_Accumulate<const double*,_Ty,_Fn2>(_InIt,_InIt,_Ty,_Fn2)' being compiled
1> with
1> [
1> _Ty=int,
1> _Fn2=CalculatePositiveElementSum::<lambda_678a44b38c5f4eac0174e267552dcb7b>,
1> _InIt=const double *
1> ]
1> c:\teaching\2016\ips\ibulaev\oop\lab02\1_1_vector\vectorconverting.cpp(24): note: see reference to function template instantiation '_Ty std::accumulate<std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,int,CalculatePositiveElementSum::<lambda_678a44b38c5f4eac0174e267552dcb7b>>(_InIt,_InIt,_Ty,_Fn2)' being compiled
1> with
1> [
1> _Ty=int,
1> _InIt=std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,
1> _Fn2=CalculatePositiveElementSum::<lambda_678a44b38c5f4eac0174e267552dcb7b>
1> ]
1> main.cpp
1> Generating Code...
1> 1_1_vector.vcxproj -> C:\teaching\2016\ips\ibulaev\oop\lab02\x64\Debug\1_1_vector.exe
1> 1_1_vector.vcxproj -> C:\teaching\2016\ips\ibulaev\oop\lab02\x64\Debug\1_1_vector.pdb (Partial PDB)
2> stdafx.cpp
2> main.cpp
2> VectorConverting.cpp
2>c:\vs2015\vc\include\numeric(20): warning C4244: '=': conversion from 'double' to 'int', possible loss of data
2> c:\vs2015\vc\include\numeric(30): note: see reference to function template instantiation '_Ty std::_Accumulate<const double*,_Ty,_Fn2>(_InIt,_InIt,_Ty,_Fn2)' being compiled
2> with
2> [
2> _Ty=int,
2> _Fn2=CalculatePositiveElementSum::<lambda_65a1d0f657dc98725fdf0d721fd271c0>,
2> _InIt=const double *
2> ]
2> c:\teaching\2016\ips\ibulaev\oop\lab02\1_1_vector\vectorconverting.cpp(24): note: see reference to function template instantiation '_Ty std::accumulate<std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,int,CalculatePositiveElementSum::<lambda_65a1d0f657dc98725fdf0d721fd271c0>>(_InIt,_InIt,_Ty,_Fn2)' being compiled
2> with
2> [
2> _Ty=int,
2> _InIt=std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,
2> _Fn2=CalculatePositiveElementSum::<lambda_65a1d0f657dc98725fdf0d721fd271c0>
2> ]
2> Generating Code...
BOOST_AUTO_TEST_CASE(positive_values_should_be_summarized)
{
vector<double> testVector = { 2, 3, 4 };
auto sum = CalculatePositiveElementSum(testVector);
BOOST_CHECK_EQUAL(sum, 9);
BOOST_CHECK_CLOSE_FRACTION(CalculatePositiveElementSum({0.5, -0.1, 3.4}), 3.9, 1e-5);
}
автозапуск тестов