Advertisement
TheLegend12

Untitled

Sep 21st, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 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 operatorName(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. if (operator1 == 1)
  91. {
  92. str1 = '+';
  93. res1 = a + b;
  94. }
  95. else if (operator1 == 2)
  96. {
  97. str1 = '-';
  98. res1 = a - b;
  99. }
  100. else if (operator1 == 3)
  101. {
  102. str1 = '/';
  103. res1 = a / b;
  104. }
  105. else if (operator1 == 4)
  106. {
  107. str1 = '*';
  108. res1 = a * b;
  109. }
  110.  
  111. if (operator2 == 1)
  112. {
  113. str2 = '+';
  114. res2 = res1 + c;
  115. }
  116. else if (operator2 == 2)
  117. {
  118. str2 = '-';
  119. res2 = res1 - c;
  120. }
  121. else if (operator2 == 3)
  122. {
  123. str2 = '/';
  124. res2 = res1 / c;
  125. }
  126. else if (operator2 == 4)
  127. {
  128. str2 = '*';
  129. res2 = res1 * c;
  130. }
  131. if (operator3 == 1)
  132. {
  133. str3 = '+';
  134. res3 = res2 + d;
  135. }
  136. else if (operator3 == 2)
  137. {
  138. str3 = '-';
  139. res3 = res2 - d;
  140. }
  141. else if (operator3 == 3)
  142. {
  143. str3 = '/';
  144. res3 = res2 / d;
  145. }
  146. else if (operator3 == 4)
  147. {
  148. str3 = '*';
  149. res3 = res2 * d;
  150. }
  151. if (res3 == 24)
  152. {
  153. ++counter;
  154. while(isUserInputInvalid != true)
  155. if (counter >= startValue && counter <= endValue)
  156. {
  157. cout << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
  158. cout << " = " << res3;
  159. cout << endl;
  160. }
  161. if (counter == puzzleChosen)
  162. {
  163. cout << "Puzzle chosen is #" << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
  164. cout << endl;
  165. cout << "The numbers to use are " << a << " " << b << " " << c << " " << d;
  166. cout << endl;
  167. cout << "Enter the three operators to be used (+, -, * or / ):" << endl;
  168. cin >> userOperator1;
  169. cin >> userOperator2;
  170. cin >> userOperator3;
  171. userOperators = userOperator1 + userOperator2 + userOperator3;
  172. cout << userOperator1 << userOperator2 << userOperator3;
  173. cout << endl;
  174. if (str1 == userOperator1 && str2 == userOperator2 && str3 == userOperator3)
  175. {
  176. isUserInputInvalid = true;
  177. cout << a << userOperator1 << b << " = " << res1 << endl;
  178. cout << res1 << userOperator2 << c << " = " << res2 << endl;
  179. cout << res2 << userOperator3 << d << " = " << res3 << endl;
  180. cout << "Well done!" << endl;
  181. }
  182. else if(userOperators.size() > 3 || userOperators.size() < 3){
  183. isUserInputInvalid = true;
  184. cout << endl;
  185. cout << "invalid number of characters entered, please try again";
  186. cout << endl;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. return 0;
  198. }
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement