返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,
: H* I2 C: c9 `7 t一个很方便的函数模板,可以并且只可以计算含括号的四则表达式& z) z3 h; U) S$ ~" y
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
, l; v( y8 T9 w7 {: _) z  O参数解释:; L6 D/ k* N4 [# Q% l( e
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
$ Q8 ]% I+ V4 S+ fnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
6 v# K  [2 j8 V( W+ x( V1 B返回值:
" \8 F2 [" u1 a: J返回非0表示计算成功,0表示计算失败有错误
) u) P5 `3 v8 {8 V
! I- a) h$ i7 W  W  o
4 g' f5 ]; F$ u$ L# {: @6 R
, f0 u6 H# }! v9 G0 m2 n4 e8 M) n程序代码:
- x- o3 W! S6 k' i2 R1 c! M) }+ I8 ^8 g- e3 g
namespace fy_Exp{; ]/ t6 v- F0 a; m5 l0 j# i
namespace {template <class _T>
8 v5 i/ @% ]8 y0 n) U9 zinline _T GetExpValue(_T t[], char& csym){% s# ~0 k; e# {" N5 h: g
    char c=csym; csym=0;* @/ S  G4 Q( L- P: F& F
    switch(c){5 t) v- l( y0 N3 d: x
    case '+':return t[0] += t[1];
2 O6 Z( ?8 H% U7 c    case '-':return t[0] -= t[1];  t1 k/ a: _2 P3 @
    case '*':return t[0] *= t[1];
- H  E, ^+ e* W8 _$ t9 p    default: return t[0] /= t[1];//case '/':
7 l6 w. L- l* D% ~6 e    }) D" q$ p6 T8 E5 b8 E, w, j- I
}}0 P: d/ P* j  L8 ~1 c
template <class _T, class _Tstream>+ T1 V2 M- M: T+ |8 V
/* _Tstream: inputstream, _T: get return value
$ R9 N" c  C2 V  I  n$ i* Return nonzero if get value successfully */
$ o& D3 M6 k0 ^# t- D, Eint GetExpValue(_Tstream& istrin, _T& nReturn){& o! w. Y9 s; W. j. B. M) |
    _T t[3] = {0}; //雨中飞燕之作( }4 V, x) x: a, B4 Z) Q
    char csym[3] = "++";9 ^2 q2 g0 i& a& x% X- Z/ T# f
    int nLevel = 1, nERR = 0;
( I& @  I& m+ O  u" s3 p    if(!(istrin>>t[1]))istrin.clear();
6 p& n# P% j1 k9 F  N  q3 K    for(;;){
7 S  C9 d5 W) s5 V7 I7 \4 F        if(istrin>>csym[2]){
* \1 K+ N4 c0 I            switch(csym[2]){/ _( X6 c1 Z& V8 h' \$ i; p: }9 p' k
            case '(':
  l+ p1 k1 w& h7 b1 a( x! P" X. l                if(!csym[1]){nLevel=0x100; nERR=1;}else
' Q7 r, J9 m: s                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
0 A, [9 {- s8 |/ j. d& L4 @                else{nLevel=0x100; nERR=1;}
* P# c" Q4 A# _1 p) m                break;
& z. i7 |% f5 |4 S: |" z& Y            case ')':
) _2 e- B3 Y7 V) g                {nLevel = 0x100;}break;
6 _( o1 h& Q3 c* ?! P            case '+':case '-':case '*':case '/':
. _" J& t# z5 C% E; G/ M' W& D                {csym[nLevel++] = csym[2];}break;
; z- ?1 z  H4 x5 z& c            case ' ':case '\r':case '\n':case '\t':continue;4 I# ?" `" o. ^
            default:1 i! E/ J6 M1 n- _0 b
                {nLevel=0x100; nERR=1;}
; x1 @: ]  M, P# _+ M. G            }
9 @8 c. T5 c. ~) L            if(nLevel==0x100)break;' t9 d6 b( z4 Q4 S8 U$ H1 T
            if(nLevel&0x10 || istrin>>t[2]){% L6 A0 U- ^# a) @- l
                nLevel &= 0xF;! E0 ~6 P+ G; _$ k7 C
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}7 p: ^6 S/ `$ D) O7 i% J
                if(csym[1]=='*'||csym[1]=='/'){
! C: J! U- |* h5 c& Z7 Z                    GetExpValue(t+1, csym[1]);0 w7 D. w* {6 c2 ^2 [* h
                }* z: r$ J& {0 V$ ^" Q
                else{
: n6 ]2 l# L' S9 ]) e) B& M                    GetExpValue(t, csym[0]);
7 b" g& P9 u+ v8 O4 c                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
+ L2 Z5 w7 b0 e6 p                }
" q! I- s7 @$ @/ Q+ X8 g, @1 P: X                nLevel = 1;& V; a$ J) O0 X" s# A) ~
            }
. v- m" z$ d5 h$ V$ N( L9 \2 z            else istrin.clear();
& d# K% s% N3 T: t0 d4 f. F" H        }
& m+ ~) A# @& N) i# F( {8 @' J9 B        else{nERR = -1; break;}
7 S# c$ E# h1 s; j    }$ H2 _2 ]0 k9 n2 B
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
1 y0 L1 M+ r8 l. C, ]7 n    else nReturn=GetExpValue(t, csym[0]);
4 W; ~& J% D; I3 W$ ^    return nERR==-1?1:0;
, m0 g, x2 Y* R" B: t# q}}- |) o, K# d; q9 y

7 U  G4 @2 v! |% r! Y
1 N4 [! A5 R) |0 Q# ?) x; w, P4 A9 k; L8 h
函数模板使用示例:
/ q6 O. w& z& d% w9 l在以上那段代码的后面加上以下代码:
- R/ `, \; S0 [0 F, f& @! m! G% d7 Q
9 G/ |7 c" f  y0 {

) a- J* s* M( M2 t程序代码: . O( w7 u4 G5 T) q# c2 \
  U  {8 \! d1 x, U
#include<strstream>
3 K: M3 H3 S2 {  A( _7 r! g. f#include<iostream>
: R0 m( G# e5 c9 P9 N#include<string>
- g; e% I  M1 Qusing namespace std;
/ J$ l2 e5 \8 }- Y/ ^0 X9 ^int main(void); m; q2 s! ?+ v( H* A0 D
{
, J  R, y; o1 a$ J7 s    string s1;
6 {9 c9 C5 Q# v    while(cin>>s1)
) p) f  o6 V+ b3 z7 r    {
# r. n' _3 _+ _* n0 K! ^* [        istrstream isin(s1.data());( B# N5 A, k3 n8 r
        double d;. b7 S: F! N& S0 e+ b  ~
        if(fy_Exp::GetExpValue(isin, d))2 S+ g! s4 H) r. @
        {2 l! H: c/ s& S: f- U+ U" G$ d
            cout<<d<<endl;
" |# j; b. _" q        }8 |3 U8 M$ B) ~- k8 T. \
        else
9 v1 J) H: O9 G# C/ Z( u        {
2 C2 P5 B# G7 b            cout<<"ERROR"<<endl;
& A$ \- k  C: T2 y        }4 a# a$ T  h$ s' Q. |8 V+ v; l
    }' @% }2 {, X2 c  @4 B  b7 A& p
    return 0;
( b5 E; n8 o1 S9 i1 _9 L3 U, m}
) c& Y0 k" }! ?- i# I0 T' a+ ~
/ x. n8 K7 u3 i! Z% q8 {
( p  [+ H+ c) _: ]+ J# I/ [然后编译执行就可以了(*^_^*)
+ y) M, P  ?' e: J, H' P0 v& R其它:TC++上一定编译错误,不保证在VC6上也能通过编译
! J6 ]% g. N# f) {: Z3 r4 t      建议使用VC7或VC更高版本,或者使用GNU C++编译

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