Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions tchervinsky.alexei/T1/datastruct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#include "datastruct.hpp"
#include <iomanip>

std::istream& tchervinsky::operator>>(std::istream& in, tchervinsky::Delimiter&& dest)
{
std::istream::sentry sentry(in);
if (!sentry)
{
return in;
}
char c = '\0';
in >> c;
if (in && (c != dest.delimiter))
{
in.setstate(std::ios::failbit);
}
return in;
}

std::istream& tchervinsky::operator >> (std::istream& in, tchervinsky::DataStruct& dest)
{
char c{ '\0' };
in >> std::skipws >> tchervinsky::Delimiter{ '(' } >> std::noskipws;
if (!in)
{
return in;
}
in >> std::noskipws >> tchervinsky::Delimiter{ ':' } >> std::noskipws;
if (!in)
{
return in;
}
bool keyfields[3]{ false };
for (std::size_t i = 0; i < 3; ++i)
{
std::string key;
in >> key;
if (key.size() != 4)
{
in.setstate(std::ios::failbit);
return in;
}
if (key.substr(0, 3) != "key")
{
in.setstate(std::ios::failbit);
return in;
}
c = key[3];
switch (c)
{
case '1':
{
in >> std::skipws >> dest.key1 >> std::noskipws
>> tchervinsky::Delimiter{ 'l' } >> tchervinsky::Delimiter{ 'l' }
>> tchervinsky::Delimiter{ ':' };
if (!in)
{
return in;
}
keyfields[0] = true;
break;
}
case '2':
{
in >> std::skipws >> tchervinsky::Delimiter{ '#' };
if (!in)
{
return in;
}
in >> std::noskipws >> tchervinsky::Delimiter{ 'c' };
if (!in)
{
return in;
}
in >> std::noskipws >> tchervinsky::Delimiter{ '(' };
if (!in)
{
return in;
}
double realvalue;
in >> std::noskipws >> realvalue;
if (!in)
{
in.setstate(std::ios::failbit);
return in;
}
dest.key2.real(realvalue);
double imagvalue;
in >> std::skipws >> imagvalue;
if (!in)
{
in.setstate(std::ios::failbit);
return in;
}
dest.key2.imag(imagvalue);
in >> std::noskipws >> tchervinsky::Delimiter{ ')' };
if (!in)
{
return in;
}
in >> std::noskipws >> tchervinsky::Delimiter{ ':' };
if (!in)
{
return in;
}
keyfields[1] = true;
break;
}
case '3':
{
std::string key3string;
std::getline(in, key3string, '"');
std::getline(in, key3string, '"');
if (!in)
{
in.setstate(std::ios::failbit);
return in;
}
dest.key3 = key3string;
keyfields[2] = true;
in >> std::noskipws >> c >> std::noskipws;
break;
}
default:
{
in.setstate(std::ios::failbit);
return in;
}
}
}
if (((keyfields[0] == keyfields[1]) == keyfields[2]) == true)
{
in >> std::noskipws >> tchervinsky::Delimiter{ ')' } >> std::noskipws;
if (!in)
{
return in;
}
}
else
{
in.setstate(std::ios::failbit);
return in;
}
return in;
}

std::ostream& tchervinsky::operator << (std::ostream& out, const tchervinsky::DataStruct& src)
{
out << "(";
out << ":key1 " << src.key1 << 'l' << 'l';
out << ":key2 " << "#c(" << std::fixed << std::setprecision(1) << src.key2.real() << " " << src.key2.imag() << ')';
out << ":key3 " << '\"' << src.key3 << '\"';
out << ":)";
return out;
}

bool tchervinsky::operator < (const tchervinsky::DataStruct& a, const tchervinsky::DataStruct& b)
{
if (a.key1 != b.key1)
{
return a.key1 < b.key1;
}
else if (a.key2 != b.key2)
{
double akey2mod = std::fabs(std::sqrt(std::pow(a.key2.real(), 2) + std::pow(a.key2.imag(), 2)));
double bkey2mod = std::fabs(std::sqrt(std::pow(b.key2.real(), 2) + std::pow(b.key2.imag(), 2)));
return akey2mod < bkey2mod;
}
else
{
return a.key3.size() < b.key3.size();
}
}
27 changes: 27 additions & 0 deletions tchervinsky.alexei/T1/datastruct.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _DATA_STRUCT_HPP_
#define _DATA_STRUCT_HPP_

#include <iostream>
#include <string>
#include <ccomplex>

namespace tchervinsky
{
struct Delimiter
{
char delimiter;
};

struct DataStruct
{
long long key1;
std::complex<double> key2;
std::string key3;
};

std::istream& operator>>(std::istream& in, Delimiter&& dest);
std::istream& operator >> (std::istream& in, DataStruct& dest);
std::ostream& operator << (std::ostream& out, const DataStruct& dest);
bool operator < (const DataStruct& a, const DataStruct& b);
}
#endif
30 changes: 30 additions & 0 deletions tchervinsky.alexei/T1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "datastruct.hpp"
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
#include <limits>

int main()
{
std::vector< tchervinsky::DataStruct > data;
do
{
if (!std::cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n');
}
std::copy(
std::istream_iterator< tchervinsky::DataStruct >(std::cin),
std::istream_iterator< tchervinsky::DataStruct >(),
std::back_inserter(data));
} while (!std::cin.eof());

std::sort(data.begin(), data.end());

std::copy(data.begin(), data.end(), std::ostream_iterator< tchervinsky::DataStruct >(std::cout, "\n"));

return 0;
}