Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.6 kB
2
Indexable
Never
#include <iostream>

class Stack
{
public:
    Stack()
    {
        size = 20;
        items = new char[size];
        top_index = -1;
    }

    ~Stack()
    {
        delete[] items;
    }

    void push(char value)
    {
        if (!full())
        {
            ++top_index;
            items[top_index] = value;
        }
        else
        {
            std::cout << "Stack is full!" << std::endl;
            return;
        }
    }

    char pop()
    {
        if (!empty())
        {
            char popped = items[top_index];
            --top_index;
            return popped;
        }
        else
        {
            std::cout << "Stack is empty!" << std::endl;
        }
    }

    char top()
    {
        if (!empty())
        {
            return items[top_index];
        }
        else
        {
            std::cout << "Stack is empty!" << std::endl;
        }
    }

    bool empty() const
    {
        return top_index == -1;
    }

    bool full() const
    {
        return top_index == size - 1;
    }

private:
    char* items;
    int top_index;
    int size;
};

bool isOperator(char c)
{
    return (c == '+' || c == '-' || c == '*' || c == '/' || c == 'N' || c == 'I');
}

int getPriority(char op)
{
    if (op == 'N' || op == 'I')
    {
        return 3;
    }
    else if (op == '*' || op == '/')
    {
        return 2;
    }
    else if (op == '+' || op == '-')
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void infixToPostfix(const char* infix, char* postfix)
{
    Stack operatorStack;
    int k = 0;

    for (int i = 0; i < infix[i]; ++i)
    {
        if (infix[i] == ' ' || infix[i] == ',')
        {
            continue;
        }
        else if (isdigit(infix[i]))
        {
            postfix[k++] = infix[i];
        }
        else if (infix[i] == '(')
        {
            operatorStack.push(infix[i]);
        }
        else if (infix[i] == ')')
        {
            while (!operatorStack.empty() && operatorStack.top() != '(')
            {
                postfix[k++] = operatorStack.top();
                operatorStack.pop();
            }
            operatorStack.pop();
            postfix[k++] = ' ';
        }
        else if (isOperator(infix[i]))
        {
            postfix[k++] = ' ';
            while (!operatorStack.empty() && getPriority(operatorStack.top()) >= getPriority(infix[i]))
            {
                postfix[k++] = operatorStack.top();
                operatorStack.pop();
            }
            operatorStack.push(infix[i]);
        }
    }

    while (!operatorStack.empty())
    {
        postfix[k++] = operatorStack.top();
        operatorStack.pop();
    }

    postfix[k] = '\0';
}

int calculateOperation(int a, int b, int c, char op)
{
    switch (op)
    {
    case '+':
        return a + b;
    case '-':
        return a - b;
    case '*':
        return a * b;
    case '/':
        if (b == 0)
        {
            std::cout << "ERROR";
        }
        else
        {
            return a / b;
        }
    case 'N':
        return -b;
    case 'I':
        if (a > 0)
        {
            return b;
        }
        else
        {
            return c;
        }
    default:
        return 0;
    }
}

int evaluateExpression(char* postfix)
{
    Stack operandStack;

    for (int i = 0; postfix[i]; ++i)
    {
        if (postfix[i] == ' ' || postfix[i] == ',')
        {
            continue;
        }
        else if (isdigit(postfix[i]))
        {
            int operand = 0;
            while (isdigit(postfix[i]))
            {
                operand = operand * 10 + (postfix[i] - '0');
                ++i;
            }
            --i;
            operandStack.push(operand);
        }
        else if (isOperator(postfix[i]))
        {
            if (postfix[i] == 'N') 
            {
                int operand3 = operandStack.top();
                operandStack.pop();
                int result = calculateOperation(0, operand3, 0, postfix[i]);
                operandStack.push(result);
            }
            else if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/')
            {
                int operand2 = operandStack.top();
                operandStack.pop();
                int operand1 = operandStack.top();
                operandStack.pop();
                int result = calculateOperation(operand1, operand2, 0, postfix[i]);
                operandStack.push(result);
            }
            else if (postfix[i] == 'I')
            {
                int operand4 = operandStack.top();
                operandStack.pop();
                int operand5 = operandStack.top();
                operandStack.pop();
                int operand6 = operandStack.top();
                operandStack.pop();
                int result = calculateOperation(operand4, operand5, operand6, postfix[i]);
                operandStack.push(result);
            }
        }
    }

    return operandStack.top();
}

int main()
{
    char expression[] = "N 12 + 2 * ( 3 * 4 + 10 / 5 ).";

    char postfix[50];
    infixToPostfix(expression, postfix);

    std::cout << postfix << std::endl;

    int result = evaluateExpression(postfix);
    std::cout << "Wynik: " << result << std::endl;


    return 0;
}
Leave a Comment