-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (84 loc) · 3.35 KB
/
main.cpp
File metadata and controls
99 lines (84 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <map>
#include<string>
#include<vector>
#include<chrono>
#include "Q1.h"
#include "Q2.h"
#include "Q3.h"
#include "Q4.h"
#include "Q5.h"
#include "Q6.h"
#include "Q7.h"
#include "FileUtility.h"
using std::string;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::map;
int main() {
cout << "Task 1 : Converting infix to prefix propositional logic formula" << endl;
string infix;
getline(cin,infix);
cout << "Prefix propositional logic formula : " << infixToPrefix(infix) << endl;
std::cout << "--- Test Case 1 ---" << std::endl;
std::string prefix1 = "+ p1 ~ q1";
std::cout << "Prefix Expression: " << prefix1 << std::endl;
// Build the parse tree
TreeNode* root1 = buildParseTree(prefix1);
// Print the infix expression to verify the tree structure
std::cout << "Resulting Infix: ";
printInfix(root1);
std::cout << std::endl;
std::cout << "Expected Infix: (p1+(~q1))" << std::endl;
set<string> vars = {"p1","q1"};
printTruthTable(root1,vars);
// Clean up the allocated memory
deleteTree(root1);
std::cout << "\n--- Test Case 2 ---" << std::endl;
std::string prefix2 = "* + p q > ~ r s";
std::cout << "Prefix Expression: " << prefix2 << std::endl;
TreeNode* root2 = buildParseTree(prefix2);
std::cout << "Resulting Infix: ";
printInfix(root2);
std::cout << std::endl;
std::cout << "Expected Infix: ((p+q)*((~r)>s))" << std::endl;
std::cout << "Height of parse tree is : " << getParseTreeHeight(root2)<< std::endl;
deleteTree(root2);
map<string, bool> truthValues;
truthValues["p"] = false;
string test = "p+~p";
cout << "Infix to prefix is : "<< infixToPrefix(test) << endl;
TreeNode* root3 = buildParseTree(infixToPrefix(test));
printInfix(root3);
cout << endl;
cout << "Height of parse tree is : "<< getParseTreeHeight(root3) << endl;
cout << "Truth value is : " << evaluateTruthValue(root3, truthValues) << endl;
string testcnf = "((p+~p)*((~r)>s))";
TreeNode* root4= buildParseTree(infixToPrefix(testcnf));
printInfix(root4);
cout << endl;
root4 = toCNF(root4);
printInfix(root4);
std::pair<int,int> val = isCNF(root4);
cout << endl;
cout <<" Number of valid clauses : " << val.first << endl << "Number of invalid clauses : " << val.second << endl;
// Testing on file
auto start = std::chrono::high_resolution_clock::now();
TreeNode* root5 = buildParseTreeFromFile("C:\\Users\\gmbha\\OneDrive\\Desktop\\compute\\Logic_Assignment\\test2.cnf");
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
cout << "Parse tree built in " << duration.count() << " ms" << std::endl;
start = std::chrono::high_resolution_clock::now();
cout << "Checking number of valid clauses " << endl;
root5 = toCNF(root5);
//printInfix(root5);
std::pair<int,int> val1 = isCNF(root5);
cout <<" Number of valid clauses : " << val1.first << endl << "Number of invalid clauses : " << val1.second << endl;
end = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::seconds>(end - start);
cout << "Clauses calculated in " << duration1.count() << " s" << std::endl;
deleteTree(root5);
return 0;
}