Advertisement
TheLegend12

Untitled

Sep 21st, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5. using namespace std;
  6.  
  7. float calculateOperation(float a, int sign, float b){
  8. if(sign == 0){
  9. return a + b;
  10. }
  11. else if(sign == 1){
  12. return a - b;
  13. }
  14. else if(sign == 2){
  15. return a * b;
  16. }
  17. else if(sign == 3){
  18. return a / b;
  19. }
  20. else{
  21. return NaN;
  22. }
  23. }
  24.  
  25. string changeOperator(int sign){
  26. if (sign == 0){
  27. return "+";
  28. }
  29. else if (sign == 1){
  30. return "-";
  31. }
  32. else if (sign == 2){
  33. return "*";
  34. }
  35. else if (sign == 3){
  36. return "/";
  37. }
  38. else{
  39. return "?";
  40. }
  41. }
  42.  
  43. int main()
  44. {
  45. float a;
  46. float b;
  47. float c;
  48. float d;
  49. float res1;
  50. float res2;
  51. float res3;
  52. int operator1;
  53. int operator2;
  54. int operator3;
  55. string str1;
  56. string str2;
  57. string str3;
  58. int counter = 0;
  59. int startValue;
  60. int endValue;
  61. string userOperator1;
  62. string userOperator2;
  63. string userOperator3;
  64. srand(time(nullptr));
  65. string userOperators;
  66. int puzzleChosen = rand() % 3185 + 1;
  67. bool isUserInputInvalid = false;
  68.  
  69. cout << "Enter the beginning solution to be printed:";
  70. cin >> startValue;
  71. cout << endl;
  72. cout << "Enter the end solution to be printed: ";
  73. cin >> endValue;
  74. cout << endl;
  75.  
  76. for (a = 1; a <= 9; ++a)
  77. {
  78. for (b = 1; b <= 9; ++b)
  79. {
  80. for (c = 1; c <= 9; ++c)
  81. {
  82. for (d = 1; d <= 9; ++d)
  83. {
  84. for (operator1 = 1; operator1 <= 4; ++operator1)
  85. {
  86. for (operator2 = 1; operator2 <= 4; ++operator2)
  87. {
  88. for (operator3 = 1; operator3 <= 4; ++operator3)
  89. {
  90. displayChar1 = changeOperator(operator1);
  91. displayChar2 = changeOperator(operator2);
  92. displayChar3 = changeOperator(operator3);
  93. result = calculateNumbers(a, operator1, b);
  94. result = calculateNumbers(result, operator2, c);
  95. result = calculateNumbers(result, operator3 , d);
  96. // if (operator1 == 1)
  97. // {
  98. // str1 = '+';
  99. // res1 = a + b;
  100. // }
  101. // else if (operator1 == 2)
  102. // {
  103. // str1 = '-';
  104. // res1 = a - b;
  105. // }
  106. // else if (operator1 == 3)
  107. // {
  108. // str1 = '/';
  109. // res1 = a / b;
  110. // }
  111. // else if (operator1 == 4)
  112. // {
  113. // str1 = '*';
  114. // res1 = a * b;
  115. // }
  116.  
  117. // if (operator2 == 1)
  118. // {
  119. // str2 = '+';
  120. // res2 = res1 + c;
  121. // }
  122. // else if (operator2 == 2)
  123. // {
  124. // str2 = '-';
  125. // res2 = res1 - c;
  126. // }
  127. // else if (operator2 == 3)
  128. // {
  129. // str2 = '/';
  130. // res2 = res1 / c;
  131. // }
  132. // else if (operator2 == 4)
  133. // {
  134. // str2 = '*';
  135. // res2 = res1 * c;
  136. // }
  137. // if (operator3 == 1)
  138. // {
  139. // str3 = '+';
  140. // res3 = res2 + d;
  141. // }
  142. // else if (operator3 == 2)
  143. // {
  144. // str3 = '-';
  145. // res3 = res2 - d;
  146. // }
  147. // else if (operator3 == 3)
  148. // {
  149. // str3 = '/';
  150. // res3 = res2 / d;
  151. // }
  152. // else if (operator3 == 4)
  153. // {
  154. // str3 = '*';
  155. // res3 = res2 * d;
  156. if (res3 == 24)
  157. {
  158. ++counter;
  159. while(isUserInputInvalid != true)
  160. if (counter >= startValue && counter <= endValue)
  161. {
  162. cout << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
  163. cout << " = " << res3;
  164. cout << endl;
  165. }
  166. if (counter == puzzleChosen)
  167. {
  168. cout << "Puzzle chosen is #" << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
  169. cout << endl;
  170. cout << "The numbers to use are " << a << " " << b << " " << c << " " << d;
  171. cout << endl;
  172. cout << "Enter the three operators to be used (+, -, * or / ):" << endl;
  173. cin >> userOperator1;
  174. cin >> userOperator2;
  175. cin >> userOperator3;
  176. userOperators = userOperator1 + userOperator2 + userOperator3;
  177. cout << userOperator1 << userOperator2 << userOperator3;
  178. cout << endl;
  179. if (str1 == userOperator1 && str2 == userOperator2 && str3 == userOperator3)
  180. {
  181. isUserInputInvalid = true;
  182. cout << a << userOperator1 << b << " = " << res1 << endl;
  183. cout << res1 << userOperator2 << c << " = " << res2 << endl;
  184. cout << res2 << userOperator3 << d << " = " << res3 << endl;
  185. cout << "Well done!" << endl;
  186. }
  187. else if(userOperators.size() > 3 || userOperators.size() < 3){
  188. isUserInputInvalid = true;
  189. cout << endl;
  190. cout << "invalid number of characters entered, please try again";
  191. cout << endl;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. return 0;
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement