[英]Infix to postfix evaluating unary negative
I'm sure this has been asked a few times but the other questions I looked at didn't really help me much. 我确定已经问过几次了,但是我看过的其他问题并没有真正帮助我。 Alright so here goes: I've got three functions one that converts an infix expression to a postfix, one that's a preprocessor and one that evaluates the postfix expression.
好的,这里是这样:我有三个函数,一个将中缀表达式转换为后缀,一个是预处理器,另一个计算后缀表达式。 What I'm having trouble with is evaluating a unary negative expression.
我遇到的麻烦是评估一元否定表达。 If I put in my entire code it'll be super long so I'm only going to post up the parts that deal with the negative/minus case:
如果我输入完整的代码,它将非常长,因此我只会发布处理负数/负数情况的部分:
here's my output: input: -3 这是我的输出:输入:-3
after preprocess: 3 postfix = -3 then a segmentation fault when it should output " total = -3 " 预处理后:3后缀= -3,则出现分段错误,应输出“ total = -3”
#include "postfix.h"
#include "stack.h"
#include <cstdlib>
#include <cmath>
#include <cstdio>
void eval_postfix(char* postfix){
Stack<double> stack;
char fchar[100];
int j=0;
double a, b, convert, total = 0.0;
for(int i=0; postfix[i] != '\0'; i++){
switch(postfix[i]){
case '-':
a = stack.top();
stack.pop();
b = stack.top();
stack.pop();
total = b-a;
stack.push(total);
break;
I'm pretty sure the error is in that part of the function, I've been trying different things but nothing has been working, more times than not I get a segmentation fault or a zero. 我很确定错误是在函数的那部分中,我一直在尝试不同的事情,但是什么也没起作用,我多次遇到分段错误或为零。 I originally tried to apply what I did in the infix2postfix expression (which obviously didn't work) But here's the rest of my code for the negative/minus case...
我最初试图应用我在infix2postfix表达式中所做的事情(这显然不起作用),但是这是我的代码的其余部分,用于负号/减号情况...
void infix2postfix(char* infix, char* postfix){
Stack<char> stack;
stack.push('\0');
int pc = 0;
bool c;
for(unsigned int i = 0; infix[i] != '\0'; i++){
//use the switch method to define what to do for each of the operators
switch(infix[i]){
case '-':
c = 0;
//unary negative
if(i==0){
postfix[pc++] = infix[i];
c = 1;
}
else if((infix[i-1] == '*' ||
infix[i-1] == '^' ||
infix[i-1] == '*' ||
infix[i-1] == '/' ||
infix[i-1] == '+' ||
infix[i-1] == '-')&&
i>0){
postfix[pc++]= infix[i];
c=1;
}
else{
if(stack.top() == '*' || stack.top() == '/' || stack.top() == '^'){
while(stack.top() != '\0' && stack.top() != '('){
postfix[pc++] = stack.top();
postfix[pc++] = ' ';
stack.pop();
}
}
}
if (c==0)
stack.push('-');
break;
void preprocessor(char* input){
char output[100];
int oc = 0;
for(unsigned int i=0; input[i] != '\0'; i++){
if((input[i] == '-' && (input[i-1] == '*' || input[i-1] == '^' || input[i-1] == '*'
|| input[i-1] == '/' || input[i-1] == '+' || input[i-1] == '-')
&& i>0)){
//output[oc++] = '0';
output[oc++] = input[i];
}
I'm almost certain that whatever error it is I made (or whatever edit I need to do) is probably something really simple that I just can't see (cause that's usually the case with me) but any nudge in the right direction would be highly appreciated! 我几乎可以肯定,无论我犯什么错误(或需要做任何编辑),都是我看不到的(因为通常是我这样),但任何朝着正确方向前进的事情都会很简单高度赞赏!
**Note: the formatting of my code may not be accurate cause I only copied and pasted the parts I felt were relevant. **注意:我的代码格式可能不正确,因为我只复制并粘贴了我认为相关的部分。
It seems to me like what is happening is that your code that evaluates the postfix expression, when it sees a minus sign, treats it as a subtraction. 在我看来,正在发生的事情是,您的评估后缀表达式的代码在看到减号时将其视为减法。 In particular, probably you push 3 onto the stack, and then encounter a minus sign: this triggers the code in the first block you posted, which tries to pop two elements off of the stack and subtract them.
特别是,可能您将3压入堆栈,然后遇到减号:这将触发您发布的第一个块中的代码,该代码试图从堆栈中弹出两个元素并将它们相减。 However there is only one element on the stack, namely the 3.
但是,堆栈上只有一个元素,即3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.