use typedefed types, BirthTime
This commit is contained in:
+1
-1
@@ -1 +1 @@
|
|||||||
g++ -Ofast -flto -fipa-pta -s *.cpp\
|
g++ -std=gnu++20 -Ofast -flto -fipa-pta -s -DNDEBUG *.cpp\
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
g++ *.cpp
|
g++ -std=gnu++20 -g -Og -Wall *.cpp
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "common/basicTypes.h"
|
||||||
|
|
||||||
enum Relation : char;
|
enum Relation : U8;
|
||||||
|
|
||||||
unsigned strLen(const char* str);
|
unsigned strLen(const char* str);
|
||||||
void strCopy(char *to, const char* from);
|
void strCopy(char *to, const char* from);
|
||||||
bool strComp(const char* firts, const char* second); //0 - diffrent 1 - same
|
bool strComp(const char* firts, const char* second); //0 - diffrent 1 - same
|
||||||
char* mergeStr(const char* first, const char* second);
|
char* mergeStr(const char* first, const char* second);
|
||||||
Relation strToRel(const char* str);
|
Relation strToRel(const char* str);
|
||||||
|
|||||||
@@ -1,238 +1,261 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "tree.h"
|
#include "tree.h"
|
||||||
#include "help.h"
|
#include "help.h"
|
||||||
|
|
||||||
int main()
|
using std::cin;
|
||||||
{
|
using std::cout;
|
||||||
/*Tree test1("Dechkovi"); //addTree Dechkovi addTree Karamanovi load 0 load 1 (to work with these example trees)
|
|
||||||
Tree test2("Karamanovi");
|
int main()
|
||||||
|
{
|
||||||
test1.addPerson("Rumen", 1968, 8, 9, 0);
|
/*Tree test1("Dechkovi"); //addTree Dechkovi addTree Karamanovi load 0 load 1 (to work with these example trees)
|
||||||
test1.addPerson("Nadejda", 1972, 12, 15, 1);
|
Tree test2("Karamanovi");
|
||||||
test1.addRelation(0, Husband, 1);
|
|
||||||
test1.addPerson("Venelin", 1998, 6, 9, 0, 0, 1);
|
test1.addPerson("Rumen", 1968, 8, 9, 0);
|
||||||
test1.addPerson("Vasil", 2001, 1, 16, 0, 0, 1);
|
test1.addPerson("Nadejda", 1972, 12, 15, 1);
|
||||||
test1.addRelation(2, Brother, 3);
|
test1.addRelation(0, Husband, 1);
|
||||||
test1.addPerson("Maria-Veronika", 2009, 9, 3, 1, 0, 1);
|
test1.addPerson("Venelin", 1998, 6, 9, 0, 0, 1);
|
||||||
test1.addRelation(2, Brother, 4);
|
test1.addPerson("Vasil", 2001, 1, 16, 0, 0, 1);
|
||||||
test1.addRelation(3, Brother, 4);
|
test1.addRelation(2, Brother, 3);
|
||||||
|
test1.addPerson("Maria-Veronika", 2009, 9, 3, 1, 0, 1);
|
||||||
test2.addPerson("Vasil Dimitrov", 1941, 5, 18, 0);
|
test1.addRelation(2, Brother, 4);
|
||||||
test2.addPerson("Maria", 0, 0, 0, 1);
|
test1.addRelation(3, Brother, 4);
|
||||||
test2.addRelation(0, Husband, 1);
|
|
||||||
test2.addPerson("Dimitar", 0, 0, 0, 0, 0, 1);
|
test2.addPerson("Vasil Dimitrov", 1941, 5, 18, 0);
|
||||||
test2.addPerson("Nadejda", 1972, 12, 15, 1, 0, 1);
|
test2.addPerson("Maria", 0, 0, 0, 1);
|
||||||
test2.addRelation(2, Brother, 3);
|
test2.addRelation(0, Husband, 1);
|
||||||
test2.addPerson("Branimira", 0, 0, 0, 1);
|
test2.addPerson("Dimitar", 0, 0, 0, 0, 0, 1);
|
||||||
test2.addRelation(2, Husband, 4);
|
test2.addPerson("Nadejda", 1972, 12, 15, 1, 0, 1);
|
||||||
test2.addPerson("Ioan", 0, 0, 0, 0, 2, 4);
|
test2.addRelation(2, Brother, 3);
|
||||||
test2.addPerson("Rumiana", 0, 0, 0, 1, 2, 4);
|
test2.addPerson("Branimira", 0, 0, 0, 1);
|
||||||
test2.addRelation(5, Brother, 6);
|
test2.addRelation(2, Husband, 4);
|
||||||
|
test2.addPerson("Ioan", 0, 0, 0, 0, 2, 4);
|
||||||
test1.saveTree();
|
test2.addPerson("Rumiana", 0, 0, 0, 1, 2, 4);
|
||||||
test2.saveTree();*/
|
test2.addRelation(5, Brother, 6);
|
||||||
|
|
||||||
|
test1.saveTree();
|
||||||
Array<Tree> trees;
|
test2.saveTree();*/
|
||||||
|
|
||||||
bool quit=0;
|
Tree firstTree("firstTree");
|
||||||
char input[128];
|
firstTree.addPerson("Адам,", {0, 1, 6}, true);
|
||||||
short i; //Number of the entered command
|
firstTree.addPerson("Ева", {0, 1, 6}, false);
|
||||||
const short numCom = 23;
|
firstTree.addRelation(0, Husband, 1);
|
||||||
const char* const commandList[] =
|
|
||||||
{
|
firstTree.addPerson("Кайн", {}, true, 0, 1);
|
||||||
"quit", //0
|
firstTree.addPerson("Авел", {}, true, 0, 1);
|
||||||
"rename", //1
|
firstTree.addPerson("Сит", 230, true, 0, 1);
|
||||||
"load", //2
|
firstTree.addRelation(2, Brother, 3);
|
||||||
"save", //3
|
firstTree.addRelation(2, Brother, 4);
|
||||||
"addPerson", //4
|
|
||||||
"addPersonWP", //5 Adds a person and connects him to his parents, by their number
|
firstTree.print();
|
||||||
"addRelation", //6 Adds a relation between two people input by name
|
firstTree.saveTree();
|
||||||
"addRelationID", //7 -||- by number
|
|
||||||
"findID", //8
|
|
||||||
"equate", //9 Enter the number of two trees, the second becomes equal to the first
|
Array<Tree> trees;
|
||||||
"combine", //10
|
|
||||||
"subtract", //11
|
bool quit = false;
|
||||||
"removePerson", //12
|
char input[128];
|
||||||
"removePersonID", //13
|
short i; //Number of the entered command
|
||||||
"removeRelation", //14
|
const short numCom = 24;
|
||||||
"removeRelationID", //15
|
const char* const commandList[] =
|
||||||
"addTree", //16
|
{
|
||||||
"prtRelatives", //17
|
"quit", //0
|
||||||
"prtMember", //18
|
"rename", //1
|
||||||
"prtAncestors", //19
|
"load", //2
|
||||||
"chooseTree", //20
|
"save", //3
|
||||||
"prtCommonAncestor",//21
|
"addPerson", //4
|
||||||
"prtOldestAncestors"//22
|
"addPersonWP", //5 Adds a person and connects him to his parents, by their number
|
||||||
};
|
"addRelation", //6 Adds a relation between two people input by name
|
||||||
|
"addRelationID", //7 -||- by number
|
||||||
unsigned curTree = 0, //Currently selected tree ID
|
"findID", //8
|
||||||
id, secondId; //Indexes
|
"equate", //9 Enter the number of two trees, the second becomes equal to the first
|
||||||
Tree *newTree;
|
"combine", //10
|
||||||
char name[128], secondName[128];
|
"subtract", //11
|
||||||
unsigned char day, month;
|
"removePerson", //12
|
||||||
short year;
|
"removePersonID", //13
|
||||||
bool sex;
|
"removeRelation", //14
|
||||||
unsigned father, mother;
|
"removeRelationID", //15
|
||||||
|
"addTree", //16
|
||||||
do
|
"prtRelatives", //17
|
||||||
{
|
"prtMember", //18
|
||||||
std::cout << "Enter a command\n\n";
|
"prtAncestors", //19
|
||||||
std::cin >> input;
|
"chooseTree", //20
|
||||||
for (i = 0; i < numCom; ++i)
|
"prtCommonAncestor", //21
|
||||||
{
|
"prtOldestAncestors",//22
|
||||||
if (strComp(input, commandList[i]))
|
"prtTree" //23
|
||||||
break;
|
};
|
||||||
}
|
|
||||||
|
unsigned curTree = 0, //Currently selected tree ID
|
||||||
switch (i)
|
id, secondId; //Indexes
|
||||||
{
|
Tree *newTree;
|
||||||
default:
|
char name[128], secondName[128];
|
||||||
std::cout << "Unknown command, try again\n\n";
|
U8 day, month; //TODO add h m
|
||||||
break;
|
U16 year;
|
||||||
case 0: //quit
|
bool sex;
|
||||||
quit = 1;
|
unsigned father, mother;
|
||||||
break;
|
|
||||||
case 1: //rename
|
do
|
||||||
std::cout << "Enter new name\n\n";
|
{
|
||||||
std::cin >> name;
|
std::cout << "Enter a command\n";
|
||||||
trees[curTree].rename(name);
|
std::cin >> input;
|
||||||
break;
|
for (i = 0; i < numCom; ++i)
|
||||||
case 2: //load
|
{
|
||||||
if (!trees[curTree].loadTree())
|
if (strComp(input, commandList[i]))
|
||||||
std::cout << "File doesnt exist or is empty.\n\n";
|
break;
|
||||||
break;
|
}
|
||||||
case 3: //save
|
|
||||||
trees[curTree].saveTree();
|
switch (i)
|
||||||
break;
|
{
|
||||||
case 4: //addPerson
|
default:
|
||||||
std::cin >> name >> day >> month >> year >> sex;
|
std::cout << "Unknown command, try again\n\n";
|
||||||
trees[curTree].addPerson(name, year, month, day, sex);
|
break;
|
||||||
break;
|
case 0: //quit
|
||||||
case 5: //addPersonWP
|
quit = 1;
|
||||||
std::cin >> name >> day >> month >> year >> sex >> father >> mother;
|
break;
|
||||||
trees[curTree].addPerson(name, year, month, day, sex, father, mother);
|
case 1: //rename
|
||||||
break;
|
std::cout << "Enter new name\n\n";
|
||||||
case 6: //addRelation
|
std::cin >> name;
|
||||||
std::cin >> name >> input >> secondName;
|
trees[curTree].rename(name);
|
||||||
while (!strToRel(input))
|
break;
|
||||||
{
|
case 2: //load
|
||||||
std::cout << "Invalid Relation, try again!\n";
|
cin >> input;
|
||||||
std::cin >> input;
|
trees.push(Tree(input));
|
||||||
}
|
if (!trees[trees.gNum() - 1].loadTree())
|
||||||
if(!trees[curTree].addRelation(name, strToRel(input), secondName))
|
std::cout << "File doesnt exist or is empty.\n\n";
|
||||||
std::cout << "Failure! One or both of the relatives don't exist.\n\n";
|
break;
|
||||||
break;
|
case 3: //save
|
||||||
case 7: //addRelationID
|
trees[curTree].saveTree();
|
||||||
std::cin >> id >> input >> secondId;
|
break;
|
||||||
while (!strToRel(input))
|
case 4: //addPerson
|
||||||
{
|
std::cin >> name >> day >> month >> year >> sex;
|
||||||
std::cout << "Invalid Relation, try again!\n";
|
trees[curTree].addPerson(name, BirthTime(year, month, day) , sex);
|
||||||
std::cin >> input;
|
break;
|
||||||
}
|
case 5: //addPersonWP
|
||||||
if(trees[curTree].addRelation(id, strToRel(input), secondId))
|
std::cin >> name >> day >> month >> year >> sex >> father >> mother;
|
||||||
std::cout << "Failure! One or both of the relatives don't exist.\n\n";
|
trees[curTree].addPerson(name, BirthTime(year, month, day), sex, father, mother);
|
||||||
break;
|
break;
|
||||||
case 8: //findID
|
case 6: //addRelation
|
||||||
std::cin >> name;
|
std::cin >> name >> input >> secondName;
|
||||||
id = trees[curTree].findId(name);
|
while (!strToRel(input))
|
||||||
if (id == Nobody)
|
{
|
||||||
std::cout << "No such person in tree " << curTree <<"\n\n";
|
std::cout << "Invalid Relation, try again!\n";
|
||||||
else
|
std::cin >> input;
|
||||||
std::cout << id << "\n\n";
|
}
|
||||||
break;
|
if(!trees[curTree].addRelation(name, strToRel(input), secondName))
|
||||||
case 9: //equate
|
std::cout << "Failure! One or both of the relatives don't exist.\n\n";
|
||||||
std::cin >> id >> secondId;
|
break;
|
||||||
trees[id] = trees[secondId];
|
case 7: //addRelationID
|
||||||
break;
|
std::cin >> id >> input >> secondId;
|
||||||
case 10: //combine
|
while (!strToRel(input))
|
||||||
std::cin >> id >> secondId;
|
{
|
||||||
trees[id] += trees[secondId];
|
std::cout << "Invalid Relation, try again!\n";
|
||||||
break;
|
std::cin >> input;
|
||||||
case 11: //subtract
|
}
|
||||||
std::cin >> id >> secondId;
|
if(trees[curTree].addRelation(id, strToRel(input), secondId))
|
||||||
trees[id] -= trees[secondId];
|
std::cout << "Failure! One or both of the relatives don't exist.\n\n";
|
||||||
break;
|
break;
|
||||||
case 12: //removePerson
|
case 8: //findID
|
||||||
std::cin >> name;
|
std::cin >> name;
|
||||||
if (trees[curTree].findId(name) != Nobody)
|
id = trees[curTree].findId(name);
|
||||||
trees[curTree].removePerson(name);
|
if (id == Nobody)
|
||||||
else
|
std::cout << "No such person in tree " << curTree <<"\n\n";
|
||||||
std::cout << "No such member!\n\n";
|
else
|
||||||
break;
|
std::cout << id << "\n\n";
|
||||||
case 13: //removePersonID
|
break;
|
||||||
std::cin >> id;
|
case 9: //equate
|
||||||
if (trees[curTree].gNumP() > id)
|
std::cin >> id >> secondId;
|
||||||
trees[curTree].removePerson(id);
|
trees[id] = trees[secondId];
|
||||||
else
|
break;
|
||||||
std::cout << "No such member!\n\n";
|
case 10: //combine
|
||||||
break;
|
std::cin >> id >> secondId;
|
||||||
case 14: //removeRelation
|
trees[id] += trees[secondId];
|
||||||
std::cin >> name >> secondName;
|
break;
|
||||||
if (trees[curTree].gNumP() > trees[curTree].findId(name) && trees[curTree].gNumP() > trees[curTree].findId(secondName))
|
case 11: //subtract
|
||||||
trees[curTree].removeRelation(name, secondName);
|
std::cin >> id >> secondId;
|
||||||
else
|
trees[id] -= trees[secondId];
|
||||||
std::cout << "No such member!\n\n";
|
break;
|
||||||
break;
|
case 12: //removePerson
|
||||||
case 15: //removeRelationID
|
std::cin >> name;
|
||||||
std::cin >> id >> secondId;
|
if (trees[curTree].findId(name) != Nobody)
|
||||||
if (trees[curTree].gNumP() > id && trees[curTree].gNumP() > secondId)
|
trees[curTree].removePerson(name);
|
||||||
trees[curTree].removeRelation(id, secondId);
|
else
|
||||||
else
|
std::cout << "No such member!\n\n";
|
||||||
std::cout << "No such member\n\n";
|
break;
|
||||||
break;
|
case 13: //removePersonID
|
||||||
case 16: //addTree
|
std::cin >> id;
|
||||||
std::cin >> name;
|
if (trees[curTree].gNumP() > id)
|
||||||
newTree = new Tree(name);
|
trees[curTree].removePerson(id);
|
||||||
trees.push(*newTree);
|
else
|
||||||
delete newTree;
|
std::cout << "No such member!\n\n";
|
||||||
break;
|
break;
|
||||||
case 17: //printRelatives
|
case 14: //removeRelation
|
||||||
std::cin >> id;
|
std::cin >> name >> secondName;
|
||||||
if (id < trees[curTree].gNumP())
|
if (trees[curTree].gNumP() > trees[curTree].findId(name) && trees[curTree].gNumP() > trees[curTree].findId(secondName))
|
||||||
trees[curTree].printRel(id);
|
trees[curTree].removeRelation(name, secondName);
|
||||||
else
|
else
|
||||||
std::cout << "No such member\n\n";
|
std::cout << "No such member!\n\n";
|
||||||
break;
|
break;
|
||||||
case 18: //printMember
|
case 15: //removeRelationID
|
||||||
std::cin >> id;
|
std::cin >> id >> secondId;
|
||||||
if (id < trees[curTree].gNumP())
|
if (trees[curTree].gNumP() > id && trees[curTree].gNumP() > secondId)
|
||||||
trees[curTree].printMember(id);
|
trees[curTree].removeRelation(id, secondId);
|
||||||
else
|
else
|
||||||
std::cout << "No such member\n\n";
|
std::cout << "No such member\n\n";
|
||||||
break;
|
break;
|
||||||
case 19: //printAncestors
|
case 16: //addTree
|
||||||
std::cin >> id;
|
std::cin >> name;
|
||||||
/*if (id < trees[curTree].gNumP())
|
newTree = new Tree(name);
|
||||||
trees[curTree].printAncestors(id);
|
trees.push(*newTree);
|
||||||
else
|
delete newTree;
|
||||||
std::cout << "No such member\n\n";*/
|
break;
|
||||||
break;
|
case 17: //printRelatives
|
||||||
case 20: //chooseTree
|
std::cin >> id;
|
||||||
std::cout << "Enter tree ID\n\n";
|
if (id < trees[curTree].gNumP())
|
||||||
std::cin >> id;
|
trees[curTree].printRel(id);
|
||||||
if (id < trees.gNum())
|
else
|
||||||
curTree = id;
|
std::cout << "No such member\n\n";
|
||||||
else
|
break;
|
||||||
std::cout << "No such tree\n\n";
|
case 18: //printMember
|
||||||
break;
|
std::cin >> id;
|
||||||
case 21: //prtCommonAncestor
|
if (id < trees[curTree].gNumP())
|
||||||
std::cin >> id >> secondId;
|
trees[curTree].printMember(id);
|
||||||
if (id < trees[curTree].gNumP() || secondId < trees[curTree].gNumP())
|
else
|
||||||
trees[curTree].printMember(trees[curTree].findCommonAncestor(id, secondId));
|
std::cout << "No such member\n\n";
|
||||||
else
|
break;
|
||||||
std::cout << "No such member\n\n";
|
case 19: //printAncestors
|
||||||
break;
|
std::cin >> id;
|
||||||
case 22: //prtOldestAncestors
|
/*if (id < trees[curTree].gNumP())
|
||||||
std::cin >> id;
|
trees[curTree].printAncestors(id);
|
||||||
if (id < trees[curTree].gNumP())
|
else
|
||||||
trees[curTree].printOldestAncestors(id);
|
std::cout << "No such member\n\n";*/
|
||||||
else
|
break;
|
||||||
std::cout << "No such member\n\n";
|
case 20: //chooseTree
|
||||||
break;
|
std::cout << "Enter tree ID\n\n";
|
||||||
}
|
std::cin >> id;
|
||||||
|
if (id < trees.gNum())
|
||||||
} while (!quit);
|
curTree = id;
|
||||||
|
else
|
||||||
return 0;
|
std::cout << "No such tree\n\n";
|
||||||
|
break;
|
||||||
|
case 21: //prtCommonAncestor
|
||||||
|
std::cin >> id >> secondId;
|
||||||
|
if (id < trees[curTree].gNumP() || secondId < trees[curTree].gNumP())
|
||||||
|
trees[curTree].printMember(trees[curTree].findCommonAncestor(id, secondId));
|
||||||
|
else
|
||||||
|
std::cout << "No such member\n\n";
|
||||||
|
break;
|
||||||
|
case 22: //prtOldestAncestors
|
||||||
|
std::cin >> id;
|
||||||
|
if (id < trees[curTree].gNumP())
|
||||||
|
trees[curTree].printOldestAncestors(id);
|
||||||
|
else
|
||||||
|
std::cout << "No such member\n\n";
|
||||||
|
break;
|
||||||
|
case 23:
|
||||||
|
trees[curTree].print();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (!quit);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+62
-42
@@ -1,29 +1,54 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include"person.h"
|
#include"person.h"
|
||||||
#include"help.h"
|
#include "help.h"
|
||||||
#include<iostream>
|
|
||||||
|
using std::cout;
|
||||||
|
|
||||||
/*Person::Person():
|
BirthTime::BirthTime(U16 Year, U8 Month, U8 Day, U8 Hour, U8 Minute)
|
||||||
name(nullptr),
|
: year(Year)
|
||||||
numRel(0),
|
, month(Month)
|
||||||
day(0),
|
, day(Day)
|
||||||
month(0),
|
, hour(Hour)
|
||||||
year(0),
|
, minute(Minute)
|
||||||
isMale(true)
|
{
|
||||||
{}*/
|
assert(month <= 12 && day <= 31 && hour <= 23 && minute <= 59 && "Invalid BirthTime data");
|
||||||
|
}
|
||||||
|
|
||||||
|
void BirthTime::print() const
|
||||||
|
{
|
||||||
|
cout << "Birth: ";
|
||||||
|
if (day != UNKNOWN)
|
||||||
|
cout << (I32)day;
|
||||||
|
else
|
||||||
|
cout << "??";
|
||||||
|
cout << '.';
|
||||||
|
|
||||||
|
if (month != UNKNOWN)
|
||||||
|
cout << (I32)month;
|
||||||
|
else
|
||||||
|
cout << "??";
|
||||||
|
cout << '.';
|
||||||
|
|
||||||
|
if (year != UNKNOWN)
|
||||||
|
cout << (I32)year;
|
||||||
|
else
|
||||||
|
cout << "????";
|
||||||
|
cout << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
Person::Person(const char* Name, unsigned char Day, unsigned char Month, short Year, bool IsMale) :
|
|
||||||
numRel(0),
|
Person::Person(const char* Name, BirthTime Birth, bool IsMale)
|
||||||
day(Day),
|
: name(Name)
|
||||||
month(Month),
|
, numRel(0)
|
||||||
year(Year),
|
, birth(Birth)
|
||||||
isMale(IsMale)
|
, isMale(IsMale)
|
||||||
{
|
{
|
||||||
name = new char[strLen(Name) + 1];
|
|
||||||
strCopy(name, Name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Person& Person::operator=(const Person &other)
|
/*Person& Person::operator=(const Person &other)
|
||||||
{
|
{
|
||||||
if (&other!=this)
|
if (&other!=this)
|
||||||
{
|
{
|
||||||
@@ -35,58 +60,53 @@ Person& Person::operator=(const Person &other)
|
|||||||
year = other.year;
|
year = other.year;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
Person::~Person()
|
|
||||||
{
|
|
||||||
delete[] name;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool Person::operator==(const Person &other)const
|
bool Person::operator==(const Person &other)const
|
||||||
{
|
{
|
||||||
return strComp(name, other.name) && isMale == other.isMale && (day == other.day || !day || !other.day) && (month == other.month || !month || !other.month) && (year == other.year || !year || !other.year);
|
return name == other.name && isMale == other.isMale; //TODO && day == other.day || !day || !other.day) && (month == other.month || !month || !other.month) && (year == other.year || !year || !other.year);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Person::save(std::ofstream &file)const
|
void Person::save(std::ofstream &file)const
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
char nameLen = strLen(name);
|
char nameLen = name.size();
|
||||||
file.write(reinterpret_cast<char*>(&nameLen), sizeof(nameLen));
|
file.write(reinterpret_cast<char*>(&nameLen), sizeof(nameLen));
|
||||||
file.write(name, nameLen * sizeof(*name));
|
file.write(name.data(), nameLen * sizeof(name[0]));
|
||||||
file.write(reinterpret_cast<const char*>(&numRel), sizeof(numRel));
|
file.write(reinterpret_cast<const char*>(&numRel), sizeof(numRel));
|
||||||
file.write(reinterpret_cast<const char*>(&isMale), sizeof(isMale));
|
file.write(reinterpret_cast<const char*>(&isMale), sizeof(isMale));
|
||||||
file.write(reinterpret_cast<const char*>(&day), sizeof(day));
|
//file.write(reinterpret_cast<const char*>(&day), sizeof(day)); TODO
|
||||||
file.write(reinterpret_cast<const char*>(&month), sizeof(month));
|
//file.write(reinterpret_cast<const char*>(&month), sizeof(month));
|
||||||
file.write(reinterpret_cast<const char*>(&year), sizeof(year));
|
//file.write(reinterpret_cast<const char*>(&year), sizeof(year));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Person::load(std::ifstream &file)
|
void Person::load(std::ifstream &file)
|
||||||
{
|
{
|
||||||
char nameLen;
|
char nameLen;
|
||||||
file.read((char*)&nameLen, sizeof(nameLen));
|
file.read((char*)&nameLen, sizeof(nameLen)); //Check len
|
||||||
|
|
||||||
name = new char[nameLen + 1];
|
C8 nameBuf[128];
|
||||||
file.read(name, nameLen * sizeof(*name));
|
file.read(nameBuf, nameLen * sizeof(*nameBuf));
|
||||||
name[nameLen] = '\0';
|
nameBuf[nameLen] = '\0';
|
||||||
|
name = nameBuf;
|
||||||
|
|
||||||
file.read(reinterpret_cast<char*>(&numRel), sizeof(numRel));
|
file.read(reinterpret_cast<char*>(&numRel), sizeof(numRel));
|
||||||
file.read(reinterpret_cast<char*>(&isMale), sizeof(isMale));
|
file.read(reinterpret_cast<char*>(&isMale), sizeof(isMale));
|
||||||
file.read(reinterpret_cast<char*>(&day), sizeof(day));
|
//file.read(reinterpret_cast<char*>(&day), sizeof(day)); TODO
|
||||||
file.read(reinterpret_cast<char*>(&month), sizeof(month));
|
//file.read(reinterpret_cast<char*>(&month), sizeof(month));
|
||||||
file.read(reinterpret_cast<char*>(&year), sizeof(year));
|
//file.read(reinterpret_cast<char*>(&year), sizeof(year));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Person::rename(const char* newName)
|
void Person::rename(const char* newName)
|
||||||
{
|
{
|
||||||
delete[] name;
|
name = newName;
|
||||||
name = new char[strLen(newName) + 1];
|
|
||||||
strCopy(name, newName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Person::print()const
|
void Person::print()const
|
||||||
{
|
{
|
||||||
std::cout << name << "\nBirthday: " << (int)day << "." << (int)month << "." << year << "\nSex: ";
|
std::cout << name << '\n';
|
||||||
|
birth.print();
|
||||||
std::cout << (isMale ? "Male\n\n" : "Female\n\n");
|
std::cout << (isMale ? "Male\n\n" : "Female\n\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "common/basicTypes.h"
|
#include "common/basicTypes.h"
|
||||||
|
|
||||||
|
struct BirthTime
|
||||||
|
{
|
||||||
|
static const U16 UNKNOWN = 0;
|
||||||
|
/*explicit*/ BirthTime(U16 Year=UNKNOWN, U8 Month=UNKNOWN, U8 Day=UNKNOWN, U8 Hour=UNKNOWN, U8 Minute=UNKNOWN);
|
||||||
|
void print() const;
|
||||||
|
|
||||||
|
U16 year;
|
||||||
|
U8 month, day, hour, minute;
|
||||||
|
};
|
||||||
|
|
||||||
class Person
|
class Person
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Person() = default;
|
Person() = default;
|
||||||
Person(const char* Name, unsigned char Day, unsigned char Monthay, short Year, bool Sex);
|
Person(const char* Name, BirthTime Birth, bool Sex);
|
||||||
//
|
|
||||||
Person& operator=(const Person &other);
|
|
||||||
~Person();
|
|
||||||
|
|
||||||
bool operator==(const Person &other)const;
|
bool operator==(const Person &other)const;
|
||||||
|
|
||||||
@@ -22,9 +31,8 @@ public:
|
|||||||
friend class Tree;
|
friend class Tree;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char *name;
|
std::string name;
|
||||||
unsigned numRel; //Number of close relatives - mother, father, brother, sister, wife/husband, child, ex-wife/husband
|
unsigned numRel; //Number of close relatives - mother, father, brother, sister, wife/husband, child, ex-wife/husband
|
||||||
unsigned char day, month; //Birth
|
BirthTime birth;
|
||||||
short year;
|
|
||||||
bool isMale;
|
bool isMale;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,56 +1,20 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include<fstream>
|
||||||
#include"tree.h"
|
#include"tree.h"
|
||||||
#include"person.h"
|
#include"person.h"
|
||||||
#include"help.h"
|
#include"help.h"
|
||||||
#include<fstream>
|
|
||||||
|
using std::cout;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
Tree::Tree()
|
||||||
|
: treeName("Unnamed")
|
||||||
|
, numPeople(0)
|
||||||
|
{}
|
||||||
|
|
||||||
Tree Tree::operator+ (const Tree& other)//
|
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 *newId = new unsigned[other.numPeople]; //The indexes members of the II tree will have in result
|
||||||
|
|
||||||
unsigned idCounter = numPeople;
|
unsigned idCounter = numPeople;
|
||||||
@@ -191,17 +155,16 @@ Tree& Tree::operator-= (const Tree& other)
|
|||||||
|
|
||||||
void Tree::rename(const char* newName)
|
void Tree::rename(const char* newName)
|
||||||
{
|
{
|
||||||
delete[] treeName;
|
treeName = newName;
|
||||||
treeName = new char[strLen(newName) + 1];
|
|
||||||
strCopy(treeName, newName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned Tree::findId(const char* name, const short year, const unsigned char month, const unsigned char day)const
|
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,)
|
//Person sought(name, day, month, year,)
|
||||||
for (unsigned i = 0; i < numPeople; ++i)
|
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 i;
|
||||||
}
|
}
|
||||||
return Nobody;
|
return Nobody;
|
||||||
@@ -226,35 +189,29 @@ unsigned Tree::findCommonAncestor(const unsigned firstId, const unsigned secondI
|
|||||||
commonAncestorRec(secondId, visited);
|
commonAncestorRec(secondId, visited);
|
||||||
|
|
||||||
unsigned oldestId = Nobody;
|
unsigned oldestId = Nobody;
|
||||||
short oldestBday = SHRT_MAX;
|
U16 oldestBday = SHRT_MAX;
|
||||||
for (unsigned i = 0; i < numPeople; ++i)
|
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;
|
oldestId = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return oldestId;
|
return oldestId;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tree::saveTree()const
|
void Tree::saveTree() const
|
||||||
{
|
{
|
||||||
char* fileName = mergeStr(treeName, ".people");
|
std::ofstream peopleFile(treeName+ ".people", std::ios::binary);
|
||||||
std::ofstream peopleFile(fileName, std::ios::binary);
|
|
||||||
delete[] fileName;
|
|
||||||
|
|
||||||
peopleFile.write(reinterpret_cast<const char*>(&numPeople), sizeof(numPeople));
|
peopleFile.write(reinterpret_cast<const char*>(&numPeople), sizeof(numPeople));
|
||||||
for (unsigned i = 0; i < numPeople; ++i)
|
for (unsigned i = 0; i < numPeople; ++i)
|
||||||
people[i].save(peopleFile);
|
people[i].save(peopleFile);
|
||||||
peopleFile.close();
|
peopleFile.close();
|
||||||
|
|
||||||
fileName = mergeStr(treeName, ".relatives");
|
std::ofstream relativeFile(treeName + ".relatives", std::ios::binary);
|
||||||
std::ofstream relativeFile(fileName, std::ios::binary);
|
std::ofstream relationFile(treeName + ".relations", std::ios::binary);
|
||||||
delete[] fileName;
|
|
||||||
fileName = mergeStr(treeName, ".relations");
|
|
||||||
std::ofstream relationFile(fileName, std::ios::binary);
|
|
||||||
delete[] fileName;
|
|
||||||
|
|
||||||
/*relativeFile.write((char*)&numPeople, sizeof(numPeople));
|
/*relativeFile.write((char*)&numPeople, sizeof(numPeople));
|
||||||
relationFile.write((char*)&numPeople, sizeof(numPeople));*/
|
relationFile.write((char*)&numPeople, sizeof(numPeople));*/
|
||||||
@@ -274,9 +231,7 @@ void Tree::saveTree()const
|
|||||||
|
|
||||||
bool Tree::loadTree()
|
bool Tree::loadTree()
|
||||||
{
|
{
|
||||||
char* fileName = mergeStr(treeName, ".people");//c
|
std::ifstream peopleFile(treeName + ".people", std::ios::binary);
|
||||||
std::ifstream peopleFile(fileName, std::ios::binary);
|
|
||||||
delete[] fileName;
|
|
||||||
if (peopleFile.peek() == std::char_traits<char>::eof())
|
if (peopleFile.peek() == std::char_traits<char>::eof())
|
||||||
{
|
{
|
||||||
peopleFile.close();
|
peopleFile.close();
|
||||||
@@ -290,12 +245,8 @@ bool Tree::loadTree()
|
|||||||
people.sNum(numPeople);
|
people.sNum(numPeople);
|
||||||
peopleFile.close();
|
peopleFile.close();
|
||||||
|
|
||||||
fileName = mergeStr(treeName, ".relatives");
|
std::ifstream relativeFile(treeName + ".relatives", std::ios::binary);
|
||||||
std::ifstream relativeFile(fileName, std::ios::binary);
|
std::ifstream relationFile(treeName + ".relations", std::ios::binary);
|
||||||
delete[] fileName;
|
|
||||||
fileName = mergeStr(treeName, ".relations");
|
|
||||||
std::ifstream relationFile(fileName, std::ios::binary);
|
|
||||||
delete[] fileName;
|
|
||||||
|
|
||||||
relatives.resize(numPeople);
|
relatives.resize(numPeople);
|
||||||
relations.resize(numPeople);
|
relations.resize(numPeople);
|
||||||
@@ -318,9 +269,9 @@ bool Tree::loadTree()
|
|||||||
return 1;
|
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);
|
people.push(newPerson);
|
||||||
Array<unsigned> newRelative;
|
Array<unsigned> newRelative;
|
||||||
relatives.push(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
|
void Tree::printRel(const unsigned id)const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
|
||||||
#include <climits>
|
#include <climits>
|
||||||
#include"array.h"
|
#include "array.h"
|
||||||
|
#include "person.h"
|
||||||
|
|
||||||
class Person;
|
enum Relation : U8
|
||||||
|
|
||||||
enum Relation : char
|
|
||||||
{
|
{
|
||||||
Invalid = 0,
|
Invalid = 0,
|
||||||
Father,
|
Father,
|
||||||
@@ -26,18 +25,13 @@ const unsigned Nobody = UINT_MAX; //Òîçè íîìåð ùå áúäå çàïàç
|
|||||||
|
|
||||||
class Tree
|
class Tree
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
char *treeName;
|
|
||||||
unsigned numPeople;
|
|
||||||
Array<Person> people; //The data for each member
|
|
||||||
Array<Array<unsigned>> relatives; //The relatives of each member
|
|
||||||
Array<Array<Relation>> relations; //The type of relation each member has with his relatives
|
|
||||||
public:
|
public:
|
||||||
Tree();
|
Tree();
|
||||||
Tree(const char *Name);
|
template <class Str>
|
||||||
Tree(const Tree&);
|
Tree(Str&& Name) : treeName(std::forward<Str>(Name)), numPeople(0) {}
|
||||||
|
/*Tree(const Tree&);
|
||||||
Tree& operator= (const Tree& other);
|
Tree& operator= (const Tree& other);
|
||||||
~Tree();
|
~Tree();*/
|
||||||
|
|
||||||
Tree operator+ (const Tree& other);
|
Tree operator+ (const Tree& other);
|
||||||
Tree operator- (const Tree& other);
|
Tree operator- (const Tree& other);
|
||||||
@@ -50,14 +44,15 @@ public:
|
|||||||
unsigned findCommonAncestor(const unsigned firstId, const unsigned secondId)const; //oldest common ancestor ID
|
unsigned findCommonAncestor(const unsigned firstId, const unsigned secondId)const; //oldest common ancestor ID
|
||||||
void saveTree()const;//
|
void saveTree()const;//
|
||||||
bool loadTree();
|
bool loadTree();
|
||||||
void addPerson(const char *, short year, unsigned char month, unsigned char day, bool sex, unsigned father = Nobody, unsigned mother = Nobody);
|
void addPerson(const char *name, BirthTime birth, bool isMale, unsigned father = Nobody, unsigned mother = Nobody);
|
||||||
bool addRelation(const char* firstName, const Relation, const char* secondName); //Adds a new relation or edits an old one. 0 - failure 1 - success
|
bool addRelation(const char* firstName, const Relation, const char* secondName); //Adds a new relation or edits an old one. 0 - failure 1 - success
|
||||||
bool addRelation(const unsigned firstId, const Relation, const unsigned secondId, bool opposite=0); //0 - failure 1 - success
|
bool addRelation(const unsigned firstId, const Relation, const unsigned secondId, bool opposite=0); //0 - failure 1 - success
|
||||||
void removePerson(const unsigned id); //Removes a member and the last one takes his ID
|
void removePerson(const unsigned id); //Removes a member and the last one takes his ID
|
||||||
void removePerson(const char*, const short year = 0, const unsigned char month = 0, const unsigned char day = 0);
|
void removePerson(const char*, const short year = 0, const unsigned char month = 0, const unsigned char day = 0);
|
||||||
void removeRelation(const unsigned firstId, const unsigned secondId);
|
void removeRelation(const unsigned firstId, const unsigned secondId);
|
||||||
void removeRelation(const char* firstName, const char* secondName);
|
void removeRelation(const char* firstName, const char* secondName);
|
||||||
|
|
||||||
|
void print()const;
|
||||||
void printRel(const unsigned id)const; //Prints out close relatives
|
void printRel(const unsigned id)const; //Prints out close relatives
|
||||||
void printMember(const unsigned id)const;
|
void printMember(const unsigned id)const;
|
||||||
void printOldestAncestors(const unsigned id)const;//
|
void printOldestAncestors(const unsigned id)const;//
|
||||||
@@ -66,6 +61,12 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void commonAncestorRec(const unsigned &id, const Array<char> &visited)const;
|
void commonAncestorRec(const unsigned &id, const Array<char> &visited)const;
|
||||||
|
|
||||||
|
std::string treeName;
|
||||||
|
unsigned numPeople;
|
||||||
|
Array<Person> people; //The data for each member
|
||||||
|
Array<Array<unsigned>> relatives; //The relatives of each member
|
||||||
|
Array<Array<Relation>> relations; //The type of relation each member has with his relatives
|
||||||
|
|
||||||
//Search, ect.
|
//Search, ect.
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user