获得本站免费赞助空间请点这里
返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,4 e: M/ A" v: B! \" o/ D2 E
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式, g9 v4 u7 `$ D
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
; _4 S; Z" {- n% G! B4 ?: |, y参数解释:
1 `/ F8 b1 b3 u2 D: H+ R3 `, Aistrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
/ ?  ]$ @  S4 Y8 @nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定8 c# J; H, y& P) d- I
返回值:/ }+ e0 j: Z7 M2 O, k6 S* L
返回非0表示计算成功,0表示计算失败有错误
3 P9 v1 a) V  Y- Z) G6 P% @4 s/ Y
. b3 E. e( \  \9 ^5 ?0 o
9 _/ n* Z& Z4 A
程序代码:
( Z0 l* Z% T0 Z+ B0 D4 t  G2 h" I. V1 ^2 G/ j
namespace fy_Exp{
$ ]! q2 Q, ~  f' L5 e4 xnamespace {template <class _T>' E* K. \. k6 ]
inline _T GetExpValue(_T t[], char& csym){: j' \7 d2 n& ^) V, ~4 K5 }$ z
    char c=csym; csym=0;0 @( Z2 p- S$ P; L& Y9 J8 l0 e
    switch(c){; J+ M, K2 M2 N/ d4 ~
    case '+':return t[0] += t[1];4 e" S6 @+ G. y% ^5 B% H* g
    case '-':return t[0] -= t[1];/ x/ x# i# \5 K2 F# L/ @; Z
    case '*':return t[0] *= t[1];
% e/ P) E$ i+ H5 {4 {    default: return t[0] /= t[1];//case '/':
# Q. ]; K% a$ j4 Q    }: \7 S6 `/ I/ Y1 t
}}
8 `+ h4 H0 \$ {0 I  r+ l& e  B0 R, _template <class _T, class _Tstream>
2 {7 g" R1 l" F( H2 i/* _Tstream: inputstream, _T: get return value# y3 f: m! X9 b. y* R$ c$ Z
* Return nonzero if get value successfully */5 z# v  d2 R: X
int GetExpValue(_Tstream& istrin, _T& nReturn){" T- W- H# w0 f2 ], |
    _T t[3] = {0}; //雨中飞燕之作
4 q! V0 c4 F0 N  j8 w    char csym[3] = "++";
0 f$ o; k# l5 _  \* x' U    int nLevel = 1, nERR = 0;9 f$ \8 j& P- d2 n2 u0 S; k
    if(!(istrin>>t[1]))istrin.clear();
9 Q' _5 o1 S& E2 d" S$ L0 J    for(;;){
# I7 L0 M7 r0 R6 y3 \1 `        if(istrin>>csym[2]){, Q. [8 h- }- P# f
            switch(csym[2]){* L, K0 w! B& C+ `
            case '(':
/ ^* D% M; a! C) k                if(!csym[1]){nLevel=0x100; nERR=1;}else
! z4 Z; @0 o" c# t                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;7 l4 V3 R' Q0 [/ p& _
                else{nLevel=0x100; nERR=1;}7 {( p  x* D6 s, C5 l" s: E# N+ l' \
                break;
! K9 u1 H3 f/ `* [& |3 N            case ')':
! f! S- T$ B( L8 n7 }                {nLevel = 0x100;}break;
% G- {# o9 I. `2 a            case '+':case '-':case '*':case '/':
2 D4 G* z) r* z! d4 i2 C$ b                {csym[nLevel++] = csym[2];}break;
+ R7 q. |0 u; R( t+ @            case ' ':case '\r':case '\n':case '\t':continue;" [1 q, x& _) y, U/ x+ n
            default:
& U/ b1 M% R" y& A3 ]# _, u  I! w                {nLevel=0x100; nERR=1;}+ C* I2 e- d6 x# @/ a+ h
            }
9 W& t9 y4 P) H. |            if(nLevel==0x100)break;# f: [6 ]2 d6 [; G9 r9 f
            if(nLevel&0x10 || istrin>>t[2]){
: _- C2 P/ Z# y4 _+ I                nLevel &= 0xF;- L; S7 f5 M* H! v& E8 \! l: C
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
. Q# y8 K3 b+ H) \9 g& |  O                if(csym[1]=='*'||csym[1]=='/'){5 g& G; _- R" e$ a0 e% r
                    GetExpValue(t+1, csym[1]);  a# W: H% A  B  l& s7 p+ ^4 l1 ^; n
                }( _% \) d0 o4 E& c
                else{+ L' @; a) H1 P1 L& b% a& \
                    GetExpValue(t, csym[0]);: M% O& P8 b! J
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;5 o) d( r8 `% c6 j. ?
                }9 O& y9 a5 x* F% R) H  O3 v3 d% w
                nLevel = 1;2 l& u9 o# Q( u' Y9 a" ~
            }  X: n" M  K% u& G+ P0 c" z
            else istrin.clear();
9 N5 F$ p0 {. v1 s1 y8 z3 R: f( o        }
% J0 s* S6 H- G        else{nERR = -1; break;}
- n( G3 [- v' C9 k0 Y* i    }
; @8 t. E  c: \7 T8 \+ G" L% K    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
6 e6 ~  Q" W, R: |: ?) D    else nReturn=GetExpValue(t, csym[0]);
2 \' ~: K4 W# @4 U- z' O    return nERR==-1?1:0;
* t3 o! p- q: j* X1 V}}3 u) [' y& V2 T/ C
5 k7 x  z# x  y7 r0 q6 B% ]9 s

) ?5 U6 H& t4 F7 ?
. w2 \5 ^) j3 L函数模板使用示例:
4 Z: C, B! T2 `9 w" P8 E$ f在以上那段代码的后面加上以下代码:2 K' c# a1 x. F" \0 W, E
+ y# a$ `  K, G/ a/ d& T4 o

0 N; J+ X5 H! H$ V0 ?: M" C# T
8 t9 X6 C7 b. H& |! v4 ?% {# {程序代码: ; j( y# M' B5 h, H

  [$ I$ H' i0 p#include<strstream>
2 _, _- [2 j: w9 h7 a/ M#include<iostream>  Y: W8 E( N) r
#include<string>
1 c. o- v3 F# V) a8 [using namespace std;! @/ G! Z: y2 L) @; s
int main(void)
- O9 w7 q* r$ B3 W# I7 H8 t{
2 ^- j2 S) A& L% k" Y* e    string s1;
6 o$ w2 J( O$ B( r2 I) v  k0 {) G    while(cin>>s1)- ^: x8 ]6 l( ^9 h7 I3 H
    {2 u! L" b4 ?  c1 \8 ~
        istrstream isin(s1.data());
( d. _$ c: [' j# c0 @7 r        double d;
7 a" r; e. e5 Y' `' i4 W        if(fy_Exp::GetExpValue(isin, d))
4 l  {! Q' W8 e        {! v& ~: ?! P3 ]# z( X
            cout<<d<<endl;
/ o% ^9 T' ?5 z: x3 O        }! p0 v9 ~" S3 @/ L
        else0 l. ~# o2 @# }( `0 z# m7 ~
        {. R( t  ^- M8 M! _9 |# S
            cout<<"ERROR"<<endl;
- ]# {9 n# Z; i' i1 W& E        }
) ^0 O) z. P) @, M1 H& b* ^    }- {5 [# @' d* ]) e3 u/ C- n0 c
    return 0;) ?/ G; t; D; `. M7 r$ r
}  a( s- g1 R8 ]
* ^1 P) x; Y/ R4 Q( X

' o1 i% ~( p' `% z. J7 ^0 i5 }然后编译执行就可以了(*^_^*)1 D0 z  w8 q" N* S' M
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
2 [  R! V8 z9 Y4 I+ K      建议使用VC7或VC更高版本,或者使用GNU C++编译

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