【发布时间】:2012-10-09 17:48:41
【问题描述】:
我正在自学 C++。
我正在尝试组合多项式。为此,我定义了简单的类:
Polynomial<T>、Term<T> 和 Coefficient<T>(也可能只是
complex<T>) 使用简单的值组合。我已经定义了所需的运算符重载。
多项式通过对其项进行排序进行比较 (std::sort)。
我正在处理combineLikeTerms();该方法调用时会先调用
另一个将对该术语向量进行排序的成员方法。例如:
4x^3 + 5x^2 + 3x - 4
将是一个可能的结果排序向量。
问题:
我在这个向量上使用了两个迭代器,我试图合并相邻的项 相同的顺序。
假设我们排序后的初始向量是这样的:
4x^3 - 2x^3 + x^3 - 2x^2 + x ...
函数完成迭代后,临时堆栈向量将 看起来像这样 2x^3 + x^3 - 2x^2 + x ...如果我们看起来仍然有类似的术语 这需要再次重构。
我该怎么做?我正在考虑使用递归。
// ------------------------------------------------------------------------- //
// setPolynomialByDegreeOfExponent()
// should be called before combineLikeTerms
template <class T>
void Polynomial<T>::setPolynomialByDegreeOfExponent()
{
unsigned int uiIndex = _uiNumTerms - 1;
if ( uiIndex < 1 )
{
return;
}
struct _CompareOperator_
{
bool operator() ( math::Term<T> a, Term<T> b )
{
return ( a.getDegreeOfTerm() > b.getDegreeOfTerm() );
} // operator()
};
stable_sort( _vTerms.begin(), _vTerms.end(), _CompareOperator_() );
} // setPolynomialByDegreeOfExponent
// ------------------------------------------------------------------------- //
// addLikeTerms()
template <class T>
bool Polynomial<T>::addLikeTerms( const Term<T>& termA, const Term<T>& termB, Term<T>& result ) const
{
if ( termA.termsAreAlike( termB ) )
{
result = termA + termB;
return true;
}
return false;
} // addLikeTerms
// ------------------------------------------------------------------------- //
// combineLikeTerms()
template <class T>
void Polynomial<T>::combineLikeTerms()
{
// First We Order Our Terms.
setPolynomialByDegreeOfExponent();
// Nothing To Do Then
if ( _vTerms.size() == 1 )
{
return;
}
Term<T> result; // Temp Variable
// No Need To Do The Work Below This If Statement This Is Simpler
if ( _vTerms.size() == 2 )
{
if ( addLikeTerms( _vTerms.at(0), _vTerms.at(1) )
{
_vTerms.clear();
_vTerms.push_back( result );
}
return;
}
// For 3 Ore More Terms
std::vector<Term<T>> vTempTerms; // Temp storage
std::vector<Term<T>>::iterator it = _vTerms.begin();
std::vector<Term<T>>::iterator it2 = _vTerms.begin()+1;
bool bFound = addLikeTerms( *it, *it2, result );
while ( it2 != _vTerms.end() )
{
if ( bFound )
{
// Odd Case Last Three Elems
if ( (it2 == (_vTerms.end()-2)) && (it2+1) == (_vTerms.end()-1)) )
{
vTempTerms.push_back( result );
vTempTerms.push_back( _vTerms.back() );
break;
}
// Even Case Last Two Elems
else if ( (it2 == (_vTerms.end()-1)) && (it == (_vTerms.end()-2)) )
{
vTempTerms.push_back( result );
break;
}
else
{
vTempTerms.push_back( result );
it += 2; // Increment by 2
it2 += 2; "
bFound = addLikeTerms( *it, *it2, result );
}
}
else {
// Push Only First One
vTempTerms.push_back( *it );
it++; // Increment By 1
it2++; "
// Test Our Second Iterator
if ( it2 == _vTerms.end() )
{
vTempTerms.push_back( *(--it2) ); // same as using _vTerms.back()
}
else
{
bFound = addLikeTerms( *it, *it2, result );
}
}
}
// Now That We Have Went Through Our Container, We Need To Update It
_vTerms.clear();
_vTerms = vTempTerms;
// At This point our stack variable should contain all elements from above,
// however this temp variable can still have like terms in it.
// ??? Were do I call the recursion and how do I define the base case
// to stop the execution of the recursion where the base case is a
// sorted std::vector of Term<T> objects that no two terms that are alike...
// I do know that the recursion has to happen after the above while loop
} // combineLikeTerms
有人可以帮我找到下一步吗?我很高兴听到显示的代码中的任何错误/效率问题。 我爱c++
【问题讨论】:
-
哇。也许你可以把这个问题说得更中肯。这将是 TL;我担心 DR。 (另外,格式化也有帮助)
-
你的多项式类是在哪里定义的?为什么它不作为其系数的向量在内部存储?需要模板吗?它的参数类型会不会是 double 类型?是的,我在您的示例中看到系数都是整数,但 double 更有意义,或者这是“使用模板的练习”。
-
我冒昧地将散文删减了一些。我觉得这会帮助人们看到问题。
-
@FrancisCugler 我把它拿出来是因为我读它的方式你问这里递归是否合适。抱歉,如果这歪曲了您的问题。无论如何,我认为你有两个答案应该可以继续
-
@FrancisCugler 在 C++ 中,以
_开头后跟一个大写字母的标识符和包含__的标识符是为编译器开发人员保留的,您永远不应该使用它们。您的类和函数有这么多标识符,那么为什么要使用规则禁止的_CompareOperator_之类的东西呢?
标签: c++ algorithm templates recursion vector