Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct Stack
- {
- string* m_pArr = nullptr;
- unsigned int m_iSize = 0;
- };
- Stack copy_stack(const Stack& otherStack)
- {
- Stack newStack;
- newStack.m_pArr = new std::string[otherStack.m_iSize];
- for (size_t i = 0; i < otherStack.m_iSize; i++)
- {
- newStack.m_pArr[i] = otherStack.m_pArr[i];
- }
- newStack.m_iSize = otherStack.m_iSize;
- return newStack;
- }
- void push(Stack& otherStack, const std::string& otherValue)
- {
- std::string* newArr = new std::string[otherStack.m_iSize + 1];
- for (size_t i = 0; i < otherStack.m_iSize; i++)
- {
- newArr[i] = otherStack.m_pArr[i];
- }
- newArr[otherStack.m_iSize] = otherValue;
- delete[] otherStack.m_pArr;
- otherStack.m_pArr = newArr;
- otherStack.m_iSize++;
- }
- void pop(Stack& otherStack)
- {
- std::string* newArr = new std::string[otherStack.m_iSize - 1];
- for (size_t i = 0; i < otherStack.m_iSize - 1; i++)
- {
- newArr[i] = otherStack.m_pArr[i];
- }
- delete[] otherStack.m_pArr;
- otherStack.m_pArr = newArr;
- otherStack.m_iSize--;
- }
- void clear(Stack& otherStack)
- {
- delete[] otherStack.m_pArr;
- otherStack.m_pArr = nullptr;
- otherStack.m_iSize = 0;
- }
- std::string& top(const Stack& otherStack)
- {
- return *(otherStack.m_pArr + otherStack.m_iSize - 1);
- }
- bool empty(const Stack& otherStack)
- {
- return !otherStack.m_iSize;
- }
- void output_stack(const Stack& otherStack)
- {
- for (size_t i = 0; i < otherStack.m_iSize; i++)
- {
- cout << otherStack.m_pArr[i] << " ";
- }
- }
- void foo1()
- {
- Stack stk1;
- Stack stk2;
- int size1;
- int size2;
- cout << "Enter first stack size: ";
- cin >> size1;
- for (int i = 0; i < size1; i++)
- {
- cout << "Elem " << i + 1 << ": ";
- int tempval;
- cin >> tempval;
- push(stk1, std::to_string(tempval));
- }
- cout << "Enter second stack size: ";
- cin >> size2;
- for (int i = 0; i < size2; i++)
- {
- cout << "Elem " << i + 1 << ": ";
- int tempval;
- cin >> tempval;
- push(stk2, to_string(tempval));
- }
- size1 *= (size1 > 0);
- size2 *= (size2 > 0);
- int* resultArr = new int[size1 + size2];
- for (int i = size1 - 1; i >= 0; i--)
- {
- resultArr[i] = stoi(top(stk1));
- pop(stk1);
- }
- for (int i = size1; i < size1 + size2; i++)
- {
- resultArr[i] = stoi(top(stk2));
- pop(stk2);
- }
- cout << "Result array: ";
- for (size_t i = 0; i < size1 + size2; i++)
- {
- std::cout << resultArr[i] << " ";
- }
- if (resultArr) delete[] resultArr;
- }
- int get_priority(char c) {
- int prior = 0;
- switch (c)
- {
- case '(':
- prior = 5;
- case ')':
- prior = 6;
- case '+':
- case '-':
- prior = 1;
- break;
- case '*':
- case '/':
- prior = 2;
- break;
- default:
- break;
- }
- return prior;
- }
- Stack get_polsk(const Stack& q)
- {
- Stack stk = copy_stack(q);
- Stack reverseQ;
- while (!empty(stk))
- {
- push(reverseQ, top(stk));
- pop(stk);
- }
- Stack result;
- while (!empty(reverseQ))
- {
- std::string value = top(reverseQ);
- pop(reverseQ);
- if (!isdigit(value.front()))
- {
- if (empty(stk))
- {
- if (value.front() != '(')
- {
- push(stk, value);
- }
- }
- else if ((get_priority(value.front()) >= get_priority(top(stk).front())))
- {
- if (value.front() == ')') {
- while (top(stk).front() != '(' && !empty(stk))
- {
- push(result, top(stk));
- pop(stk);
- }
- if (!empty(stk)) pop(stk);
- }
- else {
- if (value.front() != '(')
- {
- push(stk, value);
- }
- }
- }
- else
- {
- while (!empty(stk))
- {
- push(result, top(stk));
- pop(stk);
- }
- push(stk, value);
- }
- }
- else
- {
- push(result, value);
- }
- }
- while (!empty(stk))
- {
- push(result, top(stk));
- pop(stk);
- }
- return result;
- }
- Stack get_expr()
- {
- //(A*B-C)/D-F/E
- float A = 12;
- float B = 3;
- float C = 14;
- float D = 2;
- float E = 9;
- float F = 18;
- cout << "Expression: (A*B-C)/D-F/E" << std::endl;
- cout << "Enter A: ";
- cin >> A;
- cout << "Enter B: ";
- cin >> B;
- cout << "Enter C: ";
- cin >> C;
- cout << "Enter D: ";
- cin >> D;
- cout << "Enter E: ";
- cin >> E;
- cout << "Enter F: ";
- cin >> F;
- Stack exprStack;
- push(exprStack, "(");
- push(exprStack, std::to_string(A));
- push(exprStack, "*");
- push(exprStack, std::to_string(B));
- push(exprStack, "-");
- push(exprStack, std::to_string(C));
- push(exprStack, ")");
- push(exprStack, "/");
- push(exprStack, std::to_string(D));
- push(exprStack, "-");
- push(exprStack, std::to_string(F));
- push(exprStack, "/");
- push(exprStack, std::to_string(E));
- //std::string exprStr;
- //exprStr += "(" + std::to_string(A) + "*" + std::to_string(B) + "-" + std::to_string(C) + ")";
- //exprStr += "/" + std::to_string(D) + "-" + std::to_string(F) + "/" + std::to_string(E);
- return exprStack;
- }
- bool isOperator(const char ch)
- {
- if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
- return true;
- return false;
- }
- float operation(const float first, const float second, const char op)
- {
- if (op == '+')
- return first + second;
- else if (op == '-')
- return first - second;
- else if (op == '*')
- return first * second;
- else if (op == '/')
- return first / second;
- else
- return -1;
- }
- float get_result_by_polsk(const Stack& otherStack)
- {
- float result = 0;
- Stack temp_stk = copy_stack(otherStack);
- Stack temp_stk1;
- while (!empty(temp_stk))
- {
- push(temp_stk1, top(temp_stk));
- pop(temp_stk);
- }
- Stack temp_stk2;
- float first;
- float second;
- while (!empty(temp_stk1))
- {
- while (isdigit(top(temp_stk1).front()) && !empty(temp_stk1))
- {
- push(temp_stk2, top(temp_stk1));
- pop(temp_stk1);
- }
- char sign = 0;
- if (!empty(temp_stk1))
- {
- sign = top(temp_stk1).front();
- pop(temp_stk1);
- }
- second = stof(top(temp_stk2));
- pop(temp_stk2);
- first = stof(top(temp_stk2));
- pop(temp_stk2);
- result = operation(first, second, sign);
- if (result == -1)
- {
- clear(temp_stk1);
- clear(temp_stk2);
- break;
- }
- if (!empty(temp_stk1))
- {
- push(temp_stk1, std::to_string(result));
- }
- }
- return result;
- }
- int main()
- {
- foo1();
- cout << endl;
- cout << endl;
- //"(6*7-3)/2-18/9"
- Stack startStk = get_expr();
- cout << "\nStart expression: ";
- output_stack(startStk);
- cout << endl;
- Stack pstk = get_polsk(startStk);
- cout << "\nPost form expression: ";
- output_stack(pstk);
- cout << "\nResult: " << get_result_by_polsk(pstk) << endl;
- clear(startStk);
- clear(pstk);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment