返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,
$ S  z4 ^$ ^+ ^$ o, K一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
  A* i' D$ s% I- M" D( \/ I8 I+ r7 g只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)  n9 A$ E5 j' y1 t' a- X
参数解释:
0 L. N# n5 ~$ Q, w7 {" R1 Distrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流; \8 M* m7 n, n; z
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定7 ?8 N9 N8 `9 F7 R, i
返回值:% i9 r6 p- p1 w- S8 `
返回非0表示计算成功,0表示计算失败有错误
  r9 F, {& s  B+ ^! E% Y0 O9 d
+ o" B3 ~+ C8 N7 ]: m
% D8 V5 H, j% s2 o8 A$ f, I  D: ~' O
程序代码: 1 o1 v2 O& F! @6 y# H' g: L
! H( N$ k- P+ c5 q3 L
namespace fy_Exp{0 q3 Q3 a1 ~; R# l4 ?
namespace {template <class _T>+ A6 \) g( N5 @
inline _T GetExpValue(_T t[], char& csym){) I0 Y; Y7 I3 u1 @
    char c=csym; csym=0;
9 Y0 Y- n: A9 U    switch(c){
/ ~) t# `  J+ z3 t& S8 A" w    case '+':return t[0] += t[1];" N7 E% f" G; H1 G2 m0 b
    case '-':return t[0] -= t[1];
8 a5 b4 v3 B2 J! U    case '*':return t[0] *= t[1];
9 V: o8 X  j% s4 x. h; A5 |& Z    default: return t[0] /= t[1];//case '/':+ r/ Y& E& ?, z) p: `6 }
    }$ q+ q( T' u  b) H$ l  U5 I! [
}}
/ \5 `3 V% q( J+ I4 Rtemplate <class _T, class _Tstream>
# _" H; W; W! O0 }$ z; H4 B, ~# n/* _Tstream: inputstream, _T: get return value
; F, s' n5 r6 [% u6 {; `1 T. D* Return nonzero if get value successfully */; F3 t. [% c. [; u
int GetExpValue(_Tstream& istrin, _T& nReturn){8 e  k/ z) C. j1 H
    _T t[3] = {0}; //雨中飞燕之作
! a3 U/ b  ?8 D( t0 k6 v4 d7 S    char csym[3] = "++";
7 r" y5 |& q# @% K" u    int nLevel = 1, nERR = 0;, R- a- |) `6 ]
    if(!(istrin>>t[1]))istrin.clear();/ Z) S. h6 ]: Y: K+ c
    for(;;){4 x. D# s/ n8 k" S
        if(istrin>>csym[2]){2 z9 R" N+ w4 A! o
            switch(csym[2]){
/ u! H+ e" i  o1 \            case '(':
6 @3 @. ~* B+ N" M* J6 J                if(!csym[1]){nLevel=0x100; nERR=1;}else7 Y$ W1 N& j+ K* s* p
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;% G! F' g$ z" J$ g0 x2 y7 [* ^
                else{nLevel=0x100; nERR=1;}
5 X$ O7 X9 g) E7 }7 ^: }! M                break;
4 H$ s4 W4 H, v: ^( G6 M            case ')':
, y# h# |. Z1 |' ~                {nLevel = 0x100;}break;: N5 j8 [; Q5 f& Y/ j+ h1 M2 L! ^) v; T
            case '+':case '-':case '*':case '/':# w" z4 `; G7 G/ C2 I. k% H
                {csym[nLevel++] = csym[2];}break;
' p! \3 o- S0 E5 G5 F6 n2 R. g            case ' ':case '\r':case '\n':case '\t':continue;* a" m9 _5 r# |
            default:7 P, W2 |" C* |
                {nLevel=0x100; nERR=1;}
! H! u5 d* @' g$ f, F            }6 P4 P3 ?9 a# ?. g) x$ f& k$ L+ A
            if(nLevel==0x100)break;
+ N9 c* ^) G& \            if(nLevel&0x10 || istrin>>t[2]){% k/ r0 [9 K! e4 Y" Q4 X
                nLevel &= 0xF;
6 q; l- Z' ^+ o3 d0 t9 @6 C                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}& \7 c, w$ j8 s  m8 O
                if(csym[1]=='*'||csym[1]=='/'){; Z- }9 w1 Y# h7 L7 ^0 e9 Y
                    GetExpValue(t+1, csym[1]);4 @8 X# N& g' f, t" l: e
                }% h4 i( K3 x( s; I9 i
                else{+ W. T" e5 F5 @- {& j
                    GetExpValue(t, csym[0]);& y' v% G( I% c: C6 a3 y
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;# Q3 W# d6 x1 Y, S1 Z) j
                }2 l3 R' Z$ T& B% u( n
                nLevel = 1;0 }1 k( V" I! E/ J" j# K
            }
: ^, k/ n7 Z+ k9 [            else istrin.clear();
# B, _  J; s5 P" P5 W, A3 ?        }7 \( a7 W4 H& c  H9 n9 s' T- i& E
        else{nERR = -1; break;}2 V# ~4 i% V4 S; ^
    }
1 x- ^3 v' X5 l; b    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
5 j9 x% N. E  o    else nReturn=GetExpValue(t, csym[0]);) d& ~+ R  |+ i( w
    return nERR==-1?1:0;
  b5 G- _- t& ]6 {* P}}+ ]( W; l% }8 m: f3 h) N- P

' o) Y. q1 ]1 G5 t; s# x
) ?2 v6 h1 x% `! i- `( p4 p! |
/ n7 j9 M2 \) G. X8 a, {函数模板使用示例:0 `, L- Y/ A& x, j
在以上那段代码的后面加上以下代码:
( b8 X5 o8 @; g" l, I' d( @
" }) S3 T) K5 }2 F
' |; i" |0 P! s& s' h# X
; q1 e# X3 \2 x% [6 @程序代码: - G1 k. |3 N$ j; L

5 e' `1 h! p# E" ^3 v  A4 N#include<strstream>
) @! }3 M. F/ g6 I1 q#include<iostream>
* {/ m6 z7 q" |/ U#include<string>
& ?' o; y, X! m+ s+ f9 C- pusing namespace std;. {* }9 W$ L  X6 c. h* m! _; {
int main(void). y% f3 }9 c# ^% c7 ]
{* v: a$ i, d7 |0 R# N
    string s1;9 i9 `- }& K$ m# g' W9 j
    while(cin>>s1); F7 N6 |0 l/ @2 H- l
    {
  k) Z" G% G9 N! N        istrstream isin(s1.data());
5 j4 \7 J; G. s+ g4 }        double d;" P6 A2 S1 n, ~9 t; D$ s
        if(fy_Exp::GetExpValue(isin, d))
/ Q; n; m. g# }5 C1 L        {3 n1 S0 N& C" U: ]
            cout<<d<<endl;
- ~: a7 y3 _/ ^! i( i1 @" j, H/ M% f0 L        }
% P9 K" T: G3 M6 a        else3 u3 U* E% k! |0 D5 w
        {
! W) h! B$ \) T6 e, y+ U: s: P5 X+ E            cout<<"ERROR"<<endl;
& \7 Z' t) [0 h2 H        }
# v: W% j. o% q) i& Q  d    }3 ~1 X/ K$ ~( N8 R/ B
    return 0;
  G/ U. S+ B; y}
  f& c+ t6 E; \4 _- `! l, b4 A* I: Q$ {% J% m2 h1 m; Q

: F6 c; _4 [9 i9 u然后编译执行就可以了(*^_^*)+ \, u" `8 A* ~2 B* x  \) }" ]) t. _$ }
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
3 p+ ^: ]) F" g$ x" P& ?      建议使用VC7或VC更高版本,或者使用GNU C++编译

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