Advertisement
TheLegend12

Untitled

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