This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
csce420pine64backup/hw2/hw2pr3/hw2pr3.cpp

178 lines
3.5 KiB
C++
Raw Normal View History

2017-10-31 00:48:38 -05:00
// 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;
}