Advertisement
TheLegend12

Untitled

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