Advertisement
TheLegend12

Untitled

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