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

|
一个计算四则表达式的模板
在9月8日那天我特意编写的,给大家分享的," i6 `3 B9 {& M; Q) e8 E9 a
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式 w: g3 p: Q- k8 g0 w1 d% q3 y2 w9 ?# t
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
. F$ x' R) s$ r; K* y, W5 s3 u参数解释:
# @1 [" h. `, Q# listrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流 l+ r; X# l4 k. K3 \
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
: |, | m q: n4 h3 b; }6 v3 j返回值:
* |8 C! a; f6 d0 p! X返回非0表示计算成功,0表示计算失败有错误6 y, ?% [: R1 y4 r8 P+ @$ Z
# E& x' b& ~! y; Q, O
* b) `" p h- i; H Q* _1 k; G" L& V9 F4 K) ]+ S
程序代码: ; Q0 A8 ]3 n& |& H& I' h% ~' S6 g
4 W% L" [0 A! ~! inamespace fy_Exp{2 f9 s0 M% j" G5 [, D
namespace {template <class _T>$ u0 ]! w' X, x4 I
inline _T GetExpValue(_T t[], char& csym){" o6 c7 x) @$ A
char c=csym; csym=0;
% k P& i! J7 ^8 H$ A' h switch(c){
5 `2 V8 Y, ` ]! n case '+':return t[0] += t[1];
# \3 ]; _6 ~" w) I' t case '-':return t[0] -= t[1];
% U1 g/ C) ?3 r2 D/ i case '*':return t[0] *= t[1];7 ?; |9 F$ p- G$ [6 `1 ?
default: return t[0] /= t[1];//case '/':% x2 w Z; G7 Q& f0 n4 M Q
}* E6 Q1 V* M, K$ X2 o0 B
}}
B- M9 {- \6 u3 b8 D8 u( B9 f. Dtemplate <class _T, class _Tstream>
: w! N0 ^8 e% w: } e$ O8 w& a/* _Tstream: inputstream, _T: get return value
8 _, s; `$ J, T3 a4 e6 A/ c- g* Return nonzero if get value successfully */
0 L- g B9 M- D* h7 E, cint GetExpValue(_Tstream& istrin, _T& nReturn){
6 w8 `; b! V! L$ V/ d! i. \ _T t[3] = {0}; //雨中飞燕之作) w [) V& _' C# y- b8 J: B7 ^
char csym[3] = "++";. u- k4 c5 t3 t8 h8 `7 N
int nLevel = 1, nERR = 0;
* `- c2 e4 @: t if(!(istrin>>t[1]))istrin.clear();
6 [: I1 O' y' ^ for(;;){
4 m8 \1 H/ f# M if(istrin>>csym[2]){
$ r: P% i( N; a, r, f" T switch(csym[2]){" M; s' e4 d% Y
case '(':
! [. z. d9 G" p if(!csym[1]){nLevel=0x100; nERR=1;}else. @2 ?4 t( ?9 ]; c6 z& k
if(!GetExpValue(istrin, t[2]))nLevel|=0x10;* X+ W9 v4 h. U% @
else{nLevel=0x100; nERR=1;}9 P9 `# ^0 o# U- l5 a) h3 I0 |0 `" g
break;
) f. z$ D" G0 \9 q0 @, w$ o0 d case ')':$ f8 e8 d; H2 O. [0 l3 d5 z- f
{nLevel = 0x100;}break;7 ?7 g$ ? Y+ j) @; F6 \9 r- Y' J2 a
case '+':case '-':case '*':case '/':
+ K C: x3 c* | R- O {csym[nLevel++] = csym[2];}break;
( K' }( O% p. W0 |5 ~% u# x5 m+ H case ' ':case '\r':case '\n':case '\t':continue;
1 R1 k6 q) s+ ] default:0 g# B$ i2 G' C3 \, @9 \$ \
{nLevel=0x100; nERR=1;}
) ?% d5 B% g8 N/ h+ J. i! | }7 e4 d3 q6 M, `2 x+ T
if(nLevel==0x100)break;
j: l H% f3 T* w& p if(nLevel&0x10 || istrin>>t[2]){$ X9 [& i5 u$ c+ R! V
nLevel &= 0xF;
2 O2 R: S8 j2 | if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}. }1 `5 w5 X/ R& X; h T3 j2 J' [
if(csym[1]=='*'||csym[1]=='/'){
3 ]! E" Z9 l# F% P7 b GetExpValue(t+1, csym[1]);
) t# l# V- X- Y' v6 ^ }
* |9 G) A b. p$ u! h6 Y2 p else{
' V5 A6 N9 H+ ^- o& v GetExpValue(t, csym[0]);# L( f* U& ~) l: B& O y: _/ Z
t[1]=t[2];csym[0]=csym[1];csym[1]=0;7 x# g' S/ N* o: k) R
}
3 B3 D3 ?2 D0 a2 Y% K nLevel = 1;
& ~+ _! R2 \ ]# ^ }! t/ _* j0 W; ^- G
else istrin.clear();; d( a- A$ y! i* [+ K- u( I
}
. w& @; n7 T1 _9 A else{nERR = -1; break;}
* [! Y$ \1 U, Z0 K! j, J7 s } t1 E$ h$ M; u( w- s. k
if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);$ c5 t' w5 ?: d
else nReturn=GetExpValue(t, csym[0]);
$ ^: o: L9 |7 Z6 L- ?3 R9 e return nERR==-1?1:0;* c& s$ e6 K5 [" E# a2 d+ S
}}+ U/ ]/ e( ], @% c
; T4 N, R* Q8 k# E: y. ~7 g
1 A$ L& F3 y. C3 h$ c/ g/ \
/ P! f' m5 I% E3 E: J8 p, I
函数模板使用示例:
0 R# h+ h+ U2 K在以上那段代码的后面加上以下代码:$ d) k, C% c; k9 n
4 `+ I, M" A* r- H: B& g3 e; g
5 i0 ~9 I: ~2 ]1 @# m8 L& i9 [. f; n. b' n' S. j
程序代码:
/ r, w1 n0 K( R, J' ~7 w7 U8 V
* h! v4 ~2 F; }/ |! P: N#include<strstream>6 u7 I. |7 ] L2 [9 `1 ?( I
#include<iostream>2 R* o+ F* D* Q% _- \/ b8 Z' V
#include<string>5 G+ @9 w% }- H8 d& O1 o! e* q, [
using namespace std;
( G Q7 Z; m4 W' J# T1 w, zint main(void)
O7 l% s% D8 [{
8 ^5 [/ B5 i; U$ ?0 H, n8 z }: R string s1;
7 i$ f m a7 v5 W* v6 W0 V) u while(cin>>s1), L$ v2 F* U5 t6 _1 J9 V0 S( Q
{
+ X6 z# s3 Z" S v) ?$ u/ y istrstream isin(s1.data()); l' T9 K& u; D2 ^- h2 k
double d;
; @5 w6 N# x& ^ if(fy_Exp::GetExpValue(isin, d))
3 T$ ?0 H2 H4 }; P: I {2 z" c3 q4 [6 x8 m" t- Z
cout<<d<<endl;
& ^3 i- t" d* |- S. ^- Z }3 U5 V% d% Q4 w
else5 C$ K/ u( c, E3 D5 N; i b( u
{/ e& T8 J" ~8 C$ h& @
cout<<"ERROR"<<endl;
9 z0 V+ a z. }, @; a' r4 T% \- O3 V }
7 a: q$ N- `% Z) L- m1 y' w }
+ f, [9 ^3 C- X% \" K# v1 b return 0;
+ ?3 `9 a' y/ ]# _}
) X, p9 t1 d4 m k: Z- f0 J8 Z/ h( B3 H E `( g- _7 F) N
0 F$ ^: ^, I, S
然后编译执行就可以了(*^_^*)/ m& L# l* Z5 {; q& V& h
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
5 o, x9 N( D1 W& X' Q& M. Y 建议使用VC7或VC更高版本,或者使用GNU C++编译 |
|