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

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

在9月8日那天我特意编写的,给大家分享的,
/ f  D* W9 E( p$ j  M, s- t- x4 g一个很方便的函数模板,可以并且只可以计算含括号的四则表达式  n8 Z" o+ b$ f8 q
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)5 f1 @! \- b# q% k0 _
参数解释:; t$ [. W' I) [' T! l4 J7 _' _
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
) j5 W: K8 F0 KnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
' o) [7 r$ y3 Q! l+ ^返回值:
4 [6 s& I6 l, t, J+ ?返回非0表示计算成功,0表示计算失败有错误
1 x8 c' {( V+ m0 |3 T9 F2 z7 e1 \3 o. x8 m

  O* P% U1 u" t% F8 p3 ?; U7 E5 U. D: M$ v
程序代码:
. d- ~2 P8 y" U. y, n! p
: p1 \' [6 M" f8 u0 k0 w1 lnamespace fy_Exp{
/ O' ]5 W% H/ m) t- y! Cnamespace {template <class _T>
9 F' {% \# A! Minline _T GetExpValue(_T t[], char& csym){/ D5 \0 r' ~. G. Z$ k, ~$ v
    char c=csym; csym=0;
7 F  |! C% ^! ?    switch(c){) |$ y. e3 \" S7 B/ G; }
    case '+':return t[0] += t[1];
$ A  A* {7 @3 B2 }0 G/ B    case '-':return t[0] -= t[1];0 L4 V2 L2 a- v- U
    case '*':return t[0] *= t[1];8 d+ g: U4 V+ P9 {
    default: return t[0] /= t[1];//case '/':) [) }5 L7 X+ L7 c8 c
    }7 f6 P0 I7 v" _+ F& d, M8 S0 t
}}1 S! l4 h5 G  X
template <class _T, class _Tstream>
- E2 S* n5 q# x+ v, S9 @* R/* _Tstream: inputstream, _T: get return value
0 K* G5 w+ Q5 p! g- Q- ?* Return nonzero if get value successfully */
9 [  U* k. {* m7 ~! I. U+ xint GetExpValue(_Tstream& istrin, _T& nReturn){% W* v( W* j: w9 j$ K0 r+ a% [
    _T t[3] = {0}; //雨中飞燕之作  _+ Z4 d* M' D
    char csym[3] = "++";
0 G2 b! C- `  e    int nLevel = 1, nERR = 0;5 I" q" @4 W/ U4 {( T
    if(!(istrin>>t[1]))istrin.clear();% j3 H. E% U, Y: B: k
    for(;;){
% M$ F6 _! U) i. N" [) A- _+ n        if(istrin>>csym[2]){
3 A' ]) _, H4 \8 V5 ?9 N$ f) @            switch(csym[2]){* X" u6 B) q8 m3 N) C+ Y* S
            case '(':; {: d2 y) C& U1 Z
                if(!csym[1]){nLevel=0x100; nERR=1;}else7 s8 N+ v) ^9 N1 o1 `
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;; O; H( N( [% b7 h3 k
                else{nLevel=0x100; nERR=1;}: J% N. R: L3 C0 ~7 P
                break;8 z$ r4 k4 w+ b3 z0 f
            case ')':2 Y% S# w3 }. U" x
                {nLevel = 0x100;}break;
* ]& Q# }: K8 G0 Q% I            case '+':case '-':case '*':case '/':+ ^! R; ]8 j. c
                {csym[nLevel++] = csym[2];}break;( s) P( W0 B7 R, f9 ~% b5 p
            case ' ':case '\r':case '\n':case '\t':continue;
8 u2 v+ E5 C* _8 j7 |* ~* G' ^- _            default:
. B7 f* `: N4 \4 d7 P# M                {nLevel=0x100; nERR=1;}  t4 S4 ~; y& w: i5 j
            }. z$ t( \! i3 A" ?- Q; x
            if(nLevel==0x100)break;
: a' Z+ ]  e) m& k1 q            if(nLevel&0x10 || istrin>>t[2]){
" p9 F5 T# ^! G4 V                nLevel &= 0xF;; f0 n/ M; i' b7 i8 O
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
$ Z9 [. L" M% \7 o! W3 H0 \                if(csym[1]=='*'||csym[1]=='/'){3 W+ X* I8 T+ D( B
                    GetExpValue(t+1, csym[1]);
! t  s( u" \1 w7 V1 |                }2 N7 K3 }+ e- O; ^* o5 a3 h
                else{- |' Y: ~. T7 h" ?8 ^
                    GetExpValue(t, csym[0]);
/ a1 y, ?" y* B, _# }! I                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
8 r8 g- h; ?" e. A# f                }0 z% o: ~4 v/ @0 M2 d) B
                nLevel = 1;- j* l. @) k* O. p5 }5 P& l1 c
            }& y1 i2 Z$ S8 \- l
            else istrin.clear();
4 @) y" P6 s/ |- C        }) K5 ]6 u% @9 s# M: B# C
        else{nERR = -1; break;}4 a% J+ Y6 }* M: s
    }" P- n. ~' r1 M
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
0 I' J# V) e4 g5 w4 Y# P4 u  g    else nReturn=GetExpValue(t, csym[0]);
* @% R" s) u3 g8 M6 u# l    return nERR==-1?1:0;  K1 A/ E9 t& ?
}}
. d6 i4 G, h+ k, a) i  E- ?) a$ D1 K! L; m5 v2 e: o$ y; V$ W

6 K0 _" q: Q% e8 v( q2 u
% N- I: a! W& }) n! I函数模板使用示例:
5 ?" y6 P' _& C) U4 Z在以上那段代码的后面加上以下代码:
6 [" ]% h2 y& |2 R9 L! ^% K, K4 f# t; o5 g. D# b  N
/ m( J# x. Q2 O8 Q" O% P: m
  b8 S$ P# {5 X+ O# K+ e, U
程序代码: ) o7 I& @! J# K6 V6 n

3 {( D. f$ q  @, c. E- a#include<strstream>/ o9 E' Q- b$ W. y. J: b5 p
#include<iostream>
4 j% f  X2 f7 D% r1 y4 J) F#include<string>' M3 ^3 m/ ^$ b$ l0 O; Y
using namespace std;
* U! c" L' @9 E. v. T9 _  V4 n* yint main(void)
/ \: I$ l6 f$ {' ]0 d# l{5 S% ^  d$ T/ d* Z
    string s1;
8 a* E- d) l7 v" h1 _8 A0 U* v    while(cin>>s1)8 n' e# b# v2 T8 V4 m' f! G; r9 ~
    {
" w- H: T, D: s6 @4 l. @* d: t7 w        istrstream isin(s1.data());
$ n3 _' P( N& r9 u        double d;0 i$ v/ v. Z. A' s7 W; D
        if(fy_Exp::GetExpValue(isin, d))- U2 W' s0 K% r' ]- h
        {
0 j, m" t' ]3 u, d! {            cout<<d<<endl;3 v2 k! j: p  V1 f5 z9 D: h3 V& s
        }. h; u* d8 |  Y) U
        else
- u/ ^( t1 X1 J8 x1 A. p4 o6 [        {& w( W! B1 [. J# m2 ~, e4 ^7 @( v
            cout<<"ERROR"<<endl;& [( W2 U9 q' w( {) D$ P/ E
        }+ ^+ D' B" W' l5 V
    }
* }) o; `! N$ P  G1 ]    return 0;
# O1 N/ S8 w% g3 s}9 ]  |1 ?3 ]* h# J  j0 E& F: n

7 H) h5 I5 Y! E7 q  X( r
% ~' o& m( f" f然后编译执行就可以了(*^_^*)' z4 S: I$ t1 T; B) F/ \
其它:TC++上一定编译错误,不保证在VC6上也能通过编译7 I6 n- z, G: t$ \: \' v
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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