Last active
October 24, 2015 10:21
-
-
Save wwylele/296d7406b78bf595af37 to your computer and use it in GitHub Desktop.
A stupid searcher for expressions containing specified numbers and resulting in specified number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <cmath> | |
#include <string> | |
#include <sstream> | |
using namespace std; | |
bool next_operatorPostion(int* operatorPostion,int numberCount){ | |
int counter = numberCount-2; | |
while(1){ | |
if(counter==-1)return false; | |
if(operatorPostion[counter]!=numberCount-1){ | |
++operatorPostion[counter]; | |
break; | |
} | |
else{ | |
operatorPostion[counter] = counter+1; | |
--counter; | |
} | |
} | |
for(int i = 1; i<numberCount-1; i++){ | |
if(operatorPostion[i]<operatorPostion[i-1]) | |
operatorPostion[i] = operatorPostion[i-1]; | |
} | |
return true; | |
} | |
bool next_operatorDel(int* operatorDel,int numberCount){ | |
int counter = numberCount-2; | |
while(1){ | |
if(counter==-1)return false; | |
if(operatorDel[counter]!=4){ | |
++operatorDel[counter]; | |
break; | |
} | |
else{ | |
operatorDel[counter] = 0; | |
--counter; | |
} | |
} | |
return true; | |
} | |
char op[] = { '+','-','*','/','^' }; | |
int opl[] = { 0,0,1,1,2 }; | |
int main(){ | |
int numberCount; | |
cout<<"How many numbers?"<<endl; | |
cin>>numberCount; | |
double* numbers = new double[numberCount]; | |
int* numberPermutation = new int[numberCount]; | |
int* operatorPostion = new int [numberCount-1];//each operator is located after which number | |
//operatorPostion[k]>=max(operatorPostion[k-1],k+1) | |
int* operatorDel = new int [numberCount-1];//each operatorDel can be 0,1,2,3,4 | |
double* stack = new double [numberCount]; | |
struct expPair{ | |
string exp; | |
int del; | |
}; | |
expPair *outStack = new expPair[numberCount]; | |
for(int i = 0; i<numberCount; i++){ | |
numberPermutation[i] = i; | |
if(i!=numberCount-1){ | |
operatorPostion[i] = i+1; | |
operatorDel[i] = 0; | |
} | |
} | |
cout<<"Please enter each number:"<<endl; | |
for(int i = 0; i<numberCount; i++){ | |
cin>>numbers[i]; | |
} | |
cout<<"What result do you want?"<<endl; | |
double result; | |
cin>>result; | |
cout<<"I am searching..."<<endl; | |
do{//for each number permutation | |
do{//for each operator postion | |
do{//for each operator combination | |
double* stack_head = stack; | |
int currentPosition = 0; | |
int nextOperatorIndex = 0; | |
while(1){ | |
*(stack_head++) = numbers[numberPermutation[currentPosition]]; | |
while(operatorPostion[nextOperatorIndex]==currentPosition){ | |
double k = *(--stack_head); | |
switch(operatorDel[nextOperatorIndex]){ | |
case 0: | |
*(stack_head-1) += k; | |
break; | |
case 1: | |
*(stack_head-1) -= k; | |
break; | |
case 2: | |
*(stack_head-1) *= k; | |
break; | |
case 3: | |
*(stack_head-1) /= k; | |
break; | |
case 4: | |
*(stack_head-1) = pow(*(stack_head-1),k); | |
break; | |
} | |
nextOperatorIndex++; | |
if(nextOperatorIndex==numberCount-1)goto end_calc; | |
} | |
currentPosition++; | |
} | |
end_calc: | |
if(abs(*(stack_head-1)-result)<0.001){ | |
cout<<result<<"= "; | |
currentPosition = 0; | |
nextOperatorIndex = 0; | |
expPair *outStackHead = outStack; | |
while(1){ | |
stringstream ss; | |
ss<<numbers[numberPermutation[currentPosition]]<<endl; | |
getline(ss,outStackHead->exp); | |
outStackHead->del = -1; | |
outStackHead++; | |
while(operatorPostion[nextOperatorIndex]==currentPosition){ | |
if(outStackHead[-2].del!=-1 && | |
opl[outStackHead[-2].del]<opl[operatorDel[nextOperatorIndex]]){ | |
outStackHead[-2].exp = | |
"("+outStackHead[-2].exp+")"; | |
} | |
if(outStackHead[-1].del!=-1 && | |
opl[outStackHead[-1].del]<=opl[operatorDel[nextOperatorIndex]]){ | |
outStackHead[-1].exp = | |
"("+outStackHead[-1].exp+")"; | |
} | |
--outStackHead; | |
outStackHead[-1].exp = outStackHead[-1].exp+" " | |
+op[operatorDel[nextOperatorIndex]]+" " | |
+outStackHead->exp; | |
outStackHead[-1].del = operatorDel[nextOperatorIndex]; | |
nextOperatorIndex++; | |
if(nextOperatorIndex==numberCount-1)goto end_out; | |
} | |
currentPosition++; | |
} | |
end_out: | |
cout<<(outStackHead-1)->exp<<endl; | |
} | |
} while(next_operatorDel(operatorDel,numberCount)); | |
} while(next_operatorPostion(operatorPostion,numberCount)); | |
} while(next_permutation(numberPermutation,numberPermutation+numberCount)); | |
delete[] numbers; | |
delete[] numberPermutation; | |
delete[] operatorPostion; | |
delete[] operatorDel; | |
delete[] stack; | |
delete[] outStack; | |
cout<<"Finished"<<endl; | |
system("PAUSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment