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

|
一个计算四则表达式的模板
在9月8日那天我特意编写的,给大家分享的,
0 D. ~- x0 h% t1 U一个很方便的函数模板,可以并且只可以计算含括号的四则表达式 h1 s0 C, \# A7 l9 |* f, K1 y$ C
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
, N% k4 R' S+ n- W+ V) {, z+ ^3 [参数解释:
P( H- P) }4 _istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流! Y& w+ e$ D! P b/ D& A
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定8 v; \' e3 N0 ^ Q4 L
返回值:; G3 Z" r2 f9 a
返回非0表示计算成功,0表示计算失败有错误
- y; W/ E9 d- y3 E7 U; E
" r' k8 H; z7 Q9 x
: e+ J) U9 v3 @8 l E* B2 D& L2 e+ d/ s
程序代码: 8 f1 _$ u- V, m% e' F# B& }* z/ _
& |8 a+ ~+ W; n5 A1 M* J6 H% Jnamespace fy_Exp{
* H4 S8 P1 G8 }( {( o0 m3 inamespace {template <class _T>
{$ z; `' h# t; @ sinline _T GetExpValue(_T t[], char& csym){/ L# p1 o2 n- c( d, A
char c=csym; csym=0;- c% T( i+ h1 i( u' H, K) N
switch(c){
, h* @2 R/ k5 J" h case '+':return t[0] += t[1];
6 h1 y6 r# B. v x case '-':return t[0] -= t[1];. G* {- H5 g7 J1 Z. C7 [6 M
case '*':return t[0] *= t[1];! L/ A3 l* `1 ] g Z* A" c
default: return t[0] /= t[1];//case '/':6 ?8 e! o9 L6 ]- Z) {1 W
}
3 \/ c4 L0 G3 `: ]8 ?8 R}}
. ]; Z' [% `( a. D: N) g9 Atemplate <class _T, class _Tstream>$ C3 q# \# d8 L; a! q4 @
/* _Tstream: inputstream, _T: get return value
; P* _1 a) W0 P3 b+ i: i$ L* Return nonzero if get value successfully */6 W( ~# P6 z- A; K" Q: K
int GetExpValue(_Tstream& istrin, _T& nReturn){% o; A* l9 l4 o [. l
_T t[3] = {0}; //雨中飞燕之作5 q$ b( d9 E) l
char csym[3] = "++";2 l' t! F$ q1 x. Z- S
int nLevel = 1, nERR = 0; c$ g$ O$ S$ v1 l; d
if(!(istrin>>t[1]))istrin.clear();6 W) E. V) W5 d2 Y& x
for(;;){8 _9 _, b% P6 G) F0 r; F) j% F
if(istrin>>csym[2]){
* {3 c. J" q& l2 h$ T. V) r/ e- Q switch(csym[2]){( b: E: e/ \' k Z! V9 W1 ?
case '(': g; @1 X' R6 ~& l* V% \+ W- t
if(!csym[1]){nLevel=0x100; nERR=1;}else" e) i [; \/ }/ q, B4 l
if(!GetExpValue(istrin, t[2]))nLevel|=0x10; d J) b: \2 V4 @/ ^( l4 F$ N
else{nLevel=0x100; nERR=1;}
$ v. B& s: v- B2 H4 j: x" | break;6 S8 e% E+ i, h$ v; b5 `
case ')':
2 u, c3 M+ t( W% Q% Q {nLevel = 0x100;}break;5 w a$ e z0 A [# N5 Z5 r
case '+':case '-':case '*':case '/':
: }- p$ e# d& X$ s+ ~$ ^ {csym[nLevel++] = csym[2];}break;
1 J5 Y0 q$ B) d, e- V case ' ':case '\r':case '\n':case '\t':continue;
5 K* j+ G0 n0 @ default:1 Q5 {% a: S' w" [3 f. W
{nLevel=0x100; nERR=1;}
& C7 a3 S Z) v* W% b% U. m, m: g }( v) J2 E1 a0 ^ c3 S6 ~* E$ y; J
if(nLevel==0x100)break;
2 [! a. C0 J: L+ b: k if(nLevel&0x10 || istrin>>t[2]){# C0 }9 X: G8 D% C: w9 h
nLevel &= 0xF;
- u% U1 c/ A7 N h; h1 ^ if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
8 E& _) k4 q* Q. c- j if(csym[1]=='*'||csym[1]=='/'){9 u2 f0 w( D, \& f5 \- y
GetExpValue(t+1, csym[1]);* d9 b- `: M- |/ V! ?
}! q' Z4 L/ P: R0 V6 O' l
else{
! h2 ^4 u6 M3 }' o" c GetExpValue(t, csym[0]);
4 N' A; O! `" O2 m% m" \" I t[1]=t[2];csym[0]=csym[1];csym[1]=0;
/ u4 k e! ?" C3 n( t/ K: I }
2 p, U8 f; T! z( F7 |6 w1 \6 L" f nLevel = 1;5 y/ p* |' k. t7 r# F7 d" l- p$ d
}8 Z9 h: e- [, _& _4 ~' c
else istrin.clear();6 U7 O: D) F+ t! k0 A7 `
}
, V# q, [2 `6 f else{nERR = -1; break;}
" L7 ^) ]- u7 z- r' v }8 E4 c+ m. N1 v7 u% i4 S6 J& s, a
if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
+ ?; J9 b3 Q! _% G$ `& T else nReturn=GetExpValue(t, csym[0]);
; f& j. J! V2 {5 h5 f return nERR==-1?1:0;7 v' \) d9 H7 A1 f
}}9 _+ n S2 O7 Z k0 e
+ b* M( t+ ^9 v0 L- i7 E1 n" w# g" b0 b8 p/ s" i
, z7 z8 x/ X' A: ~" c& |
函数模板使用示例:% S3 H6 ~% @) M
在以上那段代码的后面加上以下代码:3 h( Z) A2 B5 A; `6 U2 X3 s
& g$ B( g1 B7 _
( m$ ^3 e! M2 j/ b6 }! l
|- w; Z# Y ^! c3 Y" m程序代码: - F$ n% `( K: @' ]4 m8 |
3 a/ r: L/ N b#include<strstream>0 \% M1 Q3 }( k& s( j0 P
#include<iostream>
8 U9 ^" K2 }, k9 y1 k#include<string> U7 p2 n; a. W1 c& T) V9 H
using namespace std;
7 c+ C2 a& \4 f+ I% k1 F) mint main(void)
h( ^6 m0 [8 F6 h# Y3 w{
& z$ H& z0 J# V4 w, h+ D8 l string s1;
; M4 ^: m' Y6 w i5 `, Y while(cin>>s1)+ i( }9 Q# E! X1 v7 K4 J* C. p
{# k* R B( B, C; D, B& G
istrstream isin(s1.data());6 L/ W6 h0 Q1 S
double d;/ d# p: n4 W1 x. L% [0 j& N
if(fy_Exp::GetExpValue(isin, d))& P4 L5 S& t) t5 K- k+ _/ \
{# v% B9 M2 j* X; a+ c
cout<<d<<endl;2 r8 \' ^" o5 A; K) D4 z
}
+ h4 t3 x* V6 w9 a else5 T D1 R4 j- ^. B8 j7 D
{9 ]' t5 D- q. B% l8 G
cout<<"ERROR"<<endl;
' U8 h) k( a+ i! U }
A [6 u9 L+ P, [ }- P" ]. l. V6 ^: R8 [) L
return 0;
, H& W+ M* t+ i}; [/ c/ x, o" q# j
! |3 T" b: T p! {3 c
4 m5 D: g% X/ U' N2 S: ^% G0 D
然后编译执行就可以了(*^_^*); c* H5 f! K9 q* D( |4 D
其它:TC++上一定编译错误,不保证在VC6上也能通过编译3 i/ s# V. ^" @2 W; y$ d: ~
建议使用VC7或VC更高版本,或者使用GNU C++编译 |
|