返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,
  z5 o& L6 F4 {: j一个很方便的函数模板,可以并且只可以计算含括号的四则表达式. a- Q% ~; |) j. U2 x+ [
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
* z7 u  i1 T- y' G参数解释:
/ L& S% t1 I8 K9 d! a% Aistrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流# j4 Z/ Z) r( `9 y  D
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定9 a: [" h% d) I6 j4 y8 p1 s7 o, n' w
返回值:
4 u2 j, [+ i4 R% Y7 }: B# p7 I返回非0表示计算成功,0表示计算失败有错误
" S) Q: f9 ?" b4 Q" c
& m/ `8 z6 `- Y& l6 l; Y. S- U
4 u! g0 r+ T! [' p7 \6 z8 g
) f, o9 X" n; D. v" W) `" c0 Z程序代码: $ \" u2 Y( r2 V, \' S
9 W* ~# s* v2 @; H. Y, m# D
namespace fy_Exp{
" I  y3 U* K" t9 ~namespace {template <class _T>8 _" A8 L6 n" Q$ g7 s
inline _T GetExpValue(_T t[], char& csym){
/ B+ H4 A* W7 T2 g* ^$ _    char c=csym; csym=0;
. @) j$ X4 G/ U  `/ U    switch(c){6 M$ b; j) x  F$ U! v! c# G
    case '+':return t[0] += t[1];
/ c9 B" t+ B, ^' w/ N) a    case '-':return t[0] -= t[1];
7 x2 \" E; S; z# @9 ]    case '*':return t[0] *= t[1];* F1 h( V+ n- u
    default: return t[0] /= t[1];//case '/':
8 g! K) m; q8 h2 K) v    }( C5 k# O3 t  s
}}
' G; ]* u1 V* R+ S- ]template <class _T, class _Tstream>
; k& R' g; t: {1 l; N7 U2 y/* _Tstream: inputstream, _T: get return value7 r/ w7 y" ]6 Q  p/ Q8 _$ G( x3 W
* Return nonzero if get value successfully *// ^( M( p8 i* u
int GetExpValue(_Tstream& istrin, _T& nReturn){0 }4 Q% r6 v* `& [
    _T t[3] = {0}; //雨中飞燕之作
; V0 J3 c( X% c9 `$ x& B) w7 D    char csym[3] = "++";
9 ~/ Z' ]6 |6 P1 J, n+ E- e% y    int nLevel = 1, nERR = 0;; ~+ ?/ C; `( K) D5 e
    if(!(istrin>>t[1]))istrin.clear();& g& L% F$ y" M% J- m
    for(;;){
* [- G9 A0 e# d( s. j1 o9 Q        if(istrin>>csym[2]){8 C4 D$ _# t6 n) ?5 m
            switch(csym[2]){8 C! X, h) s, o$ O
            case '(':
9 b! b! }1 P' f' L& Z4 d                if(!csym[1]){nLevel=0x100; nERR=1;}else
: @6 ~+ D) M: U2 w" Y7 V. v$ E; g+ o) [                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
5 Y, w' ]$ @$ ^                else{nLevel=0x100; nERR=1;}
9 Z( V7 q0 D, P5 w( S( W3 P% ?                break;' ~) _7 S3 I6 u1 d5 K9 D
            case ')':: U5 h, h' Y6 L# E
                {nLevel = 0x100;}break;3 s9 p6 X) P3 k6 V* K6 _+ B
            case '+':case '-':case '*':case '/':* w$ U0 X2 M3 I3 X! g8 r( k1 k
                {csym[nLevel++] = csym[2];}break;, l0 x; r0 _  P8 U; ~" ?
            case ' ':case '\r':case '\n':case '\t':continue;' `+ J1 G8 N: j, E# \& F( @
            default:( b4 h; V& z! j& _; F4 L
                {nLevel=0x100; nERR=1;}
& E0 c6 \2 N, S, d7 {  f$ P            }
( c: ^+ @! y8 u7 R) e: h            if(nLevel==0x100)break;
3 Q7 ^, A% ^0 W! ]            if(nLevel&0x10 || istrin>>t[2]){
, w# ^2 ~* Y- b, `2 X4 I                nLevel &= 0xF;
, M. C  N. H) [                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}: \. W1 q$ A; @' @  ~' G" q, l
                if(csym[1]=='*'||csym[1]=='/'){/ m- S- a. ]" U' i
                    GetExpValue(t+1, csym[1]);3 z2 T- Z$ d4 H5 a# t+ ^
                }
/ b8 I* L" v; F1 v8 m                else{# y& Z, C0 G4 F- B; Z
                    GetExpValue(t, csym[0]);9 h- B8 c. v( e# A
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;! b/ r3 }4 A& i
                }
, V& ^/ v. |* R0 z2 n5 j2 a  c0 T                nLevel = 1;" @( H2 m4 i6 }% {! y
            }% w$ Y. ~; I) [. ~& ^/ [, T4 N
            else istrin.clear();* e/ d- g$ v( W
        }, v3 T: ^1 _/ A2 r, n, z
        else{nERR = -1; break;}
8 _/ e( {, F+ j9 H! K; l4 Y( N    }
2 l$ S3 i9 h" M; L" z# L    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);6 b1 m3 r, `2 u0 e8 t9 ^) \  B
    else nReturn=GetExpValue(t, csym[0]);. R3 P; I2 q! @6 y7 f' \  H
    return nERR==-1?1:0;
( X7 D' o7 D* r1 Y5 `5 u# {}}
/ g% K7 l6 J# `) L  J5 s$ l1 n! A& l
; v$ Z1 R! r* v9 R/ L

; h/ u, s4 u% J, }& ]5 R0 o函数模板使用示例:; {8 q$ ^' A) F  ~. P
在以上那段代码的后面加上以下代码:
. C+ ?! ]; e  R
, d. [  F  W  R8 ~9 p) ]" A; b
0 O3 Z. f1 d. o9 s6 a2 x; T
% l5 S6 S" R; {  U6 Y程序代码: 2 b% G( b& j  s5 l
9 V$ T7 l6 R: M  a. S
#include<strstream>
3 ?9 o$ X# M% F% c# z#include<iostream>8 r3 ^9 r1 T: P
#include<string>
4 A, l. F' ]# w& \using namespace std;
2 O+ a: F7 H' |* z0 v! a- P% H% }int main(void)6 D( ^7 Y! O& j; f' L
{, f; o! i- c/ C7 ~
    string s1;% k1 N0 F4 S! M' \
    while(cin>>s1)
" K; Z' y1 Q- ~" f6 K    {
( T4 M% E( \+ D' M8 f# o9 C        istrstream isin(s1.data());
: x) Q* e6 M  ?0 Z+ l$ M) ?, H        double d;( V- c) u( a8 `' d7 L
        if(fy_Exp::GetExpValue(isin, d)); ^0 R: n6 d7 _7 U8 |5 ?' }
        {* x8 V7 b- a0 V$ \0 [" ~
            cout<<d<<endl;
/ {# f$ }0 G. J6 h* d. ?* |        }, }- A% d! }4 K8 h2 `/ Z! \4 B+ p
        else5 n. {" B( Y& Q
        {
& i9 U5 R, z) D- s1 c& `$ u: |            cout<<"ERROR"<<endl;
+ p5 Y8 H9 ?1 U6 j* B: ^, [        }  u$ U. v( L% g9 J
    }
3 ~' }  E- `9 \4 i/ m. X    return 0;
1 E. d  F! H2 P+ `/ b}
' u" ^. u2 F' D- {
+ k8 Z) S! j3 z% Q% R8 w
: ?" I/ |9 d; V& I然后编译执行就可以了(*^_^*). G/ \* ?$ ]6 N
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
& k; N; o- z* J5 @/ C2 S      建议使用VC7或VC更高版本,或者使用GNU C++编译

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