算术表达式求值

闲来没事的时候写了个算术表达式求值的程序,还不够完善…估计也没有时间去完善了。

用的是中缀表达式转后缀表达式,另外还有的方法是用二叉树。

隐藏行号 复制代码 Demo
  1. /*??????????????????????????????????????????????????????????????????*/
    
  2. #ifndef __ARITHMETIC_EXPRESSION_H
    
  3. #define __ARITHMETIC_EXPRESSION_H
    
  4. namespace dskit
    
  5. {
    
  6.     #define MAXLENEXPRESSION 100
    
  7.     class ArithmeticExpression
    
  8.     {
    
  9.     public:
    
  10.         ArithmeticExpression(const char* expression);
    
  11.         int Compute();
    
  12.         bool IsValidate();
    
  13.     private:
    
  14.         void ConvertToSuffix();
    
  15.         int Priority(char a, char b);
    
  16.     private:
    
  17.         char m_data[MAXLENEXPRESSION];
    
  18.     };
    
  19. }
    
  20.  
  21. #endif
    
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i
隐藏行号 复制代码 Demo
  1. #include
         
         
    
         
         
  2. #include
         
         
    
         
         
  3. #include "arithmetic_expression.h"
    
  4. #include "stack.h"
    
  5.  
  6. dskit::ArithmeticExpression::ArithmeticExpression(const char* expression)
    
  7. {
    
  8.     strcpy(m_data, expression);
    
  9. }
    
  10.  
  11. int dskit::ArithmeticExpression::Compute()
    
  12. {
    
  13.     ConvertToSuffix();
    
  14.     //Stack s = CreateStack(MAXLENEXPRESSION);
    
  15.     
    
  16.     //DisposeStack(s);
    
  17.     return 0;
    
  18. }
    
  19.  
  20. bool dskit::ArithmeticExpression::IsValidate()
    
  21. {
    
  22.     return false;
    
  23. }
    
  24.  
  25. /*Is a priority b*/
    
  26. int dskit::ArithmeticExpression::Priority(char a, char b)
    
  27. {
    
  28.     if( ('(' == a && ')' == b) || ('(' == b && ')' == a) )
    
  29.         return 0;
    
  30.     if( ('+' == a && '-' == b) || ('-' == b && '+' == a) )
    
  31.         return 0;
    
  32.     if( ('*' == a && '/' == b) || ('/' == b && '*' == a) )
    
  33.         return 0;
    
  34.     if( ('*' == a || '/' == a) && ('+' == b || '-' == b) )
    
  35.         return 1;
    
  36.     if( ('(' == a || ')' == a) && ('+' == b || '-' == b) )
    
  37.         return 1;
    
  38.     if( ('(' == a || ')' == a) && ('*' == b || '/' == b) )
    
  39.         return 1;
    
  40.     return -1;
    
  41. }
    
  42.  
  43. void dskit::ArithmeticExpression::ConvertToSuffix()
    
  44. {
    
  45.     char p[MAXLENEXPRESSION];
    
  46.     Stack s = CreateStack(MAXLENEXPRESSION);    
    
  47.     size_t index = 0;
    
  48.     char* pTemp = m_data;
    
  49.     for(; '/0' != *pTemp; ++pTemp)
    
  50.     {    
    
  51.         if(*pTemp != ' ')
    
  52.         {
    
  53.             if('(' == *pTemp || ')' == *pTemp || '+' == *pTemp || '-' == *pTemp || '*' == *pTemp || '/' == *pTemp)
    
  54.             {
    
  55.                 if(IsEmpty(s) || '(' == *pTemp)
    
  56.                 {
    
  57.                     Push(*pTemp, s);
    
  58.                     continue;
    
  59.                 }
    
  60.  
  61.                 if(')' == *pTemp)
    
  62.                 {
    
  63.                     while(!IsEmpty(s) && Top(s) != '(')
    
  64.                     {
    
  65.                         p[index++] = Pop(s);
    
  66.                     }
    
  67.                     if(!IsEmpty(s)) Pop(s);
    
  68.                     continue;
    
  69.                 } 
    
  70.                 
    
  71.                 int iPriority = Priority(*pTemp, Top(s));
    
  72.                 if(iPriority <= 0)
    
  73.                 {
    
  74.                     p[index++] = Pop(s);
    
  75.                     while(!IsEmpty(s))
    
  76.                     {
    
  77.                         iPriority = Priority(*pTemp, Top(s));
    
  78.                         if(iPriority <= 0)
    
  79.                         {
    
  80.                             p[index++] = Pop(s);    
    
  81.                             
    
  82.                         }
    
  83.                         else
    
  84.                             break;
    
  85.                     }
    
  86.                 }
    
  87.  
  88.                 Push(*pTemp, s);
    
  89.                 
    
  90.             }
    
  91.             else
    
  92.             {
    
  93.                 p[index++] = *pTemp;
    
  94.             }    
    
  95.         }//end of if    
    
  96.     }//end of for
    
  97.  
  98.     while(!IsEmpty(s))
    
  99.         if(Top(s) != '(') p[index++] = Pop(s);
    
  100.  
  101.     p[index] = '/0';
    
  102.     //strcpy(p, m_data);
    
  103.     DisposeStack(s);
    
  104.     std::cout << p << std::endl;
    
  105. }
    
  106.  
  107. int main(int argc, char* argv[])
    
  108. {
    
  109.  
  110.     dskit::ArithmeticExpression ae("1+2*3/4-8+5*7");
    
  111.  
  112.     ae.Compute();
    
  113.     
    
  114.     return 0;
    
  115. }
    
  116.  
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值