-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount24.cpp
155 lines (142 loc) · 2.78 KB
/
count24.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
#include <iostream>
#include <stack>
#include <vector>
#include <iterator>
#include <string>
#include <sstream>
#include <assert.h>
#include <math.h>
#include <float.h>
using namespace std;
char ops[] = { '+', '-', '*', '/'};
//------
class Exp
{
Exp* subExpl;
Exp* subExpr;
char op;
float value;
public:
float getvalue()
{
return value;
}
Exp(){};
Exp(float val)
{
value = val;
subExpl = subExpr = NULL;
op = 0;
}
Exp(Exp* expL, Exp* expR, char op_in)
{
subExpl = expL;
subExpr = expR;
op = op_in;
switch (op){
case '+':
value = expL->value + expR->value;
break;
case '-':
value = expL->value - expR->value;
break;
case '*':
value = expL->value * expR->value;
break;
case '/':
if (expR->value == 0)
value == FLT_MAX;
else
value = expL->value / expR->value;
break;
default:
assert(false);
}
}
string toString()
{
if (subExpl && subExpr)
{
return string("(") + subExpl->toString() + op + subExpr->toString() + string(")");
}
else
{
stringstream ss;
ss << value;
return ss.str();
}
}
};
//return false if dived by 0
bool mergeexps(Exp* const exps, int size, int lindex, int rindex, char op, Exp* outexps)
{
if (exps[rindex].getvalue()==0 && op=='/') return false;
Exp* first = new((void*)&(outexps[0]))Exp(&(exps[lindex]), &(exps[rindex]), op);
for (int i = 0; i < size; i++)
{
if (i == lindex || i == rindex)
continue;
*(++outexps) = exps[i];
}
return true;
}
void calcnew(Exp* exps, int expsnum)
{
if (expsnum == 1){
if (fabs(exps->getvalue() - 24) < 0.0001)
{//get the expected result, output the expression
string a;
cout << exps->toString();
cout << endl;
}
return;
}
//caculate 2 exps to 1 exp
Exp* merged_exps = new Exp[expsnum - 1];
for (int i = 0; i < expsnum; i++)
{
for (int j = i + 1; j < expsnum; j++)
{
for (int k = 0; k < sizeof(ops) / sizeof(char); k++)
{
//skip the condition div by 0
if (!mergeexps(exps, expsnum, i, j, ops[k], merged_exps))
continue;
calcnew(merged_exps, expsnum - 1);
if (ops[k] == '-' || ops[k] == '/')
{
//swap the left and right
if (!mergeexps(exps, expsnum, j, i, ops[k], merged_exps))
continue;
calcnew(merged_exps, expsnum - 1);
}
}
}
}
delete merged_exps;
}
void test_cacunew()
{
while (1){
cout << "please input numbers to be caculated, seprated by space, the dest value is 24." << endl;
string str;
getline(cin, str);
stringstream ss(str);
vector<Exp> vec;
int Integer;
while (true)
{
ss >> Integer;
if (!ss)
break;
Exp exp(Integer);
vec.push_back(exp);
}
calcnew(vec.data(), vec.size());
}
}
//----------
int main()
{
test_cacunew();
}