spoj:ONP-转换表达式
spoj : ONP - Transform the Expression
我正在尝试解决spoj问题:链接它显示了运行时错误,测试用例在我的机器上运行良好。无法找出我程序中的错误。我正在使用队列和堆栈来实现它。
编辑:我已经编辑了源代码,并更正了pop函数中的int temp-->char temp。。但我仍然得到了一个运行时错误;
#include <iostream>
using namespace std;
int top = -1;
int endQueue = -1;
char stack[505];
char queue[505];
int pref(char a) {
switch (a) {
case '+':
case '-' :return 1;
break;
case '*' :
case '/' :return 2;
break;
case '^' :return 3;
break;
default: return 0;
}
}
void push(char a) {
if (top < 0)
top =0;
stack[++top] = a;
}
char pop() {
char temp = stack[top];
top--;
return temp;
}
void enque(char a) {
if (endQueue < 0)
endQueue = 0;
queue[++endQueue] = a;
}
char topElement() {
if (top > -1)
return stack[top];
else
return '0';
}
int main() {
// your code here
int t;
char temp;
char exp[500];
cin>>t;
while(t--) {
cin>>exp;
//cout<<exp;
for (int i = 0; exp[i]!= ' ';i++) {
if (exp[i]>= 'a' && exp[i] <='z') {
// variables
enque(exp[i]);
} else {
//operator
if (exp[i] == ')') {
// pop till (
// and append
while (top > -1 ) {
temp = pop();
if (temp == '(')
break;
else
enque(temp);
}
} else {
while (exp[i] != '(' && top > -1 && pref(topElement()) > pref(exp[i]) ) {
char popped = pop();
enque(popped);
}
push(exp[i]);
}
}
}
while(top > -1) {
enque(pop());
}
for (int j = 0;j<=endQueue;j++) {
cout<<queue[j];
}
cout<<"n";
}
return 0;
}
你不认为main中表达式的大小与队列和堆栈的大小不匹配吗
在pop函数中,您已将temp声明为int,而返回类型为char
相关文章:
- 如何将Javascript正则表达式转换为C++11正则表达式
- 使用向量将给定表达式转换为波兰表示法时出现运行时错误
- 如何使用 cpp 将 z3 表达式转换为字符串
- 如何将表达式转换为变量
- 不是表达式转换,而是从预序到Inorder(不是表达式)的转换
- 中缀到后缀表达式转换代码不起作用
- 使用Stacks从中缀表达式转换为后缀(C++)
- 将Perl正则表达式转换为等效的ECMAScript正则表达式
- 如何将c样式的正则表达式转换为c++样式
- 您可以将C++ std::regex 表达式转换为 C# 表达式吗?
- 使用 c++ 或 Qt 将 SQL LIKE 表达式转换为正则表达式
- 将C++Boost正则表达式转换为Python正则表达式
- 如何将C#中的Lambda表达式转换为C++
- 使用cout将表达式转换为赋值
- 我的数学表达式转换器无法正确打印
- 将带有变量捕获的lambda表达式转换为函数指针
- Lambda表达式转换
- Qt正则表达式增强正则表达式转换从字符串内部取数
- c++正则表达式将正则表达式转换为c++代码
- 助推::精神::气.如何将内联语法分析器表达式转换为独立语法,以及如何解压缩它们生成的元组