-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.cpp
218 lines (173 loc) · 5.32 KB
/
generator.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <bits/stdc++.h>
using namespace std;
//from here is where we need to check
int critical_equal = 0;
//the actual target
int target = 0;
bool isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '=';
}
// Function to get the precedence of an operator
int getPrecedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
// Function to perform the operation based on the operator
int performOperation(int num1, int num2, char op) {
switch (op) {
case '+': return num1 + num2;
case '-': return num1 - num2;
case '*': return num1 * num2;
case '/': return num1 / num2;
default: return 0; // Invalid operator
}
}
int evaluateExpression(const string& expr) {
stack<int> operands;
stack<char> operators;
stringstream ss(expr);
char ch;
while (ss >> ch) {
if (isdigit(ch)) {
// Process numbers
int num;
ss.putback(ch);
ss >> num;
operands.push(num);
} else if (isOperator(ch)) {
// Process operators
while (!operators.empty() && isOperator(operators.top())) {
int precedence1 = getPrecedence(ch);
int precedence2 = getPrecedence(operators.top());
if (precedence1 <= precedence2) {
char op = operators.top();
operators.pop();
if (operands.size() < 2) {
cerr << "Invalid expression: Not enough operands for operator " << op << endl;
return 0; // Return 0 for invalid expressions
}
int num2 = operands.top();
operands.pop();
int num1 = operands.top();
operands.pop();
int result = performOperation(num1, num2, op);
operands.push(result);
} else {
break;
}
}
operators.push(ch);
} else {
// Invalid character found
cerr << "Invalid character in the expression: " << ch << endl;
return 0; // Return 0 for invalid expressions
}
}
while (!operators.empty()) {
char op = operators.top();
operators.pop();
if (operands.size() < 2) {
cerr << "Invalid expression: Not enough operands for operator " << op << endl;
return 0; // Return 0 for invalid expressions
}
int num2 = operands.top();
operands.pop();
int num1 = operands.top();
operands.pop();
int result = performOperation(num1, num2, op);
operands.push(result);
}
if (operands.size() != 1) {
cerr << "Invalid expression: Too many operands" << endl;
return 0; // Return 0 for invalid expressions
}
return operands.top(); // Return the final result
}
bool validate(string& current){
bool flag_has_operators = false;
//here we find the equal sing pos
int where_equal = current.find("=");
//if the sign is less than our critical sign, its bad math
if(where_equal<critical_equal) return false;
//if it end with that sign, its bad math
if(current[target-1]=='=' )return false;
if(current[0]=='+')return false;
if(current[0]=='-')return false;
if(current[target-1]=='-')return false;
//left es right y right is left
string right_side = "";
string left_side = "";
for (int i = where_equal+1; i < target; ++i)
{
if (isOperator(current[i]))
{
if ((current[i]!='-'))
{
return false;
}
if (i != (where_equal+1))
{
return false;
}
}
left_side += current[i];
}
if (left_side.length()>1 and left_side[0]=='0')
{
return false;
}
if (left_side[0]=='-' and left_side[1]=='0')
{
return false;
}
for (int i = 0; i < where_equal; ++i)
{
if (isOperator(current[i]) && current[i+1]=='0' && !isOperator(current[i+2]))return false;
right_side += current[i];
}
if (right_side[0]=='0' && !isOperator(right_side[1]))return false;
if ((target%2==1) and where_equal==critical_equal)
{
if (left_side.length()!=right_side.length())
{
return false;
}
}
int ans = evaluateExpression(right_side);
int left_ans = stoi(left_side);
//here im gonna check for the things and the tangs
return ans == left_ans;
}
void printAllKLengthRec(char set[], string prefix,
int n, int k)
{
if (k == 0)
{
if (validate(prefix))cout <<(prefix) << endl;
return;
}
for (int i = 0; i < n; i++)
{
string newPrefix;
char prev = prefix[prefix.size()-1];
char next = set[i];
if(prev=='=' && next == '-');
else if((prev == '-' or prev == '+' ) and (next == '+' or next == '='))return;
newPrefix = prefix + set[i];
printAllKLengthRec(set, newPrefix, n, k - 1);
}
}
void printAllKLength(char set[], int k,int n)
{
printAllKLengthRec(set, "", n, k);
}
// Driver Code
int main()
{
cout << "First Test" << endl;
char set1[] = {'1','2','3','4','5','6','7','8','9','0','+','-','='};
target = 8;
critical_equal = target/2;
printAllKLength(set1, target, 13);
}