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

|
一个计算四则表达式的模板
在9月8日那天我特意编写的,给大家分享的,. S, b6 j. q6 f
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
7 y+ U2 T1 ^$ L只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)/ B7 F% X, z0 d/ N! N; ` W
参数解释:
' U& B! j# C" e* P. U% Y! u' ^istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流* J( ~% N8 I! Y
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
+ E; D' u2 N% n' w% ?- Y# j# [% ?7 d返回值:
; ^6 Y1 m! S4 z返回非0表示计算成功,0表示计算失败有错误
! U$ j ?7 U# h% F8 [( Y: Q* @- I0 b- F S, p
, c1 X$ }1 f: p' e" @) j7 S
& E3 C. `) F9 C8 L& I- l
程序代码: $ a$ o) b0 a4 q, ?0 u' v) l+ U4 I
; E7 I9 {; L! C; S/ Y, J4 v! K# c+ u
namespace fy_Exp{
& i7 N' _! ]+ l- S b; a8 ]9 inamespace {template <class _T>
4 d p p9 G# i# G5 y3 w2 Ginline _T GetExpValue(_T t[], char& csym){ ~$ I! y+ m }7 d; ^
char c=csym; csym=0;
1 F2 ^9 j# g) g$ z switch(c){2 b$ i# y8 `, _) W( t
case '+':return t[0] += t[1];: M4 l6 b6 I4 @
case '-':return t[0] -= t[1];6 x! a z/ W ~- W( G8 N0 {
case '*':return t[0] *= t[1];
7 w0 z* W+ ], V default: return t[0] /= t[1];//case '/':# } k) e4 N8 G& R4 l
}
& R% f" q0 g" M. i5 f V}}
2 Y- @5 e( n; c; L3 B \template <class _T, class _Tstream>
2 A ~* f* {; d+ ~2 n+ e3 w/* _Tstream: inputstream, _T: get return value
: l7 v' f% S) Q$ O. X/ p* Return nonzero if get value successfully */
4 P$ Y9 t3 E7 Aint GetExpValue(_Tstream& istrin, _T& nReturn){
- ?% b% F4 a8 G' z& r$ }% x8 h0 |- e _T t[3] = {0}; //雨中飞燕之作3 D/ N2 j, Z/ G2 z
char csym[3] = "++";# [) S2 A/ k$ t! m
int nLevel = 1, nERR = 0;
5 _% e; ?7 Z& R) M1 D if(!(istrin>>t[1]))istrin.clear(); W7 Y5 j8 p1 |; n
for(;;){
* d! ~' B! h) {) O, q if(istrin>>csym[2]){
, U$ g4 h( o0 p, c* G2 D' [7 b% A switch(csym[2]){
: Q1 J% F9 Q7 }0 [ case '(':+ D8 A/ ?3 Z! w) K+ K4 t/ O
if(!csym[1]){nLevel=0x100; nERR=1;}else
& w8 |& E6 M# }; C) t- A( h: g0 V& ~ if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
; l* W, l" E# j. R7 w else{nLevel=0x100; nERR=1;}7 U: K& G# L" ` s
break;6 G6 E9 y0 b9 J; J
case ')':
/ d6 M; \- ]! S" C/ ^7 B V+ S7 Z9 v {nLevel = 0x100;}break;3 D ?" q. y; u3 m H
case '+':case '-':case '*':case '/':) w+ H+ _5 J) ~* I2 t6 ^; m
{csym[nLevel++] = csym[2];}break;
" c. _9 J& Y! ?6 R# V7 } case ' ':case '\r':case '\n':case '\t':continue;
+ k, s9 f! f$ G# u3 r* K1 b default:
) J0 n! r1 l" g2 a {nLevel=0x100; nERR=1;}
$ G) H+ w4 c4 a) m3 A T- @5 z }; {' N7 x1 K2 \8 n- s
if(nLevel==0x100)break;1 I. b" h; m8 H/ B; A+ ~" e3 x
if(nLevel&0x10 || istrin>>t[2]){
: z, e) X6 t7 S5 c0 Y3 m" k nLevel &= 0xF;/ F3 ^# D1 j: G; F
if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}) m# q: T& t2 I) I% V
if(csym[1]=='*'||csym[1]=='/'){
- ~% \) y5 j+ s2 E6 S/ v GetExpValue(t+1, csym[1]); s: R! [& G2 S5 s* K; x
}( c* J1 G. C4 i) W1 K% ]% D
else{3 Q0 r/ n z; T" G
GetExpValue(t, csym[0]);
) s) K, U, Y1 c: M$ E4 |( d4 ^ t[1]=t[2];csym[0]=csym[1];csym[1]=0;6 f, E2 P- n8 E
}
. a/ P$ a& R4 ]; e) m nLevel = 1;, l. X$ o' w6 n; d7 v, g
}* O7 B s V. O6 I3 D
else istrin.clear();
( |3 f0 b. m- U q0 } }
# |" f* _3 K5 |3 y' P( ] else{nERR = -1; break;} s/ d o0 t( N& L% K6 W
}; L* _- l; k: @+ [" s8 D
if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);1 P$ N1 |6 ]% v
else nReturn=GetExpValue(t, csym[0]);
6 c4 O- t; k! l& g, N* V# @8 V return nERR==-1?1:0;" I% f9 P4 x3 y6 n( H
}}+ U r( U/ ` W) ?9 P' p
; d' X7 ?0 n! a* W
- o4 _) H+ T! _) Z0 h6 b7 K
2 X, [8 p0 A) u函数模板使用示例:
9 z9 K% ]! P) I% B在以上那段代码的后面加上以下代码:
& q: s: x D E" V! o, E; R( |5 A' @) R& K( w
+ D6 y8 C, N9 D$ B1 |6 F2 P! t2 h" G7 _6 j+ C9 J
程序代码: , W/ C; r6 W* P: x, r" M
1 @4 ] k5 t$ ]# n* j5 d#include<strstream>
7 q/ B4 G4 Q* O* N; I! f* r#include<iostream>, q4 L5 B, d( n8 b6 f2 g
#include<string>
( j+ @/ G4 r6 W" N: v+ }: X1 Dusing namespace std;
9 x% r6 K$ c) @8 Tint main(void)0 ]9 {/ s- O }+ v' n) q u
{
8 l; q/ t( n+ y1 a9 r2 ] string s1;3 p& D/ b4 O6 X7 {, t. u, B
while(cin>>s1)/ x2 @: q4 H! R: |8 S9 X. s* A$ {
{
5 [6 a( F/ X& O6 c istrstream isin(s1.data());
7 v* i" l L' C0 d8 ] double d;( B% o. k* [+ V/ U& S" F8 g- @: y
if(fy_Exp::GetExpValue(isin, d))
. u: n) Z( H' I# b- w {7 c$ i. k* r5 `: I! S: r
cout<<d<<endl;1 v# M2 O* Z& f: ^
}
% ]# z1 G6 e; M6 i# ?, p" M else3 f# ^, w- ^" j
{
. \" p: J6 G: b; C) @ cout<<"ERROR"<<endl;
2 u$ X2 u) |$ v9 b }! d, E2 u% \) v; Z' M" H# `* T
}; z5 P; ^5 M+ G& L, D* c0 @! @
return 0;% a! q* ^% r; f& \
}$ `& \6 V9 H4 |# ]
9 G; z/ x3 }+ A6 {& J+ R
: C# g2 @/ r5 a2 X9 v' y
然后编译执行就可以了(*^_^*)
) y; q5 _$ v3 R X. F4 ]其它:TC++上一定编译错误,不保证在VC6上也能通过编译
1 P6 H0 A9 Z2 b 建议使用VC7或VC更高版本,或者使用GNU C++编译 |
|