use typedefed types, BirthTime

This commit is contained in:
vrd
2022-08-27 16:35:40 +03:00
parent fbfe017284
commit 1bf0a7f6a4
8 changed files with 397 additions and 385 deletions
+62 -42
View File
@@ -1,29 +1,54 @@
#include <iostream>
#include <assert.h>
#include"person.h"
#include"help.h"
#include<iostream>
#include "help.h"
using std::cout;
/*Person::Person():
name(nullptr),
numRel(0),
day(0),
month(0),
year(0),
isMale(true)
{}*/
BirthTime::BirthTime(U16 Year, U8 Month, U8 Day, U8 Hour, U8 Minute)
: year(Year)
, month(Month)
, day(Day)
, hour(Hour)
, minute(Minute)
{
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),
day(Day),
month(Month),
year(Year),
isMale(IsMale)
Person::Person(const char* Name, BirthTime Birth, bool IsMale)
: name(Name)
, numRel(0)
, birth(Birth)
, 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)
{
@@ -35,58 +60,53 @@ Person& Person::operator=(const Person &other)
year = other.year;
}
return *this;
}
Person::~Person()
{
delete[] name;
}
}*/
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
{
//
char nameLen = strLen(name);
char nameLen = name.size();
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*>(&isMale), sizeof(isMale));
file.write(reinterpret_cast<const char*>(&day), sizeof(day));
file.write(reinterpret_cast<const char*>(&month), sizeof(month));
file.write(reinterpret_cast<const char*>(&year), sizeof(year));
//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*>(&year), sizeof(year));
}
void Person::load(std::ifstream &file)
{
char nameLen;
file.read((char*)&nameLen, sizeof(nameLen));
file.read((char*)&nameLen, sizeof(nameLen)); //Check len
name = new char[nameLen + 1];
file.read(name, nameLen * sizeof(*name));
name[nameLen] = '\0';
C8 nameBuf[128];
file.read(nameBuf, nameLen * sizeof(*nameBuf));
nameBuf[nameLen] = '\0';
name = nameBuf;
file.read(reinterpret_cast<char*>(&numRel), sizeof(numRel));
file.read(reinterpret_cast<char*>(&isMale), sizeof(isMale));
file.read(reinterpret_cast<char*>(&day), sizeof(day));
file.read(reinterpret_cast<char*>(&month), sizeof(month));
file.read(reinterpret_cast<char*>(&year), sizeof(year));
//file.read(reinterpret_cast<char*>(&day), sizeof(day)); TODO
//file.read(reinterpret_cast<char*>(&month), sizeof(month));
//file.read(reinterpret_cast<char*>(&year), sizeof(year));
}
void Person::rename(const char* newName)
{
delete[] name;
name = new char[strLen(newName) + 1];
strCopy(name, newName);
name = newName;
}
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");
}