使用堆栈的后缀方程组合

发布于 2025-01-27 11:44:40 字数 7145 浏览 2 评论 0原文

我正在尝试制作将fifix的程序归于postfix,但是当我输入+ -时 输出应为+ -,但我发现输出是++,如果Infix为- +输出是- 自从我开始解决这个问题以来已经一个星期了

#include <iostream>
#include <string>

using namespace std;
//classes
class arr
{
private:
    char *items;
    int size;
    int length;
public:
    //default constructor
    arr()
    {
        items = new char[100];
        size = 100;
        length = 0;
    }
    //constructor with parameters
    arr(int arraySize)
    {
        items = new char[arraySize];
        size = arraySize;
        length = 0;
    }
    //check if array is empty
    bool is_empty()
    {
        return length == 0 ? true : false;
    }
    //check if array full 
    bool is_full()
    {
        return length >= size - 1 ? true : false;
    }
    //returns array length
    int getLength()
    {
        return length;
    }
    //return array size
    int getSize()
    {
        return size;
    }
    //get array address
    char *getAddress()
    {
        return &items[0];
    }
    //fill number of items in array based on elementNum
    void fill(int elementNum)
    {
        char ch;
        cout << "Enter characters you want to add\n";
        if (elementNum > size - 1)
        {
            cout << "can't use elements number largger than " << size - 1;
            return;
        }

        for (int i = 0; i < elementNum; i++)
        {
            cin >> ch;
            insert(i, ch);
        }
    }
    //display all elements in the array
    void display()
    {
        if (is_empty())
        {
            cout << "there are no data /n";
            return;
        }
        cout << "Array items are :\n";
        for (int i = 0; i < length; i++)
        {
            cout << items[i] << "\t";
        }
        cout << endl;
    }
    void append(char ch)
    {
        insert(length, ch);
    }
    void insert(int index, char newItem)
    {
        if (is_full() || length<index || length + 1 == size)
        {
            cout << "\nSorry array is full\ncan't append letter more\n";
            return;
        }
        else {
            for (int i = length; i >= index; i--)
            {
                items[i + 1] = items[i];
            }
            items[index] = newItem;
            length++;
        }

    }
    int search(char ch)
    {
        if (!is_empty())
            for (int i = 1; i <= length; i++)
            {
                if (items[i] == ch)
                    return i;
            }
        return 0;
    }
    void del(int index)
    {
        if (is_empty() || length<index) {
            cout << "sorry can't delete item which is doesn't exist";
            return;
        }

        for (int i = index; i < length; i++)
        {
            items[i] = items[i + 1];
        }
        length--;
    }
    void changeSize(int newSize)
    {
        if (newSize <= length - 1)
        {
            cout << "can't change size because the new size is small";
            return;
        }
        char *tempItems = new char[newSize];
        size = newSize;
        for (int i = 0; i < length; i++)
        {
            tempItems[i] = items[i];
        }
        items = tempItems;
        tempItems = NULL;
    }
    //merge two arrays
    void merge(arr a)
    {
        this->size = this->size + a.getSize();
        for (int i = 0; i < a.getLength(); i++)
        {
            items[i + length] = a.getAddress()[i];
        }
        length = this->length + a.getLength();
    }

};

class stackUsingArray {
private:
    int top;
    arr a;
public:
    stackUsingArray()
    {
        top = -1;
    }
    stackUsingArray(int stackSize)
    {
        a.changeSize(stackSize);
        top = -1;
    }
    bool is_empty()
    {
        return(top == -1);
    }
    bool is_full()
    {
        return(a.is_full());
    }
    void push(char ch)
    {
        top++;
        a.append(ch);
    }
    char pop()
    {
        if (is_empty())
        {
            cout << "sorry the stack is empty";
        }
        else
            return a.getAddress()[top--];
    }
    int peekTop() {
        return top;
    }
    void display()
    {
        //first way of display

        for (int i = top; i >= 0; i--)
        {
            cout << a.getAddress()[i];
        }

        //second way of display

        //stackUsingArray c;
        //char ch;
        //for (int i = top; i >= 0; i--)
        //  c.push(this->pop());
        //for (int i = c.peekTop(); i >= 0; i--)
        //{
        //  ch=c.pop();
        //  this->push(ch);
        //  cout << ch;
        //}
    }
    int search(char ch)
    {
        for (int i = top; i >= 0; i--)
        {
            if (a.getAddress()[i] == ch)
                return i;
        }
        return -1;
    }
    char topDisplay()
    {
        return a.getAddress()[top];
    }

};
class stackUsingLinkedList {};

//functions
string infixToPostfix(string infix);
short prec(char ch);

int main()
{


    //infix and postfix

    stackUsingArray c;
    cout<<infixToPostfix("x+y-z");

    system("pause");
    return 0;
}


string infixToPostfix(string infix)
{
    string postfix = "";
    stackUsingArray sta;
    char ch;
    char test;

    for (int i = 0; i < infix.length(); i++)
    {
        switch (prec(infix[i]))
        {
        case 0:
            postfix.append(1, infix[i]);
            break;
        case 1:
            sta.push(infix[i]);
            break;
        //case 2:
        //  ch = sta.pop();
        //  while (ch != '(')
        //  {
        //      postfix.append(1, ch);
        //      ch = sta.pop();
        //  }
        //  break;
        case 3:

        //  if (sta.is_empty())
        //  {
        //      goto k270;
        //  }
        //  ch = sta.pop();
        //  while (prec(ch) > 3)
        //  {
        //      postfix.append(1, ch);
        //      if (sta.is_empty()) {
        //          //sta.push(infix[i]);
        //          goto k270;
        //      }
        //      ch = sta.pop();
        //  }
        //  sta.push(ch);
        //k270:
        //  sta.push(infix[i]);
            test = sta.topDisplay();
            if (sta.is_empty())
            {
                sta.push(infix[i]);
                test = sta.topDisplay();
            }

            else
            {
                ch = sta.pop();
                test = sta.topDisplay();
                if (prec(ch) >= 3)
                {
                    postfix += ch;
                }
                sta.push(infix[i]);

            }
        }


    }
    while (!sta.is_empty())
    {
        postfix.append(1, sta.pop());
    }

    return postfix;
}
short prec(char ch)
{
    if (ch == '(')
        return 1;
    if (ch == ')')
        return 2;
    if (ch == '+')
        return 3;
    if (ch == '-')
        return 3;
    if (ch == '*')
        return 4;
    if (ch == '/')
        return 4;
    return 0;
}

I am trying to make program that get infix to postfix but when I entered +- in the infix equation
the output should be +- but I find that the output is ++ and if infix is -+ the output is --
it have been a week since I started to solve that problem

#include <iostream>
#include <string>

using namespace std;
//classes
class arr
{
private:
    char *items;
    int size;
    int length;
public:
    //default constructor
    arr()
    {
        items = new char[100];
        size = 100;
        length = 0;
    }
    //constructor with parameters
    arr(int arraySize)
    {
        items = new char[arraySize];
        size = arraySize;
        length = 0;
    }
    //check if array is empty
    bool is_empty()
    {
        return length == 0 ? true : false;
    }
    //check if array full 
    bool is_full()
    {
        return length >= size - 1 ? true : false;
    }
    //returns array length
    int getLength()
    {
        return length;
    }
    //return array size
    int getSize()
    {
        return size;
    }
    //get array address
    char *getAddress()
    {
        return &items[0];
    }
    //fill number of items in array based on elementNum
    void fill(int elementNum)
    {
        char ch;
        cout << "Enter characters you want to add\n";
        if (elementNum > size - 1)
        {
            cout << "can't use elements number largger than " << size - 1;
            return;
        }

        for (int i = 0; i < elementNum; i++)
        {
            cin >> ch;
            insert(i, ch);
        }
    }
    //display all elements in the array
    void display()
    {
        if (is_empty())
        {
            cout << "there are no data /n";
            return;
        }
        cout << "Array items are :\n";
        for (int i = 0; i < length; i++)
        {
            cout << items[i] << "\t";
        }
        cout << endl;
    }
    void append(char ch)
    {
        insert(length, ch);
    }
    void insert(int index, char newItem)
    {
        if (is_full() || length<index || length + 1 == size)
        {
            cout << "\nSorry array is full\ncan't append letter more\n";
            return;
        }
        else {
            for (int i = length; i >= index; i--)
            {
                items[i + 1] = items[i];
            }
            items[index] = newItem;
            length++;
        }

    }
    int search(char ch)
    {
        if (!is_empty())
            for (int i = 1; i <= length; i++)
            {
                if (items[i] == ch)
                    return i;
            }
        return 0;
    }
    void del(int index)
    {
        if (is_empty() || length<index) {
            cout << "sorry can't delete item which is doesn't exist";
            return;
        }

        for (int i = index; i < length; i++)
        {
            items[i] = items[i + 1];
        }
        length--;
    }
    void changeSize(int newSize)
    {
        if (newSize <= length - 1)
        {
            cout << "can't change size because the new size is small";
            return;
        }
        char *tempItems = new char[newSize];
        size = newSize;
        for (int i = 0; i < length; i++)
        {
            tempItems[i] = items[i];
        }
        items = tempItems;
        tempItems = NULL;
    }
    //merge two arrays
    void merge(arr a)
    {
        this->size = this->size + a.getSize();
        for (int i = 0; i < a.getLength(); i++)
        {
            items[i + length] = a.getAddress()[i];
        }
        length = this->length + a.getLength();
    }

};

class stackUsingArray {
private:
    int top;
    arr a;
public:
    stackUsingArray()
    {
        top = -1;
    }
    stackUsingArray(int stackSize)
    {
        a.changeSize(stackSize);
        top = -1;
    }
    bool is_empty()
    {
        return(top == -1);
    }
    bool is_full()
    {
        return(a.is_full());
    }
    void push(char ch)
    {
        top++;
        a.append(ch);
    }
    char pop()
    {
        if (is_empty())
        {
            cout << "sorry the stack is empty";
        }
        else
            return a.getAddress()[top--];
    }
    int peekTop() {
        return top;
    }
    void display()
    {
        //first way of display

        for (int i = top; i >= 0; i--)
        {
            cout << a.getAddress()[i];
        }

        //second way of display

        //stackUsingArray c;
        //char ch;
        //for (int i = top; i >= 0; i--)
        //  c.push(this->pop());
        //for (int i = c.peekTop(); i >= 0; i--)
        //{
        //  ch=c.pop();
        //  this->push(ch);
        //  cout << ch;
        //}
    }
    int search(char ch)
    {
        for (int i = top; i >= 0; i--)
        {
            if (a.getAddress()[i] == ch)
                return i;
        }
        return -1;
    }
    char topDisplay()
    {
        return a.getAddress()[top];
    }

};
class stackUsingLinkedList {};

//functions
string infixToPostfix(string infix);
short prec(char ch);

int main()
{


    //infix and postfix

    stackUsingArray c;
    cout<<infixToPostfix("x+y-z");

    system("pause");
    return 0;
}


string infixToPostfix(string infix)
{
    string postfix = "";
    stackUsingArray sta;
    char ch;
    char test;

    for (int i = 0; i < infix.length(); i++)
    {
        switch (prec(infix[i]))
        {
        case 0:
            postfix.append(1, infix[i]);
            break;
        case 1:
            sta.push(infix[i]);
            break;
        //case 2:
        //  ch = sta.pop();
        //  while (ch != '(')
        //  {
        //      postfix.append(1, ch);
        //      ch = sta.pop();
        //  }
        //  break;
        case 3:

        //  if (sta.is_empty())
        //  {
        //      goto k270;
        //  }
        //  ch = sta.pop();
        //  while (prec(ch) > 3)
        //  {
        //      postfix.append(1, ch);
        //      if (sta.is_empty()) {
        //          //sta.push(infix[i]);
        //          goto k270;
        //      }
        //      ch = sta.pop();
        //  }
        //  sta.push(ch);
        //k270:
        //  sta.push(infix[i]);
            test = sta.topDisplay();
            if (sta.is_empty())
            {
                sta.push(infix[i]);
                test = sta.topDisplay();
            }

            else
            {
                ch = sta.pop();
                test = sta.topDisplay();
                if (prec(ch) >= 3)
                {
                    postfix += ch;
                }
                sta.push(infix[i]);

            }
        }


    }
    while (!sta.is_empty())
    {
        postfix.append(1, sta.pop());
    }

    return postfix;
}
short prec(char ch)
{
    if (ch == '(')
        return 1;
    if (ch == ')')
        return 2;
    if (ch == '+')
        return 3;
    if (ch == '-')
        return 3;
    if (ch == '*')
        return 4;
    if (ch == '/')
        return 4;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2025-02-03 11:44:40

多亏了@igortandetnik

thanks to @IgorTandetnik I figured out that I should del last item from the array when I pop from stack

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文