Skip to main content
fixed spelling error in title
Link
Edward
  • 66.6k
  • 4
  • 118
  • 282

Generating a 2-variable truth table and preformingperforming boolean arithmetic

edited title
Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Generates Generating a 2-variable truth table and preformspreforming boolean arithmetic

Source Link
phillis
  • 33
  • 1
  • 3

Generates a 2-variable truth table and preforms boolean arithmetic

My code currently generates a 2-variable truth table and lets the user select to AND/OR/NOT the variables. I was looking for advice on how to make it more concise, handle bad inputs better, and ignore case while going through.

CODE:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main() {
bool p[4] = { true, true, false, false };
bool q[4] = { true, false, true, false };

cout << "Do you want to AND or OR the two propositional variables?" << endl;
string andor;
cin >> andor;
cout << "Do you want to NOT p? Y/N" << endl;
string ansp;
cin >> ansp;
cout << "Do you want to NOT q? Y/N" << endl;
string ansq;
cin >> ansq;

if (andor == "AND" || andor == "OR" &&
    ansq == "Y" || ansq == "N" &&
    ansp == "Y" || ansp == "N") {
    if (andor == "AND" && ansp == "N" && ansq == "N") {
        cout << "p | q" << " | " << "p A q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (p[i] && q[i]);
            }
            cout << endl;
        }
    }
    else if (andor == "AND" && ansp == "Y" && ansq == "N") {
        cout << "p | q" << " | " << "~p A q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (!(p[i]) && q[i]);
            }
            cout << endl;
        }
    }
    else if (andor == "AND" && ansp == "N" && ansq == "Y") {
        cout << "p | q" << " | " << "p A ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (p[i] && !(q[i]));
            }
            cout << endl;
        }
    }
    else if (andor == "AND" && ansp == "Y" && ansq == "Y") {
        cout << "p | q" << " | " << "~p A ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (!(p[i]) && !(q[i]));
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && ansp == "N" && ansq == "N") {
        cout << "p | q" << " | " << "p V q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (p[i] || q[i]);
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && ansp == "Y" && ansq == "N") {
        cout << "p | q" << " | " << "~p V q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (!(p[i]) || q[i]);
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && ansp == "N" && ansq == "Y") {
        cout << "p | q" << " | " << "p V ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (p[i] || !(q[i]));
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && ansp == "Y" && ansq == "Y") {
        cout << "p | q" << " | " << "~p V ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << setw(1) << p[i] << " | ";
            for (int j = 0; j < 1; j++) {
                cout << setw(1) << q[i] << " | ";
                cout << setw(3) << (!(p[i]) || !(q[i]));
            }
            cout << endl;
        }
    }       
}
else {
    cerr << "ERROR: Please enter valid values - EX(AND, OR, Y, N)." << endl;
}

return 0;
}