一個(gè)很方便的C++函數(shù)模板,可以并且只可以計(jì)算含括號(hào)的四則表達(dá)式,只有一個(gè)函數(shù)接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
參數(shù)解釋:
istrin: 一個(gè)輸入流,可以是標(biāo)準(zhǔn)IO流,可以是文件流,也可以是串流
nReturn:用于接收計(jì)算結(jié)果的變量,計(jì)算所使用的類型由這個(gè)變量確定
返回值:
返回非0表示計(jì)算成功,0表示計(jì)算失敗有錯(cuò)誤
程序代碼:
以下是引用片段:
namespace fy_Exp{
namespace {template
inline _T GetExpValue(_T t[], char& csym){
char c=csym; csym=0;
switch(c){
case '+':return t[0] += t[1];
case '-':return t[0] -= t[1];
case '*':return t[0] *= t[1];
default: return t[0] /= t[1];//case '/':
}
}}
template
/* _Tstream: inputstream, _T: get return value
* Return nonzero if get value successfully */
int GetExpValue(_Tstream& istrin, _T& nReturn){
_T t[3] = {0}; //雨中飛燕之作
char csym[3] = "++";
int nLevel = 1, nERR = 0;
if(!(istrin>>t[1]))istrin.clear();
for(;;){
if(istrin>>csym[2]){
switch(csym[2]){
case '(':
if(!csym[1]){nLevel=0x100; nERR=1;}else
if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
else{nLevel=0x100; nERR=1;}
break;
case ')':
{nLevel = 0x100;}break;
case '+':case '-':case '*':case '/':
{csym[nLevel++] = csym[2];}break;
case ' ':case '\r':case '\n':case '\t':continue;
default:
{nLevel=0x100; nERR=1;}
}
if(nLevel==0x100)break;
if(nLevel&0x10 || istrin>>t[2]){
nLevel &= 0xF;
if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
if(csym[1]=='*'||csym[1]=='/'){
GetExpValue(t+1, csym[1]);
}
else{
GetExpValue(t, csym[0]);
t[1]=t[2];csym[0]=csym[1];csym[1]=0;
}
nLevel = 1;
}
else istrin.clear();
}
else{nERR = -1; break;}
}
if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
else nReturn=GetExpValue(t, csym[0]);
return nERR==-1?1:0;
}}
參數(shù)解釋:
istrin: 一個(gè)輸入流,可以是標(biāo)準(zhǔn)IO流,可以是文件流,也可以是串流
nReturn:用于接收計(jì)算結(jié)果的變量,計(jì)算所使用的類型由這個(gè)變量確定
返回值:
返回非0表示計(jì)算成功,0表示計(jì)算失敗有錯(cuò)誤
程序代碼:
以下是引用片段:
namespace fy_Exp{
namespace {template
inline _T GetExpValue(_T t[], char& csym){
char c=csym; csym=0;
switch(c){
case '+':return t[0] += t[1];
case '-':return t[0] -= t[1];
case '*':return t[0] *= t[1];
default: return t[0] /= t[1];//case '/':
}
}}
template
/* _Tstream: inputstream, _T: get return value
* Return nonzero if get value successfully */
int GetExpValue(_Tstream& istrin, _T& nReturn){
_T t[3] = {0}; //雨中飛燕之作
char csym[3] = "++";
int nLevel = 1, nERR = 0;
if(!(istrin>>t[1]))istrin.clear();
for(;;){
if(istrin>>csym[2]){
switch(csym[2]){
case '(':
if(!csym[1]){nLevel=0x100; nERR=1;}else
if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
else{nLevel=0x100; nERR=1;}
break;
case ')':
{nLevel = 0x100;}break;
case '+':case '-':case '*':case '/':
{csym[nLevel++] = csym[2];}break;
case ' ':case '\r':case '\n':case '\t':continue;
default:
{nLevel=0x100; nERR=1;}
}
if(nLevel==0x100)break;
if(nLevel&0x10 || istrin>>t[2]){
nLevel &= 0xF;
if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
if(csym[1]=='*'||csym[1]=='/'){
GetExpValue(t+1, csym[1]);
}
else{
GetExpValue(t, csym[0]);
t[1]=t[2];csym[0]=csym[1];csym[1]=0;
}
nLevel = 1;
}
else istrin.clear();
}
else{nERR = -1; break;}
}
if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
else nReturn=GetExpValue(t, csym[0]);
return nERR==-1?1:0;
}}

