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/hw2pr2/hw2pr2.cpp

254 lines
5.8 KiB
C++
Raw Permalink Normal View History

2017-10-25 07:34:40 -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 <math.h>
2017-10-27 11:42:51 -05:00
#include <limits.h>
#include <tuple>
2017-10-25 07:34:40 -05:00
#include "node.h"
using namespace std;
2017-10-27 11:42:51 -05:00
// Perform min/max on this tree.
void minmax(node *n)
2017-10-25 07:34:40 -05:00
{
2017-10-30 11:45:17 -05:00
2017-10-27 11:42:51 -05:00
if(n->getLevel() % 2 == 0)
{
int max = n->getValue();
2017-10-31 01:05:02 -05:00
2017-10-27 11:42:51 -05:00
if(n->getChildren().size() > 0)
{
for(int c = 0; c < n->getChildren().size(); c++)
{
2017-10-31 01:05:02 -05:00
n->getChildAt(c)->setBeta(n->getBeta());
n->getChildAt(c)->setAlpha(n->getAlpha());
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
//cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
minmax(n->getChildAt(c));
2017-10-25 07:34:40 -05:00
2017-10-27 11:42:51 -05:00
if(n->getChildAt(c)->getValue() > max)
{
max = n->getChildAt(c)->getValue();
}
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
if(n->getChildAt(c)->getValue() > n->getAlpha())
{
n->setAlpha(n->getChildAt(c)->getValue());
}
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
if(n->getAlpha() >= n->getBeta())
{
cout << "Beta pruning" << endl;
break;
}
2017-10-27 11:42:51 -05:00
}
}
2017-10-31 01:05:02 -05:00
2017-10-27 11:42:51 -05:00
n->setValue(max);
}
else
2017-10-25 07:34:40 -05:00
{
2017-10-27 11:42:51 -05:00
int min = n->getValue();
if(n->getChildren().size() > 0)
{
for(int c = 0; c < n->getChildren().size(); c++)
{
2017-10-31 01:05:02 -05:00
n->getChildAt(c)->setBeta(n->getBeta());
n->getChildAt(c)->setAlpha(n->getAlpha());
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
//cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
minmax(n->getChildAt(c));
2017-10-27 11:42:51 -05:00
if(n->getChildAt(c)->getValue() < min)
{
min = n->getChildAt(c)->getValue();
}
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
if(n->getChildAt(c)->getValue() < n->getBeta())
{
n->setBeta(n->getChildAt(c)->getValue());
}
2017-10-30 11:45:17 -05:00
2017-10-31 01:05:02 -05:00
if(n->getBeta() <= n->getAlpha())
{
cout << "Alpha pruning." << endl;
break;
}
2017-10-27 11:42:51 -05:00
}
}
2017-10-31 01:05:02 -05:00
2017-10-27 11:42:51 -05:00
n->setValue(min);
}
}
// 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;
2017-10-25 07:34:40 -05:00
}
2017-10-27 11:42:51 -05:00
}
tuple<node, string> makeTree(string input, node n, int level)
{
2017-10-30 11:45:17 -05:00
bool negate = false;
2017-10-27 11:42:51 -05:00
char tempc;
string tempn = "";
2017-10-25 07:34:40 -05:00
while(input.length() != 0)
{
2017-10-27 11:42:51 -05:00
tempc = input[0];
if(input.length() > 0)
{
input = input.substr(1);
}
else
{
input = "";
}
// If the char is a number.
if(isdigit(tempc))
2017-10-25 07:34:40 -05:00
{
2017-10-27 11:42:51 -05:00
tempn += tempc;
2017-10-25 07:34:40 -05:00
}
2017-10-30 11:45:17 -05:00
else if(tempc == '-')
{
negate = true;
}
2017-10-25 08:45:19 -05:00
// If the char is a comma.
else if(tempc == ',')
{
2017-10-27 11:42:51 -05:00
if(tempn.length() > 0)
{
node tempnode;
tempnode.setLevel(level);
2017-10-31 01:05:02 -05:00
int negatemp = stoi(tempn);
if(negate)
{
negatemp = -negatemp;
}
2017-10-30 11:45:17 -05:00
tempnode.setValue(negatemp);
2017-10-27 11:42:51 -05:00
tempnode.setParent(&n);
n.addChild(tempnode);
tempn = "";
2017-10-31 01:05:02 -05:00
negate = false;
2017-10-27 11:42:51 -05:00
}
2017-10-25 08:45:19 -05:00
}
// If the char is a close paranthesis.
else if(tempc == ')')
{
2017-10-27 11:42:51 -05:00
if(tempn.length() > 0)
{
node tempnode;
tempnode.setLevel(level);
2017-10-31 01:05:02 -05:00
int negatemp = stoi(tempn);
if(negate)
{
negatemp = -negatemp;
}
2017-10-30 11:45:17 -05:00
tempnode.setValue(negatemp);
2017-10-27 11:42:51 -05:00
tempnode.setParent(&n);
n.addChild(tempnode);
tempn = "";
2017-10-31 01:05:02 -05:00
negate = false;
2017-10-27 11:42:51 -05:00
}
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);
2017-10-25 08:45:19 -05:00
}
2017-10-25 07:34:40 -05:00
}
2017-10-27 11:42:51 -05:00
tuple<node, string> output(n, input);
return output;
2017-10-25 07:34:40 -05:00
}
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
{
2017-10-30 20:07:35 -05:00
file.open("hw2pr2_data.txt");
2017-10-25 07:34:40 -05:00
}
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;
}
2017-10-27 11:42:51 -05:00
node root;
root.setLevel(0);
root.setValue(INT_MIN);
tuple<node, string> tree;
tree = makeTree(input.substr(1), root, 1);
root = get<0>(tree);
2017-10-31 01:05:02 -05:00
//printTree(root);
2017-10-27 11:42:51 -05:00
minmax(&root);
2017-10-31 01:05:02 -05:00
//printTree(root);
cout << root.getValue() << endl;
2017-10-27 11:42:51 -05:00
2017-10-25 07:34:40 -05:00
return 0;
}