返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,
$ `$ ?8 D% G) n一个很方便的函数模板,可以并且只可以计算含括号的四则表达式1 d" q6 ?; V- E4 d$ a
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
' H; Q; L1 f! W9 W! K3 g参数解释:
! L0 _- D* e3 D' ^* N+ G4 Ristrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
) [3 k( U7 A6 ^nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
# t% ]  g1 p8 L# [" h+ A0 }! L% L返回值:7 ~0 ]4 N0 G; J) L4 [$ h
返回非0表示计算成功,0表示计算失败有错误: U" Q% [+ h. Z1 Y0 K9 U! S; \) n" q

! S! G& ?: y) n/ W# ~2 _ 5 k; J3 @) }# P: H, i0 B
* Y( R+ g+ ~2 Q  J: K
程序代码: ) a7 Y3 Y" v5 u. h

$ n. Y, K! @. Y. T4 Z/ vnamespace fy_Exp{
6 H) Z# t: Z2 U& rnamespace {template <class _T>6 s* R% F1 O8 L& ]; v3 t3 u
inline _T GetExpValue(_T t[], char& csym){9 I/ w* s- x6 v8 v7 I# e3 J
    char c=csym; csym=0;
* M2 K. f  H+ D2 C# ^1 ?/ i    switch(c){
  E2 Q4 g. D7 f' n8 q    case '+':return t[0] += t[1];
* R" \6 u3 s  O) |    case '-':return t[0] -= t[1];6 ?$ c6 _/ N' Z4 [  V
    case '*':return t[0] *= t[1];0 J' q5 }- e, u2 P. O5 E
    default: return t[0] /= t[1];//case '/':+ A$ T, K- I( p0 g2 k
    }0 K8 J3 Z3 J- [' t
}}
& F# L! Q7 a* S. l" Itemplate <class _T, class _Tstream>
9 ]2 ~4 ^1 b! {/* _Tstream: inputstream, _T: get return value
; d+ B( Q  X+ z7 o* |$ I* Return nonzero if get value successfully */6 M! u% J  w8 v
int GetExpValue(_Tstream& istrin, _T& nReturn){7 g9 {! ^- ]) C. V: U8 {3 L  G
    _T t[3] = {0}; //雨中飞燕之作8 R7 z/ @, a/ u; m9 @/ M
    char csym[3] = "++";" X( P8 }+ q9 d
    int nLevel = 1, nERR = 0;
" D+ k& m! Y4 w) z( n6 u$ F4 C+ a8 Y    if(!(istrin>>t[1]))istrin.clear();9 s, K, n4 X7 h" S7 c7 r" s
    for(;;){) l/ T8 O" p0 o
        if(istrin>>csym[2]){0 I' Y! F& O4 m% ~; R$ b  b
            switch(csym[2]){- G( D3 L  v- O8 j9 v2 C" H! j. x7 P
            case '(':% R, a3 G! `. Y8 O: k, f' h; J/ f$ J0 `
                if(!csym[1]){nLevel=0x100; nERR=1;}else/ L. m9 N: [/ H* e) b
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;' e$ C8 x. b! V5 K3 R  }" W
                else{nLevel=0x100; nERR=1;}& q. J- _1 U4 N  |  D" Q. _8 y
                break;1 R5 F* H' }+ U7 }  q# K
            case ')':
/ p5 {4 a  f% m6 A$ a( K# Q; t                {nLevel = 0x100;}break;
' @3 P9 d% B4 n" h            case '+':case '-':case '*':case '/':% I/ D* U/ }) c& i$ s2 S
                {csym[nLevel++] = csym[2];}break;
8 W4 D  g* p2 s* k4 G/ \$ D7 g            case ' ':case '\r':case '\n':case '\t':continue;
* E  x' w% a% U0 n+ v            default:
: N" N- `  K2 Y2 U6 Z" B9 M8 a                {nLevel=0x100; nERR=1;}
; K% K- X" j% t2 s3 F            }" }- Q) K6 @$ a2 Y' D7 @
            if(nLevel==0x100)break;
) Y. q8 g( J3 n' t5 b1 n            if(nLevel&0x10 || istrin>>t[2]){: S0 s$ H; ~5 i: e7 D. i" d
                nLevel &= 0xF;
3 I) s2 y' W0 u1 S7 d                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
* J2 i( a6 {6 n; M' Y# H                if(csym[1]=='*'||csym[1]=='/'){
1 o+ G3 P" V4 m" O4 X% _+ V1 R( D                    GetExpValue(t+1, csym[1]);! D9 D& P" y4 C2 B7 p( t+ t4 N
                }
/ _" b" U% U0 q. L' v  l                else{
% W  S  B* p% j  X3 [2 \                    GetExpValue(t, csym[0]);
2 P, a( d4 ~. U7 F                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;$ x% F, p3 A& q! S! ]& E6 F  d+ `
                }( n9 h  u' R! ~3 P* p4 T0 L
                nLevel = 1;5 P1 i! [2 z5 G* n
            }
& p; E9 e. t  S7 U. M  h' M            else istrin.clear();* J4 E$ Z& u: |& Q8 t) k) [
        }
: G$ m5 Y5 q& J! W8 {' q% K        else{nERR = -1; break;}! u+ z' K- T- D9 y% {
    }! E2 V6 a( V6 T+ i5 a& o
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
! @- Q5 r$ x' M# r* g( w5 {    else nReturn=GetExpValue(t, csym[0]);
. ~+ T4 h8 ^0 }0 N    return nERR==-1?1:0;
! w* A* ]9 I8 c( Q}}
6 y$ Y( W& r8 j. X7 k" x
: U( B( ]4 {% P: S9 x$ F
. m& C, z8 H; R; r" M: X" v. Q+ h- v
6 J" g, x1 }; x' t$ d9 ?4 z函数模板使用示例:) l1 j, \% O  L; o5 Y
在以上那段代码的后面加上以下代码:6 s* F* M/ o8 E4 U

% Q: X) g" }6 o: p( m
. q3 x: F; F# E+ m& Z3 N9 R) j% v9 ^# A" _/ p+ v
程序代码: . B7 s3 Q) d( @( q
- I! k/ @" ?% j" i& j3 Q
#include<strstream>
+ R* l% c  s1 ~8 [. m#include<iostream>
7 q" T( E: v  @& e+ A#include<string>) d. D" {$ i. f# B- e. U
using namespace std;
8 ]; o/ }8 Y. Mint main(void)
9 O6 {$ I# r0 X4 A: P7 l/ r{* Q# i9 m! @3 N4 H& A
    string s1;5 C# E* c: E1 o& n9 b
    while(cin>>s1)% _; ]8 c+ A0 u7 b0 O
    {
0 ~1 U4 T- v- x' `, P! O        istrstream isin(s1.data());. T" h4 g8 o/ x- a8 c- K6 A+ Z
        double d;
! G+ t1 a1 C) z* Z4 c6 w: G        if(fy_Exp::GetExpValue(isin, d))
5 [4 t& r4 F! K' [. T1 S: |' j# E        {! A! l/ b5 {% ~6 P5 n
            cout<<d<<endl;, F& B8 B; h5 l7 \
        }
$ w3 Y' j! j/ j1 B( H# ?7 |* E5 _        else2 v; j9 [6 x. C8 a
        {
/ d! S$ f  ]- {( N- D% p            cout<<"ERROR"<<endl;
, l0 o' \3 Y: J0 Y' m# W& G8 x        }7 u7 ?% i7 Z1 X9 j- _" Y* W* ]7 L
    }
& c3 g  s1 r0 L% x, j* T    return 0;! L" O5 g, U( Z. o+ e
}) ]' X5 M- J9 u0 R* H
/ o) K; Z+ j9 n" a4 W
: I( ?+ j& K* Z& }0 `/ z2 }, A0 c% i
然后编译执行就可以了(*^_^*)
- t3 n7 o% Q: _3 M( h其它:TC++上一定编译错误,不保证在VC6上也能通过编译
4 f% [8 L* e6 w% T5 b      建议使用VC7或VC更高版本,或者使用GNU C++编译

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