Finished problem 3.
This commit is contained in:
parent
0fa6fb2021
commit
73e5f291c0
9 changed files with 198 additions and 226 deletions
5
hw2/README.txt
Normal file
5
hw2/README.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
Go to respective folder,
|
||||||
|
make
|
||||||
|
./main
|
||||||
|
|
||||||
|
for hw1pr1 and hw1pr2 you can use ./main -f to have the program read from hw1pr*_data.txt
|
|
@ -1,223 +0,0 @@
|
||||||
// Name: Alexander Huddleston UIN: 223000555
|
|
||||||
// CSCE 420
|
|
||||||
// Due: 11:59 P.M. Monday, October 30, 2017
|
|
||||||
// hw2pr1.cpp
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <math.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <tuple>
|
|
||||||
#include "node.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
// Perform min/max on this tree.
|
|
||||||
void minmax(node *n)
|
|
||||||
{
|
|
||||||
|
|
||||||
if(n->getLevel() % 2 == 0)
|
|
||||||
{
|
|
||||||
int max = n->getValue();
|
|
||||||
|
|
||||||
if(n->getChildren().size() > 0)
|
|
||||||
{
|
|
||||||
for(int c = 0; c < n->getChildren().size(); c++)
|
|
||||||
{
|
|
||||||
minmax(n->getChildAt(c));
|
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() > max)
|
|
||||||
{
|
|
||||||
max = n->getChildAt(c)->getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n->setValue(max);
|
|
||||||
//cout << n->getValue() << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int min = n->getValue();
|
|
||||||
|
|
||||||
if(n->getChildren().size() > 0)
|
|
||||||
{
|
|
||||||
for(int c = 0; c < n->getChildren().size(); c++)
|
|
||||||
{
|
|
||||||
minmax(n->getChildAt(c));
|
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() < min)
|
|
||||||
{
|
|
||||||
min = n->getChildAt(c)->getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n->setValue(min);
|
|
||||||
//cout << n->getValue() << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is mostly just a simple debug function so I can see
|
|
||||||
// if my tree is being formed properly.
|
|
||||||
void printTree(node n)
|
|
||||||
{
|
|
||||||
cout << n.getValue() << " " << n.getLevel() << " ";
|
|
||||||
if(n.getChildren().size() != 0)
|
|
||||||
{
|
|
||||||
cout << "\n|\n";
|
|
||||||
for(node c : n.getChildren())
|
|
||||||
{
|
|
||||||
printTree(c);
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tuple<node, string> makeTree(string input, node n, int level)
|
|
||||||
{
|
|
||||||
bool negate = false;
|
|
||||||
char tempc;
|
|
||||||
string tempn = "";
|
|
||||||
|
|
||||||
while(input.length() != 0)
|
|
||||||
{
|
|
||||||
tempc = input[0];
|
|
||||||
if(input.length() > 0)
|
|
||||||
{
|
|
||||||
input = input.substr(1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
input = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the char is a number.
|
|
||||||
if(isdigit(tempc))
|
|
||||||
{
|
|
||||||
tempn += tempc;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if(tempc == '-')
|
|
||||||
{
|
|
||||||
negate = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the char is a comma.
|
|
||||||
else if(tempc == ',')
|
|
||||||
{
|
|
||||||
if(tempn.length() > 0)
|
|
||||||
{
|
|
||||||
node tempnode;
|
|
||||||
tempnode.setLevel(level);
|
|
||||||
int negatemp = stoi(tempn);
|
|
||||||
if(negate)
|
|
||||||
{
|
|
||||||
negatemp = -negatemp;
|
|
||||||
}
|
|
||||||
tempnode.setValue(negatemp);
|
|
||||||
tempnode.setParent(&n);
|
|
||||||
n.addChild(tempnode);
|
|
||||||
tempn = "";
|
|
||||||
negate = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the char is a close paranthesis.
|
|
||||||
else if(tempc == ')')
|
|
||||||
{
|
|
||||||
if(tempn.length() > 0)
|
|
||||||
{
|
|
||||||
node tempnode;
|
|
||||||
tempnode.setLevel(level);
|
|
||||||
int negatemp = stoi(tempn);
|
|
||||||
if(negate)
|
|
||||||
{
|
|
||||||
negatemp = -negatemp;
|
|
||||||
}
|
|
||||||
tempnode.setValue(negatemp);
|
|
||||||
tempnode.setParent(&n);
|
|
||||||
n.addChild(tempnode);
|
|
||||||
tempn = "";
|
|
||||||
negate = false;
|
|
||||||
}
|
|
||||||
tuple<node, string> tempt(n, input);
|
|
||||||
return tempt;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the char is an open paranthesis.
|
|
||||||
else if(tempc == '(')
|
|
||||||
{
|
|
||||||
node tempnode;
|
|
||||||
tempnode.setLevel(level);
|
|
||||||
if(level % 2 == 0)
|
|
||||||
{
|
|
||||||
tempnode.setValue(INT_MIN);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tempnode.setValue(INT_MAX);
|
|
||||||
}
|
|
||||||
tempnode.setParent(&n);
|
|
||||||
tuple<node, string> tempt = makeTree(input, tempnode, level + 1);
|
|
||||||
tempnode = get<0>(tempt);
|
|
||||||
input = get<1>(tempt);
|
|
||||||
n.addChild(tempnode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tuple<node, string> output(n, input);
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
// Initialization of input string.
|
|
||||||
string input = "";
|
|
||||||
|
|
||||||
// For testing, I want to read from a file.
|
|
||||||
// Typing in an tree from the keyboard each execution is a waste of time.
|
|
||||||
if(argc > 1)
|
|
||||||
{
|
|
||||||
if(!((string)argv[1]).substr(0, 2).compare("-f"))
|
|
||||||
{
|
|
||||||
ifstream file;
|
|
||||||
string line;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
file.open("hw2pr2_data.txt");
|
|
||||||
}
|
|
||||||
catch(exception e)
|
|
||||||
{
|
|
||||||
cerr << "File not found, or could not be opened." << endl;
|
|
||||||
}
|
|
||||||
while(!file.eof())
|
|
||||||
{
|
|
||||||
getline(file, line);
|
|
||||||
input += line;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cout << "Please input a tree: ";
|
|
||||||
cin >> input;
|
|
||||||
}
|
|
||||||
|
|
||||||
node root;
|
|
||||||
root.setLevel(0);
|
|
||||||
root.setValue(INT_MIN);
|
|
||||||
tuple<node, string> tree;
|
|
||||||
tree = makeTree(input.substr(1), root, 1);
|
|
||||||
root = get<0>(tree);
|
|
||||||
|
|
||||||
printTree(root);
|
|
||||||
minmax(&root);
|
|
||||||
|
|
||||||
printTree(root);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
(((-2,-11,10),(2,15,13),(-1,-16,-16)),((-20,13,20),(-13,7,-19),(7,3,4)),((-20,-9,-14),(-4,-6,-12),(11,18,-3)))
|
|
|
@ -1 +0,0 @@
|
||||||
((3,8,(7,(3,0,7),(8,8,2))),(4,(7,9,8),8),(((3,6,4),2,6),((9,2,9),4,7,(6,4,5)),4,(6,4,5)))
|
|
|
@ -1 +1 @@
|
||||||
(((-2,-11,10),(2,15,13),(-1,-16,-16)),((-20,13,20),(-13,7,-19),(7,3,4)),((-20,-9,-14),(-4,-6,-12),(11,18,-3)))
|
((3,8,(7,(3,0,7),(8,8,2))),(4,(7,9,8),8),(((3,6,4),2,6),((9,2,9),4,7,(6,4,5)),4,(6,4,5)))
|
||||||
|
|
4
hw2/hw2pr3/horn.txt
Normal file
4
hw2/hw2pr3/horn.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
B:-A.
|
||||||
|
D:-B,C.
|
||||||
|
A.
|
||||||
|
C.
|
7
hw2/hw2pr3/horn2.txt
Normal file
7
hw2/hw2pr3/horn2.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
Q:-P.
|
||||||
|
P:-L,M.
|
||||||
|
M:-B,L.
|
||||||
|
L:-A,P.
|
||||||
|
L:-A,B.
|
||||||
|
A.
|
||||||
|
B.
|
4
hw2/hw2pr3/horn3.txt
Normal file
4
hw2/hw2pr3/horn3.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
A:-B.
|
||||||
|
:-B.
|
||||||
|
C:-D.
|
||||||
|
D:-.
|
177
hw2/hw2pr3/hw2pr3.cpp
Normal file
177
hw2/hw2pr3/hw2pr3.cpp
Normal file
|
@ -0,0 +1,177 @@
|
||||||
|
// Name: Alexander Huddleston UIN: 223000555
|
||||||
|
// CSCE 420
|
||||||
|
// Due: 11:59 P.M. Monday, October 30, 2017
|
||||||
|
// hw2pr1.cpp
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void extractFacts(vector<string> *kb, string *kt, string *kf)
|
||||||
|
{
|
||||||
|
for(int s = kb->size() - 1; s >= 0; s--)
|
||||||
|
{
|
||||||
|
for(int c = kb->at(s).length() - 1; c >= 0; c--)
|
||||||
|
{
|
||||||
|
if(kb->at(s).at(c) == ',')
|
||||||
|
{
|
||||||
|
kb->at(s).erase(kb->at(s).begin() + c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(kb->at(s).length() > 1 && (kb->at(s).at(1) == '.'))
|
||||||
|
{
|
||||||
|
*kt = kb->at(s).at(0) + *kt;
|
||||||
|
kb->erase(kb->begin() + s);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(kb->at(s).length() > 3 && (kb->at(s).at(3) == '.'))
|
||||||
|
{
|
||||||
|
if(kb->at(s).at(0) == ':')
|
||||||
|
{
|
||||||
|
*kf = kb->at(s).at(2) + *kf;
|
||||||
|
kb->erase(kb->begin() + s);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*kt = kb->at(s).at(0) + *kt;
|
||||||
|
kb->erase(kb->begin() + s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void makeDecisions(vector<string> *kb, string *kt, string *kf)
|
||||||
|
{
|
||||||
|
int found = -1;
|
||||||
|
|
||||||
|
for(char f : *kf)
|
||||||
|
{
|
||||||
|
for(int s = kb->size() - 1; s >= 0; s--)
|
||||||
|
{
|
||||||
|
found = kb->at(s).find(f);
|
||||||
|
|
||||||
|
if(found == -1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(found == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
kb->erase(kb->begin() + s);
|
||||||
|
|
||||||
|
if(kb->size() < 1)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
found = -1;
|
||||||
|
|
||||||
|
for(int t = 0; t < kt->size(); t++)
|
||||||
|
{
|
||||||
|
for(int s = kb->size() - 1; s >= 0; s--)
|
||||||
|
{
|
||||||
|
found = kb->at(s).find(kt->at(t));
|
||||||
|
|
||||||
|
if(found == -1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(found == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
kb->at(s).erase(kb->at(s).begin() + found);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(kb->at(s).size() > 3 && (kb->at(s).at(3) == '.'))
|
||||||
|
{
|
||||||
|
if(kt->find(kb->at(s).at(0)) == -1)
|
||||||
|
{
|
||||||
|
*kt += kb->at(s).at(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
kb->erase(kb->begin() + s);
|
||||||
|
|
||||||
|
if(kb->size() < 1)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string input;
|
||||||
|
cout << "Input a filename: " << endl;
|
||||||
|
cin >> input;
|
||||||
|
|
||||||
|
ifstream file;
|
||||||
|
string line;
|
||||||
|
|
||||||
|
// Let's try to open this file.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
file.open(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(exception e)
|
||||||
|
{
|
||||||
|
cerr << "File not found, or could not be opened." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vector of clauses.
|
||||||
|
vector<string> kb;
|
||||||
|
|
||||||
|
while(!file.eof())
|
||||||
|
{
|
||||||
|
getline(file, line);
|
||||||
|
|
||||||
|
if(line.length() > 0)
|
||||||
|
{
|
||||||
|
kb.push_back(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Input\n";
|
||||||
|
|
||||||
|
for(string s : kb)
|
||||||
|
{
|
||||||
|
cout << s << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
string kt;
|
||||||
|
string kf;
|
||||||
|
|
||||||
|
// Fill the known truths and falses.
|
||||||
|
// Remove them from the parsed knowledge base.
|
||||||
|
extractFacts(&kb, &kt, &kf);
|
||||||
|
|
||||||
|
makeDecisions(&kb, &kt, &kf);
|
||||||
|
|
||||||
|
cout << "Deductions: " << kt << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in a new issue