-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
165 lines (139 loc) · 3.37 KB
/
utils.cpp
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
#include "utils.h"
bool utils::check_characters_within_range(std::string* Expression)
{
for (int i = 0; i < Expression->length(); i++)
{
//if expression char isnt: within uppercase ASCII Latin block A..Z, integers 0, 1
//and is not equal to ( ) ' + \n
if (((*Expression)[i] < 65 || (*Expression)[i] > 90)
&& (*Expression)[i] != '0' && (*Expression)[i] != '1'
&& (*Expression)[i] != '(' && (*Expression)[i] != ')'
&& (*Expression)[i] != '\'' && (*Expression)[i] != ' '
&& (*Expression)[i] != '+' && (*Expression)[i] != '\n')
{
std::cout << "invalid Syntax";
return false;
}
}
}
bool utils::check_parentheses(std::string* Expression)
{
std::stack<char> ParenthesesStack;
for (int i = 0; i < Expression->length(); i++)
{
//checks for empty parentheses () or open parenthesis followed by ' +
if (i != Expression->length() && (*Expression)[i] == '(' && ((*Expression)[i + 1] == ')' || (*Expression)[i + 1] == '\''|| (*Expression)[i + 1] == '+'))
{
return false;
}
//checks for valid parentheses
if ((*Expression)[i] == '(')
{
ParenthesesStack.push('(');
}
else if (ParenthesesStack.empty() && (*Expression)[i] == ')')
{
return false;
}
else if((*Expression)[i] == ')')
{
ParenthesesStack.pop();
}
}
if (!ParenthesesStack.empty())
{
return false;
}
return true;
}
int utils::verify_expression_syntax(std::string* Expression)
{
return check_characters_within_range(Expression) && check_parentheses(Expression);
}
std::string* utils::decimal_to_binary(int Dec, int Bits)
{
std::string* Bin = new std::string;
for (int i = 0; Dec > 0; i++)
{
*Bin = std::to_string(Dec % 2) + *Bin;
Dec /= 2;
}
int temp = (*Bin).length();
for (int i = 0; i < Bits - temp; i++)
{
*Bin = '0' + *Bin;
}
return Bin;
}
std::set<char>* utils::unique_literals(std::string* Expression)
{
std::set<char> *_uniqueLiterals = new std::set<char>;
for(int i = 0; i < Expression->length(); i++)
{
if ((*Expression)[i] >= 65 && (*Expression)[i] <= 90)
{
_uniqueLiterals->insert((*Expression)[i]);
}
}
return _uniqueLiterals;
}
normalizedString* utils::parse_string(std::string* Expression) //simple parser for minterms (for now), for any Boolean expression we'd need a infix to RND parser
{
normalizedString* RetStr = new normalizedString();
std::string temp;
for (int i = 0; i < Expression->length(); i++) //iterates on string and removes spaces
{
if ((*Expression)[i] != ' ')
temp += (*Expression)[i];
}
*Expression = temp;
std::istringstream Stream(*Expression);
std::string s;
while (std::getline(Stream, s, '+')) //normalizes string using delimiter +
{
RetStr->push_back(s);
}
return RetStr;
}
int utils::minterm_to_binary(std::string Minterm)
{
int x = 0;
for (int i = 0; i < Minterm.size(); i++)
{
x <<= 1; //shifting bit to its correct bit position depending on i
if (i != Minterm.size() && Minterm[i + 1] != '\'') //skips not operators
{
x |= 1; //setting LSB bit
}
else
{
i++; //part of skipping not op
}
}
return x;
}
int utils::count_bits(int x)
{
int count = 0;
for (int i = 0; i < 32; i++) //looping on default 32 bit integer type
{
if (x & 1 << i) //checking LSB positions from 0 to 32th bit
{
count++;
}
}
return count;
}
int utils::get_bit_position(int x)
{
int index = 0;
for (int i = 0; i < 32; i++)
{
if (x & 1 >> i)
{
index = i;
break;
}
}
return index;
}