use typedefed types, BirthTime
This commit is contained in:
@@ -1,56 +1,20 @@
|
||||
#include <iostream>
|
||||
#include<fstream>
|
||||
#include"tree.h"
|
||||
#include"person.h"
|
||||
#include"help.h"
|
||||
#include<fstream>
|
||||
|
||||
|
||||
Tree::Tree() :
|
||||
numPeople(0)
|
||||
{
|
||||
treeName = new char[strLen("Unnamed") + 1];
|
||||
strCopy(treeName, "Unnamed");
|
||||
}
|
||||
|
||||
Tree::Tree(const char *Name):
|
||||
numPeople(0)
|
||||
{
|
||||
treeName = new char[strLen(Name) + 1];
|
||||
strCopy(treeName, Name);
|
||||
}
|
||||
|
||||
Tree::Tree(const Tree& other) :
|
||||
numPeople(other.numPeople)
|
||||
{
|
||||
treeName = new char[strLen(other.treeName) + 1];
|
||||
strCopy(treeName, other.treeName);
|
||||
people = other.people;
|
||||
relatives = other.relatives;
|
||||
relations = other.relations;
|
||||
}
|
||||
|
||||
Tree& Tree::operator= (const Tree& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
rename(other.treeName);
|
||||
numPeople = other.numPeople;
|
||||
people = other.people;
|
||||
relatives = other.relatives;
|
||||
relations = other.relations;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tree::~Tree()
|
||||
{
|
||||
delete[] treeName;
|
||||
}
|
||||
#include"help.h"
|
||||
|
||||
using std::cout;
|
||||
|
||||
|
||||
Tree::Tree()
|
||||
: treeName("Unnamed")
|
||||
, numPeople(0)
|
||||
{}
|
||||
|
||||
Tree Tree::operator+ (const Tree& other)//
|
||||
{
|
||||
Tree result(mergeStr(treeName, other.treeName)); //The tree resulting from the addition
|
||||
Tree result(treeName + other.treeName); //The tree resulting from the addition
|
||||
unsigned *newId = new unsigned[other.numPeople]; //The indexes members of the II tree will have in result
|
||||
|
||||
unsigned idCounter = numPeople;
|
||||
@@ -191,17 +155,16 @@ Tree& Tree::operator-= (const Tree& other)
|
||||
|
||||
void Tree::rename(const char* newName)
|
||||
{
|
||||
delete[] treeName;
|
||||
treeName = new char[strLen(newName) + 1];
|
||||
strCopy(treeName, newName);
|
||||
treeName = newName;
|
||||
}
|
||||
|
||||
unsigned Tree::findId(const char* name, const short year, const unsigned char month, const unsigned char day)const
|
||||
{
|
||||
{
|
||||
//TODO
|
||||
//Person sought(name, day, month, year,)
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
{
|
||||
if (strComp(name, people[i].name) && (year == people[i].year || !year || !people[i].year) && (month == people[i].month || !month || !people[i].month) && (day == people[i].day || !day || people[i].day))
|
||||
if (name == people[i].name) //&& (year == people[i].year || !year || !people[i].year) && (month == people[i].month || !month || !people[i].month) && (day == people[i].day || !day || people[i].day))
|
||||
return i;
|
||||
}
|
||||
return Nobody;
|
||||
@@ -226,35 +189,29 @@ unsigned Tree::findCommonAncestor(const unsigned firstId, const unsigned secondI
|
||||
commonAncestorRec(secondId, visited);
|
||||
|
||||
unsigned oldestId = Nobody;
|
||||
short oldestBday = SHRT_MAX;
|
||||
U16 oldestBday = SHRT_MAX;
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
{
|
||||
if (visited[i] == 2 && people[i].year<oldestBday)
|
||||
if (visited[i] == 2 && people[i].birth.year<oldestBday)
|
||||
{
|
||||
oldestBday = people[i].year;
|
||||
oldestBday = people[i].birth.year;
|
||||
oldestId = i;
|
||||
}
|
||||
}
|
||||
return oldestId;
|
||||
}
|
||||
|
||||
void Tree::saveTree()const
|
||||
void Tree::saveTree() const
|
||||
{
|
||||
char* fileName = mergeStr(treeName, ".people");
|
||||
std::ofstream peopleFile(fileName, std::ios::binary);
|
||||
delete[] fileName;
|
||||
std::ofstream peopleFile(treeName+ ".people", std::ios::binary);
|
||||
|
||||
peopleFile.write(reinterpret_cast<const char*>(&numPeople), sizeof(numPeople));
|
||||
for (unsigned i = 0; i < numPeople; ++i)
|
||||
people[i].save(peopleFile);
|
||||
peopleFile.close();
|
||||
|
||||
fileName = mergeStr(treeName, ".relatives");
|
||||
std::ofstream relativeFile(fileName, std::ios::binary);
|
||||
delete[] fileName;
|
||||
fileName = mergeStr(treeName, ".relations");
|
||||
std::ofstream relationFile(fileName, std::ios::binary);
|
||||
delete[] fileName;
|
||||
std::ofstream relativeFile(treeName + ".relatives", std::ios::binary);
|
||||
std::ofstream relationFile(treeName + ".relations", std::ios::binary);
|
||||
|
||||
/*relativeFile.write((char*)&numPeople, sizeof(numPeople));
|
||||
relationFile.write((char*)&numPeople, sizeof(numPeople));*/
|
||||
@@ -274,9 +231,7 @@ void Tree::saveTree()const
|
||||
|
||||
bool Tree::loadTree()
|
||||
{
|
||||
char* fileName = mergeStr(treeName, ".people");//c
|
||||
std::ifstream peopleFile(fileName, std::ios::binary);
|
||||
delete[] fileName;
|
||||
std::ifstream peopleFile(treeName + ".people", std::ios::binary);
|
||||
if (peopleFile.peek() == std::char_traits<char>::eof())
|
||||
{
|
||||
peopleFile.close();
|
||||
@@ -290,12 +245,8 @@ bool Tree::loadTree()
|
||||
people.sNum(numPeople);
|
||||
peopleFile.close();
|
||||
|
||||
fileName = mergeStr(treeName, ".relatives");
|
||||
std::ifstream relativeFile(fileName, std::ios::binary);
|
||||
delete[] fileName;
|
||||
fileName = mergeStr(treeName, ".relations");
|
||||
std::ifstream relationFile(fileName, std::ios::binary);
|
||||
delete[] fileName;
|
||||
std::ifstream relativeFile(treeName + ".relatives", std::ios::binary);
|
||||
std::ifstream relationFile(treeName + ".relations", std::ios::binary);
|
||||
|
||||
relatives.resize(numPeople);
|
||||
relations.resize(numPeople);
|
||||
@@ -318,9 +269,9 @@ bool Tree::loadTree()
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Tree::addPerson(const char *name, short year, unsigned char month, unsigned char day, bool isMale, unsigned father, unsigned mother)
|
||||
void Tree::addPerson(const char *name, BirthTime birth, bool isMale, unsigned father, unsigned mother)
|
||||
{
|
||||
Person newPerson(name, day, month, year, isMale);
|
||||
Person newPerson(name, birth, isMale);
|
||||
people.push(newPerson);
|
||||
Array<unsigned> newRelative;
|
||||
relatives.push(newRelative);
|
||||
@@ -490,6 +441,13 @@ void Tree::removeRelation(const unsigned firstId, const unsigned secondId)
|
||||
}
|
||||
|
||||
|
||||
void Tree::print() const
|
||||
{
|
||||
//for(const auto& member: people)
|
||||
// member.print();
|
||||
for(int i=0; i<numPeople;++i)
|
||||
people[i].print();
|
||||
}
|
||||
|
||||
void Tree::printRel(const unsigned id)const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user