Advertisement
TheLegend12

Untitled

Sep 22nd, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 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, int a, int 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 << startValue;
  82. cout << endl;
  83. cout << "Enter the end solution to be printed:";
  84. cin >> endingValue;
  85. cout << endingValue;
  86. cout << endl;
  87.  
  88. for(float a = 1; a <= 9; ++a){
  89. for(float b = 1; b <= 9; ++b){
  90. for(float c = 1; c <= 9; ++c){
  91. for(float d = 1; d <= 9; ++d){
  92. for( int operator1 = 1; operator1 <= 4; ++operator1 ){
  93. for( int operator2 = 1; operator2 <= 4; ++operator2){
  94. for( int operator3 = 1; operator3 <= 4; ++operator3){
  95. string displayChar1 = changeOperator(operator1);
  96. string displayChar2 = changeOperator(operator2);
  97. string displayChar3 = changeOperator(operator3);
  98. float result1 = calculateNumbers(operator1, a, b);
  99. float result2 = calculateNumbers(operator2, result1, c);
  100. float result3 = calculateNumbers(operator3, result2, d);
  101. if (result3 == 24){
  102. if(counter >= startValue && counter <= endingValue){
  103. cout << counter << ":" << a << displayChar1 << b << displayChar2 << c << displayChar3 << d;
  104. cout << "=" << result3;
  105. cout << endl;
  106. }
  107. if (counter == puzzleChosen)
  108. {
  109. cout << "Puzzle chosen is #" << counter << ":" << a << displayChar1 << b << displayChar2 << c << displayChar3 << d;
  110. cout << endl;
  111. cout << "***End of the Debug Display***" << endl;
  112. cout << "The numbers to use are " << a << " " << b << " " << c << " " << d << endl;
  113. cout << "Enter the three operators to be used (+, -, * or / ):" << endl;
  114. bool isInvalidUserInput = false;
  115. while (true){
  116. string userOperators;
  117. cin >> userOperators;
  118. int sizeOfUserInput = userOperators.size();
  119. if(sizeOfUserInput!= 3){
  120. cout << endl;
  121. cout << "invalid number of characters entered, please try again";
  122. cout << endl;
  123. }
  124. if(stringCheck(userOperators) == false){
  125. cout << endl;
  126. cout << "invalid characters entered, please try again" << endl;
  127.  
  128. }
  129.  
  130. else if((displayChar1[0] != userOperators.at(0) || displayChar2[0] != userOperators.at(1) || displayChar3[0] != userOperators.at(2)) && (sizeOfUserInput == 3 && stringCheck(userOperators) == true)) {
  131. isInvalidUserInput = true;
  132. float res1 = calculateNumbers(reverseOperator(userOperators.at(0)), a , b);
  133. float res2 = calculateNumbers(reverseOperator(userOperators.at(1)), res1, c);
  134. float res3 = calculateNumbers(reverseOperator(userOperators.at(2)), res2, d);
  135. cout << a << " " << userOperators.at(0) << " " << b << " = " << res1 << endl;
  136. cout << res1 << " " << userOperators.at(1) << " " << c << " = " << res2 << endl;
  137. cout << res2 << " " << userOperators.at(2) << " " << d << " = " << res3 << endl;
  138. cout << "incorrect: The correct answer was:" << endl;
  139. cout << a << displayChar1[0] << b << displayChar2[0] << c << displayChar3[0] << d << endl;
  140. break;
  141. }
  142. if (displayChar1[0] == userOperators.at(0) && displayChar2[0] == userOperators.at(1) && displayChar3[0] == userOperators.at(2)){
  143. isInvalidUserInput = true;
  144. cout << a << displayChar1[0] << b << " = " << result1 << endl;
  145. cout << result1 << displayChar2[0] << c << " = " << result2 << endl;
  146. cout << result2 << displayChar3[0] << d << " = " << result3 << endl;
  147. cout << "Well done!" << endl;
  148. break;
  149. }
  150.  
  151. }
  152.  
  153.  
  154. }
  155. ++counter;
  156.  
  157. }
  158.  
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165.  
  166. }
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement