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

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

在9月8日那天我特意编写的,给大家分享的,
. v* e; S/ l" u) ]) W一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
+ H# a5 R* o& x# ]只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
' r4 B1 o; {1 ?! j6 ^参数解释:- E% H) x% v8 J# S  r" `! H
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
6 v$ W# h) S5 anReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定7 b0 Q) I& Z. y( h0 b1 t
返回值:
' ]$ J. Q0 w; y返回非0表示计算成功,0表示计算失败有错误
3 {6 M6 C' V4 h8 V/ t. Z
  \+ S* s5 G9 X( ~' o/ W5 [, z' w 3 t$ F! Q& ~5 g4 Z7 I5 R

* D# R% l% _6 P/ |. G程序代码:
6 h2 G2 e7 P0 h. V# H- _$ ^) X& m4 J: l: g
namespace fy_Exp{
, p  X% T9 r  h$ a% D. |! s( `namespace {template <class _T>
* U; d& _& t( _  P# a* F1 Xinline _T GetExpValue(_T t[], char& csym){/ o2 V3 x  a8 g# q+ k2 _
    char c=csym; csym=0;) z0 l9 v" j! M9 e' a( Z: o$ ~
    switch(c){
! `6 y. o' u0 J1 [, s$ h    case '+':return t[0] += t[1];4 z; j! b6 u/ T  o- j
    case '-':return t[0] -= t[1];
; F7 ~& w' M, ?+ c' i    case '*':return t[0] *= t[1];, m% B! U0 x. I; y! p6 S4 ?2 i
    default: return t[0] /= t[1];//case '/':
% o; w. ^' N. ^* E    }
  s* i) V, U8 A$ L! @}}' p! N' O7 l4 U! n! P' {/ ]
template <class _T, class _Tstream>% _; m8 E. m' P" }, T
/* _Tstream: inputstream, _T: get return value
4 i% R# s5 X4 B! ]) A* Return nonzero if get value successfully */
* ]6 y; Q9 v3 a/ Q/ J+ xint GetExpValue(_Tstream& istrin, _T& nReturn){
. ~! J$ s; x- x+ S. \6 ]    _T t[3] = {0}; //雨中飞燕之作# @9 y: o- v3 ]1 K& H* B3 y
    char csym[3] = "++";+ X+ g/ b8 _# K( T. A1 u' t
    int nLevel = 1, nERR = 0;1 t' D3 X  i7 P1 c/ X
    if(!(istrin>>t[1]))istrin.clear();$ W" D, X" T! k& \1 r
    for(;;){2 E( w& ?7 d$ ?, `4 K+ Q" K
        if(istrin>>csym[2]){
. [- ^6 G; t( e0 @* k1 U% i            switch(csym[2]){* l3 x) |4 m+ d- F; f9 c2 E# E+ l
            case '(':
" G0 P: E# T# a6 d                if(!csym[1]){nLevel=0x100; nERR=1;}else
+ B4 X9 o+ v# }+ P) F# ?7 ]                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
% `9 k# E7 }2 C0 d. T2 S5 J0 O: F                else{nLevel=0x100; nERR=1;}8 S$ i: T' E/ K. Y, \' _4 i
                break;8 O) m. G" k0 N: v8 j* |: v
            case ')':3 h& D2 O8 [- K/ J+ ]
                {nLevel = 0x100;}break;
& o- c+ S5 P2 M+ v+ o  a2 z5 R            case '+':case '-':case '*':case '/':
: y* ]6 Q& V6 \# v7 j/ x0 N4 x4 S* [                {csym[nLevel++] = csym[2];}break;
! _( r# d' u4 K# }* Z6 C            case ' ':case '\r':case '\n':case '\t':continue;2 r" S5 x& J$ @& l, h% [
            default:) h) e+ D7 h. @! X4 a
                {nLevel=0x100; nERR=1;}+ D" M7 S3 b& l1 Y$ y9 T( l
            }
! E$ D' X7 _: {3 j5 ?            if(nLevel==0x100)break;8 O! W" H, A/ o! }* M
            if(nLevel&0x10 || istrin>>t[2]){5 V. c! f% X6 Q/ v: e5 C; v
                nLevel &= 0xF;8 H" f  x/ K- u6 s) D% T  |' D
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}* P( }0 E' x: K2 F
                if(csym[1]=='*'||csym[1]=='/'){$ j4 q) ]7 \% x; N/ {# ]7 p
                    GetExpValue(t+1, csym[1]);
2 X& _4 q- I4 @, l- M) g$ I                }7 b$ N: `* Q- e
                else{
( v/ |. L, S4 n                    GetExpValue(t, csym[0]);
$ Q4 ?8 S/ e7 ]4 w8 k                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
7 R6 U: u4 Q# e2 l- k6 D4 G                }
8 I, V- y0 a0 W" `% M) p$ C, q                nLevel = 1;
* e5 j& f4 m/ L0 z* J+ C            }
$ i; s. S* n% R            else istrin.clear();
# V9 I, i0 b3 Z2 d5 i# I: ]        }! j2 f8 }3 `* o9 {# K0 |$ x" C
        else{nERR = -1; break;}
! c6 P& b$ H( `5 f; _    }
- r7 s+ `1 w5 @8 l+ L    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);3 l# N6 Z2 n: q* Z# A. i4 {
    else nReturn=GetExpValue(t, csym[0]);9 X$ |* O, b9 G! y; P8 O, x
    return nERR==-1?1:0;
+ D/ c0 v* t& Z1 h}}5 g! o& L: r1 U7 t' a

- {5 ]. k6 E! V5 H( I% e- f: U' ?+ E* t/ c4 X; |4 N) |

' A( \* D; N- ~7 I3 P, E- J函数模板使用示例:7 C5 `6 D" ~5 h# n
在以上那段代码的后面加上以下代码:3 S5 g. U3 n9 R* l( B9 l0 a
2 [( ]+ g: X3 U) \4 P

5 e. A' _+ A0 l" U  l6 W5 ^
$ p" J* b7 a& W% t程序代码:
8 y7 m- L2 U; H+ H% U
- o' N* l4 _! N7 e# `6 K# H6 i#include<strstream>
$ S- l" G: v' z3 A& t) v#include<iostream>
$ o3 y, _+ {% b7 K#include<string>! Y  E/ m, W/ A0 v0 d1 }. _
using namespace std;
% M. a$ q, P8 ]0 B) a' N) Aint main(void)7 l# ?5 B9 i6 P1 K& x$ _! [" r
{  [- L( f- M3 r0 {) ?
    string s1;
% w# N3 Z% |7 S" P* a- M. H    while(cin>>s1)
9 ]4 R$ h, B! ?, M    {) b7 ]! i- Y; p
        istrstream isin(s1.data());
1 f# t! t- j5 [  s) s- R        double d;
0 K' k: ^" S! C  V        if(fy_Exp::GetExpValue(isin, d))2 |3 f2 v, R9 v1 L( @
        {' ]: v' \$ w7 o3 A/ B
            cout<<d<<endl;
. ]# L$ `+ I6 h& _+ L        }
# E* U) P/ |* Z7 H+ Z- S  E- S        else
) K% X0 E; g; g+ }7 l9 R        {
) ~- v# C  P( V8 t$ P# g0 g            cout<<"ERROR"<<endl;
# h& P& k. T) o0 b! n; q3 l  P# g        }2 h( E7 b) h$ m" Q3 Z" E
    }& q1 H' J4 x7 {7 m0 K; A- v
    return 0;- @* o3 r7 l% g$ h- K' Z
}
5 [! H" c% G  \! C* p  [% x, \6 B
( o6 ]. d4 }+ q' P, c  ~' _/ W2 Q' n1 W/ @# n+ I
然后编译执行就可以了(*^_^*)
6 m4 q! s  \, I3 M5 E% x$ e其它:TC++上一定编译错误,不保证在VC6上也能通过编译
# u% C: Y! M0 q3 [  P* ?      建议使用VC7或VC更高版本,或者使用GNU C++编译

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