fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdexcept>
  4. #include <limits.h>
  5.  
  6. typedef std::vector<double> DB;
  7. typedef std::vector<int> IB;
  8.  
  9. double CalcOne(double A, double B, int O){
  10. if (O == 0) return A + B;
  11. if (O == 1) return A - B;
  12. if (O == 2) return A*B;
  13. if (O == 3){
  14. if (B == 0){
  15. std::cout << "Invlid Number: B=0. in CalcOne!";
  16. throw std::logic_error("Divide by Zero!!!! in CalcOne");
  17. }
  18. return A / B;
  19. }
  20.  
  21. return 0;
  22. }
  23. bool PopOne(DB& In, int Pos){
  24. In.erase(In.begin() + Pos);
  25. return 0;
  26. }
  27. bool PopOne(IB& In, int Pos){
  28. In.erase(In.begin() + Pos);
  29. return 0;
  30. }
  31.  
  32. double Calc(DB Number, IB Operator){
  33.  
  34. int C = 0;
  35.  
  36. while (Number.size() > 1){//先に除算と乗算を処理
  37. C = 0;
  38. for (std::size_t i = 0; i < Operator.size(); i++){
  39. if (Operator[i] == 2 || Operator[i] == 3){
  40. Number[i]= CalcOne(Number[i], Number[i + 1], Operator[i]);
  41. PopOne(Number, i + 1);
  42. PopOne(Operator, i);
  43. C++;
  44. break;
  45. }
  46. }
  47. if (C == 0) break;
  48. }
  49. while (Number.size() > 1){//足し引きを処理
  50. Number[0] = CalcOne(Number[0], Number[1], Operator[0]);
  51. PopOne(Number, 1);
  52. PopOne(Operator, 0);
  53. }
  54.  
  55. return Number[0];
  56. }
  57.  
  58. DB Input(){
  59. DB Number;
  60. int T;
  61. std::cin.clear();
  62. std::cout << "Input Numberz. Last Number use for Needed Answer."<<std::endl<<"If need end input to hit Enter after Enter EOF!" << std::endl;
  63. do{
  64. std::cin >> T;
  65. Number.push_back(T);
  66. } while (std::cin.eof() == false);
  67. //std::cin.ignore(INT_MAX);
  68. std::cin.clear();
  69. Number.pop_back();
  70. return Number;
  71. }
  72.  
  73. bool IncOperator(IB& Operator){
  74. Operator[Operator.size()-1]++;
  75. for (int i = Operator.size() - 1; i >= 0; i--){
  76. if (Operator[0] >=4)return true;
  77. if (Operator[i] >= 4) Operator[i - 1]++;
  78. Operator[i] %= 4;
  79. }
  80.  
  81. return false;;
  82. }
  83.  
  84. bool ShowFormula(DB& N, IB& O){
  85. char Op[] = "+-*/";
  86. for (std::size_t i = 0; i < N.size(); i++){
  87. if (N.size()>i) std::cout << N[i];
  88. if (O.size()>i) std::cout << Op[O[i]];
  89. }
  90. return true;
  91. }
  92.  
  93. int Process(DB& Number, IB& Operator, int Answer){
  94. int Count = 0;
  95. double Response = 0;
  96.  
  97. while (1){
  98. Response = Calc(Number, Operator);
  99. if (Response == Answer){
  100. ShowFormula(Number, Operator);
  101. std::cout << "=>" << Response << std::endl;
  102. Count++;
  103. }
  104. if (IncOperator(Operator) == true) break;
  105. }
  106. return Count;
  107. }
  108.  
  109.  
  110. int main(){
  111.  
  112. DB Number{ 1,5,3,6};
  113. IB Operator;
  114.  
  115. int Answer = 10;
  116. double Response = 0;
  117. int Count = 0;
  118.  
  119. /**/
  120. Number = Input();
  121. Answer = (int)Number.back();
  122. Number.pop_back();
  123. /**/
  124. //std::cout << "Input Needed number of Answer" << std::endl;
  125. //std::cin >> Answer;
  126. //std::cout << "Notice:1/2 = 0 like.by integer Value" << std::endl;
  127. Operator.resize(Number.size() - 1);
  128. std::cout << "[accept numberz]" << std::endl;
  129. for (auto& o : Number) std::cout << o << ' ';
  130. std::cout << std::endl;
  131. std::cout << "[accept Needed Answer =>"<<Answer<<"]" << std::endl;
  132.  
  133. Count = Process(Number, Operator, Answer);
  134.  
  135. if (Count != 0){
  136. std::cout << "i find "<<Count<<" Pattern!?" << std::endl;
  137. }
  138. else{
  139. std::cout << "i cant find any Pattern... i am sorry!" << std::endl;
  140. }
  141.  
  142. return 0;
  143.  
  144. }
Success #stdin #stdout 0s 3440KB
stdin
1 5 3 6 10 
stdout
Input Numberz. Last Number use for Needed Answer.
If need end input to hit Enter after Enter EOF!
[accept numberz]
1 5 3 6 
[accept Needed Answer =>10]
1+5*3-6=>10
1*5/3*6=>10
i find 2 Pattern!?