Author: George Wanis Ayed Wanis
Student ID: 2300280
Task: 1
Team: 31
This C++ program evaluates logical propositions using the Shunting Yard Algorithm and Postfix evaluation. It allows the user to input multiple premises and a conclusion, constructs a truth table, and determines whether the conclusion is valid, satisfiable, or falsifiable.
The program supports logical operators:
~: NOT&: AND|: OR>: IMPLICATION=: BICONDITIONAL
The project was inspired and supported by the following tutorials:
- Shunting Yard Algorithm – Comp Sci in 5
- Postfix Stack Evaluator – Comp Sci in 5
- Maps in C++ (std::map and std::unordered_map)
- DIY Programming Language #1: The Shunting Yard Algorithm
Example scenario assigned to the team:
The children are playing Football or Tennis.
If they are watching TV or playing Tennis, then they are not playing Football.
Therefore, they are not playing Football or they are not watching TV.
- Premises in logical form:
- F | T
- V | T > ~F
- Conclusion: ~F | ~V
The truth table for this example is generated automatically by the program.
The program contains the following main functions:
-
precedence(char op)
Returns the precedence of logical operators. -
toPostfix(const string &infix)
Converts an infix logical expression to postfix notation using the Shunting Yard Algorithm. -
variablesIndexInVector(const vector<char>& vars, char c)
Returns the index of a variable in a vector of variables. -
evaluateStatementInPostFix(const string &postfix, const vector<char>& vars, const vector<int>& values)
Evaluates a postfix logical expression for a given row of truth values. -
extractVariables(const vector<string>& premises, const string& conclusion)
Extracts all unique variables from the premises and conclusion. -
main()- Prompts the user to input the number of premises, the premises themselves, and the conclusion.
- Generates all combinations of truth values.
- Converts premises and conclusion to postfix notation.
- Evaluates each row of the truth table.
- Determines if the conclusion is satisfiable or valid.
- Compile the program:
g++ -o logic_evaluator main.cpp- Run the executable:
./logic_evaluator- Follow the prompts:
- Enter the number of premises.
- Enter each premise using logical operators (
~,&,|,>,=). - Enter the conclusion.
The program will output:
- The truth table with premises, conclusion, and implication results.
- Whether the conclusion is satisfiable or valid.
- Malformed expressions will trigger an error, and evaluation will stop.
- Variables are automatically detected from the input expressions.
- Supports complex logical statements with multiple operators and parentheses.
Variables the user did input: F T V
F T V | P1 P2 | C | Imp
0 0 0 | 0 0 | 1 | 1
...
Satisfiable? YES
Valid? FALSIFIABLE
This project is for educational purposes as part of a university assignment.