Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- *******************************************************************************/
- #include <iostream>
- #include <time.h>
- using namespace std;
- bool stringCheck(string userInput){
- int userInputLength = userInput.size();
- for(int i = 0; i < userInputLength; ++i){
- if((userInput.at(i) != '+') && (userInput.at(i) != '-') && (userInput.at(i) != '/') && (userInput.at(i) != '*' )){
- return false;
- }
- }
- return true;
- }
- int reverseOperator(char userOperator){
- if (userOperator == '+'){
- return 1;
- }
- else if (userOperator == '-'){
- return 2;
- }
- else if (userOperator == '/'){
- return 3;
- }
- else if (userOperator == '*'){
- return 4;
- }
- }
- float calculateNumbers(int op, int a, int b){
- float answer;
- if (op == 1){
- answer = a + b;
- }
- else if(op == 2){
- answer = a - b;
- }
- else if(op == 3){
- answer = a / b;
- }
- else if(op == 4){
- answer = a * b;
- }
- return answer;
- }
- string changeOperator(int number){
- string operatorSign;
- if (number == 1){
- operatorSign = "+";
- }
- else if (number == 2){
- operatorSign = "-";
- }
- else if (number == 3){
- operatorSign = "/";
- }
- else if (number == 4){
- operatorSign = "*";
- }
- return operatorSign;
- }
- int main()
- {
- int startValue;
- int endingValue;
- int counter = 1;
- srand(time(nullptr));
- int puzzleChosen = rand() % 3185 + 1;
- cout << "**** Begin Debug Display ****" << endl;
- cout << "Enter the beginning solution to be printed:";
- cin >> startValue;
- cout << endl;
- cout << "Enter the end solution to be printed:";
- cin >> endingValue;
- cout << endl;
- for(float a = 1; a <= 9; ++a){
- for(float b = 1; b <= 9; ++b){
- for(float c = 1; c <= 9; ++c){
- for(float d = 1; d <= 9; ++d){
- for( int operator1 = 1; operator1 <= 4; ++operator1 ){
- for( int operator2 = 1; operator2 <= 4; ++operator2){
- for( int operator3 = 1; operator3 <= 4; ++operator3){
- string displayChar1 = changeOperator(operator1);
- string displayChar2 = changeOperator(operator2);
- string displayChar3 = changeOperator(operator3);
- float result1 = calculateNumbers(operator1, a, b);
- float result2 = calculateNumbers(operator2, result1, c);
- float result3 = calculateNumbers(operator3, result2, d);
- if (result3 == 24){
- if(counter >= startValue && counter <= endingValue){
- cout << counter << ":" << a << displayChar1 << b << displayChar2 << c << displayChar3 << d;
- cout << "=" << result3;
- cout << endl;
- }
- if (counter == puzzleChosen)
- {
- cout << "Puzzle chosen is #" << counter << ":" << a << displayChar1 << b << displayChar2 << c << displayChar3 << d;
- cout << endl;
- cout << "***End of the Debug Display***" << endl;
- cout << "The numbers to use are " << a << " " << b << " " << c << " " << d << endl;
- cout << "Enter the three operators to be used (+, -, * or / ):" << endl;
- bool isInvalidUserInput = false;
- while (true){
- string userOperators;
- cin >> userOperators;
- int sizeOfUserInput = userOperators.size();
- if(sizeOfUserInput!= 3){
- cout << endl;
- cout << "invalid number of characters entered, please try again";
- cout << endl;
- }
- if(stringCheck(userOperators) == false){
- cout << endl;
- cout << "invalid characters entered, please try again" << endl;
- }
- else if((displayChar1[0] != userOperators.at(0) || displayChar2[0] != userOperators.at(1) || displayChar3[0] != userOperators.at(2)) && (sizeOfUserInput == 3 && stringCheck(userOperators) == true)) {
- isInvalidUserInput = true;
- float res1 = calculateNumbers(reverseOperator(userOperators.at(0)), a , b);
- float res2 = calculateNumbers(reverseOperator(userOperators.at(1)), res1, c);
- float res3 = calculateNumbers(reverseOperator(userOperators.at(2)), res2, d);
- cout << a << " " << userOperators.at(0) << " " << b << " = " << res1 << endl;
- cout << res1 << " " << userOperators.at(1) << " " << c << " = " << res2 << endl;
- cout << res2 << " " << userOperators.at(2) << " " << d << " = " << res3 << endl;
- cout << "incorrect: The correct answer was:" << endl;
- cout << a << displayChar1[0] << b << displayChar2[0] << c << displayChar3[0] << d << endl;
- break;
- }
- if (displayChar1[0] == userOperators.at(0) && displayChar2[0] == userOperators.at(1) && displayChar3[0] == userOperators.at(2)){
- isInvalidUserInput = true;
- cout << a << displayChar1[0] << b << " = " << result1 << endl;
- cout << result1 << displayChar2[0] << c << " = " << result2 << endl;
- cout << result2 << displayChar3[0] << d << " = " << result3 << endl;
- cout << "Well done!" << endl;
- break;
- }
- }
- }
- ++counter;
- }
- }
- }
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement