forked from subailong/datalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.cpp
160 lines (142 loc) · 3.7 KB
/
expr.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
//
// expr.cpp
// datalgo
//
// Created by Iyed Bennour on 19/11/14.
// Copyright (c) 2014 Iyed Bennour. All rights reserved.
//
#include <sstream>
#include <iostream>
#include "expr.h"
std::string opToString(Operation op) {
switch (op) {
case Operation::ADD:
return std::string("+");
break;
case Operation::DIV:
return std::string("/");
break;
case Operation::MINUS:
return std::string("-");
break;
case Operation::MULT:
return std::string("*");
break;
default:
break;
}
return std::string("");
}
BinExpr::BinExpr(std::shared_ptr<IExpr>& _exprL, std::shared_ptr<IExpr>& _exprR, Operation _op): op(_op), exprL(_exprL), exprR(_exprR) {
}
int BinExpr::eval() {
int res = 0;
switch (op) {
case Operation::ADD:
res = exprL->eval() + exprR->eval();
break;
case Operation::MINUS:
res = exprL->eval() - exprR->eval();
break;
case Operation::MULT:
res = exprL->eval() * exprR->eval();
break;
case Operation::DIV:
res = exprL->eval() / exprR->eval();
break;
default:
break;
}
return res;
}
TerminalExpr::TerminalExpr(int val) : value(val) {}
int TerminalExpr::eval() {
return value;
}
std::string TerminalExpr::toString() {
std::stringstream ss;
ss << value;
return ss.str();
}
std::string BinExpr::toString() {
std::stringstream ss;
ss << "(" << exprL->toString() << " " << opToString(op) << " " << exprR->toString() << ")";
return ss.str();
}
std::shared_ptr<IExpr> IExpr::create(int value) {
return std::shared_ptr<IExpr>(new TerminalExpr(value));
}
std::shared_ptr<IExpr> IExpr::create(std::shared_ptr<IExpr>& l, std::shared_ptr<IExpr>& r, Operation op){
if (op == Operation::MINUS) {
if (l->eval() < r->eval()) {
return nullptr;
}
else if (l->eval() == 0 || r->eval() == 0) {
return nullptr;
}
else
{
return std::shared_ptr<IExpr>(new BinExpr(l, r, op));
}
}
else if (op == Operation::DIV){
if (r->eval() == 0) {
return nullptr;
}
else if (r->eval() == 1) {
return nullptr;
}
else if (l->eval() % r->eval() == 0) {
return std::shared_ptr<IExpr>(new BinExpr(l, r, op));
}
else
{
return nullptr;
}
}
else if (op == Operation::ADD) {
if (l->eval() == 0 || r->eval() == 0)
return nullptr;
return std::shared_ptr<IExpr>(new BinExpr(l, r, op));
}
else if (op == Operation::MULT) {
if (l->eval() == 1 || r->eval() == 1)
return nullptr;
else
return std::shared_ptr<IExpr>(new BinExpr(l, r, op));
}
else {
return nullptr;
}
}
std::vector<std::shared_ptr<IExpr>> find_solution(std::vector<std::shared_ptr<IExpr>> exprs, std::shared_ptr<IExpr> sofar, int target) {
std::vector<std::shared_ptr<IExpr>> solutions;
if (sofar->eval() == target) {
solutions.push_back(sofar);
}
for (int i = 0; i < exprs.size(); i++) {
for (int j = i + 1; j < exprs.size(); j++) {
for (int k = 0; k < 4; k++) {
// combine
auto new_expr = IExpr::create(exprs[i], exprs[j], ops[k]);
if (!new_expr)
new_expr = IExpr::create(exprs[j], exprs[i], ops[k]);
//prepare the input for the next recursive call
if (new_expr) {
decltype(exprs) new_exprs;
new_exprs.push_back(new_expr);
for (int p = 0; p < exprs.size(); p++) {
if (p != i && p != j)
new_exprs.push_back(exprs[p]);
}
//recursive call
auto res = find_solution(new_exprs, new_expr, target);
for (auto e: res) {
solutions.push_back(e);
}
}
}
}
}
return solutions;
}