返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的," J9 o9 c/ S# ]  s. {- O
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式1 R: |% g& f0 ^' Y7 j9 K6 `
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
# ^# L0 q2 ~4 Z, _, n0 Y; G& U参数解释:0 Q# V: s! C- p1 d  {* `2 j# d
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流0 Y9 l: A9 u, r
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
  d% L2 K4 G" s9 P; S返回值:
7 \1 A# P2 U7 G: f  X$ T4 w* e: ^8 D返回非0表示计算成功,0表示计算失败有错误
. A4 E) v, }) B
+ t& b- L* `3 j  e& n) P! U
" z8 |' b3 [* @5 C/ n2 P# B, d( D: i) H9 t" K6 @/ |/ v
程序代码:
8 W" `, }. S0 E2 G8 W! u! C! I' N- z4 k' ~; H/ Q
namespace fy_Exp{
7 s2 X( R1 d: lnamespace {template <class _T>
+ a4 B  F  v6 m8 _* o( s8 Vinline _T GetExpValue(_T t[], char& csym){! \2 E+ y0 m* h' x3 \6 a
    char c=csym; csym=0;. p6 O* y' \1 y, q! ?4 N
    switch(c){: T* V- s; U+ J* ]( F  F
    case '+':return t[0] += t[1];. R0 [% W$ H: \
    case '-':return t[0] -= t[1];
7 Q1 `. @  v6 [7 v% z    case '*':return t[0] *= t[1];
: @# w# D/ _2 K. C4 I0 u" ~    default: return t[0] /= t[1];//case '/':/ E  z! X3 q! e4 U4 d3 V: _- Z
    }
; j+ O1 C" b" o: m  M}}0 E. n5 f4 `" B  O3 ~3 [: K
template <class _T, class _Tstream>
: ^6 I1 |' k' q" B# P5 o* D: ?/* _Tstream: inputstream, _T: get return value
$ v# V$ i& [, |5 D3 \* Return nonzero if get value successfully */
4 [4 C% d& ~! x& Fint GetExpValue(_Tstream& istrin, _T& nReturn){9 ~4 ^  r9 L; j% R5 k
    _T t[3] = {0}; //雨中飞燕之作
3 _) G6 W& K" O3 M2 g' T    char csym[3] = "++";
' J/ f) b* j) Z/ t# b+ ~, n    int nLevel = 1, nERR = 0;" M5 s  \9 r% T. j
    if(!(istrin>>t[1]))istrin.clear();5 m7 w4 `3 y. L) y+ X9 C
    for(;;){+ W, z7 ~; p2 M) P
        if(istrin>>csym[2]){+ i) ]; c! H  ?5 Y8 Q8 S" _
            switch(csym[2]){# K: J) U, y, B! R
            case '(':1 C+ A$ q$ `. Y: t: u7 r2 L
                if(!csym[1]){nLevel=0x100; nERR=1;}else( |+ y7 z' w- l
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
( s4 D5 _2 N# J! G& {' B                else{nLevel=0x100; nERR=1;}: V8 R8 R- b) j0 Q! e2 c' i) h
                break;, @6 p! L% }  ^% L! W2 m$ i8 p+ Z
            case ')':5 D! d0 u# t, x3 g! b
                {nLevel = 0x100;}break;
/ F4 x; S% S& v) g0 a. ^            case '+':case '-':case '*':case '/':
- P5 Q7 y/ W) O' Q' E2 k                {csym[nLevel++] = csym[2];}break;! C/ ?! ^$ R/ _8 k' {
            case ' ':case '\r':case '\n':case '\t':continue;
, ^2 D. H! f* b  D' x9 x" Q1 g( U            default:
! D& y( d% K. y9 i' @8 n" s0 w4 r                {nLevel=0x100; nERR=1;}
* y0 C. ~/ N( W, B" Y            }
# L! _; j$ R( O5 Y3 X4 ^            if(nLevel==0x100)break;
3 p2 v* @! o3 k6 e: W0 ?" C& e1 t            if(nLevel&0x10 || istrin>>t[2]){  P' q3 H- v' }5 e9 ]
                nLevel &= 0xF;
" G9 ~# T+ X$ w& h5 |                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
: p3 Q( N5 S# n                if(csym[1]=='*'||csym[1]=='/'){1 n" V( v: C  D; x
                    GetExpValue(t+1, csym[1]);
1 m* j7 G3 m2 I/ t) U7 G                }
, A4 s/ `% j( i% ^+ J1 N: U0 T                else{
4 A9 Y: p. f& d. r# j& ^, }  \3 q                    GetExpValue(t, csym[0]);& r8 H% A2 x5 ^( k5 V
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
4 b: X) x6 S: t' }                }
; E/ u2 g) a! x% L2 Y                nLevel = 1;% N2 B+ W% S: k  B$ N9 U7 b' |  h1 H8 J
            }
1 o+ Y9 R4 ?- o4 C! E% ?: E            else istrin.clear();
% U0 G+ q4 l& f        }7 l3 X2 |2 _# o7 ~; x6 d1 L% ~
        else{nERR = -1; break;}
7 G* T6 ?5 p- b9 f    }' ^. d1 h8 ?, z
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);. ?$ u% ^% }& e
    else nReturn=GetExpValue(t, csym[0]);0 W, r4 A# u% n
    return nERR==-1?1:0;
+ O( g( l/ t6 R. C- Q' L}}, m/ ?9 c1 d; k* A' B2 j

  H6 q5 @  F3 W
' j5 s, x/ @1 G. M: i( f) K4 o$ o+ {! _2 D+ I
函数模板使用示例:% @. x( |, J; j  j' s. ~
在以上那段代码的后面加上以下代码:
' l- E: ?' m7 A6 ^5 N7 b0 o0 d! r! `0 q# Y

) Z" c  ?  D' G
  i, d* `# F5 S( t# O0 t程序代码: + j$ C1 }1 a, Z6 U/ E

3 g- o7 l- U# Q3 g2 w#include<strstream>
6 C7 h7 P$ j3 ~. e1 v#include<iostream>  v  [  x2 I2 M: S7 T6 k% m" c& R# F
#include<string>
% t- l' n& W" A. dusing namespace std;6 e7 x2 a. U: [  z
int main(void)' X1 w6 Y* b: E, ?/ @1 r0 D4 |) i
{
, t: E  C! P  f9 e5 n    string s1;
& P6 D6 u) u9 W! ]7 R    while(cin>>s1)
$ u. ]$ Q( \) l9 T0 d. T- V8 f    {4 k1 q" J$ d7 c$ u) J
        istrstream isin(s1.data());! u, k) k- L. ^6 W. D/ _) U( h
        double d;6 ^9 E9 i; Z0 y) M) L5 ^
        if(fy_Exp::GetExpValue(isin, d))
# V3 m- F0 ?6 v5 M( R) H        {  s( s4 \8 ?: n
            cout<<d<<endl;
) J' x* P* c! |        }" [/ B6 W) {" F
        else, m6 b0 P& Y' s+ H) U
        {
7 z3 |/ e) I% M            cout<<"ERROR"<<endl;
0 V! u# [% J/ }) C( |        }
7 k. y$ \+ K; l; E. C  a7 b    }  U, {, E$ q* s& f' T  r+ e  K# _
    return 0;# @* [' ~8 L, O: d3 x+ D; z0 n+ r
}
: e" R" B5 X7 C
; h4 x. o0 G- j% C' L
# x! b7 f$ E0 Y7 S# q) J& A; k/ G然后编译执行就可以了(*^_^*)
1 E, j8 x0 U8 }* b. p3 W( G6 B其它:TC++上一定编译错误,不保证在VC6上也能通过编译
8 w# K5 \# F- T# v( @      建议使用VC7或VC更高版本,或者使用GNU C++编译

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