返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,4 t2 t- i$ W: ~  \
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式( D$ r+ P: {; c
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
' o& o$ w: _+ n参数解释:. L3 S2 T6 s4 k
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
1 P' T# [5 g& F3 a) ^1 i) o5 [nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定# P- u, h3 c4 j& Z, b+ z) B
返回值:
" e' k+ A& ]; a; P: _7 \返回非0表示计算成功,0表示计算失败有错误6 _2 c+ D% c) I8 W$ l) c

2 S% ^+ Z! }' _4 b* c
9 X; y, D8 _6 z
7 H) l' ~# d: [1 K/ `# y程序代码: ' X0 k: Q1 |* b  u" |3 t

: ?# F8 m# C1 c) P9 X. |namespace fy_Exp{1 n' o" ~7 @$ M7 O& O. Q
namespace {template <class _T>
! S8 S' K: Y/ L9 Finline _T GetExpValue(_T t[], char& csym){3 u/ Y/ s+ h- V! H
    char c=csym; csym=0;
+ c! Q/ Z# L" K8 w$ w- u' X    switch(c){
. m6 h8 }! L! J7 L    case '+':return t[0] += t[1];
7 p' i/ B  S' p4 y0 e  s$ O    case '-':return t[0] -= t[1];
9 r: _( `' ^* L& D7 Q6 e' t, B    case '*':return t[0] *= t[1];' h0 R, s7 \) ~# v& J2 _
    default: return t[0] /= t[1];//case '/':" Q- X5 W9 t2 z2 ]+ e9 w
    }
. Y( g- T' ]0 C" e& F" ?}}0 R5 H, T$ m, |* P. [4 m0 n+ }1 U: Y7 j
template <class _T, class _Tstream>
9 A. V: f# ?' |- O8 j/* _Tstream: inputstream, _T: get return value% [9 Y9 n) n8 X) U
* Return nonzero if get value successfully */: l" e) u# D% l6 }; z
int GetExpValue(_Tstream& istrin, _T& nReturn){
/ ?& ?! n0 x, J4 b    _T t[3] = {0}; //雨中飞燕之作; i! ^( p6 x/ H' {
    char csym[3] = "++";$ d8 j( l' N0 h- J( P: x. B
    int nLevel = 1, nERR = 0;
8 t) j& ~, E1 C: v' t    if(!(istrin>>t[1]))istrin.clear();! `2 Z: o- N, c6 M7 q6 `
    for(;;){  o2 o' z5 k3 F0 l9 P
        if(istrin>>csym[2]){
  j' l$ R+ G3 @& {. J+ l0 r            switch(csym[2]){- c% r7 [, N0 b/ \* s
            case '(':
. A9 P; J0 P, n                if(!csym[1]){nLevel=0x100; nERR=1;}else, R, U, V( Z5 H
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;2 P' |- @: R& l! l) ?2 a& y
                else{nLevel=0x100; nERR=1;}) k) X2 d+ L0 W9 Y- j7 D- m1 i
                break;+ ?# S, \9 i1 q7 n/ C
            case ')':
6 s; d: G7 N: y% Q) R9 b8 W                {nLevel = 0x100;}break;! e. g: v8 C3 R8 f% s8 e5 {( ^: v4 r% l8 B
            case '+':case '-':case '*':case '/':
. B- q# z/ Z0 V: }, `( x- u                {csym[nLevel++] = csym[2];}break;
; E) B% U; j4 p# G            case ' ':case '\r':case '\n':case '\t':continue;
# n% ~5 L( T$ S0 Z, @. g            default:
5 z$ V9 G% n. c                {nLevel=0x100; nERR=1;}. r4 g- \5 w! O/ M
            }# @  E, d6 ]9 V8 j' `# N
            if(nLevel==0x100)break;
3 N+ f0 |6 x8 R            if(nLevel&0x10 || istrin>>t[2]){  H& [6 n4 P; h. J0 P  H1 s
                nLevel &= 0xF;/ u* o+ q3 ^7 z- ]9 q
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
5 [  |( _9 x3 P6 r9 k$ }# {                if(csym[1]=='*'||csym[1]=='/'){
! b8 O$ ]+ G! |: h2 H                    GetExpValue(t+1, csym[1]);
8 z( h) T) j; A' s( S6 v4 @- J                }
, V" `% s0 ]) i& D1 b                else{
5 @) U( U) F) F! p1 y! F                    GetExpValue(t, csym[0]);
' m2 g0 x3 l' W: R' f! n                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;. }) _5 [; u" Q& L& d  m
                }: L0 E" F" k- a, r
                nLevel = 1;
, p9 V/ `, R) s( |( R, Q            }# N( X7 `7 d8 P5 E6 H
            else istrin.clear();
9 {: U1 _7 }- ^7 E0 N        }
& u5 U+ z% [: [. _: C2 [        else{nERR = -1; break;}
) K; A* {* m: G    }
3 h( ]3 @7 A6 F5 d) S    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
  y1 K/ t3 p* t5 a1 B, U2 G    else nReturn=GetExpValue(t, csym[0]);; `$ c1 y3 E6 g
    return nERR==-1?1:0;: \2 y: y$ C, ^/ k6 x
}}
; X" X( o. x, y7 H  o) `9 y0 d( V- s. t) n( d
$ I, P# j. b, D( f

/ }9 K4 F& Y# m& H1 w7 F函数模板使用示例:. f2 k# m, S2 u$ k1 }5 w
在以上那段代码的后面加上以下代码:
' u2 Y" h& ?% N. l+ l
% ]0 ^4 j! M7 k/ k8 m5 P
, k7 s8 v$ e2 l3 o% Q0 ?' Q* Q% x$ g, M* \2 m8 w- P
程序代码: . S, h0 Y* y! U
* h1 n6 \/ D4 d! Q" C) p
#include<strstream>
5 Y* h2 b+ C) t/ y+ L0 z' c5 Q! x#include<iostream>
8 c. f% E/ ~, N5 h0 C2 q/ P#include<string>) p( y! c- \7 x3 k6 x  [7 @
using namespace std;
1 T% J4 V2 m  q0 M  I2 I$ _int main(void). G1 ?7 s- [% ~7 S9 W
{3 t" u1 y8 B1 N, E2 X2 ?+ L) `
    string s1;
# M  g$ |( Y9 v9 y; L    while(cin>>s1)
- |, e! G% L( C* D+ z) N; x+ ^    {
8 m7 S7 [! }  c  [# v! {; v- [        istrstream isin(s1.data());) |2 @( N6 C' i$ \, I4 f  G  v
        double d;
, m. M7 V6 `/ B; M        if(fy_Exp::GetExpValue(isin, d))* v; @% h* V" \% d
        {  U% D; N1 A- z0 c; V
            cout<<d<<endl;
  A6 S3 g6 W. r5 p        }
/ g) z: A3 q- c8 e" o3 I        else) t2 G$ z8 K; D+ C# i9 R
        {
9 ~' X; g, j0 e2 T/ K            cout<<"ERROR"<<endl;5 z% W0 o" V1 P( ~9 G. M! W4 ?
        }/ R; O' m5 K  i
    }
5 `/ Y  d! t, T$ F8 t( I5 |    return 0;; _  T3 l3 i2 H& i6 Z
}
" F3 j7 x5 b, r& H8 g% `, b& n6 w, d& t+ E6 I
& r: W7 ~8 i. ~) U2 |% G
然后编译执行就可以了(*^_^*)- o! e! c1 L! `/ o2 g
其它:TC++上一定编译错误,不保证在VC6上也能通过编译7 d5 W& B% S2 M( Y, O
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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