#include <iostream>
#include <vector>
#include <stdexcept>
#include <limits.h>
typedef std::vector<int> IB;
int CalcOne(int A, int 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(IB& In, int Pos){
In.erase(In.begin() + Pos);
return 0;
}
int Calc(IB Number, IB Operator){
int C = 0;
while (Number.size() > 1){//先に除算と乗算を処理
C = 0;
for (int 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];
}
IB Input(){
IB Number;
int T;
std::cin.clear();
std::cout << "Input Numberz 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(IB& N, IB& O){
char Op[] = "+-*/";
for (int 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 main(){
IB Number;
IB Operator;
int Answer = 0;
int Response = 0;
int Count = 0;
Number = Input();
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);
while (1){
Response = Calc(Number, Operator);
if (Response == Answer){
ShowFormula(Number, Operator);
std::cout << "=>" <<Response<< std::endl;
Count++;
}
if (IncOperator(Operator) == true) break;
}
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;
}