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

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

在9月8日那天我特意编写的,给大家分享的,0 T- Z  M6 F9 t& S( |; X5 q- ^0 \/ L5 y
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式5 G9 d# i$ E) `( a0 o1 _8 ]) r
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
( ?8 |4 `7 w  Z% j# E7 h3 M- C参数解释:* L9 b9 \% a$ j- _6 k
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流: n* u5 K! p& s) e. e" A& t
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
( M" W6 Q9 p4 t! s; w% h* N# R* Q7 ]返回值:7 Y& t: |8 e  D
返回非0表示计算成功,0表示计算失败有错误
8 r1 n$ e7 l: C2 B. ?: W) Z
' R$ C! j$ p! I+ K  N" o
! g% e2 E9 u2 j" [( h9 I
3 ^& ~( r% r% l# c0 _程序代码:
1 h" [4 v4 J8 [5 y% t6 Z0 [0 K
2 X! p+ G% Z, B8 N3 i# E& s" Inamespace fy_Exp{
  U5 o7 V: g+ K( H  Y" snamespace {template <class _T>
, ^" f! D: K; ]( V% c/ g8 N2 Cinline _T GetExpValue(_T t[], char& csym){, A) W/ d/ B6 D
    char c=csym; csym=0;
. G/ ~5 ~) s4 X  f  l% H# {9 F    switch(c){
* h# i1 R7 d& k" \; y$ c4 p% e    case '+':return t[0] += t[1];# M; ^/ D* U+ j6 w, H' D: |
    case '-':return t[0] -= t[1];' I  C: S6 _! ]% z, A
    case '*':return t[0] *= t[1];
8 [& l  L  {* I: N8 l# u    default: return t[0] /= t[1];//case '/':; j$ R) H6 r& o% n0 C5 q5 a4 `
    }9 d. i8 T9 n1 v8 [. k& s
}}
) L1 b+ A8 r+ l% _template <class _T, class _Tstream>0 _+ o" J( q3 i6 L
/* _Tstream: inputstream, _T: get return value. Z2 i% g8 E6 [* O/ n
* Return nonzero if get value successfully */5 F  |* Z$ A3 r4 J' k' Q* ]% o
int GetExpValue(_Tstream& istrin, _T& nReturn){
8 }) }4 ^$ U, y! p; ~$ Q9 e( \    _T t[3] = {0}; //雨中飞燕之作; u' |5 V/ Y$ R0 W0 }
    char csym[3] = "++";
5 }: \# C: R6 g8 a6 x    int nLevel = 1, nERR = 0;
% z3 }+ A) ~; U1 m3 j6 B( `    if(!(istrin>>t[1]))istrin.clear();; t2 Q8 R7 T% n
    for(;;){
7 ^6 x& Y2 a. m) f        if(istrin>>csym[2]){$ Z) G/ m; C; M' L! v- `  }4 U
            switch(csym[2]){( U8 j; Q5 O1 A2 S5 ~- V
            case '(':
; z" y2 }  r+ n" H5 V                if(!csym[1]){nLevel=0x100; nERR=1;}else
5 a# U* d; a8 C! z1 F( T! s                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;% C9 M) H1 H. Q- r/ K! i+ y
                else{nLevel=0x100; nERR=1;}$ g" j+ w* G7 v* @
                break;5 T& V* y; g! Y8 o2 J! f  l& t
            case ')':3 ^& S  e* `0 I+ f  l- L/ C
                {nLevel = 0x100;}break;; _7 j/ U$ S. E4 W" Z
            case '+':case '-':case '*':case '/':2 n" S* e% H' t8 h7 C
                {csym[nLevel++] = csym[2];}break;' N3 S" M$ ^7 n( l5 j
            case ' ':case '\r':case '\n':case '\t':continue;, W, D- I" O# ^4 C% S9 {$ Y
            default:$ @5 }3 q0 ~0 w4 t
                {nLevel=0x100; nERR=1;}
+ i/ ^! ]& _" k$ k" `            }
; d  p2 q9 b& w% x& m            if(nLevel==0x100)break;4 w/ c6 b8 q# \# Q) o( `
            if(nLevel&0x10 || istrin>>t[2]){# _2 e+ v- r" I( ~
                nLevel &= 0xF;& n4 W" G4 |. v! U. X
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
  L- k. z! r: e                if(csym[1]=='*'||csym[1]=='/'){
8 K- K6 ]" l) L' ^                    GetExpValue(t+1, csym[1]);9 G# {0 h/ R! n. {0 o
                }7 u; o9 P; R; c$ H1 b* l
                else{
3 P9 Y. G, \0 _4 j, k. x                    GetExpValue(t, csym[0]);2 X0 x  w' O8 u9 n+ C$ w
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
7 ?. m! z# n- Q/ o" \                }
0 O# |6 I: S% h) ^                nLevel = 1;8 G  e5 O$ F# m
            }5 q; o- F/ q7 K5 O9 p& h) z- C3 J1 A
            else istrin.clear();. |! }4 U- D. R; a8 H, X
        }. p; J; c, W4 H6 ]$ N
        else{nERR = -1; break;}# O4 h/ }  [7 h( ~6 l
    }1 y+ y  l( p4 X6 M9 U
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);& d) r9 `7 D. H, s0 x
    else nReturn=GetExpValue(t, csym[0]);
  K  m2 r% j: Q6 b9 f. }6 l2 f    return nERR==-1?1:0;
7 S* r7 w2 P% Y7 I, {7 I, Q) u}}
% V& s1 e+ O7 b7 |; B) u+ u
( y1 U0 Q4 {$ _0 O2 y) s2 P: S8 \4 {

0 k* @2 v% N5 o. E; s5 Y+ ]6 w2 ~函数模板使用示例:
! u' k4 ~7 ?; a6 d在以上那段代码的后面加上以下代码:. |" O, w4 ]# R  j
' @! L  y1 q0 h$ y$ f" a4 r, w8 p( y

+ o' `  U. q5 n! e5 x5 I& F: ?  m. c: A, ^+ K* P% J: U
程序代码: , Z* o4 X' Y+ p9 K% Z5 G

5 U, I$ j  I" F5 v* G. @#include<strstream>
; I% q5 |, \# T! ~4 b3 ?$ D#include<iostream>
9 J# ~' H/ i) _7 M! P$ f#include<string>
5 t0 m* B  u% u% w" ?. ~9 C6 V( ~using namespace std;
! ^2 w' Y+ h- X+ sint main(void), z; I$ o3 X8 Q% D
{! A: M' n0 I6 [4 v6 f6 V7 E! ]
    string s1;2 d4 v% T+ T% X% _" ]& _
    while(cin>>s1)8 p7 r$ e  h8 b  X+ c4 @) Q
    {
  y% _8 G  z! f6 V2 k8 S3 z1 r        istrstream isin(s1.data());
9 y1 X5 m4 R9 d. O  s/ _        double d;. E! q; W3 [7 n0 L
        if(fy_Exp::GetExpValue(isin, d))- |6 p/ g! F# a+ C- |* s, Y" q, s
        {6 }7 W4 i" e3 S$ S( t' _; X6 K
            cout<<d<<endl;
, s" N1 w6 f; u# U' o; _4 r        }
, e. c5 f$ e- i# k/ S        else! p3 b) m. [$ M7 R" `) L
        {
+ b4 B  m( s& ], b            cout<<"ERROR"<<endl;% A2 d/ }) l& G9 N% M, C
        }! J5 b" i7 h& k9 K: D
    }
( S# _! w/ b# T7 X    return 0;" Q  [( S9 O" {
}  v; ?9 M# A9 S' X# R7 A8 \0 j
" q  x2 Y. J, C  Y$ D

3 r! \8 l6 |0 e. _# S9 W然后编译执行就可以了(*^_^*)0 `3 g( u& D8 d
其它:TC++上一定编译错误,不保证在VC6上也能通过编译- h( Z+ Y- T  j
      建议使用VC7或VC更高版本,或者使用GNU C++编译

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