返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,' `' k# H. P) X0 q4 ?
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
6 r* U6 p- ]6 M, l* l* U只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
% w8 J( V1 d( W% e2 Q, W- G参数解释:
* _1 x6 Y+ f$ g: Y. O) F2 N3 jistrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
* J: {7 b/ T$ ?2 \4 U2 qnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
6 k; V% m$ G1 i3 x: T( {1 Y+ Q2 h返回值:
' l4 h: f) m$ c0 J; f返回非0表示计算成功,0表示计算失败有错误  D  x  w. X+ }+ A
" d/ Z2 J' B' D  I' ?4 b' M+ X0 U" B$ T" S
5 G+ P; F2 Z" j, {) w% l5 N1 e

. D8 _! e0 o6 x程序代码: 2 E3 Z4 T0 M# z, ~
8 `- M7 X0 u7 M9 q1 Z7 j# @2 _1 Y
namespace fy_Exp{
. Q* H0 D) Z3 A, L' Rnamespace {template <class _T>
4 m2 k. \" r5 i! k8 S5 X: D' Sinline _T GetExpValue(_T t[], char& csym){6 w* i2 @3 [% A/ s) q( m
    char c=csym; csym=0;
5 ^2 f7 q/ p9 m3 v; p    switch(c){' x# D( K. l. s4 H' z
    case '+':return t[0] += t[1];
7 M6 B1 A+ j: g4 |2 [  u    case '-':return t[0] -= t[1];
# {6 }* }8 ~; a  x2 J0 s    case '*':return t[0] *= t[1];
. I3 ]$ F4 W- G! V4 ^" t# ?# e    default: return t[0] /= t[1];//case '/':
- O; V/ V# P3 [    }
6 n& t  r& W! P0 A! f/ u}}
" X% H1 `9 R  D) i. E! z0 R8 wtemplate <class _T, class _Tstream>
8 q7 s8 e+ c8 i8 g% k* Q' s6 |  f/* _Tstream: inputstream, _T: get return value; \  e" _& e' S6 o  C( L6 f' T  r
* Return nonzero if get value successfully */" L9 B% H# i1 q/ b3 _
int GetExpValue(_Tstream& istrin, _T& nReturn){! U8 \% L' @5 r- C6 u2 ~
    _T t[3] = {0}; //雨中飞燕之作: h- U" S- q' h1 `' T
    char csym[3] = "++";
: @" w  o) e# q: a2 o( E7 L1 b- I    int nLevel = 1, nERR = 0;
5 g% ?* i) ~$ Z" U/ e6 a  Z( A    if(!(istrin>>t[1]))istrin.clear();
9 H4 V; W/ \+ M) A1 h3 i% `0 a    for(;;){
: I. D" D1 r$ f5 e  U% o$ C        if(istrin>>csym[2]){( |7 s! p+ t+ m+ L9 A% _! x
            switch(csym[2]){& b6 C; _# E: b8 n+ F: ?+ e
            case '(':
: B% m1 i, h4 V* m1 y                if(!csym[1]){nLevel=0x100; nERR=1;}else
0 B! v3 H+ Z" c7 s* G2 f6 g                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
# w2 M1 U# ]' S7 Q                else{nLevel=0x100; nERR=1;}  ]' S% L3 @  H
                break;
6 h% F8 d8 ?4 o. v: A" z4 R; i5 U% ^7 m6 K            case ')':& s1 l; _# }* r1 O$ w/ a
                {nLevel = 0x100;}break;
. |3 m$ z- w( L. O6 K" ^. s            case '+':case '-':case '*':case '/':
. S" r9 j) f3 |2 X& f0 d( H2 R( p                {csym[nLevel++] = csym[2];}break;6 X0 b  h1 e/ m3 |% s
            case ' ':case '\r':case '\n':case '\t':continue;9 k5 u: d! i' f
            default:& m3 |' P* E' Z* w+ A
                {nLevel=0x100; nERR=1;}
% d& i- z" z9 V& l. l% f            }
5 I! i1 u. |# q4 i            if(nLevel==0x100)break;) H" p  V; N  e2 z4 O5 w
            if(nLevel&0x10 || istrin>>t[2]){
  \2 p. y7 f) J                nLevel &= 0xF;2 K. H" J1 o& [6 P9 T+ ~
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
* v3 V! r3 l+ }                if(csym[1]=='*'||csym[1]=='/'){  w: @) V3 V1 `' {1 I
                    GetExpValue(t+1, csym[1]);
$ j. k9 T7 ]) O3 S5 [1 E2 b                }6 l; m% l& K! z9 I: t' c
                else{
! p4 C1 E5 K  G6 r                    GetExpValue(t, csym[0]);6 M! r9 f9 y6 _' j
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;* K; Z# A1 h, ^' q
                }) P! a% N; I6 X8 {- y: J( C
                nLevel = 1;
, [9 _# E0 s7 e7 j1 n( q  x            }1 o: K: }( m/ p: M3 U. j
            else istrin.clear();4 s% Q2 B) A; B5 ^  t8 c8 v
        }
4 a" b7 J" S  f; |        else{nERR = -1; break;}2 v$ p" R7 y7 ?7 R
    }% J: U1 y7 C3 a$ O: x  l
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);, o2 m0 G- @$ |+ I) c
    else nReturn=GetExpValue(t, csym[0]);* h+ H& L; N, V7 S, _) |$ ~$ J
    return nERR==-1?1:0;/ V  ]) V  U- s. I5 \. [
}}$ A2 {$ E* M; D
+ I6 A' X( k  W' N  e# j

+ y/ k  I/ j1 {7 j9 e0 K
2 S3 J; ~: P: @; ^: i函数模板使用示例:
6 @& e5 z8 J* |* W5 e在以上那段代码的后面加上以下代码:
' A! [' a3 p8 X: N7 t0 ?! g6 k6 s  V2 D  x/ G
, N3 h) d$ a) B2 u

% D9 o, ^/ e4 v) ?) c% \程序代码: 8 E% G  d  l* s$ D1 h
5 l: q& M! U3 d% ?' d7 ^" k
#include<strstream>/ L# _, }1 x- W/ x
#include<iostream>
5 t4 C$ J/ H- J. j0 v% M1 |#include<string>& s3 A, I, }4 ^( I% u2 X, B* G
using namespace std;
1 B1 G4 V# [7 ?! L  T  B8 \  i8 rint main(void)7 T6 h! ^& F" f# y3 y
{2 h5 D3 z6 I4 \: @0 N6 q* P( N
    string s1;* C# n0 \# s% C  R
    while(cin>>s1)
3 Z! Z8 L+ l4 \, q$ z" {    {/ L" E" E1 |0 _1 s
        istrstream isin(s1.data());
4 e9 k0 \, m' \# r  @        double d;
& r! p; a1 b( c5 P1 a        if(fy_Exp::GetExpValue(isin, d))
, m& D/ u8 x/ X# M) V, N        {
* p* T" i: }! s  e            cout<<d<<endl;
, W" ?; c0 ?) E9 x3 V4 ^% p6 X        }
& M' k1 W0 n1 W5 d8 L9 b        else
6 @: a- H0 B# @( c        {5 D0 I; B# m  b6 N
            cout<<"ERROR"<<endl;, Y' _) c2 L0 a$ L9 w
        }
; C5 Z) |: ]  J5 ?$ \# J    }4 Y/ E/ g* \# @% _
    return 0;
6 v- O0 g. d5 Z# K- E}* f  D! Q: i( Y. T. N, |

2 A  D8 X5 x7 |$ E5 a
' L4 T; N- e, j# g然后编译执行就可以了(*^_^*)
7 I; \3 t, t2 r5 c" u其它:TC++上一定编译错误,不保证在VC6上也能通过编译
0 t* S8 a5 H/ O  |, t3 b& x" D      建议使用VC7或VC更高版本,或者使用GNU C++编译

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