返回列表 发帖

一个计算四则表达式的模板

在9月8日那天我特意编写的,给大家分享的,8 `" o( V* `8 v! K! |
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式- X# n8 C" p) \  _( `. ~
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)1 Y! \# C0 ]& A: I9 C" c
参数解释:
* _4 D- N; d0 z% \istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
/ G" d6 X9 H3 [7 B" CnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
5 z/ H2 o. q4 G4 P- S返回值:' i' X3 M# j0 w, O
返回非0表示计算成功,0表示计算失败有错误
- r* R) S: Z2 @$ F; b
) j. {' C9 E9 z* Y & T+ J0 d2 e# b5 x
- Q$ e2 n/ {5 P( o' V
程序代码: % n4 E/ q/ A0 p

) T" @- q9 o( b6 J- r. A0 }namespace fy_Exp{! W& I; c8 j' q. C! y1 Q+ Y
namespace {template <class _T>2 m$ u, l& w  L: o
inline _T GetExpValue(_T t[], char& csym){
2 d  V+ I5 H/ M    char c=csym; csym=0;
8 s% p" P- O; @  r9 W4 z    switch(c){8 E& g! I4 T* c0 k! X# [6 v/ ?
    case '+':return t[0] += t[1];
6 r' }1 ?, X- x0 ?2 C* F3 A    case '-':return t[0] -= t[1];6 J/ t4 R4 m; n- Y$ ]1 a
    case '*':return t[0] *= t[1];
3 _! d) P: W6 I  v    default: return t[0] /= t[1];//case '/':
) K. m& k* d1 A7 h    }6 P4 ~. `/ ~' m: G0 {1 \4 I
}}
' u! Z5 d; ~* ]9 D' etemplate <class _T, class _Tstream>
5 q4 b/ [( e- H% n7 P7 |8 \" C* g/* _Tstream: inputstream, _T: get return value
7 h/ f; A. [! h* Return nonzero if get value successfully */
( s; [& y1 |0 m5 tint GetExpValue(_Tstream& istrin, _T& nReturn){
8 B: r1 D) [+ f/ ]  Y* }( E    _T t[3] = {0}; //雨中飞燕之作
! c1 F: C/ _% S    char csym[3] = "++";
% k* S0 F. Y5 _3 C& O2 a9 a    int nLevel = 1, nERR = 0;2 x  b$ z  r8 m. o. m, F
    if(!(istrin>>t[1]))istrin.clear();
- w) N2 h" S8 D, j    for(;;){2 ?; `$ ~& D" i; z' K( f- _! a0 O# J( }
        if(istrin>>csym[2]){
1 j* H" w! ^2 P, O# _            switch(csym[2]){0 \" E. `$ Q9 G& x& _- A
            case '(':
" j* x6 X1 e4 m3 p0 e& \                if(!csym[1]){nLevel=0x100; nERR=1;}else3 T# m7 {' y5 R7 ^
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
/ r) z- Y. F# e: k1 }' J                else{nLevel=0x100; nERR=1;}
3 O- ]4 x0 ^1 Z( e7 u                break;6 P  }4 f) A8 k, g5 {( {  B
            case ')':
( R! d; k! H3 _% w3 _* {: W                {nLevel = 0x100;}break;& h8 E$ q8 K- w/ x
            case '+':case '-':case '*':case '/':
2 E( ^& ~0 M- ?1 p. F- ?: [4 A( B                {csym[nLevel++] = csym[2];}break;% A, D% x/ n+ N; s& }0 v+ H4 _
            case ' ':case '\r':case '\n':case '\t':continue;
2 D) J5 m* v/ d3 F; y' R, T            default:2 c! o7 W: V9 V/ V& J5 `7 ?
                {nLevel=0x100; nERR=1;}! M; Z, v& ]3 @4 q6 n
            }9 Y; ]! `* R6 b
            if(nLevel==0x100)break;
$ B# l) b) @( p            if(nLevel&0x10 || istrin>>t[2]){! o8 x, b9 S0 N" u, Z$ h" O
                nLevel &= 0xF;1 O! D- j# K- i( F/ w  |; P) s
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}! m( ~  `6 a" U7 d  z
                if(csym[1]=='*'||csym[1]=='/'){4 l4 [, J+ p5 G  h
                    GetExpValue(t+1, csym[1]);8 c8 h& e% M! R: M
                }) b, Q7 K5 P, G9 C/ Y/ J3 s
                else{$ K3 u7 M% r' D" Y$ ]' N
                    GetExpValue(t, csym[0]);7 x' d! Q, j; E2 M) X* @
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
: M( F/ b$ c; Z( L: F$ B3 c8 f                }/ a' \# b2 n" j" P
                nLevel = 1;
9 `" R3 ~$ S6 ]+ x, j  v6 P            }
  b5 M* J; K) N            else istrin.clear();
9 F1 O' T: Z& D7 G% a1 K! C        }
( `; y* i$ Z) S& `6 K% B1 g1 F        else{nERR = -1; break;}
1 }6 _& G' A+ [; w    }
1 Y/ O+ B" ^) p, }% }. ]    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);. k/ s/ \% ?4 |* R' \" t
    else nReturn=GetExpValue(t, csym[0]);- l! O) e- N& g% b& h' t0 W$ h- G
    return nERR==-1?1:0;
4 M) D5 b3 j, z/ E}}0 f0 ^& L8 K$ E7 G( H
. [; \8 D3 R  S+ b) J# |

+ b& ?1 c4 V9 {( e* _' w0 P0 S' C$ U* s. t; y5 t2 d
函数模板使用示例:; {6 h0 j" J, q1 B( s
在以上那段代码的后面加上以下代码:$ k$ K! b. w4 Y; O/ {
+ z# c1 E; f% f: h- W4 s% @

. `. w  _) U) d5 I  _, s& o% Q7 p& p3 f0 G) [' w
程序代码: / M% C4 t, u: ~( n+ |5 x  \3 f

1 E# G( ]5 f* g# w  I+ P4 a) Q#include<strstream>
6 U* E& \5 @" K! T& H) L#include<iostream>+ i) \  l. ?/ @4 X; t6 r4 J1 i
#include<string>* E  ^8 u5 l! _4 L
using namespace std;, y& t: b  ^2 q; M( e: }% W5 h
int main(void)4 }6 l! [4 i9 b4 f2 Q' I
{
  t" a1 [- w8 f+ n    string s1;# F7 E5 L! Z9 E  ~) u
    while(cin>>s1)
0 h5 `3 m- z, B5 n    {
9 \: R8 W5 |6 R* g$ Q9 O- v        istrstream isin(s1.data());- b: W8 Z( A% z' A3 \3 K* r
        double d;
) b; p- U- Z$ t' Y8 {        if(fy_Exp::GetExpValue(isin, d))
9 S5 B- J: C' {& [/ B        {: A3 }/ J4 `: p* r2 F/ {% p* ?
            cout<<d<<endl;
6 s3 x) x% j; ]! u3 D        }
( I- E; f9 _! ]0 A2 W        else
) d: [+ G/ {  J) G- i% x        {
6 ^6 `: ^% \9 {            cout<<"ERROR"<<endl;
) i5 s: E+ m; Y8 t+ W: L        }
4 c# A! I6 {+ i! R8 |) q    }( i- ^# C  p$ V) d7 ~1 y
    return 0;- G' f  y8 `4 N( d5 K7 X" [3 F$ N
}
( D; y6 i+ O4 U) t& |
! ^; W) u8 a5 V- ]- |2 ?1 S2 [; X6 K* w. V$ m+ X0 Z$ z
然后编译执行就可以了(*^_^*)
  i. k- j* `4 G其它:TC++上一定编译错误,不保证在VC6上也能通过编译* z3 D# y  P, _9 U5 ^
      建议使用VC7或VC更高版本,或者使用GNU C++编译

返回列表
【捌玖网络】已经运行: