📄 point24.cpp
字号:
#include "point24.h"
_LIT(KPanicText, "CPoint24");
_LIT(KNewLineText, "\x2029");
_LIT(KResultText, " = 24");
_LIT(KLeftParenthesisText, "(");
_LIT(KRightParenthesisText, ")");
_LIT(KSpaceText, " ");
_LIT(KFormatNumber, "%d");
_LIT(KFormatResults, "%d result(s):\x2029");
const TChar KOperatorChar[5] = {' ', '+', '-', '*', '/'};
CPoint24::CPoint24()
{
iResultOffset = 0;
iResultCount = 0;
iEditResult = NULL;
}
CPoint24::~CPoint24()
{
}
CPoint24* CPoint24::NewL()
{
CPoint24* self = new (ELeave) CPoint24;
return self;
}
void CPoint24::CalculateL(TInt aValue1, TInt aValue2,
TInt aValue3, TInt aValue4,
CEikEdwin* aEditResult)
{
__ASSERT_DEBUG(aValue1 > 0 && aValue1 < 10,
User::Panic(KPanicText, EPanicCode_ValueRange));
__ASSERT_DEBUG(aValue2 > 0 && aValue2 < 10,
User::Panic(KPanicText, EPanicCode_ValueRange));
__ASSERT_DEBUG(aValue3 > 0 && aValue3 < 10,
User::Panic(KPanicText, EPanicCode_ValueRange));
__ASSERT_DEBUG(aValue4 > 0 && aValue4 < 10,
User::Panic(KPanicText, EPanicCode_ValueRange));
__ASSERT_DEBUG(aEditResult,
User::Panic(KPanicText, EPanicCode_NullPointer));
iOriginalValue[0].iValue = aValue1;
iOriginalValue[1].iValue = aValue2;
iOriginalValue[2].iValue = aValue3;
iOriginalValue[3].iValue = aValue4;
iEditResult = aEditResult;
iResultCount = 0;
TValue* values[4] =
{
&iOriginalValue[0],
&iOriginalValue[1],
&iOriginalValue[2],
&iOriginalValue[3]
};
CalculateL(values, 4);
TBuf<20> s;
s.Format(KFormatResults, iResultCount);
CPlainText* txt = iEditResult->Text();
txt->InsertL(0, s);
}
void CPoint24::CalculateL(TValue* aValues[], TInt aValueCount)
{
__ASSERT_DEBUG(aValueCount > 1 && aValueCount <= 4,
User::Panic(KPanicText, EPanicCode_ValueRange));
TInt offset1;
TInt offset2;
TInt i;
TInt m;
TValue* temp_value[3];
for (offset1 = 0; offset1 < aValueCount; offset1++)
{
for (offset2 = 0; offset2 < aValueCount; offset2++)
{
if (offset1 == offset2)
{
// the same position
continue;
}
if (offset1 > offset2
&& aValues[offset1]->iValue == aValues[offset2]->iValue)
{
// the same value, and already used
continue;
}
// store temp value
m = 0;
for (i = 0; i < aValueCount; i++)
{
if (i != offset1 && i != offset2)
{
temp_value[m++] = aValues[i];
}
}
// calculate with all operators
for (i = 0; i < 4; i++)
{
iResultOffset = 4 - aValueCount;
TValue* value = Calculate(aValues[offset1], aValues[offset2],
(TPoint24Operator)(EPoint24OperatorAdd + i));
if (value)
{
if (aValueCount == 2)
{
if (value->iValue == 24)
{
// bingo
AddResultL();
}
}
else
{
temp_value[m] = value;
CalculateL(temp_value, aValueCount - 1);
}
}
}
}
}
}
TValue* CPoint24::Calculate(const TValue* aValue1, const TValue* aValue2,
TPoint24Operator aOperator)
{
TInt result = -1;
TValue* value = NULL;
switch (aOperator)
{
case EPoint24OperatorAdd:
if (aValue1->iValue <= aValue2->iValue)
{
result = aValue1->iValue + aValue2->iValue;
}
break;
case EPoint24OperatorSub:
if (aValue1->iValue >= aValue2->iValue && aValue2->iValue > 0)
{
result = aValue1->iValue - aValue2->iValue;
}
break;
case EPoint24OperatorMulti:
if (aValue1->iValue <= aValue2->iValue)
{
result = aValue1->iValue * aValue2->iValue;
}
break;
case EPoint24OperatorDiv:
if (aValue2->iValue > 1 && aValue1->iValue >= aValue2->iValue
&& (aValue1->iValue % aValue2->iValue) == 0)
{
result = aValue1->iValue / aValue2->iValue;
}
break;
}
if (result >= 0)
{
value = &iResultValue[iResultOffset];
value->iValue = result;
value->iLeftChild = const_cast<TValue*>(aValue1);
value->iRightChild = const_cast<TValue*>(aValue2);
value->iOperator = aOperator;
}
return value;
}
void CPoint24::AddResultL()
{
TBuf<30> s;
TBool result = GetResult(&iResultValue[2], EPoint24OperatorNone, s);
if (!result)
{
return;
}
s.Append(KResultText);
s.Append(KNewLineText);
iEditResult->SetCursorPosL(0, EFalse);
if (!iEditResult->FindL(&s))
{
CPlainText* txt = iEditResult->Text();
txt->InsertL(txt->DocumentLength(), s);
iResultCount++;
}
}
TBool CPoint24::GetResult(TValue* aValue, TPoint24Operator aUpperOperator, TDes& aBuf, TBool aIsLeftChild)
{
__ASSERT_DEBUG(aValue,
User::Panic(KPanicText, EPanicCode_NullPointer));
if (!aValue->iLeftChild || !aValue->iRightChild)
{
// end child
aBuf.Format(KFormatNumber, aValue->iValue);
return ETrue;
}
if (!IsRightOrder(aValue, EPoint24OperatorMulti))
{
return EFalse;
}
if (!IsRightOrder(aValue, EPoint24OperatorAdd))
{
return EFalse;
}
TBuf<30> s1;
TBuf<30> s2;
TBool result;
result = GetResult(aValue->iLeftChild, aValue->iOperator, s1);
if (!result)
{
return EFalse;
}
result = GetResult(aValue->iRightChild, aValue->iOperator, s2, EFalse);
if (!result)
{
return EFalse;
}
aBuf.Append(s1);
aBuf.Append(KSpaceText);
aBuf.Append(KOperatorChar[aValue->iOperator]);
aBuf.Append(KSpaceText);
aBuf.Append(s2);
if (IsNeedParenthesis(aValue, aUpperOperator, aIsLeftChild))
{
aBuf.Insert(0, KLeftParenthesisText);
aBuf.Append(KRightParenthesisText);
}
return ETrue;
}
TBool CPoint24::IsRightOrder(TValue* aValue, TPoint24Operator aOperator)
{
if (aValue->iOperator == aOperator
&& aValue->iRightChild->iOperator == aOperator)
{
if (aValue->iRightChild->iLeftChild->iValue < aValue->iLeftChild->iValue)
{
return EFalse;
}
if (aValue->iRightChild->iLeftChild->iOperator == aOperator)
{
if (aValue->iRightChild->iLeftChild->iLeftChild->iValue < aValue->iLeftChild->iValue)
{
return EFalse;
}
}
}
return ETrue;
}
TBool CPoint24::IsNeedParenthesis(TValue* aValue, TPoint24Operator aUpperOperator, TBool aIsLeftChild)
{
if (aUpperOperator == EPoint24OperatorMulti || aUpperOperator == EPoint24OperatorDiv)
{
if (aValue->iOperator == EPoint24OperatorAdd || aValue->iOperator == EPoint24OperatorSub)
{
return ETrue;
}
}
if (!aIsLeftChild && aUpperOperator == EPoint24OperatorSub)
{
if (aValue->iOperator == EPoint24OperatorAdd
|| aValue->iOperator == EPoint24OperatorSub)
{
return ETrue;
}
}
return EFalse;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -