-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cpp
More file actions
152 lines (135 loc) · 2.78 KB
/
Helpers.cpp
File metadata and controls
152 lines (135 loc) · 2.78 KB
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
#include "./Helpers.h"
#include <string>
#include <regex>
#include <cctype>
using namespace gcalc;
using namespace std;
bool gcalc::is_not_reserved_word(std::string word)
{
std::string chars[8] = {"print", "delete", "save", "load", "who", "reset", "delete", "quit"};
for (int i = 0; i < 8; i++)
{
if (word == chars[i])
{
return false;
}
}
return true;
}
bool gcalc::is_alpha(std::string word)
{
for (unsigned int i = 0; i < word.size(); i++)
{
if (!isalpha(word[i]))
{
return false;
}
}
return true;
}
bool gcalc::is_alphanumeric(std::string word)
{
for (unsigned int i = 0; i < word.size(); i++)
{
if (!isalnum(word[i]))
{
return false;
}
}
return true;
}
bool gcalc::areParanthesisBalanced(std::string expr)
{
stack<char> s;
char x;
for (unsigned int i = 0; i < expr.length(); i++)
{
if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{')
{
s.push(expr[i]);
continue;
}
if (s.empty())
return false;
switch (expr[i])
{
case ')':
x = s.top();
s.pop();
if (x == '{' || x == '[')
return false;
break;
case '}':
x = s.top();
s.pop();
if (x == '(' || x == '[')
return false;
break;
case ']':
x = s.top();
s.pop();
if (x == '(' || x == '{')
return false;
break;
}
}
return (s.empty());
}
std::string gcalc::strip_to_parantheiss_only(std::string expr, char open, char close)
{
std::string out = expr;
int j = 0;
for (unsigned int i = 0; i < expr.size(); i++)
{
if (expr[i] != open && expr[i] != close)
{
out.replace(i - j, 1, "");
j++;
}
}
return out;
}
std::string gcalc::char_to_string(char c)
{
char temp[2];
temp[0] = c;
temp[1] = '\0';
std::string s = temp;
return s;
}
int gcalc::findClosingParen(std::string text, int openPos)
{
int closePos = openPos;
int counter = 1;
while (counter > 0)
{
char c = text[++closePos];
if (c == '(')
{
counter++;
}
else if (c == ')')
{
counter--;
}
}
return closePos;
}
int gcalc::findClosingBracket(std::string text, int openPos)
{
int closePos = openPos;
int counter = 1;
while (counter > 0)
{
char c = text[++closePos];
if (c == '[')
{
counter++;
}
else if (c == ']')
{
counter--;
}
}
return closePos;
}