  
- UID
- 133
- 帖子
- 51
- 精华
- 1
- 积分
- 186
- 金币
- 55
- 威望
- 2
- 贡献
- 0

|
一个计算四则表达式的模板
在9月8日那天我特意编写的,给大家分享的,4 A4 R6 E- X/ x
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式+ m; a. v" p) H7 ]' \
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)0 y' M" e8 P! w, D$ j
参数解释:# M/ s) a) d3 s Q( P3 w) i
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
+ G- \# I: s7 a8 R/ KnReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定2 }# P! N7 Q% ^" s3 s
返回值:& C( t2 E' w5 h; I4 V
返回非0表示计算成功,0表示计算失败有错误
: D1 z( b7 n% c; r2 y
) j/ G* U' P3 s3 L
! J: @! v5 b7 H" I$ E, M
% P' y$ j/ y4 y, M& P6 t: u9 V) L程序代码: ! A" o+ }, t& E1 y6 |
0 O3 F# s5 l- N
namespace fy_Exp{
0 p, V% T- z1 Lnamespace {template <class _T>
3 k5 s5 h. _( X: c$ u$ k- |- L7 winline _T GetExpValue(_T t[], char& csym){8 w1 M5 u. T8 `8 g8 ?! a# C" Z
char c=csym; csym=0;
# }- M* l. Z: i X, ~# D$ O switch(c){5 g1 K s- i5 k" x
case '+':return t[0] += t[1];
. u' ~$ b6 P' s. T. j( v case '-':return t[0] -= t[1]; v- P! O2 X8 D+ F- j
case '*':return t[0] *= t[1];
( G3 n8 [, j0 {$ P1 c% M& j' s$ q default: return t[0] /= t[1];//case '/':: C7 W x" y$ e4 {8 l+ n# s3 u
}8 n. w( U' R. u6 R/ r/ ~: R6 `
}}$ ^! T" ? F7 |8 m
template <class _T, class _Tstream>
+ ]6 S' b& ]- d* n ?3 B" Z( p" O! S/* _Tstream: inputstream, _T: get return value
4 W) N7 @+ c+ C& ~" \7 [* Return nonzero if get value successfully */, c, A/ @% S7 s- Y( `
int GetExpValue(_Tstream& istrin, _T& nReturn){
# K* \8 x4 Z; w a* e _T t[3] = {0}; //雨中飞燕之作: g/ I) y& s! b
char csym[3] = "++";- ?# c% Q) q8 l
int nLevel = 1, nERR = 0;
4 z) K) H# s4 P& c; ?/ C' c8 Q if(!(istrin>>t[1]))istrin.clear();6 o7 o* |/ m8 j7 f
for(;;){
* l- l1 Q [1 d) c if(istrin>>csym[2]){
8 P( h2 ~. m9 T! w, ~4 g$ G9 M switch(csym[2]){2 z; T' k t x' O7 Z( |' _0 x
case '(':( q# g0 T6 \4 `2 b) V$ N
if(!csym[1]){nLevel=0x100; nERR=1;}else
4 r9 i7 |6 s) V1 ~9 [$ |# c if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
, R6 z# U& h- h* C2 W else{nLevel=0x100; nERR=1;}
. K9 d* @9 g5 X! e% n) k2 P break;! T- I/ Y2 \2 i- X$ R2 L7 U
case ')':. h d9 \7 w0 D7 r t& }6 i& y3 b
{nLevel = 0x100;}break;
- k5 i7 ?& n, D4 v) p6 X( Y( O case '+':case '-':case '*':case '/':
/ q; S/ [* @' W6 s1 A* o {csym[nLevel++] = csym[2];}break;
5 Q% w4 d5 L9 r9 |) w$ z case ' ':case '\r':case '\n':case '\t':continue;7 M" s+ G: n- L& x( t
default:
1 j# u' `$ F, ` {nLevel=0x100; nERR=1;}7 Y+ H/ \9 P, j4 p. M
}
2 ]4 d C4 E/ b: D+ i" j if(nLevel==0x100)break;
/ @6 S" \7 S5 c& p8 U if(nLevel&0x10 || istrin>>t[2]){
3 V1 t& D( e! f* H" Q8 \7 ~ nLevel &= 0xF;; O0 A" l8 \% A2 s' ~1 E5 @' t8 V
if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
$ _9 o& s! S& z+ H8 y if(csym[1]=='*'||csym[1]=='/'){
: D/ k1 {' A# I8 @; U GetExpValue(t+1, csym[1]);; M: L! |$ G0 o6 W4 @) O$ c
}
; S& F* Q1 H, A: E. {* N( ]& U: p5 t else{
" J1 v; |2 Q c o# s' o0 t$ D9 s GetExpValue(t, csym[0]);
$ k: E# t. e, a" _ t[1]=t[2];csym[0]=csym[1];csym[1]=0;/ a) z0 s0 d# ^" h5 {
}; g4 W; z3 y, `: O- Z
nLevel = 1;
, G/ w# T. u$ Y }/ @! Z9 Q( N) D5 U
else istrin.clear();
" M& y2 q6 ^2 q2 n, S& Q+ t4 l4 X }! q* g7 M' G; w7 N i; h; R- m: L
else{nERR = -1; break;}
% s% T+ l% _/ k. F5 M- @ }9 X7 Y t* {. Q ~( |- n0 v4 i
if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);% @& b$ I: I' L2 S
else nReturn=GetExpValue(t, csym[0]);" D5 V9 L4 k5 \2 n! h$ x
return nERR==-1?1:0;; g- a! z: R5 q8 ]0 d7 Y) J$ o$ b
}}1 ~* o0 l0 f& i
7 w, d P+ a: Q: X8 e' V5 B E8 l3 s& Q
1 m5 e8 P& m' `2 _" _ q" |
函数模板使用示例:
0 p2 O: @# R0 ]% N i/ ~在以上那段代码的后面加上以下代码:
; E, Y B/ H; L3 ?! G! K2 T% k. K4 p" J3 O# a; R% h4 }/ \/ h# T
: F$ D8 o4 ^ }
6 @1 g; `' ]. b ?0 `
程序代码:
( v% l' ^$ h& g6 F' _
6 }2 X: V/ @, u% N, u$ M+ M I9 D#include<strstream>* i0 h, y0 g. E% m
#include<iostream>; |5 x2 f% B6 V$ n5 V
#include<string>
- t( F5 E$ h% J' q& ~, l- l3 Yusing namespace std;2 v& r A* M7 A
int main(void)9 R1 m: w/ L4 n3 ?! `& s
{
4 e( u. I0 z! i9 t string s1;
j4 p9 s6 k3 p( J while(cin>>s1). p, [: A( s( H. O0 |/ f
{
4 D3 c8 T( O$ ~( }# R) f istrstream isin(s1.data());9 Y6 I5 O- |3 l# f0 _+ A
double d;. A/ v5 H1 ]9 L2 U7 `
if(fy_Exp::GetExpValue(isin, d))5 Y; I D* c% Q5 `* a' u
{
8 x0 L$ @6 b; G cout<<d<<endl;6 b/ ?% x6 c. c0 r$ N# }/ |/ x9 c4 i
}7 { z+ j9 O3 ?& T) `/ M
else
$ O' V4 F. R! h7 G% ^$ @& G! W {
2 g5 e- y- w, Z0 J8 I* ? cout<<"ERROR"<<endl;
- a' z+ s, s5 ^; o7 r }. }* u! k6 c0 q1 Z5 u
}4 v8 L D8 F5 q( S# `
return 0;
/ o* z( }1 H" V0 J h}1 C" s7 u9 E- C# G: i1 w, p" C9 P
' X4 W- R( f6 X& Z! t8 x4 T% t6 |+ K- j; v* x
然后编译执行就可以了(*^_^*)( U/ X; g5 h4 ?+ c4 ~8 ]( r `
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
- s) z5 i8 E5 f; d6 w/ @ 建议使用VC7或VC更高版本,或者使用GNU C++编译 |
|