返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,( C1 x. z5 B+ [/ A# A
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式6 `/ A. K: c1 R* |9 c  n; Z6 F
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
. p; T/ L( Y  |% n6 d- l6 ^参数解释:  Z9 D) K7 h# y1 S5 c0 R) R
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流, {5 b6 r8 U4 B* R4 @6 `
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定# E" S8 l$ P$ ]0 m+ f* a
返回值:
% |! k; T9 y1 K& V返回非0表示计算成功,0表示计算失败有错误# X) z3 u( E. ^6 \5 A
, F' o8 l+ Z* I+ o# }

2 e' |1 c* H# j4 Y, F8 r2 e& F
: _3 z1 ]3 ?; V3 X$ Z* {5 X程序代码: 4 f2 ^9 X/ }$ e8 Z. ^
3 i! _- f0 S( U+ K+ f
namespace fy_Exp{! M; M* z3 o9 ~( Z
namespace {template <class _T>& j  G; ]. U0 s  ^, R' S3 v4 {
inline _T GetExpValue(_T t[], char& csym){- d. _# m3 ?2 Z2 g; b# @
    char c=csym; csym=0;
8 m6 _' Z5 t" C/ W    switch(c){$ t$ p4 b) {5 {7 s1 g
    case '+':return t[0] += t[1];9 T1 }" v3 z6 i+ ~$ y
    case '-':return t[0] -= t[1];  m$ |0 N* B. ^
    case '*':return t[0] *= t[1];
, d5 y9 A# V! C; h2 v2 s# u    default: return t[0] /= t[1];//case '/':. ]; E" V  P. e& j7 s. k/ Y5 F
    }
0 \5 F* z2 t( c3 h- X}}
) P$ v  \4 f+ a7 C# {/ Ftemplate <class _T, class _Tstream>
  a6 c6 N0 \# |# j# o  l! {/* _Tstream: inputstream, _T: get return value& P- {4 g  i" @  l
* Return nonzero if get value successfully */
  \+ W; M- p& O& E) J7 I; T3 y1 Vint GetExpValue(_Tstream& istrin, _T& nReturn){
) i9 L* E7 t5 P, E7 W, j; H8 w    _T t[3] = {0}; //雨中飞燕之作3 A2 _! u. ?1 b
    char csym[3] = "++";
; |1 A- v" |; d$ w# O: y    int nLevel = 1, nERR = 0;
( |$ ~' c- R' f) F' f- y' c* M5 q2 h    if(!(istrin>>t[1]))istrin.clear();
  }6 F5 _/ z) a: h# a7 l3 n, q    for(;;){5 q+ L3 Z4 e5 s$ c; V2 E4 Z
        if(istrin>>csym[2]){
, y  i+ z" ^5 K# o            switch(csym[2]){
( }" n9 w: t% ?5 h# u+ V: B            case '(':
; {  f  @+ N# I( U, Q3 o) c4 m, _                if(!csym[1]){nLevel=0x100; nERR=1;}else
4 p- R1 i" G" Y8 p7 D4 J                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
4 n" `, t- V9 Q! m' s/ D3 y                else{nLevel=0x100; nERR=1;}2 q* f, g% V8 Q( n- L" U2 R8 p* V
                break;
2 B1 ~# h1 D6 S, e" e6 u            case ')':7 f- P, H7 F* ]. Q( `
                {nLevel = 0x100;}break;9 ]: W$ |/ M$ l# H4 z
            case '+':case '-':case '*':case '/':
! r( \4 z9 i5 x) v                {csym[nLevel++] = csym[2];}break;: w. f# \' x$ n- Y1 v2 _
            case ' ':case '\r':case '\n':case '\t':continue;
1 G  i' {# T) L1 O            default:/ K8 }5 b! E5 U+ Y! r$ c' d
                {nLevel=0x100; nERR=1;}. M& k6 i+ g0 l  k3 J3 {8 F
            }
# W! s# f/ O' S# W0 f2 k+ w% \            if(nLevel==0x100)break;
( V6 g% ?$ ^4 P& \) C- g" y5 q            if(nLevel&0x10 || istrin>>t[2]){7 w: c7 z' T* R* B2 T
                nLevel &= 0xF;5 A7 `. O5 {9 |4 y/ X$ q" n1 {5 A9 {
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}0 ^6 o9 t1 N* F' O
                if(csym[1]=='*'||csym[1]=='/'){3 |- K. p1 f6 i1 p
                    GetExpValue(t+1, csym[1]);* B& @: T' [) X
                }2 U( L" a7 u7 |! I* w/ D+ H
                else{
) G2 p( V+ z: e# ?" T                    GetExpValue(t, csym[0]);
2 W. e- o; N3 `8 t' P% k" b) @                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
/ M$ g8 a- u/ m. ~                }
7 L6 E8 p1 e: J4 n& U+ q                nLevel = 1;' u2 x" k, l) s+ r! k' h! ]
            }
- ~7 i6 O/ Y3 \) j4 k8 }            else istrin.clear();
+ W3 e" ^  \: D, r8 `: _/ @* J$ Z        }- h' K  S" b: {- `7 _9 @* z1 V* F/ e
        else{nERR = -1; break;}
7 q- e8 f! w9 E" q: N2 O# q    }+ O5 T, `- b  d& i
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);6 k! U$ P; q6 O4 D
    else nReturn=GetExpValue(t, csym[0]);
9 _: k* y$ c+ o) L7 _& Q4 A    return nERR==-1?1:0;- p7 ?% n2 W, B7 s+ J4 v+ P8 K
}}5 m8 h' G" e1 h0 V
& Z% M: v1 u. h" a" Y2 I0 t

) y0 R3 Z; b& ^  S9 d* R6 U. f2 y9 G
4 K( g% h0 _1 ]( c) N" Y8 V函数模板使用示例:4 X: M6 t# m+ s4 X& r
在以上那段代码的后面加上以下代码:
; H" R  o( c; r# O8 ]' N1 J) {& G* M- T$ z3 B$ r7 _
) s$ R) X: a3 M$ t) Z- L1 [$ n

. @! ]0 \2 Y( q- P2 F) S3 p3 P0 Z程序代码: ( ~" w8 ?+ l5 {% r: B1 L) t

' |. ~. n6 n9 U3 t) k, j, o( I#include<strstream>6 P7 P, A6 f  ]
#include<iostream>( W! r* X6 [! y1 A
#include<string>
' n3 V* k. `" n  r8 ^1 X* Xusing namespace std;
5 |1 g7 y* }' D/ q( u( Zint main(void)' X! M( e% N2 a
{
# B/ q3 ]: B2 L+ b* b' v    string s1;
3 D( k) V  `0 I3 a6 R7 B    while(cin>>s1)5 H) m8 G* i% o4 B, U4 B( l- p; Y
    {
) P" U/ Y9 c" N, _/ g! U+ u        istrstream isin(s1.data());
0 ]  v2 C8 B7 q# ~6 B$ k        double d;
7 ]' u. f2 w# i. u8 B        if(fy_Exp::GetExpValue(isin, d))
8 y1 L( c$ ]/ @8 G, I  c        {; J6 Q: I) j8 _# i! l
            cout<<d<<endl;
* b3 m. l( H+ c. X        }
0 x6 W% O6 {/ e/ r( k        else. ^! k6 V1 y  Z9 W
        {& [+ Y8 `# W! F7 C
            cout<<"ERROR"<<endl;
! D! K' k3 {4 {. a        }  z. b# m  ^5 D5 b
    }% {8 [/ W3 i' h3 D* u+ Z
    return 0;
! f$ A- K% V7 u! p; ~}
: @6 c% O- W( g( j1 m, y/ c$ d+ S
( z  |! z& x* `- v8 K; b  ^1 C2 Z8 h
然后编译执行就可以了(*^_^*)$ C3 L; q/ U9 t
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
) i% Z/ `$ y5 C- p      建议使用VC7或VC更高版本,或者使用GNU C++编译

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