基本功能计算器算法(优先级)

/*************************************************************************/  
  /*   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;  
  } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章