//下边的是C++实现代码,LZ可以作为参考:
namespace fy_Exp{
namespace {template <class _T>
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 <class _T, class _Tstream>
/* istrin: inputstream, nReturn: 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;
}}
namespace fy_Exp{
namespace {template <class _T>
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 <class _T, class _Tstream>
/* istrin: inputstream, nReturn: 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;
}}
函数模板使用示例:
在以上那段代码的后面加上以下代码:
#include<strstream>
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string s1;
while(cin>>s1)
{
istrstream isin(s1.data());
double d;
if(fy_Exp::GetExpValue(isin, d))
{
cout<<d<<endl;
}
else
{
cout<<"ERROR"<<endl;
}
}
return 0;
}
[此贴子已经被作者于2007-10-27 14:12:09编辑过]
Repeat Life=Study;Until (death);