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(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()
- {
- 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 = 1;
- 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(operator1, a, b);
- result = calculateNumbers(operator2, result, c);
- result = calculateNumbers(operator3, result, d);
- if (result == 24)
- {
- if (counter >= startValue && counter <= endValue)
- {
- cout << counter << ":" << a << displayChar1 << b << displayChar2 << c << displayChar3 << d;
- cout << "=" << result;
- 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