Advertisement
Smudla

C++ / Login Container Templates

Jun 2nd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #pragma once
  2. #ifndef LESSON09_H
  3. #define LESSON09_H
  4. #include <string>
  5. #include <iostream>
  6. //#include <ctime>
  7.  
  8.  
  9.  
  10.  
  11. //1. Vytvořte šablonovou funkci PrintRange<T>
  12. //-funkce obsahuje tři vstupní parametry :
  13. //-ostream, do kterého se budou zasílat data
  14. //- dolní a horní hranici rozsahu
  15. //- tato funkce bude vypisovat do zaslaného proudu rozptyl hodnot parametrického typu T mezi zaslanými argumenty.
  16. template<typename T>
  17. void PrintRange(std::ostream& stream, const T& LowerBound, const T& UpperBound){
  18.     for (T i = LowerBound; i < UpperBound ; i++)
  19.     {
  20.         if (i!=LowerBound){
  21.             stream << ", ";
  22.         }
  23.         stream << i;
  24.     }
  25.     stream << std::endl;
  26. }
  27.  
  28. //2. Vytvořte šablonovou strukturu PairIdPassword<ID_TYPE, PASSWORD_TYPE>, která bude obsahovat :
  29. //-vnořený datové typy IdType a PasswordType,
  30. //-atributy identifikačního čísla a hesla,
  31. //-operátory == , != , <, >, >= , <=
  32. //-pro porovnání budou rozhodující pouze identifikační čísla.
  33. template<typename ID_TYPE, typename PASSWORD_TYPE>
  34. struct PairIdPassword
  35. {
  36.     typedef ID_TYPE IdType;
  37.     typedef PASSWORD_TYPE PasswordType;
  38.     IdType id;
  39.     PasswordType pw;
  40.  
  41.     bool operator >(const PairIdPassword& right) const;                 //dekladace operator >
  42.     bool operator <(const PairIdPassword& right) const;                 //dekladace operator <
  43.     bool operator >=(const PairIdPassword& right) const;                //dekladace operator >=
  44.     bool operator <=(const PairIdPassword& right) const;                //dekladace operator <=
  45.     bool operator ==(const PairIdPassword& right) const;                //dekladace operator ==
  46.     bool operator !=(const PairIdPassword& right) const;                //dekladace operator !=
  47. };
  48.  
  49. //definice operator >
  50. template<typename ID_TYPE, typename PASSWORD_TYPE>
  51. bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator >(const PairIdPassword& right)const{
  52.     return id > right.id;
  53. }
  54.  
  55. //definice operator <
  56. template<typename ID_TYPE, typename PASSWORD_TYPE>
  57. bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator <(const PairIdPassword& right)const{
  58.     return id < right.id
  59.     }
  60.  
  61. //definice operator >=
  62. template<typename ID_TYPE, typename PASSWORD_TYPE>
  63. bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator >=(const PairIdPassword& right)const{
  64.     return id >= right.id;
  65. }
  66.  
  67. //definice  operator <=
  68. template<typename ID_TYPE, typename PASSWORD_TYPE>
  69. bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator <=(const PairIdPassword& right)const{
  70.     return id <= right.id;
  71. }
  72.  
  73. //definice operator ==
  74. template<typename ID_TYPE, typename PASSWORD_TYPE>
  75. bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator ==(const PairIdPassword& right)const{
  76.     return id == right.id;
  77. }
  78.  
  79. //definice operator =!
  80. template<typename ID_TYPE,typename PASSWORD_TYPE>
  81. bool PairIdPassword<typename ID_TYPE, typename PASSWORD_TYPE>::operator !=(const PairIdPassword& right)const{
  82.     return id != right.id;
  83. }
  84.  
  85. int RandomInt(int low, int high) {
  86.     return low + rand() % (high - low + 1);
  87. }
  88.  
  89.  
  90. class LoginGenerator
  91. {
  92. public:
  93.     LoginGenerator(size_t passwordLength);
  94.     ~LoginGenerator();
  95.     PairIdPassword<int, std::string> operator()(size_t id)const;
  96.  
  97. private:
  98.     size_t _passwordLength;
  99. };
  100.  
  101. LoginGenerator::LoginGenerator(size_t passwordLength)
  102. {
  103.     _passwordLength = passwordLength;
  104. }
  105.  
  106.  
  107. PairIdPassword<int, std::string> LoginGenerator::operator()(size_t id) const
  108.         {
  109.             PairIdPassword<int, std::string> Pw;
  110.             Pw.id = id;
  111.             for (size_t i = 0; i < _passwordLength; i++)
  112.             {
  113.                 Pw.pw += RandomInt('A', 'z');
  114.             }
  115.             return Pw;
  116.         }
  117.  
  118. LoginGenerator::~LoginGenerator()
  119. {
  120. }
  121.  
  122. class LoginContainer
  123. {
  124. public:
  125.     LoginContainer();
  126.     LoginContainer(const LoginContainer& item){
  127.         ContainerNode* ItemNode = item._first;
  128.         while (ItemNode != NULL){
  129.             ContainerNode* NewContainer = new ContainerNode();
  130.             NewContainer->item = ItemNode->item;
  131.  
  132.             ContainerNode* ThisActNode = this->_first;
  133.             if (this->_first == NULL){
  134.                 this->_first = NewContainer;
  135.                 continue;
  136.             }
  137.             while (ThisActNode->next != NULL){
  138.                 ThisActNode = ThisActNode->next;
  139.             }
  140.             ThisActNode->next = NewContainer;
  141.         }
  142.     }
  143.     ~LoginContainer();
  144.     void PushBack(const PairIdPassword<int, std::string>& item);
  145.     std::ostream& ToStream(std::ostream& os) const;
  146.  
  147. private:
  148.     int nodeCount=0;
  149.     struct ContainerNode{
  150.         PairIdPassword<int, std::string> item;
  151.         ContainerNode* next;
  152.     };
  153.     typedef struct ContainerNode* NodePtr;
  154.     NodePtr _current;
  155.     NodePtr _first;
  156.     NodePtr _temp;
  157. };
  158.  
  159.  
  160. LoginContainer::LoginContainer(){
  161.     _first = NULL;
  162.     _temp = NULL;
  163.     _current = NULL;
  164. }
  165.  
  166. LoginContainer::~LoginContainer(){
  167.     while (_first != NULL) {
  168.         ContainerNode * n = _first->next;
  169.         delete _first;
  170.         _first = n;
  171.     }
  172. }
  173.  
  174. void LoginContainer::PushBack(const PairIdPassword<int, std::string>& item){
  175.     NodePtr n = new ContainerNode;
  176.     n->next = NULL;
  177.     n->item = item;
  178.     if (_first != NULL){
  179.         _current = _first;
  180.         while (_current->next != NULL){
  181.             _current = _current->next;
  182.         }
  183.         _current->next = n;
  184.     }
  185.     else{
  186.         _first = n;
  187.     }
  188.     nodeCount++;
  189. }
  190.  
  191.  std::ostream& LoginContainer::ToStream(std::ostream& os) const{
  192.      NodePtr temp = _first;
  193.      for (size_t i = 0; i < nodeCount; i++)
  194.      {
  195.          os << temp->item.id << " " << temp->item.pw << std::endl;
  196.          temp = temp->next;
  197.      }
  198.      return os;
  199.  }
  200.  
  201.  
  202.  
  203.  
  204.  
  205. #endif LESSON09_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement