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

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

在9月8日那天我特意编写的,给大家分享的,5 f+ S9 n6 |* w3 `3 m! }
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式1 k/ J0 H( _2 h5 W% e
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
; N+ _7 r# b2 a参数解释:# N  R6 }) H8 n
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流9 }! p  ~* |& f
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
7 ~9 C6 `6 B, y3 A/ r. ?返回值:
/ P* ?% J: }% c2 y' U: X* N返回非0表示计算成功,0表示计算失败有错误  j# `' A, w% P8 e

( ^! ?) U5 b3 h' Y+ V! p+ H
# m4 Y0 `/ W0 m. b8 F4 d' {. Y/ P; j
9 i6 H0 w3 q, |5 e1 f程序代码:
& ?/ B' R/ g+ n% E) o9 O: Q$ J; W9 D* [
namespace fy_Exp{+ W' g$ g7 \* q. l4 R7 g4 G
namespace {template <class _T>
& f1 f3 |6 Q7 D9 ]0 n5 sinline _T GetExpValue(_T t[], char& csym){( G6 f8 F: T! [* U; ~* R5 A
    char c=csym; csym=0;+ f4 E1 r: ^7 w! H' C
    switch(c){
( V9 F( [5 T) q) o$ s: G; L    case '+':return t[0] += t[1];) o: [1 L! Z. `
    case '-':return t[0] -= t[1];
( _. P. N4 I4 l9 B    case '*':return t[0] *= t[1];
' b0 W4 w$ a4 ]4 Z% ?, p1 }* l    default: return t[0] /= t[1];//case '/':& E0 U& I$ n# t1 S# {. m/ T
    }
9 p+ I5 _+ E* O0 J+ b) F, O}}9 `% s2 U4 g5 R
template <class _T, class _Tstream>' ?- R$ {0 {+ ^$ B
/* _Tstream: inputstream, _T: get return value
2 [4 s% B# v7 ^( \2 u! b* Return nonzero if get value successfully */
3 o6 u& Y/ X" s/ Dint GetExpValue(_Tstream& istrin, _T& nReturn){* k% }! E) D8 l& [) A9 f: b
    _T t[3] = {0}; //雨中飞燕之作: H  }# v8 D, b* P
    char csym[3] = "++";
2 f7 J" u" [/ M6 H5 {, @" j    int nLevel = 1, nERR = 0;
( d, a; q1 n* p9 J" f! y" [- ~, P    if(!(istrin>>t[1]))istrin.clear();( `6 s+ n8 S: \& \6 Z3 S8 ]2 `5 N& P' S
    for(;;){" D' r! c) T3 r$ x* S- ?
        if(istrin>>csym[2]){
4 P6 U" ^1 G+ G0 U            switch(csym[2]){
/ |6 \: q1 R" o% Q            case '(':, A5 f/ A! y  z0 p* E, o' K& j
                if(!csym[1]){nLevel=0x100; nERR=1;}else/ r# P0 R8 E9 i# F0 L' T
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
$ {. x! i* G3 ?  W                else{nLevel=0x100; nERR=1;}% A1 H9 ]! @! K8 W
                break;
! e% j6 ?, j; M8 M- J+ q& o            case ')':
( E7 U0 r% {* z9 V, ^  b                {nLevel = 0x100;}break;4 O1 Z* z1 q8 R( Z3 \1 M, F. U4 |* G
            case '+':case '-':case '*':case '/':
+ R3 Z( e" p' }0 T4 w/ u2 i0 `2 d                {csym[nLevel++] = csym[2];}break;
& T$ F7 ]0 C' F2 y# j. a3 O; {            case ' ':case '\r':case '\n':case '\t':continue;4 B$ c2 y& d# W7 [. \+ ?2 L
            default:
. U5 b  H7 G. f" z, h                {nLevel=0x100; nERR=1;}
" E# A( P- w- S3 O# B" h            }
- g; U" M7 x, E6 S- d9 ^            if(nLevel==0x100)break;
6 A. B4 K- m1 H* S3 @% U; z            if(nLevel&0x10 || istrin>>t[2]){
7 c6 t: _3 j* W* ~                nLevel &= 0xF;
+ J  y/ Q3 N, V, B$ o0 Q3 [" O# `: r9 U! W                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
; E7 E5 l* \# P* V: \                if(csym[1]=='*'||csym[1]=='/'){6 w/ w& t! V7 I
                    GetExpValue(t+1, csym[1]);
9 b' a" Z  n) b4 K: Y2 Y6 m, ^1 T                }, d4 k; {: m. D4 b
                else{+ X/ A- H6 \0 I* u2 w; z
                    GetExpValue(t, csym[0]);
0 w6 F+ Z- ?6 g: t; f2 e                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;/ I- f6 I. r, \5 R
                }% W+ l3 R. v1 X/ l2 o
                nLevel = 1;
1 i9 G( c' _8 ]4 |9 b: x8 s            }( V! C5 r# _2 i8 B9 F/ Q1 R
            else istrin.clear();
# ?2 c8 w1 e8 V% ]* C# x+ M        }
' \7 m6 `  `2 _; ~4 y        else{nERR = -1; break;}
! s" M: J6 B; z5 X    }
3 S  T8 W) P% @5 {7 t5 S% W4 W    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);7 p7 W1 U% ]% |! f9 o
    else nReturn=GetExpValue(t, csym[0]);1 C) c$ O1 o7 W5 m
    return nERR==-1?1:0;
* e/ t* v8 v, Y4 M}}
9 H9 ^& ~6 b* }8 n0 ^9 W( Q4 ~& K: S$ T- s7 \+ j
: k/ P2 a$ z* K( B' u
! f5 c5 N- g" x  h  U
函数模板使用示例:" D+ ]# y8 w& K- Z3 O
在以上那段代码的后面加上以下代码:
* t$ I, I. v) W# p' R
) g" q2 i2 B8 O0 Z- o" f & l% @) f+ z9 O
7 t. m! B3 k* K
程序代码: + V  h& j; G2 P/ i) h

6 w. L0 B; {+ O; y& h- `#include<strstream>
0 I9 z! Y' l3 D9 z#include<iostream>
0 m" Y9 y, q5 _0 Z2 M5 ^3 |3 _#include<string>! @6 d) j2 H4 P; v. x
using namespace std;
. E8 P  L' l  m2 vint main(void)
% ]2 [# b1 ]1 L{- ^3 W/ g6 ?, _; q3 r( o
    string s1;
. i. y- u8 s% M$ n8 H8 o    while(cin>>s1)
; ~1 G+ C: B3 W, S1 ~    {& J0 l' H8 c) o! I5 W& ]% l: a: r
        istrstream isin(s1.data());( k) }$ b0 i  W8 d0 M2 [
        double d;- B$ z$ V. D$ G( }
        if(fy_Exp::GetExpValue(isin, d))- n; w& A; ?9 ~: B0 `0 C! ]
        {
2 s9 m$ Z: q8 J# B! {            cout<<d<<endl;
9 _: B5 k, r* `: F        }# F1 \2 s4 S; [2 M- L( t
        else( u. l2 `# h6 t, s& [: R' L
        {9 K+ G; F4 V  N4 ?( a& Y, h
            cout<<"ERROR"<<endl;
0 Q8 f  `) t1 k' }        }# q* K9 S2 N2 y
    }
+ J% F2 y, |& h1 Q9 n5 T/ X    return 0;3 `" S; h5 C+ s: C" A* \
}, t2 k$ _& l, i+ f7 a7 N

, `" {+ ~, Y) R  g! J
: R; R5 s9 j& s2 x5 S% ~然后编译执行就可以了(*^_^*)+ _6 S- y6 R7 D- v8 x0 ^
其它:TC++上一定编译错误,不保证在VC6上也能通过编译' ?0 j  p# p2 f& g. }
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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