C++ C++;中缀到后缀转换器-知道错误在哪里,但不会出错';不进入if语句?

C++ C++;中缀到后缀转换器-知道错误在哪里,但不会出错';不进入if语句?,c++,C++,所以我基本上让中缀到后缀转换器和计算器工作,但是,当文本文件中的输入有多组括号(如(2+4)*(3+4))时,它会中断,我知道它在哪里中断。然而,我似乎找不到解决这个问题的办法。我在失败的地方放了一条评论 我整晚都在尝试这些东西,不管出于什么原因,它都不会进入if()语句 我把我的代码放在下面: 标题: #ifndef STACK_H #define STACK_H ///////////////////////////////////////////////////// ////Inc

所以我基本上让中缀到后缀转换器和计算器工作,但是,当文本文件中的输入有多组括号(如(2+4)*(3+4))时,它会中断,我知道它在哪里中断。然而,我似乎找不到解决这个问题的办法。我在失败的地方放了一条评论

我整晚都在尝试这些东西,不管出于什么原因,它都不会进入if()语句

我把我的代码放在下面:

标题:

    #ifndef STACK_H
#define STACK_H
/////////////////////////////////////////////////////
////Includes/////////////////////////////////////////
/////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
/////////////////////////////////////////////////////
using namespace std;

/*-------------------------------------------------------------------------------------------------------------*/
template <typename Object>
class Stack
{
private:
    class stackListNode
        {
        public:
            Object data;
            stackListNode *next;
        private:
            //Nothing to declare-->placeholder since class 
            //sets data members to private initially
        };

    stackListNode *top;

public:
    /////////////////////////////////////////////////
    //Constructor Function//////////////////////////
    Stack() {top = NULL;}

        /////////////////////////////////////////////////
        //Rest of functions defined inline for simplicity

        void push(char token)   // Push token onto the stack and create new node for top of stack
            {
                stackListNode *newNode = new stackListNode;
                newNode->data = token;
                newNode->next = top;
                top = newNode;
            }

        char pop()
            {
                if(empty())
                {
                    cout << "Stack is empty!\n" << endl;
                    return NULL;                
                }

                stackListNode *temp = top;
                top = temp->next;
                return temp->data;
            }

        char peek()
            {
                if(empty())
                {
                    cout << "Stack is empty!\n" << endl;
                    //exit(1);
                    return NULL;                
                }
                return top->data;
            }

        int empty()
            {
                return top == NULL;
            }


        int isempty()
        {
            int i = 1;
            if(empty())
            {
                return i;
            }
        }
};

#endif  
#ifndef堆栈
#定义堆栈
/////////////////////////////////////////////////////
////包括/////////////////////////////////////////
/////////////////////////////////////////////////////
#包括
#包括
#包括
#包括
#包括
#包括
#包括
/////////////////////////////////////////////////////
使用名称空间std;
/*-------------------------------------------------------------------------------------------------------------*/
模板
类堆栈
{
私人:
类stackListNode
{
公众:
对象数据;
stackListNode*next;
私人:
//类之后没有要声明的-->占位符
//最初将数据成员设置为私有
};
stackListNode*顶部;
公众:
/////////////////////////////////////////////////
//构造函数//////////////////////////
堆栈(){top=NULL;}
/////////////////////////////////////////////////
//为简单起见,其余函数以内联方式定义
void push(char-token)//将令牌推送到堆栈上,并为堆栈顶部创建新节点
{
stackListNode*newNode=新建stackListNode;
newNode->data=token;
新建节点->下一步=顶部;
top=newNode;
}
char pop()
{
if(空())
{
cout数据;
}
char peek()
{
if(空())
{
cout在
isempty()
方法中,如果堆栈不是空的,则不会返回值

....

int isempty()
{
    int i = 1;
    if(empty())
    {
        return i;
    }
}

....
在上面的示例中,如果
empty()
返回
false
,则缺少一个return语句。这将导致未定义的行为。从表面上看,此未定义的行为似乎导致返回一个杂项非零整数


我只需要使用
empty()
方法,并去掉
isempty()
方法。

当我们有STL时,真的有必要实现自己的
堆栈吗?:)你真的应该使用调试器而不是依赖StackOverflow。调试器的使用速度要快得多。正如我在回复之前的帖子时所说的,你的
empty
函数需要返回
bool
而不是
int
。运算符==
的结果是
bool
,而不是
int
....

int isempty()
{
    int i = 1;
    if(empty())
    {
        return i;
    }
}

....