返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,: q- O$ |3 \5 P1 R
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
: A0 k( {0 ]! p. m+ X8 T' G. R# @. \只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)4 y7 ?- G* m/ G' O) p, ?
参数解释:
7 X2 b5 P# [4 a+ K' Mistrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流& _- L/ F, f) R* I  W+ `' y0 ]
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
9 ~, Y  ~$ Q/ y3 ]% ~$ P返回值:4 J1 {- J: I/ o9 k
返回非0表示计算成功,0表示计算失败有错误
3 K/ J9 ?! D  c3 p( z! k" |; `4 M( Q+ D

7 c- S' d- O+ i  |
% M  p+ r; h- k( t程序代码: 6 |% ?3 g# o. z, j% B; P

. }- q3 @' x, n5 D. N4 @) r0 Fnamespace fy_Exp{! a5 U0 H" h) ~1 n7 x
namespace {template <class _T>8 i8 a1 r& a5 T( D8 Z& `) w; g
inline _T GetExpValue(_T t[], char& csym){" D8 i+ q: }5 `  e2 m
    char c=csym; csym=0;
6 p( b) E% X. r0 j8 `9 E/ U# f    switch(c){" `: i: ?8 ?7 I% {7 y5 \2 \
    case '+':return t[0] += t[1];# v% R& r7 x1 w1 _+ @
    case '-':return t[0] -= t[1];* {1 d. q0 j; H  ?/ z( g8 S( U' g
    case '*':return t[0] *= t[1];
7 T, S) b6 `- A    default: return t[0] /= t[1];//case '/':
( K7 f6 M. R3 _0 a; g: g    }
% l  ~8 n4 W. h' i9 h8 L# P}}' Q/ G( g' ]& O$ s2 Z
template <class _T, class _Tstream>
3 C, P* c/ E0 p/ [# l/* _Tstream: inputstream, _T: get return value
( g+ ^1 ~: Z/ h& l; L+ X* Return nonzero if get value successfully */
6 @9 L7 J6 s: ]- ]0 x8 hint GetExpValue(_Tstream& istrin, _T& nReturn){$ z. [! g7 L* J  P* U- l8 ^' c: B! E
    _T t[3] = {0}; //雨中飞燕之作
% P- [4 D$ x+ O5 i5 \" ^/ u    char csym[3] = "++";! c+ t$ x2 z4 h
    int nLevel = 1, nERR = 0;
# P! V$ u. e& x2 E. M    if(!(istrin>>t[1]))istrin.clear();# Y' v, N/ Y9 y" z; L" F5 p
    for(;;){
% J$ [: \) d4 U1 f5 C6 S  w        if(istrin>>csym[2]){6 \2 }) t9 r  Q
            switch(csym[2]){7 Q( L; Z" m7 X$ u- u! f
            case '(':6 w" v7 x) Y) d3 [+ u4 j
                if(!csym[1]){nLevel=0x100; nERR=1;}else
* z( ~6 D2 d6 ^. ~1 Q$ ~+ c, D                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
& e5 \( f2 \% _                else{nLevel=0x100; nERR=1;}3 `! G' r2 q* E4 {7 r& x9 O
                break;: w  I1 a2 P6 ]2 x
            case ')':
% J- V- d* {2 p( s: s0 _                {nLevel = 0x100;}break;5 c1 o7 O0 V$ z" q
            case '+':case '-':case '*':case '/':
1 p. B/ c" z# y& r4 u0 F  ~                {csym[nLevel++] = csym[2];}break;
! K. J, M2 C' L9 v# y! Q& `2 f            case ' ':case '\r':case '\n':case '\t':continue;
/ j# s# ^+ i: ^" [4 e( y/ d  Z/ @, ^            default:5 T7 v1 _- B7 |$ f) y
                {nLevel=0x100; nERR=1;}' V  f2 W! u# O+ g) z* W+ j' `
            }3 ]  m$ B5 y9 S& V  c( m
            if(nLevel==0x100)break;
/ p* k. j1 u, w' `            if(nLevel&0x10 || istrin>>t[2]){* Q& p! \+ C+ {( b$ `
                nLevel &= 0xF;) J1 S3 s* t4 r; J
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}, Y) E, |  x1 i9 V8 l6 G9 f
                if(csym[1]=='*'||csym[1]=='/'){
8 `; Y# \# W6 q: b) m1 D                    GetExpValue(t+1, csym[1]);
5 [9 P  f7 Y+ z- s% m) ?                }" w" E; @7 z3 V/ q; u+ R
                else{9 p7 I! l0 G* A; q4 _( l
                    GetExpValue(t, csym[0]);5 H" Q- A9 a6 x0 r
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;" H  C) z. c. M. `) p3 `
                }
  S; x: E3 u( S1 y( m                nLevel = 1;" \) D& N" w3 A5 r7 A7 n
            }# k+ \* A$ }! n, \  E( ?
            else istrin.clear();# f# |  d' j: ~) G' S
        }3 y. ]4 }9 V( z0 L2 N& I
        else{nERR = -1; break;}6 Q# [* w  q9 N& `. ^: S: D2 o
    }
1 }' K' [' n" |7 d' H7 ~5 |    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);; L( L2 {7 K8 j* P# Z2 Q0 x9 Y
    else nReturn=GetExpValue(t, csym[0]);4 W6 t% T9 r" `' j
    return nERR==-1?1:0;! @7 V( x# r! k! z; Q
}}2 g. E5 n9 g& ?% i8 W  D1 s3 q' ~

7 r: [6 Y0 f* G2 Z2 _: k& G8 \, E. V3 |0 _+ S( n( }3 x! m( d% P$ _4 H
# N) F. g" D/ }: q* I6 J5 f: c' V
函数模板使用示例:; V  X0 S  I' \, S' d
在以上那段代码的后面加上以下代码:+ B2 {! `$ Y& {7 ^$ H( [3 r* V. r
  ]1 E: @* F' c
8 y, _; I3 U5 s& C9 R% V( w" b( B
9 A! g5 a$ K% L) ?& f
程序代码:   k& V# W* O: V! o* f0 A

& E2 u: ], F" k7 p2 q, y5 V#include<strstream>
( P3 C: D' \0 P2 o3 e* J; |#include<iostream>% O7 T' A1 ~. z" j6 U
#include<string>
1 `/ x: ]- j" p. T" Ousing namespace std;2 ], N- f( N% j* J
int main(void)
" f3 `, L, a- ~{
+ q9 g% q* I; W) t4 g9 U' p    string s1;
3 e% Z! r! Q0 F' z+ n" `9 c    while(cin>>s1)% c) z9 c  \- ]: E7 B
    {4 }2 Y7 i) @' O
        istrstream isin(s1.data());
3 X* s% \6 b/ e. I3 f        double d;
+ Y" c; V: G0 u: W# k# ]' p; ?        if(fy_Exp::GetExpValue(isin, d)): h; H1 F  O5 W! c/ s% a( z
        {8 N/ j: a* g4 B# Q* P6 ^
            cout<<d<<endl;: W' ?/ W. _( ^' V/ l2 B8 e" h( q
        }
) \! h6 ~- C5 _        else
3 Y1 G" y/ u" @        {
8 F  s( q8 T, I            cout<<"ERROR"<<endl;
; v$ ]6 B9 O+ L1 z+ Y! c        }
$ I3 H3 |: d7 B    }. ]/ P9 w  b7 P8 w2 F8 e' X7 o! ]
    return 0;
, N4 |4 e! \3 o! U}
/ g+ @1 Z, Y* c; J) A
% L, n7 R2 ?; Z( [, L9 r3 t7 z/ e/ O9 v4 x3 |6 Q/ x+ E
然后编译执行就可以了(*^_^*)
! c  l2 O. g+ \8 u3 @6 d其它:TC++上一定编译错误,不保证在VC6上也能通过编译4 D- \" k1 n3 Q' Q; [
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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