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

|
一个计算四则表达式的模板
在9月8日那天我特意编写的,给大家分享的,& G, e, {4 J9 O5 ` Y, x' O
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
. S1 z* o4 O _6 Z. T只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)3 \: M8 G* w1 K) u
参数解释:* d+ k- Z& e) w+ r. {% ~0 r; M
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流- X/ O' T5 u: ~+ H: A H3 W
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
( ]5 s+ Y0 r8 B( I返回值:
% o/ r t% n$ |. [9 o) `返回非0表示计算成功,0表示计算失败有错误% v1 R4 V7 ~; s" b$ \" U
: a0 F! Y2 p$ h- ~- `2 Y # m( |7 f! j& X* z3 q4 m
0 j7 H/ v |0 l1 D+ [- z1 h4 ^程序代码:
. x! W3 t; M. D0 Q' V6 R$ O7 Z: A! F/ v7 H4 m
namespace fy_Exp{4 ? b5 D6 L! P) R1 k" B$ h: t
namespace {template <class _T>
% T1 O3 W1 A$ g$ i" {inline _T GetExpValue(_T t[], char& csym){6 ]7 Y- i: _/ b& B0 ~1 m: P
char c=csym; csym=0;
9 `' H* j* D! W# M4 @ switch(c){
0 P- }* y( C) ^* d3 S9 s case '+':return t[0] += t[1];2 G; A& U4 R8 j' g: i( N
case '-':return t[0] -= t[1];
% w0 |/ I" m# C: ^$ e" y* B+ ? case '*':return t[0] *= t[1];
+ c2 R j% c1 U5 P default: return t[0] /= t[1];//case '/':
& I6 b6 d* F( r0 ^& O }- y3 Y' M" _; t7 g. s" A7 x' ]
}}
7 ^% b% v5 }- E5 r/ D" A8 P, ttemplate <class _T, class _Tstream>
, n- V; R3 I/ }' _' j/* _Tstream: inputstream, _T: get return value& s2 \. ~/ h* D! P8 Q% k# d9 S! c$ M
* Return nonzero if get value successfully */
1 @& \, N" Z4 yint GetExpValue(_Tstream& istrin, _T& nReturn){8 U3 y7 K4 R# k
_T t[3] = {0}; //雨中飞燕之作3 q$ T8 j3 y1 f6 Z1 b
char csym[3] = "++";
) J) ^9 v* x% K int nLevel = 1, nERR = 0;: L- ]' Q' y% w \0 b
if(!(istrin>>t[1]))istrin.clear();
9 z" }# z, l2 Q `7 K: S for(;;){
: X( t' _2 U9 I% t if(istrin>>csym[2]){( Y* y p, m3 N
switch(csym[2]){: ?0 w+ W/ D$ v
case '(':6 P* ~8 ^) b% V" n: _" [
if(!csym[1]){nLevel=0x100; nERR=1;}else
! N% y" x( m: ]3 G" k if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
8 [4 d4 O" b5 e" z# T else{nLevel=0x100; nERR=1;}
) ]/ {+ T0 f0 V4 ]2 J2 F break;
, C$ S& Y# u7 d% \# J6 _" {" |4 ~ case ')':
" K! C! M) f* u3 ` {nLevel = 0x100;}break;% o ^( k$ }2 N2 [
case '+':case '-':case '*':case '/':1 q$ p* Z: v T8 ]& @
{csym[nLevel++] = csym[2];}break;* f9 v( T: d5 _8 a
case ' ':case '\r':case '\n':case '\t':continue;* B1 R1 r) t6 \) T( O
default:' s2 B" e' c( ]
{nLevel=0x100; nERR=1;}1 G' P/ h! r: e
}
0 N' Y; r7 P. E3 q& w if(nLevel==0x100)break;
5 [1 V$ A9 f/ ~1 W if(nLevel&0x10 || istrin>>t[2]){
7 C* m# f8 Q$ S% }: V: O u0 Y nLevel &= 0xF;' l0 ^. d6 B7 t) o
if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}; J3 D. S% w, W$ E9 b- F
if(csym[1]=='*'||csym[1]=='/'){
3 I: I) H; D/ Z3 c! ^ GetExpValue(t+1, csym[1]);
1 S' Z- u# ?; U9 k2 | }+ p" o4 b: z% z! T' J9 {" _
else{/ Y! Q7 A& B- P) z
GetExpValue(t, csym[0]);
# b1 j/ P0 A, ?: n/ D t[1]=t[2];csym[0]=csym[1];csym[1]=0;# B% Q. n9 M2 H8 X
}
) U: \- f7 F" f( u nLevel = 1;
6 E$ z# W. u; T! P! ?. {+ ]4 Y }( W' t; A: T: c: g3 U% M |6 h
else istrin.clear();
! H" a, t& C. w% v5 z" | }( ~! I- y/ q( [" m. w7 k
else{nERR = -1; break;}3 n1 m6 u! s2 `/ T
}! U- e5 v6 ^) O1 V
if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]); L2 O! [0 Y* ~5 m {/ P, W
else nReturn=GetExpValue(t, csym[0]);; j4 f3 F* f; q; |
return nERR==-1?1:0;
8 f: u& h. K3 L5 s% |- A}}2 s$ d: v' q% Z. q- I7 {
% K8 J. {0 ^8 p' P
& ?. B: h( r8 l S0 q9 y
1 d$ ~2 `* r+ a2 N函数模板使用示例:' J' M& ?5 ^) S9 {
在以上那段代码的后面加上以下代码:
( i) p3 O) K! Y
1 P5 [2 F5 G4 z
0 F4 V! e |, ]- C" z3 L. q0 r8 @9 n s# R: d
程序代码: ( |; }, O- n; ~# A
& C2 ]3 S4 H, ]$ Z- e1 G
#include<strstream>9 B. Z, [1 ^$ @, D) o7 {
#include<iostream>" j5 P, f: c& }( x# h. |- d/ L
#include<string>( \/ b7 M, J% J9 H- r" z3 h
using namespace std;
; W ]2 F- E0 u6 O+ p: u5 fint main(void) A. H5 l# A/ e [# C
{. N+ }6 c9 g- `/ D/ R( W
string s1;, l- b6 B1 c/ d0 J; q3 w
while(cin>>s1). W" f' p0 D# c# {
{: y; H4 P6 O& V6 u% V$ X8 a
istrstream isin(s1.data());
: d0 T# r; N8 s2 @* Z: {- ~ double d;
, m: g) ?9 C* J2 n$ z* \ if(fy_Exp::GetExpValue(isin, d))
! d" K" D* S+ _* P+ b {: j! e2 W# Z) j3 ?4 o8 A0 b$ |
cout<<d<<endl;
: l$ _) _( \% Q) | }
1 v& d4 R5 ~3 c u1 N else
8 G+ A! |# r0 c- P' D! p {# j6 r4 o) @( l/ S) l8 N
cout<<"ERROR"<<endl;: d% v. T( g' u; n6 K# d6 h$ N
}
) t1 ^! i# h" z3 t5 y }
& j% h/ v7 j/ T! g return 0;
" `$ M/ ]$ G5 m9 y. B) d0 i4 j l ?}$ {- A7 T) ~8 `
9 w: P: `: O( ]
9 B' g0 d+ K/ R5 _/ v然后编译执行就可以了(*^_^*)
& [ n; h8 N9 f. E( w其它:TC++上一定编译错误,不保证在VC6上也能通过编译
( b/ N8 r# R3 t1 y 建议使用VC7或VC更高版本,或者使用GNU C++编译 |
|