将中缀转换为前缀表示法

时间:2016-05-10 16:18:01

标签: c++ stack queue polish-notation

我有一个任务:制作一个程序(C ++),它可以转换" infix"符号"前缀"并使用自己的"堆栈和队列"变现。

但我得到:"Critical error detected c0000374""Free Heap block modified at ... after it was freed"最后一串void main() { /*...*/ system("pause"); }或最后一串void toPrefix();

有人可以帮我指出我的错误吗?

Source.cpp

#include "iostream"
#include "fstream"
#include "string"
#include "Stack.h"
#include "Queue.h"

void toPrefix(const std::string& first)
{
    int length = first.length();
    char test = NULL, operand = NULL;
    char *ptr = &test, *op_ptr = &operand;
    Queue<char> List;
    std::string Output;
    Stack<char> OpStack;
    for (int i = length - 1; i >= 0; i--) List.push(first[i]); //
    while (List.getsize() != 0)
    {
        List.pop(ptr);
        if (test >= 48 && test <= 57) //is it number?
        {
            Output.insert(Output.begin(), test);
        }
        if (test == '*' || test == '/' || test == '-' || test == '+')
        {
            OpStack.push(test);
        }
        if (test == ')')
        {
            OpStack.push(test);
        }
        if (test == '(')
        {
            OpStack.pop(op_ptr);
            while (operand != ')')
            {
                Output.insert(Output.begin(), operand);
                OpStack.pop(op_ptr);
            }
        }
    }
}

void main()
{
    const std::string& first = "9-(2+2)";
    toPrefix(first);
    system("pause");
}

Queue.h:

#include<iostream>

template <typename T>
class Queue
{
private:
    struct queue_element
    {
        T value;
        queue_element *next;
    };

    queue_element *first;
    queue_element *last;
    int size;

public:
    Queue()
    {
        first = new(queue_element);
        last = first;
        first->value = -1;
        first->next = 0;
        size = 0;
    }
    Queue(T x)
    {
        first = new(queue_element);
        last = first;
        first->value = x;
        first->next = 0;
        size = 1;
    }

    int getsize()
    {
        return size;
    }

    void push(T value)
    {
        if (size == 0)
        {
            size++;
            last = first;
            first->value = value;
            first->next = 0;
        }
        else
        {
            size++;
            queue_element *temp = new(queue_element);
            temp->next = 0;
            temp->value = value;
            last->next = temp;
            last = temp;
        }
    }

    void pop(T* ret)
    {
        if (size == 0)
        {
            std::cout << "Queue is empty!" << std::endl;
            return;
        }
        queue_element *temp = first;
        *ret = first->value;
        first = first->next;
        size--;
    }

    void peek(T *ret)
    {
        if (size == 0)
        {
            std::cout << "Queue is empty!" << std::endl;
            return;
        }
        *ret = first->value;
    }
};

Stack.h

#include <iostream>

template <typename T>
class Stack
{
private:
    struct stack_element
    {
        T value;
        stack_element *next;
    };
    stack_element *first;
    stack_element *last;
    int size;

public:
    Stack()
    {
        last = new(stack_element);
        first = last;
        last->value = -1;
        last->next = first;
        size = 0;
    }
    Stack(T x)
    {
        last = new(stack_element);
        first = last;
        last->value = x;
        last->next = 0;
        size = 1;
    }

    int getsize()
    {
        return size;
    }

    void push(T value)
    {
        if (size == 0)
        {
            size++;
            last->value = value;
            last->next = first;
        }
        else
        {
            size++;
            stack_element *temp = new(stack_element);
            temp->next = last;
            temp->value = value;
            last = temp;
        }
    }

    void pop(T* ret)
    {
        if (size == 0)
        {
            std::cout << "Stack is empty!" << std::endl;
            return;
        }
        stack_element *temp = last;
        *ret = last->value;
        last = last->next;
        delete temp;
        size--;
    }

    void peek(T *ret)
    {
        if (size == 0)
        {
            std::cout << "Stack is empty!" << std::endl;
            return;
        }
        *ret = first->value;
    }
};

1 个答案:

答案 0 :(得分:0)

嗯......我认为问题出在你身上Stack

您传递给toPrefix()的字符串为"9-(2+2)"

因此Stack<char> OpStack中定义的toPrefix()中的操作是(如果我理解的话)

  1. 使用默认(无参数)构造函数构造

  2. push()-

  3. 对应 函件od pop()

    中的
  4. (

  5. push()+

  6. 对应
  7. push())

  8. 对应

    好吧,让我们看看它里面发生了什么

    1)构造后使用默认构造函数

    我们有

    1a)size == 0

    1b)firstlastfirst->nextlast->next指向相同的已分配内存区域

    1c)first->value == last->value == (char)-1

    2)第一次致电push()后(-

    我们有

    2a)size == 1

    2b)firstlastfirst->nextlast->next指向相同的已分配内存区域

    2c)first->value == last->value == '-'

    3)第一次致电pop()

    之后

    我们有

    3a)size == 0

    3b)指向同一DELETED内存区域的 firstlastfirst->nextlast->next

    3c)first->value == last->value == '-'

    4)第二次呼叫push()+}

    4a)size递增

    4b)在DELETED区域写入(last->value = value;

    4c)再次写入(last->next = first;)在DELETED区域

    我想这可以解释你的问题。

    p.s。:&#34;使用调试器&#34;来自Rambo Ramon和Sam Varshavchik的建议是一个很好的建议(恕我直言)

    p.s.2:抱歉我的英语不好