后缀表达式求值
postfix -expression evaluation
我试图实现后缀表达式求值,这是我的代码:
#include<iostream>
#include<string.h>
using namespace std;
template < class T > class Stack {
private:
T * s;
int n;
public:
Stack(int maxn) {
s = new T[maxn];
n = 0;
}
int empth() const {
return n == 0;
}
void push(T item) {
s[n++] = item;
}
int pop() {
return s[--n];
}
};
int main()
{
string a = "598+46**7+*";
int n = a.length();
Stack < int >save(n);
for (int i = 0; i < n; i++) {
if (a[i] == "+")
save.push(save.pop() + save.pop());
if (a[i] == "*")
save.push(save.pop() * save.pop());
if ((a[i] >= '0') && (a[i] <= '9'))
save.push(0);
while ((a[i] >= '0') && (a[i] <= '9'))
save.push(10 * save.pop() + (a[i++] - '0'));
}
cout << save.pop() << endl;
return 0;
}
但是我得到一个编译错误(我在linux (ubuntu 11.10)中实现它):
postfix.cpp:35:13: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
postfix.cpp:37:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
如何解决这个问题?
for(int i=0;i<n;i++){
if(a[i]=="+")
save.push(save.pop()+save.pop());
if(a[i]=="*")
比较字符
时需要使用单引号for(int i=0;i<n;i++){
if(a[i]=='+')
save.push(save.pop()+save.pop());
if(a[i]=='*')
这是计算后缀表达式的链接。
相关文章:
- 如何使用 ASCII 转换使用字符堆栈计算后缀表达式
- 多位数后缀表达式
- 检查字符串是否包含有效的后缀表达式
- 在C++中从后缀表达式字符串打印单个操作
- C++后缀表达式未定义与未指定行为
- 如何从后缀表达式生成表达式树
- 中缀到后缀表达式转换代码不起作用
- 使用Stacks从中缀表达式转换为后缀(C++)
- 我想看到一个在整个后缀表达式的上下文中查找转换类型 id 的示例
- 使用堆栈评估后缀表达式时出现分段错误
- 如何正确测试我的后缀表达式是否有效
- 从 txt 文件中将后缀表达式作为字符串读取
- 在C++中使用树计算后缀表达式
- 后缀表达式中的等号运算符
- 计算后缀表达式.pop() 返回不正确的值来计算函数
- 使用堆栈C++后缀表达式
- 计算具有两个以上操作数的后缀表达式时出现问题
- 如何在表达式中计算后缀和前缀增量运算符
- 在c++中计算后缀表达式时接收堆栈错误
- 后缀表达式求值