返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,, R" z$ l* O" [( ^0 A8 m
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
# j: _4 y/ u  d只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
0 ~5 W% _* K" N9 _/ w参数解释:
' `; ^7 A9 s* g; f/ mistrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
0 \( X6 L- u6 R& d- NnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
+ ]; j/ Y* d0 z) M1 @5 A! W' R返回值:
  ^/ \6 W* K* t% U) B返回非0表示计算成功,0表示计算失败有错误
8 i) x+ K5 n9 k' `7 b
9 [- d* I: z4 ~4 d0 ], R+ x
' h1 t- f7 @+ d) f/ `1 d+ B9 q1 s( Q* n
程序代码:
! B' k) P- a, [& }* P+ A8 o4 k4 Z
2 q: a& e1 s+ G+ R8 Z& Q8 gnamespace fy_Exp{
4 [- X$ i( Z0 g, Unamespace {template <class _T>
+ V# Q7 N4 Q; r( t: linline _T GetExpValue(_T t[], char& csym){
8 {; s: |. g5 o& n# r$ Z    char c=csym; csym=0;
) Q& Q2 a# l, o* s! L/ D+ t3 x    switch(c){' p: b% S: a  h7 _6 ~
    case '+':return t[0] += t[1];
  _7 f5 C% h6 {" R6 u8 K" ]* s    case '-':return t[0] -= t[1];
' ]. f' _' j1 S$ I% C    case '*':return t[0] *= t[1];0 r2 X  Z, `3 b8 ~: s- ^+ g% g
    default: return t[0] /= t[1];//case '/':
. h% K% T0 j6 T, }    }
5 I+ C- G8 ^6 ?0 f6 x( L" b8 P}}
) T. J# E9 }7 S( v" a$ n& Stemplate <class _T, class _Tstream>* n. m  s5 b# r, j$ T" U
/* _Tstream: inputstream, _T: get return value
) k) U8 S0 P- k1 _* Return nonzero if get value successfully */  O! M- C! f( Y1 B9 s  A0 S
int GetExpValue(_Tstream& istrin, _T& nReturn){% @/ x. S/ E3 Y
    _T t[3] = {0}; //雨中飞燕之作
3 M! m( \" b! d5 J  g1 Z- W/ g1 U    char csym[3] = "++";2 u. _9 {% I$ g1 t
    int nLevel = 1, nERR = 0;
% ]& D% @5 \2 x* e' g' {    if(!(istrin>>t[1]))istrin.clear();( Y0 ]  E" r+ s- k! h8 m
    for(;;){
% ]7 e% C7 W/ _( E4 }        if(istrin>>csym[2]){+ x" }( P; i: y8 }% o
            switch(csym[2]){
3 C' k; [$ t' U            case '(':3 [. u+ u  V* T" _, l4 ?
                if(!csym[1]){nLevel=0x100; nERR=1;}else! \! b# M# k6 `* H5 h# a
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;) ^) I$ [9 U, o% S; f" y6 t
                else{nLevel=0x100; nERR=1;}
& `" T; |2 l4 z9 M+ Q                break;, D7 d* R6 N) s+ b3 y3 E
            case ')':
! X  I. G) Z0 ~2 R& r/ H2 R" B; [                {nLevel = 0x100;}break;
$ b1 N2 w: P; Q            case '+':case '-':case '*':case '/':
# c; `1 }, V3 f2 D( i                {csym[nLevel++] = csym[2];}break;) O9 p* `1 I/ u" o6 g
            case ' ':case '\r':case '\n':case '\t':continue;% A: V+ |2 ^; k7 N6 j$ K3 m' V
            default:
9 X' j) q- g$ b7 R                {nLevel=0x100; nERR=1;}; `# Q2 k% }) z  }5 A$ H: p5 n. m6 |
            }. o5 L& x$ f* f! D3 m# y
            if(nLevel==0x100)break;% n3 q" _$ o$ P/ Z/ L
            if(nLevel&0x10 || istrin>>t[2]){' x( J5 f+ S" l
                nLevel &= 0xF;
( H3 s1 p2 r, a                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
: G- S2 ~8 h' F2 v2 U) j1 @8 y                if(csym[1]=='*'||csym[1]=='/'){& M/ c/ h8 T( h! T1 M# E
                    GetExpValue(t+1, csym[1]);
1 i9 ^& v& t9 f2 [2 D; c                }
& s( G, z" U. t! _) w8 h                else{
! `: m1 H0 ]* Q2 z                    GetExpValue(t, csym[0]);
5 k* S& Y: Y/ j0 ?# V- i, w                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
$ ^. L' A+ \; N% K# `/ ^                }# v! ]& i, [/ G' k
                nLevel = 1;6 E, f  y: c- S9 z) T; J) P1 J' A
            }6 y* U( y' B1 M" l
            else istrin.clear();
# a$ g0 L# |1 p! Q) t        }% g" G( n$ I: C9 j
        else{nERR = -1; break;}
% d& V0 F0 e- y    }
! `/ x$ o/ \/ T6 Q5 z! B. G" t  v    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
) A( s; V7 v; }! t    else nReturn=GetExpValue(t, csym[0]);1 ^- @5 u0 Y; _8 U/ j4 ~
    return nERR==-1?1:0;
. u8 T4 {) Q  T3 u}}
( I: ?, n: r# q+ H3 {7 c7 @4 y
2 \4 \6 [# P0 ]
6 e/ u0 a3 I; ]0 j
! ]3 G$ j8 Q6 f7 e8 d3 Z% h函数模板使用示例:
+ N0 Q" J% h8 T6 l5 ~在以上那段代码的后面加上以下代码:
/ |5 q; }# e; n
- D% F/ J5 ]8 I& u 4 g/ q* c9 K: h6 j% Y0 k

8 Y8 q% d0 {" K, E程序代码:
$ H3 ~2 N* N9 o9 u4 ?/ |. G  h$ c( b" E
9 W" v9 L1 p+ U. A#include<strstream>
# M1 @3 I( n$ B7 r+ |. U# J#include<iostream>
2 w+ r( y9 O4 \$ x; ?3 E# @5 b#include<string>0 v! Q8 C: z; c+ }9 Z
using namespace std;( U4 U" w# C& B) w
int main(void)6 ~( A* N0 R0 N; p
{$ ]" K% v+ _; a+ f
    string s1;
& t2 ?4 H; _0 f7 q- L* f    while(cin>>s1)6 X5 W8 ^2 @  v8 x9 s" Q/ v$ b# q
    {- Z) F0 G5 L' G. V- g
        istrstream isin(s1.data());
2 F/ z- o: z1 I! ]* K& {; ~9 R        double d;6 d. i- ^( f& }, }4 W$ ~
        if(fy_Exp::GetExpValue(isin, d))
$ M# `' d- {9 T9 L2 G2 Z( G4 ]        {+ i0 y; K7 Y* J+ r$ r2 E
            cout<<d<<endl;
8 L3 [! a' ~+ S' q) c$ E9 {        }
/ j: ^! l2 G# U1 g3 r/ o/ I& U. b        else3 \4 D; T& y8 P& [: r7 u( o2 M
        {, Z9 g% U1 F8 A, i
            cout<<"ERROR"<<endl;
( B& V) _) K3 {9 X: D# P        }
: l5 |( D  n! b4 K" c6 D    }! y3 Q5 \8 ?5 S
    return 0;+ \. `& @' s) h. }2 A  Z
}& k8 p+ d" G2 o

; _. k0 J- F: F- ^& H6 E# m, s
, U; e# q- E( p0 z  Q然后编译执行就可以了(*^_^*); v! ]# i' d8 L/ Z' x- z+ F% u" T
其它:TC++上一定编译错误,不保证在VC6上也能通过编译* Y: _% Z2 K! |1 L4 x3 `( C* t
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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