我有一个银行帐户系统,需要循环浏览帐户中的交易,并在某个日期之前删除任何我有工作职能的日期:
void TransactionList::deleteTransactionsUpToDate(const Date d) {
ostringstream os;
TransactionList tempTrList(*this);
TransactionList trlRecent;
cout << "\n This is everything in the list \n" << tempTrList.toFormattedString();
List<Transaction> tempList = listOfTransactions_;
while (tempTrList.size() > 0 )
{
if (tempList.first().getDate() <d)
{
listOfTransactions_.deleteOne(tempTrList.newestTransaction());
tempTrList.deleteGivenTransaction(tempList.first());
}
else
{
trlRecent.addNewTransaction(tempTrList.newestTransaction());
}
Transaction tr = tempTrList.newestTransaction();
tempList.deleteOne(tr);
tempTrList.deleteFirstTransaction();
}
trlRecent.putDataInStream(os,trlRecent);
cout << "Non Deltete files" <<trlRecent.toFormattedString();
}
但是我需要使用递归。我试图这样做,并且递归过程似乎与循环完全相同,但是它最后一次出现Debug错误,我无法理解为什么。
我的递归函数代码是:
void TransactionList::deleteTransactionsUpToDate
(const Date d, TransactionList trlRecent, TransactionList
tempTrList,List<Transaction> tempList)
{
ostringstream os;
if (tempTrList.size() == 0)
{
tempTrList = (*this);
tempList = listOfTransactions_;
cout << "\n This is everything in the list \n"<<tempTrList.toFormattedString();
}
if (tempTrList.size() > 0 )
{
if (tempList.first().getDate() <d)
{
listOfTransactions_.deleteOne(tempTrList.newestTransaction());
tempTrList.deleteGivenTransaction(tempList.first());
//cout << tempTrList.size();
}
else
{
trlRecent.addNewTransaction(tempTrList.newestTransaction());
}
//Transaction tr = ;
//cout << tempTrList.toFormattedString() << "\n\n";
/*cout << "test";*/
tempList.deleteOne(tempTrList.newestTransaction());
tempTrList.deleteFirstTransaction();
deleteTransactionsUpToDate(d, trlRecent, tempTrList, tempList);
}
if (trlRecent.size() >0)
trlRecent.putDataInStream(os,trlRecent);
cout << "Non Delteted files" <<trlRecent.toFormattedString();
}
如果有人可以提供建议,那将非常感谢。 (抱歉,对于文本字段,格式不是很好的行太长了。)