返回列表 发帖

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

在9月8日那天我特意编写的,给大家分享的,
# z2 l' Y! P( B, {3 L/ Y0 O/ f一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
/ E, V% `1 I; {3 L* H# B只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)9 }% M" f% W2 G, P2 ~3 W
参数解释:/ k, S1 b& i) t5 }6 j( y0 u
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流+ p7 e2 B0 W" h  P7 l( Z4 n3 [
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定6 W/ {* a) {/ Y7 y
返回值:( {6 P3 Y! W4 k5 Z4 O0 m
返回非0表示计算成功,0表示计算失败有错误. E9 g' v0 J& r* ~$ E

! t. g  `  X( C) B
: [) x) S8 Z* S/ ]! S
) a7 p" R9 @8 ]4 ]& a; W+ S程序代码:
* V6 [# c8 U( h2 ^/ H% s1 S. K' k3 O# K# z
namespace fy_Exp{
# Q. b9 q( l9 N2 Q+ R( Knamespace {template <class _T>& U! V: ?" W& \6 c
inline _T GetExpValue(_T t[], char& csym){
5 V, S6 J" I) n    char c=csym; csym=0;; l) o! C3 v8 b$ V0 t
    switch(c){
5 r9 D6 M7 @! \! S# X9 E    case '+':return t[0] += t[1];- z1 x& Z+ Y2 S6 @1 M0 D4 e
    case '-':return t[0] -= t[1];
8 w( T- e6 ?; R7 }    case '*':return t[0] *= t[1];
0 s9 r5 f' X8 ?1 r) p/ u4 Q1 M3 Q    default: return t[0] /= t[1];//case '/':% l+ S  y$ o  x* i- O( |* a
    }
9 `+ D6 \3 x: m% I( l+ p}}/ Z$ N. N7 C0 O( G. T# W2 J" |6 C5 n6 b
template <class _T, class _Tstream>+ j- w6 r9 P# K! v' |, ^  C
/* _Tstream: inputstream, _T: get return value
; ?- }# J% ?! M# P) c! n) I" Y* Return nonzero if get value successfully */
. Z, |/ i, t6 M2 r, h7 w, J! @2 mint GetExpValue(_Tstream& istrin, _T& nReturn){) U0 a9 ]6 I3 S7 N
    _T t[3] = {0}; //雨中飞燕之作7 q2 R$ W( {/ }
    char csym[3] = "++";  A9 s- F" }# R) |- M5 G
    int nLevel = 1, nERR = 0;
. P; L3 l/ M6 f) U0 p5 D" g    if(!(istrin>>t[1]))istrin.clear();/ d1 \( n5 ?" n  \3 r0 @5 _
    for(;;){
# D9 E' o$ h! V( `        if(istrin>>csym[2]){$ f- j, Z' q  p0 P2 Z! h, a; m
            switch(csym[2]){4 k' Q, d4 d1 K5 y+ p
            case '(':$ ~" Z( {. l; q: p& n7 Z
                if(!csym[1]){nLevel=0x100; nERR=1;}else
# n6 g0 X  c+ l3 A5 A, P3 ?                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
, t6 o3 J) s/ V0 f0 m2 F5 ]                else{nLevel=0x100; nERR=1;}
7 U6 i- B; ~1 v/ C0 Z                break;5 _/ G+ b8 K: c7 A; f5 D) r9 |2 a% Y. B
            case ')':& L, U# u# h) |; D4 j& {
                {nLevel = 0x100;}break;0 a0 n4 j9 y1 z: N# X% c
            case '+':case '-':case '*':case '/':% e7 f; S3 N# l$ _; l3 n( O
                {csym[nLevel++] = csym[2];}break;
# w( y0 Q. {4 O3 Y2 `& R9 j# E            case ' ':case '\r':case '\n':case '\t':continue;/ U# W2 J3 e0 l$ b7 g2 Y& W
            default:% g* s7 }' y) @! l8 q
                {nLevel=0x100; nERR=1;}* U( L/ G) Z8 P5 q8 g7 Z9 d* }
            }( o: s) v& C0 H2 ]& ?; K6 p
            if(nLevel==0x100)break;3 t$ U4 [& i& f3 ?
            if(nLevel&0x10 || istrin>>t[2]){, B! `5 p' |4 E# ?4 U; U) @; ]6 r
                nLevel &= 0xF;& ^& m: f2 D2 Y/ L
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
- v7 d% A  h! ^( z9 B. b1 d0 c                if(csym[1]=='*'||csym[1]=='/'){
# c/ l- t0 E  q( g# q                    GetExpValue(t+1, csym[1]);
% d7 {7 v0 ^. W; P" H                }
4 C; @/ V# U7 m4 T4 }                else{
5 q* ]" `  B6 D3 S- J, ?5 [                    GetExpValue(t, csym[0]);
0 o$ b1 }+ [$ |7 f6 @, M1 }5 W                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;: V$ A& w6 x+ L% m* Y/ x3 z: `* H7 r
                }/ ?2 s) f7 z& ]' n  W8 s  ?
                nLevel = 1;) L0 D! P8 c, \' C. i+ t* p
            }
5 R$ x$ C" u. T: C* i1 _, d            else istrin.clear();) q6 [, P8 e3 Q: P
        }
+ W6 \5 f9 z6 m        else{nERR = -1; break;}6 o9 T# f8 [8 w7 S
    }( ^4 T$ c+ B! j
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
4 I) L! Z! I$ f# I" L" ^8 R  |; s0 W    else nReturn=GetExpValue(t, csym[0]);( a) F+ J# B  _  Q3 q
    return nERR==-1?1:0;0 T2 |" ~+ u- r' ?. ?3 \2 w" F; ]
}}
; k- a8 b3 j2 s. F; F0 G& A+ S7 u- P
. x/ r* A# W6 i; S; F, n9 q$ Q) R
4 \- K2 U* D1 A! Z# D- @! v# |$ h- G  L! v( }+ z
函数模板使用示例:
" ?# e4 Q' }5 T5 }2 o! L& _在以上那段代码的后面加上以下代码:
& J5 n; e4 @  ~( s0 R1 Z: C2 Z; U3 u, z3 ^4 O' D
" P+ a0 j3 c0 ?7 d9 V6 [$ q# g' F; i

) w- \  y, K/ [+ t" q8 ?程序代码:
5 j8 o6 x5 Y2 Z3 x: i) o* \
5 S3 g6 q# h9 r+ P. a$ H* y#include<strstream>
: Y7 m3 i2 r: h  z#include<iostream>1 I  ^2 w# Y" g! p2 }
#include<string>- A. X( S" \5 E
using namespace std;( K) q$ O8 ]# F" v; A+ R7 E
int main(void)+ O2 N! c$ W! S& R: m" F" M% B
{
' |( N# Q+ P! X( Z    string s1;! N' ^$ w, A% Y$ m3 D3 w1 h5 T
    while(cin>>s1)2 i& l$ S. i+ \; T" [
    {
# B" B0 U" Z; I1 l6 ^1 F2 w' t) O3 `        istrstream isin(s1.data());8 o; V# z( [# N% }5 w! b1 _4 J
        double d;
. C* B& l  S. m        if(fy_Exp::GetExpValue(isin, d))& L  m  V- V" z( F+ p# E
        {
' k0 B8 K* L; L8 e/ V& j            cout<<d<<endl;( g3 v6 [( K: j: O' h( U) I% I
        }
! G) d# M* u5 `" v        else
, J: F2 Z7 d1 b- y: e        {( s: h& V8 j1 q" R
            cout<<"ERROR"<<endl;
. ?& K/ o% |3 L        }
& @; l/ \* @+ c# D    }
1 w' d/ m+ T. ]/ m    return 0;
: w. F" z# R) r- x8 q$ M}; S& _& i( w% l
7 V3 i  H# S/ F/ V7 b: ]6 y
: P* L6 s5 Z' @
然后编译执行就可以了(*^_^*)1 P3 ^3 k1 Z$ Q( M
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
& N* ^) b  N8 M  X+ i& x/ R2 h% p      建议使用VC7或VC更高版本,或者使用GNU C++编译

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