This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_applications.h
145 lines (125 loc) · 3.34 KB
/
stack_applications.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Created by vicky on 10-12-2023.
//
#ifndef LAB_STACK_APPLICATIONS_H
#define LAB_STACK_APPLICATIONS_H
#include "include/Stack/stack.h"
int __pred(char op){
if(op == '^')
return 2;
if(op == '*' || op == '/')
return 1;
if(op == '+' || op == '-')
return 0;
return -1;
}
char __match(char bracket){
if (bracket == '(')
return ')';
else if (bracket == '[')
return ']';
else if (bracket == '{')
return '}';
else
return '\0';
}
bool __is_bracket(char symbol){
if (symbol == '(' || symbol == '[' || symbol == '{' || symbol == ')' || symbol == ']' || symbol == '}')
return true;
return false;
}
std::string postfix(std::string expr){
if(expr.length() == 0)
return "";
std::string output = "";
Stack<char> stack;
for(int i = 0; i < expr.length(); ++i){
char c = expr[i];
if(c >= 'a' && c <= 'z')
output += c;
else if (c == '('){
stack.push(c);
}
else if(c == ')'){
while(stack.get() != '(')
output += stack.pop();
stack.pop();
}
else if(c == '^' || c == '*' || c == '/' || c == '+' || c == '-') {
while(!stack.isEmpty() && __pred(c) <= __pred(stack.get())){
output += stack.pop();
}
stack.push(c);
}
else{
std::cout << "Invalid character " << c << "\na-z ()+-^*/ are valid characters" << std::endl;
}
}
while(!stack.isEmpty()){
if(stack.get() == '('){
std::cout << "Invalid Expression" << std::endl;
return "";
}
output += stack.pop();
}
std::cout << output << std::endl;
return output;
}
void infix(std::string expr){
Stack<std::string> stack;
for(int i = 0; i < expr.length(); ++i){
char c = expr[i];
if(c >= 'a' && c <= 'z'){
std::string str = std::string(1,c);
stack.push(str);
}
else if(c == '^' || c == '*' || c == '/' || c == '+' || c == '-'){
std::string op2 = stack.pop();
std::string op1 = stack.pop();
std::string binaryOp = "(" + op1 + c + op2 + ")";
stack.push(binaryOp);
}
}
std::cout << stack.pop() << std::endl;
}
bool bracketMatching(std::string expr){
Stack<char> stack;
for(auto _char:expr){
if(!__is_bracket(_char)){
continue;
}
if (_char == '(' || _char == '[' || _char == '{'){
stack.push(_char);
}
else{
if(__match(stack.get()) == _char){
stack.pop();
}
else{
std::cout << "Was expecting " << __match(stack.get()) << " but got " << _char << std::endl;
return false;
}
}
}
return true;
}
template <typename T>
Stack<T>* stackSort(Stack<T> s){
const int tarSize = s.getStackSize();
Stack<T>* s1 = new Stack<T>;
T tmp;
tmp = s.pop();
s1 -> push(tmp);
tmp = s.pop();
while(s1 -> getStackSize() != tarSize){
if(s1 -> isEmpty() || tmp >= s1 -> get()){
s1 -> push(tmp);
tmp = s.pop();
}
else{
s.push(s1 -> pop());
}
}
return s1;
}
#endif //LAB_STACK_APPLICATIONS_H