表达式求值的问题
下面是一个表达式求值的程序,而且可用,我现在想把它改成表达式中只有BOOL型的值,只有1和0,然后求表达式的值,当然其中的/-不用,其他保留。当我把堆栈改为bool型的,提示stack<bool,class std::deque<bool,class std::allocator<bool> > >' to 'class std::stack<float,class std::deque<float,class std::allocator<float> >
这种错误,该好何解决啊???
或都怎么改这个程序啊??谢谢!
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <stack>
#include <cctype>
using namespace std;
void MPN(void);
void Intro();
int main(void)
{
cout << "**************欢迎使用**************" <<endl;
Intro();
MPN();
return 0;
}
bool Prr(char,char);
void Cal(stack<float>&,stack<char>&);
float Dot(float,float);
void MPN(void)
{
int used,pr;
float argt;
char c;
stack<float> num;
stack<char> oper;
argt=0;pr=0;used=0;
while(cin>>c, !cin.eof())
{
if (isdigit(c))
{
used=1;
argt=argt*10+(c-'0');
continue;
} //将输入的数字类字符转换成数字;
if (c=='+'||c=='-'||c=='*'||c=='/'||c=='.')
{
if(used)
{
num.push(argt);argt=0;
used=0;
} //将转化好的数字压入数字栈;
if (oper.empty()||oper.top()=='(') {oper.push(c);continue;}
//向操作符栈里压入第一个操作符;解决括号问题;
while(!Prr(c,oper.top()))
{
Cal(num,oper);
if (oper.empty()||oper.top()=='(') break;
} //根据运算符号优先级,调整运算顺序,并处理优先运算部分;
oper.push(c);
continue;
}
if (c=='(')
{
pr++;oper.push(c);continue; //括号入栈,作为括号内运算的标志;
}
if (c==')')
{
pr--; //括号结束,整理括号内的运算结果;
if (used) {num.push(argt);argt=0;used=0;}
while(!oper.empty())
{
if (oper.top()=='(') break;
Cal(num,oper);
}
oper.pop();
continue;
}
if (c=='=') //遇到‘=’号,输出结果;
{
if (pr) {cout<<"括号数目不匹配,请检查并重新输入!"<<endl;return;}
if (used) num.push(argt);
while(!oper.empty()) Cal(num,oper);
cout<<"表达式的值为: "<<num.top()<<endl;
return;
}
else {cout<<"表达式输入错误,请重新输入!"<<endl;return;}
}
}
bool Prr(char s,char t)
{
if ((s=='*'||s=='/')&&(t=='+'||t=='-')) return true;
if (s=='.') return true;
return false;
}
void Cal(stack<float>& num,stack<char>& oper)
{
float a,b,r;
b=num.top();num.pop();
a=num.top();num.pop();
switch( oper.top() )
{
case ('+'):r= a+b;break;
case ('-'):r= a-b;break;
case ('*'):r= a*b;break;
case ('/'):r= a/b;break;
case ('.'):r=Dot(a,b);
};
num.push(r);
oper.pop();
}
float Dot(float m, float n) //处理小数
{
while (n>=1) n/=10;
n+=m;
return n;
}
void Intro() //功能简介
{
cout<<endl<<"####主要功能如下:"<<endl;
cout<<"支持‘+’、‘-’,‘*’、‘/’四则混合运算,包括多重括号,及浮点数运算"
<<endl;
cout<<"例如,3.14*((25-4)/6+(70+2)/9)+4="<<endl;
cout<<"请注意一定要输入‘=’号以示结束。"<<endl;
cout<<endl<<"Powered by Gideon , Kdevelop on Redhat9"<<endl;
cout<<endl<<"请输入待求表达式:"<<endl;
}