返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,
; O" `5 [% V. U1 l9 m% g+ K一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
6 h$ e5 h2 J# n" B4 x, M只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)" c! m. v- P& B- o
参数解释:
& V* }- s! l* X/ h7 W% Histrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
1 C5 D# d: Y3 F% l+ `nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
# P6 j  U5 L' s) C/ ]3 D! K9 L返回值:
  `+ s! t0 j# z; ]返回非0表示计算成功,0表示计算失败有错误& u1 `& o5 O( Z9 p
: M! j! `2 z- o0 P% h: K; V6 M; c
2 v1 ^6 K  W* q3 m* G: C4 q

/ @7 n7 B8 M* m) A' _& X程序代码:
" U  k& l' d1 O1 _5 V  t* e" j6 s% I! i' s
namespace fy_Exp{
/ b0 Q  E9 }6 V7 {7 y+ qnamespace {template <class _T>6 m6 @' s0 T; y  }3 \2 j% ?& E: `. [$ L+ `
inline _T GetExpValue(_T t[], char& csym){
% S. H% ]( Z+ g  |    char c=csym; csym=0;
. I9 f+ C( J% m$ W5 \. w2 n+ Q    switch(c){7 n1 J# |3 N2 r; M# e  V) y
    case '+':return t[0] += t[1];% [2 d# W. I  N: O  D" g4 h. g
    case '-':return t[0] -= t[1];
; N: ]7 j. j. V    case '*':return t[0] *= t[1];
, j( k3 R4 ~; \+ f) i3 U% I! B# D    default: return t[0] /= t[1];//case '/':- e( @( t: T, _
    }1 L, U2 j. Z! Z4 [: M7 U+ y% k+ G
}}7 F' |, _5 a. b6 L3 v: I1 x2 j3 X
template <class _T, class _Tstream>' P9 u5 |- Y  `& w, E2 V( V6 i
/* _Tstream: inputstream, _T: get return value! g9 N8 O' v/ o
* Return nonzero if get value successfully */
9 \+ h5 a" _4 n  zint GetExpValue(_Tstream& istrin, _T& nReturn){/ _& n! J. H, d/ x2 l
    _T t[3] = {0}; //雨中飞燕之作
. W7 c: l# m- U    char csym[3] = "++";" ?3 g% {6 |/ ]) b0 G% H+ \
    int nLevel = 1, nERR = 0;# d  Q* N8 L7 i# z
    if(!(istrin>>t[1]))istrin.clear();
. a! [& K+ _5 m2 O- }8 m    for(;;){2 x) T4 Y; z7 a& L) s3 D/ j4 v8 p
        if(istrin>>csym[2]){1 z$ s- N4 D( o/ T
            switch(csym[2]){& U+ w6 q- W: Z- W' w" y1 ]* M6 y
            case '(':
! ?3 M' o% ^9 T' d! {9 C                if(!csym[1]){nLevel=0x100; nERR=1;}else
" n/ N9 ~* I6 ]$ i1 |                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;6 R8 m+ N6 \7 M1 ~
                else{nLevel=0x100; nERR=1;}5 v6 i( u% \& T) \7 p: `5 A
                break;" t8 R+ a: U7 c; {2 Q
            case ')':
$ c5 V5 r& c1 [9 D$ }' d+ ]                {nLevel = 0x100;}break;9 H, G7 ~* X$ G0 J+ b4 f' g: p. f0 w
            case '+':case '-':case '*':case '/':
) v9 g+ D0 e  H' s3 M  A) p) D                {csym[nLevel++] = csym[2];}break;& v* s; v( V2 g4 A+ j: p
            case ' ':case '\r':case '\n':case '\t':continue;
" w4 o- P7 ?, r* d            default:
: a" ^! e8 C5 ]! \                {nLevel=0x100; nERR=1;}
& q6 V& D* J4 i5 |) ]            }4 A8 L7 @  ]: C3 n# H0 v
            if(nLevel==0x100)break;
: @* U( Q( W5 M            if(nLevel&0x10 || istrin>>t[2]){9 u" h/ G' t; u6 |
                nLevel &= 0xF;. D4 v( ~8 M1 T- n
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
0 V% n5 L# P4 i! [& s                if(csym[1]=='*'||csym[1]=='/'){% i5 z- P& p" H1 ?: L0 f
                    GetExpValue(t+1, csym[1]);, w/ C/ y& l+ u/ Q* s) |
                }) ^3 T" H8 x$ H* K  v* `" c* D
                else{
1 \( _2 d7 Q! K; y- [. E8 N. h                    GetExpValue(t, csym[0]);1 F9 h) j" l9 @+ E. b4 Q
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
# k7 z( w7 H3 |; a5 O3 u* @                }, |7 L# w. l8 I9 u3 {) E. l' P
                nLevel = 1;. ~. I" b3 ]- X: X% Y
            }5 M2 H+ q9 Y- p, V+ x" P) ?
            else istrin.clear();
& @# t2 u! w8 Q' k        }
# m1 f, q4 ~8 W* G" u6 q1 F8 i        else{nERR = -1; break;}
5 u, U4 ~4 R+ J' u& H; [6 c) J0 d    }
; z7 U8 S# D/ n+ d. c    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
6 ~$ m* I( \; w: i3 |0 r/ C# [    else nReturn=GetExpValue(t, csym[0]);
0 g; T1 z! s/ O; g2 }7 I6 h    return nERR==-1?1:0;+ d. u& ]  z( r+ v
}}9 c9 N/ E, ^& U) c# U0 W* r

8 w0 m6 Q% o! b# F2 S
0 `; H: {, q9 t3 W1 I
/ E  N3 u4 ^9 h* Q" l3 f3 z# a4 @函数模板使用示例:
" N- T4 e4 m+ @' x& `7 ?+ K在以上那段代码的后面加上以下代码:/ T8 R. h, d) r- m

% x5 s" [+ D# C4 S" W& e1 _$ L - W4 L' a6 |) Y) f4 Q0 ?

1 d# l, }- j. c" V程序代码:
5 Q. ^1 J$ q8 d9 Y8 P
' k! e! e4 x) U2 a& x6 L#include<strstream>1 S/ J# ~# h3 v1 J- h$ O
#include<iostream>
* X( a) \# P8 I8 ~+ @#include<string>
  E3 I) y  B. k; |' [$ y8 nusing namespace std;& J, E/ b. T! q# W& p7 v: T% p3 Y
int main(void)
" ]6 L* `, d& c1 C{. ~" V0 t3 ]: H7 y4 z1 Q9 C* a
    string s1;
- h' Z3 l+ y1 u4 D; C/ S    while(cin>>s1)
3 h3 _$ t: s$ r' D. K4 g$ `    {5 \3 n4 |8 K& w" B6 d: J8 I1 O
        istrstream isin(s1.data());
+ V" Z) J" w4 N9 }! ^        double d;7 U9 s8 Y; f  y% z/ g7 C% m0 J  M
        if(fy_Exp::GetExpValue(isin, d))' s: v* V' p4 j3 Z2 z5 f# b( L- F
        {- K+ Y. L: x+ ~, Y+ t& f
            cout<<d<<endl;9 M% o1 B& m5 f
        }5 M% ^* F9 r. [) P# @  P
        else1 |. W6 t0 m) \6 \9 n$ r* D3 M
        {
! s+ j% t6 y; b) C9 s# p: @) t            cout<<"ERROR"<<endl;* ?% j% V2 [* d, x. j* D. P
        }/ s- o1 M+ q' N. B! f
    }3 P$ O7 h' [2 ~4 _. M% Z" M( Z
    return 0;
2 _3 C& N( _. |8 Q, m0 l}( u2 c6 F/ K( l) j
4 H" @5 \; m- D( j# G/ o4 `

. x- i9 d: P( q7 n' }然后编译执行就可以了(*^_^*)
+ B) f( _4 w0 d其它:TC++上一定编译错误,不保证在VC6上也能通过编译. n' ^3 Y/ {1 O, \! l, k
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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