返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,: y- I' b; D+ G
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
. x2 z7 n' K9 @- b$ I只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)' N; l4 q" j! \
参数解释:$ g  \2 `' Z5 @4 Z, P$ V. D
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
! D8 b* a/ i6 ~, f/ C/ U$ T' FnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定9 T( i  Q4 t. N# O% e! X
返回值:8 G8 x) P+ X2 p% B# j% }, ~
返回非0表示计算成功,0表示计算失败有错误
' b! L( P; A) C8 p) S, X! n( E  q* @& n- M$ F0 I) p8 U

' n: a% q- E, a0 H' |; R
7 E2 p/ w! G! y8 Z7 |程序代码:
' V+ W5 @/ ]$ r3 h8 d# P, L& H. X- d- s7 L9 v' t
namespace fy_Exp{
; I) s5 l1 s9 E' F" \namespace {template <class _T>1 `; A. I$ o# Q; T9 c% }
inline _T GetExpValue(_T t[], char& csym){
3 n9 M- j# P% Z" R    char c=csym; csym=0;
2 B3 y" P# z" A9 e% [    switch(c){
/ T' _: g8 _8 Y4 o0 A" j2 G    case '+':return t[0] += t[1];0 m# o" t8 q- z9 r& J
    case '-':return t[0] -= t[1];, k7 K( b1 C, m9 P1 P$ `2 Q
    case '*':return t[0] *= t[1];% K5 l- _, C" c7 D( t% L
    default: return t[0] /= t[1];//case '/':' b/ I8 E3 F( |# y0 y! A# s% @
    }; ^' S7 R% w) g, O/ y2 p& @6 n1 h
}}7 N* e- W) O, t2 ^: R$ s6 W
template <class _T, class _Tstream>7 Y  N4 V2 x% u4 L0 c; Z8 G5 j
/* _Tstream: inputstream, _T: get return value
" j. `2 V6 j" U8 K* Return nonzero if get value successfully */
& @2 T+ I8 K! t! t9 R5 ~1 o1 qint GetExpValue(_Tstream& istrin, _T& nReturn){# `. V  l/ A: b
    _T t[3] = {0}; //雨中飞燕之作9 Z- m6 T2 u! M6 u0 C( `7 B- O* l
    char csym[3] = "++";9 ^$ D; F+ Q: n& D+ r6 [
    int nLevel = 1, nERR = 0;1 y: b3 a5 U: ~3 e+ I
    if(!(istrin>>t[1]))istrin.clear();: _# [! s& j% O& U0 V) \
    for(;;){
- a1 n; {  S. U0 n; S& o' w        if(istrin>>csym[2]){
) }( G  ^6 l: S            switch(csym[2]){
2 ~' p5 v' Y( O- S3 ~2 R. Q            case '(':" J# s+ T4 u' m( O0 C8 N0 T0 S
                if(!csym[1]){nLevel=0x100; nERR=1;}else
' |% Z( {) C- L9 o3 ~" a8 p2 X                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
2 p( `8 x" m$ W/ y8 y                else{nLevel=0x100; nERR=1;}, o" x  _1 _! g2 b# e. X; `; o! r
                break;) H, |" t/ Y' P% x; O9 C
            case ')':
1 D' \; i3 U6 P) ~' `                {nLevel = 0x100;}break;6 n" b) A5 j; o
            case '+':case '-':case '*':case '/':4 ^5 k% Q( l9 g. m7 O
                {csym[nLevel++] = csym[2];}break;4 c' g9 I2 m: m0 E
            case ' ':case '\r':case '\n':case '\t':continue;! {2 j8 z. q  Z1 e: q) v$ g
            default:, n7 \( [$ P8 L* Y9 i/ C6 m$ [
                {nLevel=0x100; nERR=1;}
" d3 K# C3 J8 u; o* J. U' `3 Z! [            }
  M  a5 U' d) o& ^            if(nLevel==0x100)break;
3 Z  j7 r8 @6 @) O7 w6 b0 V& B            if(nLevel&0x10 || istrin>>t[2]){! f6 {4 e) d) J
                nLevel &= 0xF;
% _* x3 A; ~# P' t                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}) w  m2 ~3 U% H3 C+ y
                if(csym[1]=='*'||csym[1]=='/'){( t8 m* X$ c  p( i$ R
                    GetExpValue(t+1, csym[1]);2 Y8 n$ O9 f% \! N
                }
6 k2 f. A3 ^- S5 M$ K                else{
* r5 ]' O' }( d; M                    GetExpValue(t, csym[0]);* O( x# A  d; L+ z
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
, G8 O/ V, N3 p# y4 [' D$ d                }+ {1 B# N" c4 ?4 i$ C
                nLevel = 1;
: k6 p  l+ p. p& M            }/ m  }. ?& ~" h: r# ~- |
            else istrin.clear();
3 S1 D1 D" _+ ?7 O6 t" b        }
# n! x- K. M) t+ \# a1 `7 u        else{nERR = -1; break;}
% A8 ?8 P$ G, v9 ^    }
9 j% j: g7 X3 y7 o9 p, ^    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);# Y" f, i, @/ P) q! [" ^! n
    else nReturn=GetExpValue(t, csym[0]);' V) k) [' h0 X, O  D! d
    return nERR==-1?1:0;
  ~0 H" @4 H2 I$ A5 [& [7 u3 f0 p) k}}2 J/ Y3 p: @$ c1 {8 x) P) V

8 A. ^7 d3 L& O* Y- n. n6 U2 ^; E

# l2 C$ J( t6 e1 G! X( A' V& h4 K函数模板使用示例:
5 U$ F+ w% b1 g! \在以上那段代码的后面加上以下代码:
: f( K% b: I' D9 w2 }+ m* T8 C* t
) S- O6 A) H# u$ X0 N! [& P! L$ ? * h7 ~! X9 U1 P, c- Z5 x
7 h) {3 G3 @  T$ O* a+ ~
程序代码:
1 t6 s$ X" Z* c) E3 u3 q# o5 W( ~/ F
#include<strstream>" C8 K6 z: P# }+ O
#include<iostream>
5 A# ]& `7 p5 {) d#include<string>, h# F2 P, x& p% Z2 ]5 [
using namespace std;) M) n9 [. n2 C
int main(void)
. z) j! l; t5 M4 f8 T& W! @  V{( J2 S5 K! U" ]+ Q7 _2 i
    string s1;  @6 o/ p/ i" r; a
    while(cin>>s1); n/ Q4 S0 ~  @
    {, t9 M9 Q/ {! @
        istrstream isin(s1.data());0 g4 Y6 q& a3 n# n% j& E
        double d;% g; U1 Q# {1 E" U4 B$ o, p
        if(fy_Exp::GetExpValue(isin, d))
' g# B( _! X/ @        {
! `7 t2 G! f2 q/ `            cout<<d<<endl;
4 Q5 c/ Q0 \9 F) L1 r6 f, \        }& o5 \+ H# h, Y7 d0 l/ ]  a& ]( h
        else
9 \/ L5 D$ h+ X        {
1 q/ [2 B0 \  w1 ~            cout<<"ERROR"<<endl;* @+ @( D; v3 H5 G, Q; L
        }
7 P$ w& z/ `5 T    }9 r' E4 F3 h0 L& M
    return 0;; N" M, V7 r3 j0 m3 V
}9 y- F4 D+ G5 a7 Q9 k
% n. {# K4 w0 f+ d; w- S6 Z5 |

! l0 {2 N/ R5 j( K) T然后编译执行就可以了(*^_^*)$ {" \( {4 R8 K* t/ C
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
! {. j3 _6 q/ k3 z5 i) m$ r      建议使用VC7或VC更高版本,或者使用GNU C++编译

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