-
-
Save pemby/1923e1a04434650f2ba7 to your computer and use it in GitHub Desktop.
main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "SkillTree.h" | |
using namespace std; | |
int main (int argc, const char *argv[]) | |
{ | |
SkillTree student ("Student"); | |
student.Display (cout); | |
// not sure what I am doing wrong at this point... | |
student.AddSkill ("Alphabet", "Mastery of letters and sounds", 0); | |
return 0; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Skill.cpp | |
// lab420 | |
// | |
// Created by admini on 3/19/15. | |
// Copyright (c) 2015 school. All rights reserved. | |
// | |
#include "Skill.h" | |
Skill::Skill (std::ostream& ci_out_stream) | |
{ | |
} | |
Skill::~Skill() | |
{ | |
//dtor | |
} | |
Skill::Skill (const Skill& other) | |
{ | |
//copy ctor | |
name = other.name; | |
description = other.description; | |
level = other.level; | |
} | |
Skill& Skill::operator= (const Skill& rhs) | |
{ | |
if (this == &rhs) | |
{ | |
return *this; // handle self assignment | |
} | |
//assignment operator | |
name = rhs.name; | |
description = rhs.description; | |
level = rhs.level; | |
parentName = rhs.parentName; | |
return *this; | |
} | |
Skill::Skill() : name (""), description (""), level (-1) | |
{ | |
} | |
Skill::Skill (std::string ci_Skill_Name) | |
{ | |
name = ci_Skill_Name; | |
} | |
std::ostream& operator<< (std::ostream& out, Skill& aSkill) | |
{ | |
std::cout << aSkill.Getname() << std::endl; | |
return out; | |
} | |
Skill::Skill (char *ci_Skill_Name, char *ci_Description, int ci_level) : parentName ("Root") | |
// adding name "fake root" so I have a label - to keep track of root. // | |
{ | |
name = ci_Skill_Name; | |
description = ci_Description; | |
level = ci_level; | |
} | |
Skill::Skill (char *ci_Skill_Name, char *ci_Description, int ci_level, char *ci_parentName) | |
{ | |
name = ci_Skill_Name; | |
description = ci_Description; | |
level = ci_level; | |
parentName = ci_parentName; | |
} | |
Skill::Skill ( std::string ci_Skill_Name, std::string ci_Description, int ci_level) | |
// adding name "fake root" so I have a label - to keep track of root. // | |
{ | |
name = ci_Skill_Name; | |
description = ci_Description; | |
level = ci_level; | |
} | |
Skill::Skill (std::string ci_Skill_Name, std::string ci_Description, int ci_level, std::string ci_parentName) | |
{ | |
name = ci_Skill_Name; | |
description = ci_Description; | |
level = ci_level; | |
parentName = ci_parentName; | |
} | |
void Skill::Display (std::ostream& ci_Skill_out) | |
{ | |
Skill::Skill_out = &ci_Skill_out; | |
*Skill_out << " - " << this->name << " -- " << this->description << " " << "[Lvl: " << this->level << "]" << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Skill | |
{ | |
public: | |
Skill (std::ostream& ci_out_stream); | |
Skill(); | |
Skill (std::string ci_Skill_Name); | |
Skill (std::string ci_Skill_Name, std::string ci_Description, int ci_level); | |
Skill (char *ci_Skill_Name, char *ci_Description, int ci_level); | |
Skill (std::string ci_Skill_Name, std::string ci_Description, int ci_level, std::string ci_parentName); | |
Skill (char *ci_Skill_Name, char *ci_Description, int ci_level, char *ci_parentName); | |
virtual ~Skill(); | |
Skill (const Skill& other); | |
Skill& operator= (const Skill& other); | |
void Display (std::ostream& ci_Skill_out); | |
std::string GetParentName() | |
{ | |
return parentName; | |
} | |
void SetParentName (const std::string ci_string) | |
{ | |
parentName = ci_string; | |
} | |
std::string const Getname() | |
{ | |
return name; | |
} | |
friend std::ostream& operator<< (std::ostream& out, const Skill& aSkill); | |
std::string name; | |
std::string description; | |
int level; | |
std::string parentName; | |
std::ostream *Skill_out; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SkillTree.h" | |
SkillTree::SkillTree() | |
{ | |
init_gtree(); | |
} | |
void SkillTree::init_gtree() | |
{ | |
root_ptr = nullptr; | |
} | |
SkillTree::SkillTree (char *input) | |
{ | |
this->name = input; | |
} | |
SkillTree *SkillTree::leftchild() | |
{ | |
return this->leftchild(); | |
} | |
SkillTree *SkillTree::rightchild() | |
{ | |
return this-> rightchild(); | |
} | |
Skill *SkillTree::Getdata() | |
{ | |
return this-> Getdata(); | |
} | |
SkillTree *SkillTree::title() | |
{ | |
return this->title(); | |
} | |
void SkillTree::setLeftChild (SkillTree leftchild) | |
{ | |
leftchild = leftchild; | |
} | |
void SkillTree::setRightChild (SkillTree rightchild) | |
{ | |
rightchild = rightchild; | |
} | |
void SkillTree::setelement (Skill element) | |
{ | |
setelement (element); | |
} | |
int SkillTree::numnodes (SkillTree tree) | |
{ | |
int descendants = 0; | |
SkillTree tmp; | |
if (&tree == NULL) | |
{ | |
return 0; | |
} | |
for (tmp = *tree.leftchild(); &tmp != NULL; tmp = *rightchild() ) | |
{ | |
descendants = descendants + numnodes (tmp); | |
} | |
return descendants; | |
} | |
int SkillTree::height (SkillTree tree) | |
{ | |
if (&tree == NULL) | |
{ | |
return 0; | |
} | |
int childHeight = 0; | |
for (SkillTree tmp = *tree.leftchild(); &tmp != NULL; tmp = *tmp.rightchild() ) | |
{ | |
childHeight = std::max (childHeight, height (tmp) ); | |
} | |
return childHeight; | |
} | |
int SkillTree::numLeaves (SkillTree tree) | |
{ | |
if (&tree == NULL) | |
{ | |
return 0; | |
} | |
if (tree.leftchild() == NULL) | |
{ | |
return 1 + numLeaves (*tree.rightchild() ); | |
} | |
return numLeaves (*leftchild() ) + numLeaves (*rightchild() ); | |
} | |
// not using. | |
void SkillTree::print (SkillTree tree, int offset) | |
{ | |
if (&tree != NULL) | |
{ | |
for (int i = 0; i < offset; i++) | |
{ | |
std::cout << "\t"; | |
std::cout << tree.Getdata(); | |
print (*tree.leftchild(), offset + 1); | |
print (*tree.rightchild(), offset); | |
} | |
} | |
} | |
BOOLEAN SkillTree::is_empty() | |
{ | |
return ( this == NULL ); | |
} | |
void SkillTree::print_tree() | |
{ | |
print_gtree_sibil ( root_ptr ); | |
} | |
void SkillTree::print_gtree_sibil ( GTREE_NODE_PTR tree_ptr ) | |
{ | |
GTREE_NODE_PTR curr_ptr = tree_ptr; | |
static int n = 0; | |
if ( root_ptr == NULL ) | |
{ | |
printf ( "\n General tree is empty. \n" ); | |
} | |
while ( curr_ptr != NULL ) | |
{ | |
if ( n > 0 ) | |
{ | |
std::cout << tree_ptr->data.Getname() << std::endl; | |
} | |
n++; | |
print_gtree_sibil ( curr_ptr->sibling_listptr ); | |
if ( &curr_ptr->data) | |
{ | |
std::cout << tree_ptr->data.Getname() << std::endl; | |
} | |
curr_ptr = curr_ptr->first_childptr; | |
} | |
} | |
void SkillTree::Display (std::ostream& ci_out_stream) | |
{ | |
SkillTree::Skill_out = &ci_out_stream; | |
*Skill_out << "Skill Tree: " << this->Getname() << std::endl; | |
if (is_empty() ) | |
{ | |
*Skill_out << " Empty " << std::endl; | |
} | |
else | |
{ | |
print_tree(); | |
} | |
} | |
void SkillTree::AddSkill (char *inname, char *indesc, int inlevel) | |
{ | |
GTREE_NODE_PTR temp = get_root(); | |
Skill localSkill (inname, indesc, inlevel); | |
this->setelement (localSkill); | |
} | |
void SkillTree::AddSkill (char *name, char *desc, int level, char *parentName) | |
{ | |
Skill temp (name, desc, level, parentName); | |
this->setelement (temp); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment