-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
177 lines (135 loc) · 4.04 KB
/
utils.hpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef UTILS_HPP
#define UTILS_HPP
#include <vector>
#include <string>
#include <sstream>
#include "variable.hpp"
#include <stack>
#include <queue>
string queueNth(const std::queue<string>& originalQueue, int n) {
if (n < 1 || n > originalQueue.size()) {
std::cerr << "Invalid value of n." << std::endl;
return "Not found"; // Return an error value
}
std::queue<string> copyQueue = originalQueue;
// Dequeue elements from the copy queue until the nth element is found
for (int i = 1; i < n; i++) {
copyQueue.pop();
}
// The nth element is now at the front of the copy queue
return copyQueue.front();
}
void insertAtIndex(std::queue<std::string>& q, int index, const std::string& element) {
std::queue<std::string> tempQueue;
// Pop elements from the original queue and push them into the temporary queue
while (index > 0 && !q.empty()) {
tempQueue.push(q.front());
q.pop();
index--;
}
// Insert the new element at the specified index
tempQueue.push(element);
// Push the remaining elements from the temporary queue back into the original queue
while (!tempQueue.empty()) {
q.push(tempQueue.front());
tempQueue.pop();
}
}
std::vector<std::string> tokenize(std::string input){
std::istringstream iss(input);
std::vector<std::string> tokens;
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
return tokens;
}
bool check_number(std::string str) {
for (int i = 0; i < str.length(); i++)
if (isdigit(str[i]) == false)
return false;
return true;
}
int isVariable(string name, vector<Variable> vars){
for(Variable &v : vars){
if(v.getName().compare(name)==0){
return 1;
}
}
return 0;
}
Variable *findVariable(string name, vector<Variable> vars){
for(Variable &v : vars){
if(v.getName().compare(name)==0){
return &v;
}
}
return NULL;
}
int indexOf(string w, vector<string> words){
for(int i = 0; i< words.size(); i++){
if(words[i].compare(w)==0){
return i;
}
}
return -1;
}
int findNthFromTop(std::stack<int> st, int n) { // AI generated
if ( n > st.size() || n<1) {
std::cerr << "Invalid value of n or stack size." << std::endl;
return -1; // Return an error value
}
std::vector<int> tempVec;
// Pop elements from the stack and store them in a temporary vector
while (!st.empty()) {
tempVec.push_back(st.top());
st.pop();
}
// Get the n-th element from the top
int nthElement = tempVec[n];
// Restore the original stack
for (int i = tempVec.size() - 1; i >= 0; i--) {
st.push(tempVec[i]);
}
return nthElement;
}
stack<int> reverseStack(stack<int> s) {
stack<int> reversed;
while (!s.empty()) {
int element = s.top();
s.pop();
reversed.push(element);
}
return reversed;
}
void removeAtIndex(std::queue<std::string>& q, int index) {
if (index < 0 || index >= q.size()) {
// Invalid index, nothing to remove
return;
}
std::queue<std::string> tempQueue;
// Pop elements from the original queue and push them into the temporary queue, skipping the element at the specified index
for (int i = 0; i < q.size(); i++) {
if (i != index) {
tempQueue.push(q.front());
}
q.pop();
}
// Copy elements back from the temporary queue to the original queue
while (!tempQueue.empty()) {
q.push(tempQueue.front());
tempQueue.pop();
}
}
void deleteLast(std::queue<std::string>& strQueue) {
if (!strQueue.empty()) {
int size = strQueue.size();
for (int i = 0; i < size - 1; i++) {
std::string frontElement = strQueue.front();
strQueue.pop();
strQueue.push(frontElement);
}
strQueue.pop(); // Remove the last element (newest)
}
}
#endif