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 calculateNumbers(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 1;
- }
- }
- 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;
- float result;
- 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;
- string inputLine;
- string op1, op2, op3;
- int puzzleChosen = rand() % 3185 + 1;
- bool isUserInputInvalid = false;
- string displayChar1;
- string displayChar2;
- string displayChar3;
- 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 = 0; operator1 <= 3; ++operator1)
- {
- for (operator2 = 0; operator2 <= 3; ++operator2)
- {
- for (operator3 = 0; operator3 <= 3; ++operator3)
- {
- displayChar1 = changeOperator(operator1);
- displayChar2 = changeOperator(operator2);
- displayChar3 = changeOperator(operator3);
- result = calculateNumbers(a, operator1, b);
- result = calculateNumbers(result, operator2, c);
- result = calculateNumbers(result, operator3 , d);
- if (res3 == 24)
- {
- ++counter;
- 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;
- getline(cin, inputLine);
- op1=inputLine[0];
- op2=inputLine[1];
- op3=inputLine[2];
- // userOperators = userOperator1 + userOperator2 + userOperator3;
- // cout << userOperator1 << userOperator2 << userOperator3;
- userOperators = op1 + op2 + op3;
- cout << endl;
- if (str1 == op1 && str2 == op2 && str3 == op3)
- {
- 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