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
|
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
string calcGoal(float in1,float in2,float in3,float in4,int goal){
string result;
float n[]={in1,in2,in3,in4};
float a=0; // a through d are used to iterate over n[]
float b=0;
float c=0;
float d=0;
int f=0; // f through h are used to permute the three operations between in1, in2, in3, and in4
int g=0;
int h=0;
for (a, a<4., a++){
for (b, b<4., b++){
if (b==a) {continue;}
for (c, c<4., c++){
if ((c==a) || (c==b)) {continue;}
d=6-(a+b+c);
for (f, f,5, f++){
for (g, g<5, g++){
for (h, h<5, h++){
for ( int assoc=1, assoc<6, assoc++){
if (evaluate(n[a],n[b],n[c],n[d],f,g,h,assoc)==goal) {result=formated(n[a],n[b],n[c],n[d],f,g,h,assoc)+"\n";}
}
}
}
}
}
}
}
return result;
}
float evaluate(float in1,float in2,float in3,float in4,int oper1,int oper2,int oper3, int assoc){
if (assoc==1){ // (1 o 2) 2 (3 o 4)
return eval(eval(in1,in2,in3,oper1),eval(in3,in4,oper3),oper2);
} else if (assoc==2){ // ((1 o 2) o 3) o 4
return eval(eval(eval(in1,in2,oper1),in3,oper2),in4,oper3);
} else if (assoc==3){ // ((1 o (2 o 3)) o 4
return eval(eval(in1,eval(in2,in3,oper2),oper1),in4,oper3);
} else if (assoc==4){ // 1 o (2 o (3 o 4))
return eval(in1,eval(in2,eval(in3,in4,oper3),oper2)oper1);
} else if (assoc==5){ // 1 o ((2 o 3) o 4)
return eval(in1,eval(eval(in2,in3,oper2),in4,oper3),oper1);
} else { return; }
}
string formated(float in1,float in2,float in3,float in4,int oper1,int oper2,int oper3, int assoc){
if (assoc==1){ return "("+in1+form_oper(oper1)+in2")"+form_oper(oper2)+"("+in3+form_oper(oper3)+in4+")";
} else if (assoc==2){ return "(("+in1+form_oper(oper1)+in2+")"+form_oper(oper2)+in4;
} else if (assoc==3){ return "("+in1+form_oper(oper1)+"("+in2+form_oper(oper2)+in3+")"+")"+form_oper(oper3)+in4;
} else if (assoc==4){ return in1+form_oper(oper1)+"("+in2+form_oper(oper2)+"("+in3+form_oper(oper3)+in4+"))";
} else if (assoc==5){ return in1+form_oper(oper1)+"(("+in2+form_oper(oper2)+in3+")"+form_oper(oper3)+in4+")";
} else { return; }
}
float eval(float in1,float in2,int oper){
switch (oper){
case 0:
return in1 + in2;
break;
case 1:
return in1 - in2;
break;
case 2:
return in1 * in2;
break;
case 3:
return in1 / in2;
break;
case 4:
return pow(in1,in2);
break;
}
}
string form_oper(int oper){
switch (oper){
case 0:
return '+';
break;
case 1:
return '-';
break;
case 2:
return '*';
break;
case 3:
return '/';
break;
case 4:
return '^';
break;
}
}
int main(){
/* Given the numbers inputted by the user, find how to make a chosen goal number using
* addition, subtraction, multiplication, division, and exponents. */
cout << "Enter goal number: ";
cin << int goal;
cout << "Enter 4 numbers to construct <goal> with: ";
float in[];
for ( int i=0, i<4, i++ ){
cin << in[i];
}
cout << calcGoal(in[0],in[1],in[2],in[3],goal);
return 0;
}
|