• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            我的玻璃盒子

            (轉(zhuǎn)載)用C++實(shí)現(xiàn)的加、減、乘、除表達(dá)式計(jì)算

            原帖地址:http://www.shnenglu.com/jb8164/archive/2008/01/02/40211.html

            // 頭文件 Calc.h

            #ifndef __CALC_H__
            #define __CALC_H__

            #include 
            <stack>

            #define ascii_int(x) (x >= 0x30 && x <= 0x39) ? (x - 0x30) : (x)
            const int GREATER =  1;
            const int EQUAL   =  0;
            const int LESS    = -1;

            class Calculate {
            public:
              
            int  evaluteExpr(char *exp);

            private:
              
            int  getLevel(char ch);
              
            bool isOperator(char ch);
              
            int  compareOpteratorLevel(char inputChar, char optrStackTop);
              
            int  calc(int num1, int num2, char op);
              
            void evaluate(char ch);

            private:
              std::stack
            <int>  _opnd_stack;
              std::stack
            <char> _optr_stack;
              
            static char _optr[];
              
            static int  _level[];
            }
            ;

            #endif

            // 頭文件的實(shí)現(xiàn)代碼 Calc.cxx

            #include "Calc.h"

            char Calculate::_optr[] = {'#''(''+''-''*''/'')'};
            int Calculate::_level[] = 0,   1,   2,   2,   3,   3,   4 };

            // Get current operator level for calculating
            int Calculate::getLevel(char ch) {
              
            for (int i = 0*(_optr+i) != '\0'++i) 
                
            if (*(_optr+i) == ch) 
                  
            return *(_level+i);
            }


            // Calculate the operands
            int Calculate::calc(int num1, int num2, char op) {
              
            switch (op) 
                
            {
                
            case '+':
                  
            return num1 + num2;
                
            case '-':
                  
            return num1 - num2;
                
            case '*':
                  
            return num1 * num2;
                
            case '/':
                  
            return num1 / num2;
                }

            }


            // judge inputing character is operator or not
            bool Calculate::isOperator(char ch) {
              
            for (char *= _optr; *!= '\0'++p)
                
            if (*== ch) 
                  
            return true;

              
            return false;
            }


            // Compare level of input operator and the top operator of operator stack
            int Calculate::compareOpteratorLevel(char inputChar, char optrStackTop) {
            //   if (inputChar == '(' && optrStackTop == ')') 
            //     return EQUAL;
            //   else 
              if (inputChar == '(')
                
            return GREATER;

              
            if (inputChar == ')' && optrStackTop == '('
                
            return EQUAL;
              
            else if (inputChar == ')'
                
            return LESS;

              
            if (inputChar == '#' && optrStackTop == '#'
                
            return EQUAL;
            //   else if (inputChar == '#')
            //     return LESS;

              
            return (getLevel(inputChar) > getLevel(optrStackTop)) ? GREATER : LESS;
            }


            // Evaluate value while inputing operators
            void Calculate::evaluate(char ch) {
              
            char op;
              
            int num, result;

              
            if (!isOperator(ch)) {
                _opnd_stack.push(ascii_int(ch));
                
            return ;
              }


              
            switch (compareOpteratorLevel(ch, _optr_stack.top())) 
                
            {
                
            case GREATER :
                  _optr_stack.push(ch);
                  
            break;

                
            case EQUAL :
                  _optr_stack.pop();
                  
            break;

                
            case LESS :
                  num 
            = _opnd_stack.top();
                  _opnd_stack.pop();

                  result 
            = _opnd_stack.top();
                  _opnd_stack.pop();

                  op 
            = _optr_stack.top();
                  _optr_stack.pop();

                  result 
            = calc(result, num, op);
                  _opnd_stack.push(result);
                  evaluate(ch);
                  
            break;
                }

            }


            // Evaluate user specified expression
            int Calculate::evaluteExpr(char *exp) {
              _optr_stack.push(
            '#');
              
            for (char *=exp; *!= '\0'++p )
                evaluate(
            *p);

              
            int result = _opnd_stack.top();
              _opnd_stack.pop();

              
            return result;
            }


            // 測(cè)試代碼 calc_test.cxx

            #include <iostream>
            #include 
            "Calc.h"
            using namespace std;

            int main(void{
              Calculate 
            *calc = new Calculate();
              cout 
            << "1+3*(4+7) = " 
                   
            << calc->evaluteExpr("1+3*(4+7)#"
                   
            << endl;
              cout 
            << "((1+2)) = " 
                   
            << calc->evaluteExpr("((1+2))#"
                   
            << endl;
              cout 
            << "3*8+9/7-5-9+(1-9)/4 = " 
                   
            << calc->evaluteExpr("3*8+9/7-5-9+(1-9)/4#"
                   
            << endl;
              cout 
            << "(6-7)*(5+9) = " 
                   
            << calc->evaluteExpr("(6-7)*(5+9)#"
                   
            << endl;
              cout 
            << "0*8+0/6-9+(7-1) = " 
                   
            << calc->evaluteExpr("0*8+0/6-9+(7-1)#"
                   
            << endl;

              delete calc;
            }


            用 MinGW/G++ 3.4.5 編譯如下:
              g++  -o test.exe  Calc.cxx  Calc_test.cxx

            作為一個(gè)演示算法夠了, 但代碼還是有一些缺點(diǎn):
               (1) 只能處理一位數(shù)的加、減、乘、除表達(dá)式計(jì)算(可帶括號(hào))
               (2) 沒有任何錯(cuò)誤處理,例如不能在表達(dá)式中有空格

            posted on 2008-01-23 15:03 深藍(lán)色系統(tǒng) 閱讀(387) 評(píng)論(1)  編輯 收藏 引用 所屬分類: 皮皮片片

            評(píng)論

            # re: (轉(zhuǎn)載)用C++實(shí)現(xiàn)的加、減、乘、除表達(dá)式計(jì)算 2009-08-07 10:40 遠(yuǎn)古毛利人

            看這篇用Boost的Spirit實(shí)現(xiàn)的方案:
            http://blog.csdn.net/Waiting4you/archive/2009/02/07/3867782.aspx  回復(fù)  更多評(píng)論   

            導(dǎo)航

            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            統(tǒng)計(jì)

            常用鏈接

            留言簿(75)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久91精品综合国产首页| 久久99国产精品久久久| 2021久久国自产拍精品| 国产成人精品久久亚洲高清不卡| 久久激情五月丁香伊人| 亚洲愉拍99热成人精品热久久| 亚洲中文字幕无码久久2020| 亚洲AV日韩精品久久久久久久| 久久国产成人精品麻豆| 日韩一区二区久久久久久 | 久久嫩草影院免费看夜色| 亚洲va久久久噜噜噜久久男同 | 久久久中文字幕日本| 香蕉久久久久久狠狠色| 亚洲国产天堂久久综合网站| 久久青青草原亚洲av无码app | 精产国品久久一二三产区区别| 无码精品久久久久久人妻中字| 久久国产乱子精品免费女| 欧美一区二区久久精品| 国产精品久久久久久久久| 久久人人爽人人爽人人爽| 91麻精品国产91久久久久| 久久综合综合久久综合| 亚洲人AV永久一区二区三区久久| 精品水蜜桃久久久久久久| 久久亚洲中文字幕精品有坂深雪| 亚洲欧美日韩精品久久亚洲区| 国产精品久久久久久久久鸭| 亚洲成色WWW久久网站| 性高朝久久久久久久久久| 国产精品成人久久久| 中文字幕无码久久人妻| 久久WWW免费人成—看片| 精品久久一区二区三区| www.久久热.com| 97久久香蕉国产线看观看| 久久精品国产久精国产思思| 一本色综合网久久| 亚洲va久久久噜噜噜久久狠狠 | 国产精品久久久久久久久免费|