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

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

在9月8日那天我特意编写的,给大家分享的,
5 e7 X8 y; s) W) V( f  m一个很方便的函数模板,可以并且只可以计算含括号的四则表达式  m9 c& l$ P* M' D  D
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)) M9 w5 [# `0 S0 w$ k( B
参数解释:
& {2 G7 j- Y) j& [istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流0 P# ~7 X4 S0 d( N. w2 A* t* ~
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
0 L" W0 p4 y, X8 f2 ^- I: i  C返回值:
9 Z. ]  l% _$ R2 h8 |返回非0表示计算成功,0表示计算失败有错误
+ Y( r& r0 r; M7 u' N" G, B, r  u, U
# s: v5 [7 ?4 h' f. d
; r% H. \- H% Q) x4 c% q6 v
4 E7 u+ W" f8 P; D4 h程序代码:
9 ]# b6 M( [- S2 }0 R$ F- s2 f9 ~2 r4 O( S
namespace fy_Exp{9 \. `+ q$ `+ {
namespace {template <class _T>
8 |' V% P. z8 Q' \/ m* B. minline _T GetExpValue(_T t[], char& csym){
& w" A  E  C% ~+ T- m    char c=csym; csym=0;
+ B( I0 {& F6 w    switch(c){2 Q9 w/ ~% ]0 F: X3 ], z* i) c) o
    case '+':return t[0] += t[1];
9 [3 Y- ?/ m+ A: E7 m+ k    case '-':return t[0] -= t[1];
! M7 Y$ u: \2 @8 }& Z3 D# s    case '*':return t[0] *= t[1];
5 o6 W; n6 c; i* l7 P$ K3 G6 A+ ?' e    default: return t[0] /= t[1];//case '/':6 m1 a, A, e' b- x: l8 i
    }, `; W2 [4 v4 c- y
}}
* ~+ T/ r6 V( S' R# Htemplate <class _T, class _Tstream>
. _3 J$ ?' }  @; d( U/* _Tstream: inputstream, _T: get return value7 a8 }8 y1 X7 O* O% b$ t3 I
* Return nonzero if get value successfully */0 X  q# A' m4 w/ t7 T2 ^, U
int GetExpValue(_Tstream& istrin, _T& nReturn){3 t$ K, M; b, v" t$ a
    _T t[3] = {0}; //雨中飞燕之作
+ Q  l( V. s8 j1 {0 K( [. d% l! Q    char csym[3] = "++";! C) N, v" w9 s! N4 S5 r/ ?- Q; t
    int nLevel = 1, nERR = 0;
( a( p- v( E4 r, B8 v1 H    if(!(istrin>>t[1]))istrin.clear();/ j, C# E* J  }, K+ }/ o7 }9 p/ T, v
    for(;;){
3 Z) ^; ?# f% c: X, K        if(istrin>>csym[2]){
$ O$ P. a. `* P            switch(csym[2]){
3 J4 S- ^! m: g  A            case '(':9 ]3 v' p! R  s8 ?; J& Q+ u
                if(!csym[1]){nLevel=0x100; nERR=1;}else6 _) G' D% J% k$ w4 ]* ~
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;. C0 W+ o9 Z' s6 h, z, H
                else{nLevel=0x100; nERR=1;}
. P4 u" a7 M: ~0 u8 {$ K                break;# w) R3 W+ R9 M4 T/ e6 r  g! }
            case ')':0 |! k& @3 ^; t  ]  k+ G4 i
                {nLevel = 0x100;}break;% P* s: `* b" Z) f
            case '+':case '-':case '*':case '/':+ L2 f$ Z3 s: Q8 {
                {csym[nLevel++] = csym[2];}break;
+ W! ~7 b- d. D' |+ W  w" W            case ' ':case '\r':case '\n':case '\t':continue;
/ U" P- m4 I! O1 |            default:# h: Q' B+ i% V6 @
                {nLevel=0x100; nERR=1;}
" {$ N+ @) ]  c; J6 h9 z            }. S! E3 M9 a6 P2 R) h6 _  b! ]5 w
            if(nLevel==0x100)break;
3 e9 p; U" V; L4 ]5 ^, ~            if(nLevel&0x10 || istrin>>t[2]){5 |" w9 [* ~; i, y, X9 ~& P, X
                nLevel &= 0xF;- d: t5 g& {& J6 W1 t# |
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
8 n" {2 F7 ~  L0 E( x, ?9 a                if(csym[1]=='*'||csym[1]=='/'){6 b" K- U2 I  s9 Q9 F4 ~& x
                    GetExpValue(t+1, csym[1]);
) C# G+ G% N6 F+ Y+ b  z" K. ~                }8 C2 [) J5 W! h5 g, \
                else{3 J5 w4 w8 l0 C
                    GetExpValue(t, csym[0]);- m2 G8 f% [4 W- c! l. h
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
+ h% C% G, M! V2 l( q* `) b                }% P$ K* K. O+ N
                nLevel = 1;
$ A. E& f5 C, W/ j% Y; z; H            }$ F- N9 A$ E6 i
            else istrin.clear();( U# k1 b! Z0 L
        }
  d. c" J1 }1 Z9 O# q5 J; _# \5 I        else{nERR = -1; break;}) _! B( N# g. @5 _  t
    }
' I& ^" a. @; x& o* b+ _    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);/ v& G( P" @: Z$ w$ g
    else nReturn=GetExpValue(t, csym[0]);
- ?1 `4 B! s& m4 O    return nERR==-1?1:0;
# v, r/ ^* o: m5 D4 h}}
: F  e! O$ ?" R) ^' R% n* z; A& M' Z7 j/ |! d& p& Y$ i- w; ^; f

9 T, }; m0 K3 I1 P
6 G& X2 p0 f" C8 o函数模板使用示例:* s+ C" P! l) M. E
在以上那段代码的后面加上以下代码:
2 K9 N  P+ K2 A
) ?0 C3 J7 G5 ~% p ; z9 `- e4 ^, j4 U

" ^+ N& `2 b) L7 G* y' }6 O程序代码: 8 Z+ P! b+ V6 f; a
- w* R4 B' {9 q8 _
#include<strstream>
# M$ S$ M3 J& o$ X#include<iostream>
; B6 [* U1 Q6 Z5 q, Y" ^#include<string>
# H4 b7 L4 z2 O- qusing namespace std;
3 |: o- ~% b, P# `6 w3 k- Pint main(void)
" M) F, o) o, Y. T- T/ [; F5 z7 W{
$ [7 e) w7 T- Q0 j  S    string s1;
. [3 |* C& x$ w0 r* ]+ ]    while(cin>>s1)& _( b9 G5 Q+ D% v) U$ Q) N
    {+ X4 t4 D- W' ]1 `. N
        istrstream isin(s1.data());' t( P) d* h9 B
        double d;
8 \/ ]% k. K" F; S2 i' h# ?        if(fy_Exp::GetExpValue(isin, d))
$ I3 n4 }* {5 d0 Q( j        {' g& e2 q+ a' w5 l1 I7 O: O
            cout<<d<<endl;
! {4 I& q4 b5 L4 Y2 U$ V& m        }& ]- d! E$ Y3 U/ a3 _4 _7 I
        else
1 ?, p2 p, ?/ K- A3 _4 m* [2 P        {
, g" y( H, s; e% \+ h            cout<<"ERROR"<<endl;
  K9 \. S! X8 u2 [" V  e1 S$ L3 Y        }
. J2 w) y0 V& o# P5 o% [+ q5 o    }1 ?8 D+ T0 p! e* C  R( u- d
    return 0;
# J8 a; ~4 x4 S/ a+ u" t* J}
. m/ p, G: y) q0 `8 w6 ]/ v! j' G
# l8 p/ s" `# J$ x3 ?7 y" A5 H, O$ m! ~/ e  ^' V- r
然后编译执行就可以了(*^_^*): F7 n4 s( i$ I3 B8 n
其它:TC++上一定编译错误,不保证在VC6上也能通过编译8 c" u1 \6 E$ Q- m
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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