#include <iostream>
#include <vector>
#include <stdexcept>
#include <limits.h>
typedef std::vector<double> DB;
typedef std::vector<int> IB;
double CalcOne(double A, double B, int O){
if (O == 0) return A + B;
if (O == 1) return A - B;
if (O == 2) return A*B;
if (O == 3){
if (B == 0){
std::cout << "Invlid Number: B=0. in CalcOne!";
throw std::logic_error("Divide by Zero!!!! in CalcOne");
}
return A / B;
}
return 0;
}
bool PopOne(DB& In, int Pos){
In.erase(In.begin() + Pos);
return 0;
}
bool PopOne(IB& In, int Pos){
In.erase(In.begin() + Pos);
return 0;
}
double Calc(DB Number, IB Operator){
int C = 0;
while (Number.size() > 1){//先に除算と乗算を処理
C = 0;
for (std::size_t i = 0; i < Operator.size(); i++){
if (Operator[i] == 2 || Operator[i] == 3){
Number[i]= CalcOne(Number[i], Number[i + 1], Operator[i]);
PopOne(Number, i + 1);
PopOne(Operator, i);
C++;
break;
}
}
if (C == 0) break;
}
while (Number.size() > 1){//足し引きを処理
Number[0] = CalcOne(Number[0], Number[1], Operator[0]);
PopOne(Number, 1);
PopOne(Operator, 0);
}
return Number[0];
}
DB Input(){
DB Number;
int T;
std::cin.clear();
std::cout << "Input Numberz. Last Number use for Needed Answer."<<std::endl<<"If need end input to hit Enter after Enter EOF!" << std::endl;
do{
std::cin >> T;
Number.push_back(T);
} while (std::cin.eof() == false);
//std::cin.ignore(INT_MAX);
std::cin.clear();
Number.pop_back();
return Number;
}
bool IncOperator(IB& Operator){
Operator[Operator.size()-1]++;
for (int i = Operator.size() - 1; i >= 0; i--){
if (Operator[0] >=4)return true;
if (Operator[i] >= 4) Operator[i - 1]++;
Operator[i] %= 4;
}
return false;;
}
bool ShowFormula(DB& N, IB& O){
char Op[] = "+-*/";
for (std::size_t i = 0; i < N.size(); i++){
if (N.size()>i) std::cout << N[i];
if (O.size()>i) std::cout << Op[O[i]];
}
return true;
}
int Process(DB& Number, IB& Operator, int Answer){
int Count = 0;
double Response = 0;
while (1){
Response = Calc(Number, Operator);
if (Response == Answer){
ShowFormula(Number, Operator);
std::cout << "=>" << Response << std::endl;
Count++;
}
if (IncOperator(Operator) == true) break;
}
return Count;
}
int main(){
DB Number{ 1,5,3,6};
IB Operator;
int Answer = 10;
double Response = 0;
int Count = 0;
/**/
Number = Input();
Answer = (int)Number.back();
Number.pop_back();
/**/
//std::cout << "Input Needed number of Answer" << std::endl;
//std::cin >> Answer;
//std::cout << "Notice:1/2 = 0 like.by integer Value" << std::endl;
Operator.resize(Number.size() - 1);
std::cout << "[accept numberz]" << std::endl;
for (auto& o : Number) std::cout << o << ' ';
std::cout << std::endl;
std::cout << "[accept Needed Answer =>"<<Answer<<"]" << std::endl;
Count = Process(Number, Operator, Answer);
if (Count != 0){
std::cout << "i find "<<Count<<" Pattern!?" << std::endl;
}
else{
std::cout << "i cant find any Pattern... i am sorry!" << std::endl;
}
return 0;
}