Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #ifndef LESSON09_H
- #define LESSON09_H
- #include <string>
- #include <iostream>
- //#include <ctime>
- //1. Vytvořte šablonovou funkci PrintRange<T>
- //-funkce obsahuje tři vstupní parametry :
- //-ostream, do kterého se budou zasílat data
- //- dolní a horní hranici rozsahu
- //- tato funkce bude vypisovat do zaslaného proudu rozptyl hodnot parametrického typu T mezi zaslanými argumenty.
- template<typename T>
- void PrintRange(std::ostream& stream, const T& LowerBound, const T& UpperBound){
- for (T i = LowerBound; i < UpperBound ; i++)
- {
- if (i!=LowerBound){
- stream << ", ";
- }
- stream << i;
- }
- stream << std::endl;
- }
- //2. Vytvořte šablonovou strukturu PairIdPassword<ID_TYPE, PASSWORD_TYPE>, která bude obsahovat :
- //-vnořený datové typy IdType a PasswordType,
- //-atributy identifikačního čísla a hesla,
- //-operátory == , != , <, >, >= , <=
- //-pro porovnání budou rozhodující pouze identifikační čísla.
- template<typename ID_TYPE, typename PASSWORD_TYPE>
- struct PairIdPassword
- {
- typedef ID_TYPE IdType;
- typedef PASSWORD_TYPE PasswordType;
- IdType id;
- PasswordType pw;
- bool operator >(const PairIdPassword& right) const; //dekladace operator >
- bool operator <(const PairIdPassword& right) const; //dekladace operator <
- bool operator >=(const PairIdPassword& right) const; //dekladace operator >=
- bool operator <=(const PairIdPassword& right) const; //dekladace operator <=
- bool operator ==(const PairIdPassword& right) const; //dekladace operator ==
- bool operator !=(const PairIdPassword& right) const; //dekladace operator !=
- };
- //definice operator >
- template<typename ID_TYPE, typename PASSWORD_TYPE>
- bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator >(const PairIdPassword& right)const{
- return id > right.id;
- }
- //definice operator <
- template<typename ID_TYPE, typename PASSWORD_TYPE>
- bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator <(const PairIdPassword& right)const{
- return id < right.id
- }
- //definice operator >=
- template<typename ID_TYPE, typename PASSWORD_TYPE>
- bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator >=(const PairIdPassword& right)const{
- return id >= right.id;
- }
- //definice operator <=
- template<typename ID_TYPE, typename PASSWORD_TYPE>
- bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator <=(const PairIdPassword& right)const{
- return id <= right.id;
- }
- //definice operator ==
- template<typename ID_TYPE, typename PASSWORD_TYPE>
- bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator ==(const PairIdPassword& right)const{
- return id == right.id;
- }
- //definice operator =!
- template<typename ID_TYPE,typename PASSWORD_TYPE>
- bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator !=(const PairIdPassword& right)const{
- return id != right.id;
- }
- int RandomInt(int low, int high) {
- return low + rand() % (high - low + 1);
- }
- class LoginGenerator
- {
- public:
- LoginGenerator(size_t passwordLength);
- ~LoginGenerator();
- PairIdPassword<int, std::string> operator()(size_t id)const;
- private:
- size_t _passwordLength;
- };
- LoginGenerator::LoginGenerator(size_t passwordLength)
- {
- _passwordLength = passwordLength;
- }
- PairIdPassword<int, std::string> LoginGenerator::operator()(size_t id) const
- {
- PairIdPassword<int, std::string> Pw;
- Pw.id = id;
- for (size_t i = 0; i < _passwordLength; i++)
- {
- Pw.pw += RandomInt('A', 'z');
- }
- return Pw;
- }
- LoginGenerator::~LoginGenerator()
- {
- }
- class LoginContainer
- {
- public:
- LoginContainer();
- LoginContainer(const LoginContainer& item){
- ContainerNode* ItemNode = item._first;
- while (ItemNode != NULL){
- ContainerNode* NewContainer = new ContainerNode();
- NewContainer->item = ItemNode->item;
- ContainerNode* ThisActNode = this->_first;
- if (this->_first == NULL){
- this->_first = NewContainer;
- continue;
- }
- while (ThisActNode->next != NULL){
- ThisActNode = ThisActNode->next;
- }
- ThisActNode->next = NewContainer;
- }
- }
- ~LoginContainer();
- void PushBack(const PairIdPassword<int, std::string>& item);
- std::ostream& ToStream(std::ostream& os) const;
- private:
- int nodeCount=0;
- struct ContainerNode{
- PairIdPassword<int, std::string> item;
- ContainerNode* next;
- };
- typedef struct ContainerNode* NodePtr;
- NodePtr _current;
- NodePtr _first;
- NodePtr _temp;
- };
- LoginContainer::LoginContainer(){
- _first = NULL;
- _temp = NULL;
- _current = NULL;
- }
- LoginContainer::~LoginContainer(){
- while (_first != NULL) {
- ContainerNode * n = _first->next;
- delete _first;
- _first = n;
- }
- }
- void LoginContainer::PushBack(const PairIdPassword<int, std::string>& item){
- NodePtr n = new ContainerNode;
- n->next = NULL;
- n->item = item;
- if (_first != NULL){
- _current = _first;
- while (_current->next != NULL){
- _current = _current->next;
- }
- _current->next = n;
- }
- else{
- _first = n;
- }
- nodeCount++;
- }
- std::ostream& LoginContainer::ToStream(std::ostream& os) const{
- NodePtr temp = _first;
- for (size_t i = 0; i < nodeCount; i++)
- {
- os << temp->item.id << " " << temp->item.pw << std::endl;
- temp = temp->next;
- }
- return os;
- }
- #endif LESSON09_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement