Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- using namespace std;
- float calculateOperation(float a, int sign, float b){
- if(sign == 0){
- return a + b;
- }
- else if(sign == 1){
- return a - b;
- }
- else if(sign == 2){
- return a * b;
- }
- else if(sign == 3){
- return a / b;
- }
- else{
- return NaN;
- }
- }
- string changeOperator(int sign){
- if (sign == 0){
- return "+";
- }
- else if (sign == 1){
- return "-";
- }
- else if (sign == 2){
- return "*";
- }
- else if (sign == 3){
- return "/";
- }
- else{
- return "?";
- }
- }
- int main()
- {
- float a;
- float b;
- float c;
- float d;
- float res1;
- float res2;
- float res3;
- int operator1;
- int operator2;
- int operator3;
- string str1;
- string str2;
- string str3;
- int counter = 0;
- int startValue;
- int endValue;
- string userOperator1;
- string userOperator2;
- string userOperator3;
- srand(time(nullptr));
- string userOperators;
- int puzzleChosen = rand() % 3185 + 1;
- bool isUserInputInvalid = false;
- cout << "Enter the beginning solution to be printed:";
- cin >> startValue;
- cout << endl;
- cout << "Enter the end solution to be printed: ";
- cin >> endValue;
- cout << endl;
- for (a = 1; a <= 9; ++a)
- {
- for (b = 1; b <= 9; ++b)
- {
- for (c = 1; c <= 9; ++c)
- {
- for (d = 1; d <= 9; ++d)
- {
- for (operator1 = 1; operator1 <= 4; ++operator1)
- {
- for (operator2 = 1; operator2 <= 4; ++operator2)
- {
- for (operator3 = 1; operator3 <= 4; ++operator3)
- {
- displayChar1 = changeOperator(operator1);
- displayChar2 = changeOperator(operator2);
- displayChar3 = changeOperator(operator3);
- result = calculateNumbers(operator1, a, b);
- result = calculateNumbers(operator2, result, c);
- result = calculateNumbers(operator3, result, d);
- // if (operator1 == 1)
- // {
- // str1 = '+';
- // res1 = a + b;
- // }
- // else if (operator1 == 2)
- // {
- // str1 = '-';
- // res1 = a - b;
- // }
- // else if (operator1 == 3)
- // {
- // str1 = '/';
- // res1 = a / b;
- // }
- // else if (operator1 == 4)
- // {
- // str1 = '*';
- // res1 = a * b;
- // }
- // if (operator2 == 1)
- // {
- // str2 = '+';
- // res2 = res1 + c;
- // }
- // else if (operator2 == 2)
- // {
- // str2 = '-';
- // res2 = res1 - c;
- // }
- // else if (operator2 == 3)
- // {
- // str2 = '/';
- // res2 = res1 / c;
- // }
- // else if (operator2 == 4)
- // {
- // str2 = '*';
- // res2 = res1 * c;
- // }
- // if (operator3 == 1)
- // {
- // str3 = '+';
- // res3 = res2 + d;
- // }
- // else if (operator3 == 2)
- // {
- // str3 = '-';
- // res3 = res2 - d;
- // }
- // else if (operator3 == 3)
- // {
- // str3 = '/';
- // res3 = res2 / d;
- // }
- // else if (operator3 == 4)
- // {
- // str3 = '*';
- // res3 = res2 * d;
- if (res3 == 24)
- {
- ++counter;
- while(isUserInputInvalid != true)
- if (counter >= startValue && counter <= endValue)
- {
- cout << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
- cout << " = " << res3;
- cout << endl;
- }
- if (counter == puzzleChosen)
- {
- cout << "Puzzle chosen is #" << counter << ":" << a << " " << str1 << " " << b << " " << str2 << " " << c << " " << str3 << " " << d;
- cout << endl;
- cout << "The numbers to use are " << a << " " << b << " " << c << " " << d;
- cout << endl;
- cout << "Enter the three operators to be used (+, -, * or / ):" << endl;
- cin >> userOperator1;
- cin >> userOperator2;
- cin >> userOperator3;
- userOperators = userOperator1 + userOperator2 + userOperator3;
- cout << userOperator1 << userOperator2 << userOperator3;
- cout << endl;
- if (str1 == userOperator1 && str2 == userOperator2 && str3 == userOperator3)
- {
- isUserInputInvalid = true;
- cout << a << userOperator1 << b << " = " << res1 << endl;
- cout << res1 << userOperator2 << c << " = " << res2 << endl;
- cout << res2 << userOperator3 << d << " = " << res3 << endl;
- cout << "Well done!" << endl;
- }
- else if(userOperators.size() > 3 || userOperators.size() < 3){
- isUserInputInvalid = true;
- cout << endl;
- cout << "invalid number of characters entered, please try again";
- cout << endl;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement