返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,( {9 Y) S, k/ D
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
. z( Y4 H/ x: `* n5 [( r4 ]只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
: c9 u3 t' Q% s. w参数解释:7 T6 p/ Y: `+ v/ ~; [7 ]# v6 B
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
0 d  o4 @* x6 j& l7 y% AnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定) N5 o& d, B& E/ l4 x2 {
返回值:
& S! v0 Q3 u) ?, v7 M返回非0表示计算成功,0表示计算失败有错误  U# _" R& e: S" l+ s# ^
$ S+ f$ f8 ~% r
4 z+ b- ]) G2 C6 p
# m$ p( I% |( d3 n) t$ u
程序代码:
; V" _9 e8 u# c& }5 p+ F! \& ?
/ B& b' l8 O' s) e. ]namespace fy_Exp{
4 G1 q% O' w5 ]/ r, ^- l/ Q3 i$ }' qnamespace {template <class _T>( w% U9 f! s/ o- X" d3 |
inline _T GetExpValue(_T t[], char& csym){4 T' o# i  I- G$ c5 a% s
    char c=csym; csym=0;
6 l7 G6 J; n: \    switch(c){
; C6 j1 ?+ m2 D. s    case '+':return t[0] += t[1];+ s: x6 D( b6 C+ R
    case '-':return t[0] -= t[1];
( ?7 z, D5 _& U  l: m" C  O0 y    case '*':return t[0] *= t[1];
5 M8 W  E& `2 A5 K% J$ g+ M  j! o    default: return t[0] /= t[1];//case '/':8 y% _8 y- N- o
    }. o; t; j& n. e) N" o2 W
}}
; e% _. @- ^8 _6 K$ _1 c$ g% ]template <class _T, class _Tstream>- [' w6 [3 I, _# b& G! ^' `
/* _Tstream: inputstream, _T: get return value8 L0 H7 l3 W  s* @. F; ?1 |! j
* Return nonzero if get value successfully */
4 @. ?$ D& ~  W* T/ ]2 Oint GetExpValue(_Tstream& istrin, _T& nReturn){+ r) {- n8 h7 _  ~- [! Z8 ^
    _T t[3] = {0}; //雨中飞燕之作; S4 o# @+ C& }" O
    char csym[3] = "++";
  x5 j7 M! v0 q( a    int nLevel = 1, nERR = 0;& H! x7 B- A, N+ @0 q5 F9 s) Z! K- F7 R
    if(!(istrin>>t[1]))istrin.clear();: Y& Z! \) o% m. C* T/ V
    for(;;){( c0 E* `6 v9 i' L5 k; y
        if(istrin>>csym[2]){
* }2 R1 B$ i  Q" O  a            switch(csym[2]){
  b9 [% M3 q! k; `# P+ @2 s7 Q' W            case '(':
4 r! y; ?* }3 Q1 X" c" `. W: I                if(!csym[1]){nLevel=0x100; nERR=1;}else* S+ I$ r, c' k: O# j
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;2 U$ f* U( a3 j3 Y
                else{nLevel=0x100; nERR=1;}) Y+ h' q, r* U  w4 y
                break;
* g0 U2 K3 E8 z* y( C            case ')':
# z0 p% X9 j2 ^  G2 w: d$ j                {nLevel = 0x100;}break;2 c/ l, t8 x0 j
            case '+':case '-':case '*':case '/':& A* K, _! e$ J7 z) [* u
                {csym[nLevel++] = csym[2];}break;
" c3 U8 H! [- E# X            case ' ':case '\r':case '\n':case '\t':continue;/ K7 `# x& }. n" {+ Z8 Z
            default:
9 R) H8 P' ?* N" \  W; ~$ [                {nLevel=0x100; nERR=1;}
1 j+ I, R' Z. K9 a            }9 p* f+ A/ N5 P* y' n
            if(nLevel==0x100)break;
1 M$ e, q8 E6 j) d1 R            if(nLevel&0x10 || istrin>>t[2]){
5 A1 Z) \) H, ?                nLevel &= 0xF;
0 j+ i7 W# }+ _' n4 J0 r                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}4 W# H' x1 s( }6 f
                if(csym[1]=='*'||csym[1]=='/'){
# M, Z' Y6 U$ U                    GetExpValue(t+1, csym[1]);
  \" m) M+ L( f                }
& v* T6 {; D$ Q. v/ P                else{
* C' n7 x$ u; \0 Z                    GetExpValue(t, csym[0]);: x$ x! U3 s5 m0 m
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;( E) u* I! Z, u; G
                }& `0 m0 Y# H. `: R  T
                nLevel = 1;! V6 _- r; [- R
            }
. I3 ?0 b: G' @7 N# M. y            else istrin.clear();8 _# O% P+ p4 f; l! T' w9 r/ q
        }9 W+ v* B( z8 |2 U: A  n- ~, b" o
        else{nERR = -1; break;}
! |+ y7 N7 _& J; O1 ?1 j    }
8 ^4 S& D5 O1 B1 I    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
0 m( N" @" \3 o* L+ Y    else nReturn=GetExpValue(t, csym[0]);. s* M3 C8 Q! c2 [" j6 s/ R7 \
    return nERR==-1?1:0;- P. E# q9 K: T* S1 O& O1 j
}}
8 f6 U& }& u# i9 l, x$ G$ @
8 X% o- C& p1 a% V, O' k, R9 |0 \/ u! _" y7 e

% m$ D( V- ?3 u  d+ l! |函数模板使用示例:% e$ j1 @1 i6 Q0 n6 f- l# K, s
在以上那段代码的后面加上以下代码:3 m8 s) z& v! Z) S( x

* V4 t* u5 x3 m8 h
8 g1 K  b) v$ a# d
' h: E0 c9 V' j程序代码:
8 R: p8 @6 J) E
# @2 {1 Q- P  W# @#include<strstream>
8 k" ?  y  K6 }. A1 \& X, M#include<iostream>
4 f: O3 @! p( g* y/ I1 ^#include<string>& [% l2 \( Q2 I+ J
using namespace std;
: F* S) P  Z% W/ @1 t% dint main(void)
. e5 }$ J, ]! w/ I% k- f{
0 e" g8 t9 s/ W2 p9 n    string s1;: Z8 x, q$ _# J2 Y  I  x; T6 s! q
    while(cin>>s1)
, R- D, L, D7 I2 \% |3 q) d0 l, g    {
. K$ m. m; n7 g7 y7 C; l        istrstream isin(s1.data());
) L2 H, `; D+ ]1 w: q8 h1 f        double d;/ g; {3 z2 O1 n. f# F9 b
        if(fy_Exp::GetExpValue(isin, d))
2 Z* m5 h" `4 @' H" m% N        {
# o5 E. O& f7 Q  R* j( D' l            cout<<d<<endl;
2 |2 f% v) j8 R( V! M        }
1 w  s. X9 g) t9 f        else' t$ W: j& S. J* a* @2 s; K
        {' L- l& r+ m$ U# M/ f' |9 l
            cout<<"ERROR"<<endl;
/ d# }4 l) a/ ]7 j/ ?; R& J0 R        }
, W5 Q. W' y- T# o5 h3 j) Z    }; s& }8 o5 t4 e/ G9 k. O* j
    return 0;
( w) Y! ^+ ~6 K) c}
% b! E0 t% }  l, e( Y! R5 v% C
8 P0 Q4 c1 P, r& v2 Z7 Y" n& g# m6 Y( S3 l$ k) c
然后编译执行就可以了(*^_^*)
$ g, q9 b# ^1 Q4 H0 F! k其它:TC++上一定编译错误,不保证在VC6上也能通过编译
( i$ s- L& [% H% b, R      建议使用VC7或VC更高版本,或者使用GNU C++编译

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