double PostCalculate::calculate(std::string *p,int size)
{
std::stack
std::string postExp[20];
int count=0; //对 后缀表达式 计数
for(int i=0;i
if(!isSign(p)) //若不是符号 ,即为数字
{
postExp[count]=*p; //将数字直接放入后缀表达式
count++;
}
else if(*p=="+" ||*p=="-")
{
while(!a.empty() && a.top()!="(") //弹出栈中元素直到 栈空 或者 左括号
{
postExp[count]=a.top();
count++;
a.pop();
}
a.push(*p); //将当前元素压栈
}
else if(*p=="*" || *p=="/")
{
while(!a.empty() && a.top()!="(" && a.top()!="+" && a.top()!="-")
{
postExp[count]=a.top();
count++;
a.pop();
}
a.push(*p);
}
else if(*p=="(")
{
a.push(*p);
}
else if(*p==")")
{
while(!a.empty() && a.top()!="(")
{
postExp[count]=a.top();
count++;
a.pop();
}
if(!a.empty() && a.top()=="(")
a.pop();
}
p++;
}
while(!a.empty())
{
postExp[count++]=a.top();
a.pop();
}
double result=postCal(postExp,count);
return result;
}
std::string PostCalculate::dispose(std::string first, std::string second, std::string op)
{
stringstream t;
double r;
double a=stringToDouble(first);
double b=stringToDouble(second);
if(op=="+")
r=a+b;
else if(op=="-")
r=a-b;
else if(op=="*")
r=a*b;
else if(op=="/")
r=a/b;
t<
}
double PostCalculate::postCal(std::string *p,int size)
{
std::stack
for(int i=0;i
if(!isSign(p)) //遇到数字 压栈
res.push(*p);
else //遇到符号
{
string f=res.top(); //取第2操作数
res.pop();
string s=res.top();//取第1操作数
string r=dispose(s,f,*p);
res.push(r);
}
p++;
}
return stringToDouble(res.top());
}
inline bool PostCalculate::isSign(std::string* s)
{
return (*s=="+"|| *s=="-"|| *s=="*"|| *s=="/"|| *s=="("|| *s==")");
}
inline double PostCalculate::stringToDouble(std::string c) //通过流 实现转换 String -> Double
{
double temp;
stringstream a;
a.str(c);
a>>temp;
return temp;
}
入口函数是pCal.calculate(expression,num)//参数1 是string数组,参数2是数组实际个数,程序里设计是字符数不超过20个,想修改可以在PostCalculate::calculate函数的第4行修改
double
PostCalculate::calculate(std::string
*p,int
size)
{
std::stack
a;
std::string
postExp[20];
int
count=0;
//对
后缀表达式
计数
for(int
i=0;i
if(!isSign(p))
//若不是符号
,即为数字
{
postExp[count]=*p;
//将数字直接放入后缀表达式
count++;
}
else
if(*p=="+"
||*p=="-")
{
while(!a.empty()
&&
a.top()!="(")
//弹出栈中元素直到
栈空
或者
左括号
{
postExp[count]=a.top();
count++;
a.pop();
}
a.push(*p);
//将当前元素压栈
}
else
if(*p=="*"
||
*p=="/")
{
while(!a.empty()
&&
a.top()!="("
&&
a.top()!="+"
&&
a.top()!="-")
{
postExp[count]=a.top();
count++;
a.pop();
}
a.push(*p);
}
else
if(*p=="(")
{
a.push(*p);
}
else
if(*p==")")
{
while(!a.empty()
&&
a.top()!="(")
{
postExp[count]=a.top();
count++;
a.pop();
}
if(!a.empty()
&&
a.top()=="(")
a.pop();
}
p++;
}
while(!a.empty())
{
postExp[count++]=a.top();
a.pop();
}
double
result=postCal(postExp,count);
return
result;
}
std::string
PostCalculate::dispose(std::string
first,
std::string
second,
std::string
op)
{
stringstream
t;
double
r;
double
a=stringToDouble(first);
double
b=stringToDouble(second);
if(op=="+")
r=a+b;
else
if(op=="-")
r=a-b;
else
if(op=="*")
r=a*b;
else
if(op=="/")
r=a/b;
t<
t.str();
}
double
PostCalculate::postCal(std::string
*p,int
size)
{
std::stack
res;
for(int
i=0;i
if(!isSign(p))
//遇到数字
压栈
res.push(*p);
else
//遇到符号
{
string
f=res.top();
//取第2操作数
res.pop();
string
s=res.top();//取第1操作数
string
r=dispose(s,f,*p);
res.push(r);
}
p++;
}
return
stringToDouble(res.top());
}
inline
bool
PostCalculate::isSign(std::string*
s)
{
return
(*s=="+"||
*s=="-"||
*s=="*"||
*s=="/"||
*s=="("||
*s==")");
}
inline
double
PostCalculate::stringToDouble(std::string
c)
//通过流
实现转换
String
->
Double
{
double
temp;
stringstream
a;
a.str(c);
a>>temp;
return
temp;
}
入口函数是pCal.calculate(expression,num)//参数1
是string数组,参数2是数组实际个数,程序里设计是字符数不超过20个,想修改可以在PostCalculate::calculate函数的第4行修改
就一个堆栈的应用。