获得本站免费赞助空间请点这里
返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,& @0 ?; E0 n2 O
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式# y) B! S* o7 `; H; I; E3 N+ t
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
0 e9 h4 h8 ]. i: [$ x; X, l参数解释:2 y4 Q% a/ q; ]4 l, e; D
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流& X- G3 |+ B- u- M; P7 w
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定0 |2 A: i# I' ]
返回值:3 b  t( I" s; V$ {) O
返回非0表示计算成功,0表示计算失败有错误" N2 d  r7 b3 }# m( w- t/ l

8 w+ s! f1 T7 B7 `$ l; V- _( u , w; i  v6 z$ i: V5 Y; P! B6 \

* n8 _9 j3 p1 a4 v2 q+ F$ M程序代码:
1 D  _& D& ?  u
; o. i: X/ P# i9 m% V# O& _# f+ D0 Unamespace fy_Exp{
- d: z  o2 t" v% v* W9 w6 snamespace {template <class _T>" A+ U# U0 _3 N) f
inline _T GetExpValue(_T t[], char& csym){
& `8 D; z0 k' B    char c=csym; csym=0;
; [# a/ F6 M- u    switch(c){
4 g$ y, e: e/ F; M  G% t    case '+':return t[0] += t[1];
. ?% M% |- C8 q6 Z6 Q    case '-':return t[0] -= t[1];. \0 d' V$ H" f$ Z
    case '*':return t[0] *= t[1];/ Y9 Q( B% R  d
    default: return t[0] /= t[1];//case '/':
" c6 ~$ k) M. O8 v& {# r4 b4 r    }
/ f' H4 Y% R( l( V& X}}
: u8 X6 O! }" [template <class _T, class _Tstream>8 {* R9 x0 c# k/ ^' q& M' o$ o
/* _Tstream: inputstream, _T: get return value4 n. y6 c  b3 {
* Return nonzero if get value successfully */$ O7 ^) b" P$ k& j% n
int GetExpValue(_Tstream& istrin, _T& nReturn){- }0 G5 U, H6 |7 f3 V6 }9 F) W- S
    _T t[3] = {0}; //雨中飞燕之作
& y6 P  k$ ?6 ^! g9 Z. n. d3 z    char csym[3] = "++";
4 X4 g2 B" z1 t7 Y9 u: q. T    int nLevel = 1, nERR = 0;
( G8 V! U6 K% B' F9 \    if(!(istrin>>t[1]))istrin.clear();
/ h0 b; r0 h, I9 C) _0 N1 Y    for(;;){( _" |$ X  Z8 H- h3 o( I- b
        if(istrin>>csym[2]){: f  l& c5 U9 `% E
            switch(csym[2]){
3 |; h6 B- E% Y, P5 S9 ]) ?            case '(':
/ G3 \) ~4 d  a$ q                if(!csym[1]){nLevel=0x100; nERR=1;}else
4 f$ N% W! t3 B* B9 z( M8 N* _                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
" W+ q# n; h+ j" g" z5 E                else{nLevel=0x100; nERR=1;}
4 X, a* b2 R- }0 |5 j9 m2 r5 E                break;
2 }% Y0 R5 T4 `; X            case ')':* ?$ S0 j0 |: ^8 X; l- z! n
                {nLevel = 0x100;}break;
6 L4 p% C$ B& N- L            case '+':case '-':case '*':case '/':( Q* O$ u1 A8 J2 L9 t7 u! Q
                {csym[nLevel++] = csym[2];}break;
: c6 [6 }- I% m- k7 f2 x/ E( ]1 L5 y            case ' ':case '\r':case '\n':case '\t':continue;( r$ A1 s2 ^8 ^* M- m
            default:+ M5 a5 @! N  T( I$ r
                {nLevel=0x100; nERR=1;}
' @) I# k- @4 c3 M" U& U            }; c5 x3 n; A7 z1 O3 R6 z
            if(nLevel==0x100)break;+ f3 ]; t2 E6 \$ C7 _% g- h( Y& c
            if(nLevel&0x10 || istrin>>t[2]){+ z" p7 y$ x# ?0 N  y" j6 _: V
                nLevel &= 0xF;
6 r; C  }0 r2 `3 t! K6 ^5 E                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
$ e# g9 S$ _/ U4 o+ B2 u- ?                if(csym[1]=='*'||csym[1]=='/'){
  f. U5 m" {* G" V                    GetExpValue(t+1, csym[1]);7 u1 ]7 w0 }+ m0 j" E
                }7 [" C  [0 L( X; P5 K
                else{# C  V: o$ h8 j$ c. g# z
                    GetExpValue(t, csym[0]);- r0 U- p- e* }( s
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
. N0 w/ C- f% O, u. W* ]- K8 {! o$ n                }
$ P! q. {5 I# b9 r6 O: N$ I, p4 F                nLevel = 1;% o3 b+ t; S+ }# ]2 ?: \) H
            }
, B1 t" Y  D3 H! Q. r            else istrin.clear();
  y* }4 ?6 ]9 `- h" Y        }
0 R0 `6 A# @! X9 z        else{nERR = -1; break;}
6 D, X, F( W& E3 ^    }, ~, k5 {' t8 p5 |
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
$ Q% p; C! a: x1 N+ G    else nReturn=GetExpValue(t, csym[0]);6 `1 K0 t; y$ c( K& h
    return nERR==-1?1:0;
9 H5 s! ?# g5 L* O7 Y: ?& d}}9 h4 Q3 y! d* D+ G! h; X& N" ~* @

: P% b. O, A# t' c. W
4 J  P  _  V; w) ^1 t3 C  c0 ?, Q) T7 J% p$ p$ ?* N% ^, y
函数模板使用示例:
1 y" T7 {- F/ k- N在以上那段代码的后面加上以下代码:
( R3 l! u& w! e% |: L6 X3 }8 N9 ?' W6 J

6 ]6 A- t, d) M! W1 K! G. G( t. p1 e2 G
程序代码: ) B1 L% G/ |4 n! _- f

# O# Z- D8 D) ]& y+ T8 n#include<strstream>
0 ^' R1 j1 b: T! f& B+ Z#include<iostream>+ Y+ U& q& D9 }0 Y) K2 A
#include<string>& a+ ~) A" ^0 H& s. x( B6 v
using namespace std;
- q+ C  P- u6 H9 [# Xint main(void)
* S  s) W. l1 H, W9 a{9 X- G; L5 `9 |# I
    string s1;
1 V5 p8 o* V1 q& y% v2 V& S8 F    while(cin>>s1)6 _* o1 N; X0 A
    {
$ ~3 u  r& ?& Q7 i7 j: w        istrstream isin(s1.data());) O% V- I: _' x% A+ e3 O& Z+ _
        double d;- _8 |, z* \/ v/ n  [
        if(fy_Exp::GetExpValue(isin, d))$ h% t  r! j& C( a
        {
( Y5 ~1 Z% A) P            cout<<d<<endl;
. @+ \* Y8 L7 j" i0 J        }+ A/ t0 H7 k8 ?2 A
        else
/ h4 D; N) \( w# `4 [3 p& u        {& a8 X7 @* ?  {# F/ a' R+ R" G; v7 N
            cout<<"ERROR"<<endl;8 R; y* Y& l# C( K6 O# H7 F( T
        }( Q+ r, `9 t. _, F/ J% q4 ?
    }
6 H4 g! ]' J+ H0 ~% A    return 0;; ?$ n( W4 c  V" e- X, i% I1 d  v
}
  Q% i  i8 ~* f0 b2 o  o( _. N$ }% z; ]: _- B
  K3 ~  A3 O+ z# j& i
然后编译执行就可以了(*^_^*)  n' N) _& m% C7 I) @% c$ U
其它:TC++上一定编译错误,不保证在VC6上也能通过编译! u6 Q% N( ]$ l
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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