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

/*************************************************************************/  
  /*   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;  
  } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值