我无法使用堆栈找到前缀号的总和
I can not find sum of the prefix number using stack
#include <iostream>
#include <string>
using namespace std;
class StackNode
{
public:
StackNode * topPtr = NULL;
StackNode* next;
string item;
bool push( string newItem) {
// create a new node
StackNode *newPtr = new StackNode;
// set data portion of new node
newPtr->item = newItem;
// insert the new node
newPtr->next = topPtr;
topPtr = newPtr;
return true;
}
bool pop() {
if (topPtr == NULL)
return false;
// stack is not empty; delete top
else{
StackNode *temp = topPtr;
topPtr = topPtr->next;
// return deleted node to system
temp->next = NULL; // safeguard
delete temp;
return true;
}
}
int ope(string op, string val1,string val2)
{
int vaL1 = stoi(val1);
int vaL2 = stoi(val2);
int res = 0;
if( op == "*")
res = vaL1 * vaL2;
if( op == "/")
res = vaL1 / vaL2;
if( op == "-")
res = vaL1 - vaL2;
if( op == "+")
res = vaL1 + vaL2;
return res;
}
int cal(string pre_exp[],int len)
{
int numb = 0;
for(int i = len -1;i>=0;i--)
{
if ( pre_exp[i] == "*" || pre_exp[i] == "/" || pre_exp[i] == "+" || pre_exp[i] == "-")
{
string op1 = topPtr->item;
pop();
string op2 = topPtr->item;
pop();
numb = numb + ope(pre_exp[i],op1,op2);
}
else
{
push( (pre_exp[i]));
}
}
return numb;
}
int main()
{
StackNode nbr;
string eyoo[] = {"+","-","2","3","9"};
cout<< nbr.cal(eyoo,5)<<endl;
return 0;
}
大家好,我正在尝试求前缀表达式的和。我的代码在这里。奇怪的是,我没有得到任何输出。 cal 方法没有 return 数字,可能程序卡在了 calc 方法的 for 循环中。有人能帮助我吗? pop 和 push 方法有效,我用 display 方法测试了它们。问题一定出在stoi的使用上,或者我说的calc方法上。
string op1 = topPtr->item;
pop();
string op2 = topPtr->item;
pop();
你总是弹出一个运算符的 2 个操作数,假设你推送结果,但你不这样做,在给定时间 topPtr 变为空,以你的例子为例当你做 string op2 = topPtr->item;
对我来说 numb = numb + ope(pre_exp[i],op1,op2);
必须通过将 ope(pre_exp[i],op1,op2)
的结果推入堆栈中两个弹出值
的位置来替换
所以 {"+","-","2","3","9"}
:
- 推 9
- 推 3
- 推 2
-
所以 pop=2 - pop=3 = -1,你必须推 -1
+
所以 pop=-1 + pop=9 = 8 然后你推 8(没有推 -1 在获得第二个时堆栈为空之前操作数)
- 一切都完成了,所以你弹出结果 = 8
不过我很惊讶你在表达式的末尾加注星标,我不确定在所有情况下都可以计算出很好的结果,为什么你不从头开始?
最后一句话:你所有的方法都尽量是内联的(在class中定义),我们在方法很小的时候使用内联,最好将定义移出class你的方法
#include <iostream>
#include <string>
using namespace std;
class StackNode
{
public:
StackNode * topPtr = NULL;
StackNode* next;
string item;
bool push( string newItem) {
// create a new node
StackNode *newPtr = new StackNode;
// set data portion of new node
newPtr->item = newItem;
// insert the new node
newPtr->next = topPtr;
topPtr = newPtr;
return true;
}
bool pop() {
if (topPtr == NULL)
return false;
// stack is not empty; delete top
else{
StackNode *temp = topPtr;
topPtr = topPtr->next;
// return deleted node to system
temp->next = NULL; // safeguard
delete temp;
return true;
}
}
int ope(string op, string val1,string val2)
{
int vaL1 = stoi(val1);
int vaL2 = stoi(val2);
int res = 0;
if( op == "*")
res = vaL1 * vaL2;
if( op == "/")
res = vaL1 / vaL2;
if( op == "-")
res = vaL1 - vaL2;
if( op == "+")
res = vaL1 + vaL2;
return res;
}
int cal(string pre_exp[],int len)
{
int numb = 0;
for(int i = len -1;i>=0;i--)
{
if ( pre_exp[i] == "*" || pre_exp[i] == "/" || pre_exp[i] == "+" || pre_exp[i] == "-")
{
string op1 = topPtr->item;
pop();
string op2 = topPtr->item;
pop();
numb = numb + ope(pre_exp[i],op1,op2);
}
else
{
push( (pre_exp[i]));
}
}
return numb;
}
int main()
{
StackNode nbr;
string eyoo[] = {"+","-","2","3","9"};
cout<< nbr.cal(eyoo,5)<<endl;
return 0;
}
大家好,我正在尝试求前缀表达式的和。我的代码在这里。奇怪的是,我没有得到任何输出。 cal 方法没有 return 数字,可能程序卡在了 calc 方法的 for 循环中。有人能帮助我吗? pop 和 push 方法有效,我用 display 方法测试了它们。问题一定出在stoi的使用上,或者我说的calc方法上。
string op1 = topPtr->item;
pop();
string op2 = topPtr->item;
pop();
你总是弹出一个运算符的 2 个操作数,假设你推送结果,但你不这样做,在给定时间 topPtr 变为空,以你的例子为例当你做 string op2 = topPtr->item;
对我来说 numb = numb + ope(pre_exp[i],op1,op2);
必须通过将 ope(pre_exp[i],op1,op2)
的结果推入堆栈中两个弹出值
所以 {"+","-","2","3","9"}
:
- 推 9
- 推 3
- 推 2
-
所以 pop=2 - pop=3 = -1,你必须推 -1+
所以 pop=-1 + pop=9 = 8 然后你推 8(没有推 -1 在获得第二个时堆栈为空之前操作数)- 一切都完成了,所以你弹出结果 = 8
不过我很惊讶你在表达式的末尾加注星标,我不确定在所有情况下都可以计算出很好的结果,为什么你不从头开始?
最后一句话:你所有的方法都尽量是内联的(在class中定义),我们在方法很小的时候使用内联,最好将定义移出class你的方法