Advertisement
TheLegend12

Untitled

Sep 21st, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5. using namespace std;
  6.  
  7. float calculateNumbers(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 1;
  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. float result;
  53. int operator1;
  54. int operator2;
  55. int operator3;
  56. string str1;
  57. string str2;
  58. string str3;
  59. int counter = 0;
  60. int startValue;
  61. int endValue;
  62. string userOperator1;
  63. string userOperator2;
  64. string userOperator3;
  65. srand(time(nullptr));
  66. string userOperators;
  67. string inputLine;
  68. string op1, op2, op3;
  69. int puzzleChosen = rand() % 3185 + 1;
  70. bool isUserInputInvalid = false;
  71. string displayChar1;
  72. string displayChar2;
  73. string displayChar3;
  74.  
  75. cout << "Enter the beginning solution to be printed:";
  76. cin >> startValue;
  77. cout << endl;
  78. cout << "Enter the end solution to be printed: ";
  79. cin >> endValue;
  80. cout << endl;
  81.  
  82. for (a = 1; a <= 9; ++a)
  83. {
  84. for (b = 1; b <= 9; ++b)
  85. {
  86. for (c = 1; c <= 9; ++c)
  87. {
  88. for (d = 1; d <= 9; ++d)
  89. {
  90. for (operator1 = 0; operator1 <= 3; ++operator1)
  91. {
  92. for (operator2 = 0; operator2 <= 3; ++operator2)
  93. {
  94. for (operator3 = 0; operator3 <= 3; ++operator3)
  95. {
  96. displayChar1 = changeOperator(operator1);
  97. displayChar2 = changeOperator(operator2);
  98. displayChar3 = changeOperator(operator3);
  99. result = calculateNumbers(a, operator1, b);
  100. result = calculateNumbers(result, operator2, c);
  101. result = calculateNumbers(result, operator3 , d);
  102.  
  103. if (res3 == 24)
  104. {
  105. ++counter;
  106. if (counter >= startValue && counter <= endValue)
  107. {
  108. cout << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
  109. cout << " = " << res3;
  110. cout << endl;
  111. }
  112. if (counter == puzzleChosen)
  113. {
  114. cout << "Puzzle chosen is #" << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
  115. cout << endl;
  116. cout << "The numbers to use are " << a << " " << b << " " << c << " " << d;
  117. cout << endl;
  118. cout << "Enter the three operators to be used (+, -, * or / ):" << endl;
  119. getline(cin, inputLine);
  120. op1=inputLine[0];
  121. op2=inputLine[1];
  122. op3=inputLine[2];
  123. // userOperators = userOperator1 + userOperator2 + userOperator3;
  124. // cout << userOperator1 << userOperator2 << userOperator3;
  125. userOperators = op1 + op2 + op3;
  126. cout << endl;
  127. if (str1 == op1 && str2 == op2 && str3 == op3)
  128. {
  129. cout << a << userOperator1 << b << " = " << res1 << endl;
  130. cout << res1 << userOperator2 << c << " = " << res2 << endl;
  131. cout << res2 << userOperator3 << d << " = " << res3 << endl;
  132. cout << "Well done!" << endl;
  133. }
  134. else if(userOperators.size() > 3 || userOperators.size() < 3){
  135. isUserInputInvalid = true;
  136. cout << endl;
  137. cout << "invalid number of characters entered, please try again";
  138. cout << endl;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement