基本功能計算器算法(優先級)
/*************************************************************************/
/* FUNCTION: Analyzer */
/* DESCRIPTION:分析器 */
/* INPUTS: key,鍵值 */
/* OUTPUTS: NONE */
/* RETURN: null */
/*************************************************************************/
double Analyzer(UINT8 byCurOp, double dOprand2)
{
UINT8 byPreOp, byPrePreOp, byPreKey;
double dOprand1, dResult;
static UINT8 byOp = ENDC;
static double dRes = 0;
static double dOpr = 0;//這三個靜態變量用來保存+=類操作的運算符和數據
static BOOL bFirst = TRUE;//第一層調用標誌,==運算只在第一層運算
if (byCurOp == OTHER)
{
byOp = ENDC;
return 0;
}
byPreOp = PopOperator();
dResult = dOprand2;
//優先級高,直接入棧
if (OpePriority[byCurOp][byPreOp] == '>')
{
PushOperator(byPreOp);
PushData(dOprand2);
PushOperator(byCurOp);
return dResult;
}
byPreKey = OperatorType[g_byKeyPrevious];
switch(byCurOp) {
case ENDC: //輸入等號
if (byPreOp == ENDC || byPreOp == LBRACKET)
{
PushOperator(byPreOp);
if (!bFirst || byOp == ENDC)
{
return dResult;
}
//=NUM=類運算第一層
if (byPreKey == ENDC || TypeOfKey[g_byKeyPrevious] == NUMBER
|| TypeOfKey[g_byKeyPrevious] == DOT
|| TypeOfKey[g_byKeyPrevious] == SIGN
|| TypeOfKey[g_byKeyPrevious] == BACK
|| TypeOfKey[g_byKeyPrevious] == MEMORY)
{
byPreOp = byOp;
byPreKey = ENDC;
break;
}
//=FUNCTION=類運算第一層
if (TypeOfKey[g_byKeyPrevious] == FUNCTION)
{
byPreOp = byOp;
dOpr = dOprand2;
byPreKey = ENDC;
break;
}
//計算完畢
return dResult;
}
//+=類運算
if (TypeOfKey[g_byKeyPrevious] == OPERATOR)
{
byPrePreOp = GetOperator();
//去掉1+4*=之類表達式的4*
if (byPrePreOp != LBRACKET && byPrePreOp != ENDC)
{
PopData();
dResult = Analyzer(byCurOp, GetData());
return dResult;
}
}
//記錄1+2=的+2,以便下次輸入=時使用
byOp = byPreOp;
dOpr = dOprand2;
break;
case RBRACKET:
if (g_byBracketNum > 0)
{
if (byPreOp == LBRACKET)
{
g_byBracketNum --;
return dResult;
}
//去掉(1+4*)之類表達式的+4*
else if (TypeOfKey[g_byKeyPrevious] == OPERATOR)
{
dResult = Analyzer(byCurOp, PopData());
return dResult;
}
}
else //沒有左括號,右括號直接去掉
{
PushOperator(byPreOp);
return dResult;
}
break;
}
//+==類運算
if (byOp != ENDC && byPreKey == ENDC)
{
dOprand1 = dRes;
dOprand2 = dOpr;
}
else
{
dOprand1 = PopData();
}
switch(byPreOp) {
case ADD://計算部分。。。
}
if (dResult >= CALC_MAX_NUM || dResult <= -CALC_MAX_NUM)
{
strcpy(g_aDataIn, CALC_OVERFLOW);
return CALC_ERROR;
}
bFirst = FALSE;
dResult = Analyzer(byCurOp, dResult);
bFirst = TRUE;
dRes = dResult;
return dResult;
}
/* FUNCTION: Analyzer */
/* DESCRIPTION:分析器 */
/* INPUTS: key,鍵值 */
/* OUTPUTS: NONE */
/* RETURN: null */
/*************************************************************************/
double Analyzer(UINT8 byCurOp, double dOprand2)
{
UINT8 byPreOp, byPrePreOp, byPreKey;
double dOprand1, dResult;
static UINT8 byOp = ENDC;
static double dRes = 0;
static double dOpr = 0;//這三個靜態變量用來保存+=類操作的運算符和數據
static BOOL bFirst = TRUE;//第一層調用標誌,==運算只在第一層運算
if (byCurOp == OTHER)
{
byOp = ENDC;
return 0;
}
byPreOp = PopOperator();
dResult = dOprand2;
//優先級高,直接入棧
if (OpePriority[byCurOp][byPreOp] == '>')
{
PushOperator(byPreOp);
PushData(dOprand2);
PushOperator(byCurOp);
return dResult;
}
byPreKey = OperatorType[g_byKeyPrevious];
switch(byCurOp) {
case ENDC: //輸入等號
if (byPreOp == ENDC || byPreOp == LBRACKET)
{
PushOperator(byPreOp);
if (!bFirst || byOp == ENDC)
{
return dResult;
}
//=NUM=類運算第一層
if (byPreKey == ENDC || TypeOfKey[g_byKeyPrevious] == NUMBER
|| TypeOfKey[g_byKeyPrevious] == DOT
|| TypeOfKey[g_byKeyPrevious] == SIGN
|| TypeOfKey[g_byKeyPrevious] == BACK
|| TypeOfKey[g_byKeyPrevious] == MEMORY)
{
byPreOp = byOp;
byPreKey = ENDC;
break;
}
//=FUNCTION=類運算第一層
if (TypeOfKey[g_byKeyPrevious] == FUNCTION)
{
byPreOp = byOp;
dOpr = dOprand2;
byPreKey = ENDC;
break;
}
//計算完畢
return dResult;
}
//+=類運算
if (TypeOfKey[g_byKeyPrevious] == OPERATOR)
{
byPrePreOp = GetOperator();
//去掉1+4*=之類表達式的4*
if (byPrePreOp != LBRACKET && byPrePreOp != ENDC)
{
PopData();
dResult = Analyzer(byCurOp, GetData());
return dResult;
}
}
//記錄1+2=的+2,以便下次輸入=時使用
byOp = byPreOp;
dOpr = dOprand2;
break;
case RBRACKET:
if (g_byBracketNum > 0)
{
if (byPreOp == LBRACKET)
{
g_byBracketNum --;
return dResult;
}
//去掉(1+4*)之類表達式的+4*
else if (TypeOfKey[g_byKeyPrevious] == OPERATOR)
{
dResult = Analyzer(byCurOp, PopData());
return dResult;
}
}
else //沒有左括號,右括號直接去掉
{
PushOperator(byPreOp);
return dResult;
}
break;
}
//+==類運算
if (byOp != ENDC && byPreKey == ENDC)
{
dOprand1 = dRes;
dOprand2 = dOpr;
}
else
{
dOprand1 = PopData();
}
switch(byPreOp) {
case ADD://計算部分。。。
}
if (dResult >= CALC_MAX_NUM || dResult <= -CALC_MAX_NUM)
{
strcpy(g_aDataIn, CALC_OVERFLOW);
return CALC_ERROR;
}
bFirst = FALSE;
dResult = Analyzer(byCurOp, dResult);
bFirst = TRUE;
dRes = dResult;
return dResult;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.