Advertisement
Rafpast

OstatnieLaboratorium

Jun 15th, 2022
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. #include "Napis.h"
  2.  
  3.  
  4. int main()
  5. {
  6.     char tekstChar = 'l', tekstChars[10] = "Bb";// a asa
  7.     std::string tekst = "Aa", tekst2 = "bB";//Alka
  8.     Napis<char>  tap(tekstChars), pap("bB"), nap, map("aA"), bap(tekstChar);
  9.     //Napis<wchar_t>
  10.  
  11.     std::cout << "map: " << map << " pap: " << pap << " tap: " << tap << " bap: " << bap << "\n";
  12.     std::cout << "map: " << map++ << " pap: " << pap-- << " tap: " << --tap << " bap: " << ++bap << "\n";
  13.     std::cout << "map: " << map << " pap: " << pap << " tap: " << tap << " bap: " << bap << "\n";
  14.  
  15.     nap = map + pap;
  16.     std::cout << nap;
  17.     std::cout << std::boolalpha;
  18.     std::cout << "\n(map == pap) = " << (map == pap) << "\n(map != tap) = " << (map != tap) << "\n(map == bap) = " << (map == bap) << " \n";
  19.     std::cout << "((tekst <=> tekst2) < 0) = " << ((tekst <=> tekst2) < 0) << std::endl;
  20.     std::cout << "((pap <=> map) > 0) = " << ((pap <=> map) > 0) << std::endl;
  21.     std::cout << "((pap <=> map) == 0) = " << ((pap <=> map) == 0) << std::endl;
  22.     std::cout << "((pap <=> map) < 0) = " << ((pap <=> map) < 0) << std::endl;
  23.  
  24.     tekstChar = 'k';
  25.     return 0;
  26. }
  27.  
  28. #pragma once
  29. #include <iostream>
  30. #include <fstream>
  31. #include <compare>
  32. #include <vector>
  33. #include <string>
  34.  
  35. using std::ostream;
  36.  
  37. template <typename T>
  38. class Napis
  39. {
  40. private:
  41.  
  42.     int lenght;
  43.     std::vector<T> writing;
  44.  
  45. public:
  46.  
  47.     Napis() noexcept;
  48.     Napis(T newThings);
  49.     Napis(const T* newThings);
  50.     Napis(std::vector<T> newThings);
  51.  
  52.  
  53.     template <typename T> friend std::basic_ostream<T>& operator<<(std::basic_ostream<T>& os, const Napis<T>& dt);
  54.     std::vector<T> whatIsInside();
  55.     bool operator ==(const Napis<T>& v);
  56.     bool operator !=(const Napis<T>& v);
  57.     std::strong_ordering operator <=>(const Napis<T>& v);
  58.     Napis operator +(const Napis<T>& v);
  59.     Napis<T>& operator =(const Napis<T>& v);
  60.     Napis<T>& operator ++();
  61.     Napis<T>& operator --();
  62.     Napis<T> operator ++(int);
  63.     Napis<T> operator --(int);
  64.     int howLong() noexcept;
  65.     char at(int i);
  66.  
  67. };
  68.  
  69. template<typename T>
  70. Napis<T>::Napis() noexcept
  71. {
  72.     lenght = 0;
  73. }
  74.  
  75.  
  76. template<typename T>
  77. Napis<T>::Napis( const T* newThings)
  78. {
  79.     lenght = strlen(newThings);
  80.  
  81.     for (int i = 0; i < lenght; i++)
  82.     {
  83.         writing.push_back(*newThings);
  84.         newThings++;
  85.     }
  86. }
  87.  
  88.  
  89. template<typename T>
  90. Napis<T>::Napis(T newThings)
  91. {
  92.     lenght = 1;
  93.  
  94.     for (int i = 0; i < lenght; i++)
  95.     {
  96.         writing.push_back(newThings);
  97.         newThings++;
  98.     }
  99. }
  100.  
  101. template<typename T>
  102. Napis<T>::Napis(std::vector<T> newThings)
  103. {
  104.     lenght = newThings.size();
  105.  
  106.     for (int i = 0; i < lenght; i++)
  107.     {
  108.         writing.push_back(newThings.at(i));
  109.     }
  110. }
  111.  
  112. template<typename T>
  113. int Napis<T>::howLong() noexcept
  114. {
  115.     return this->lenght;
  116. }
  117.  
  118.  
  119. template<typename T>
  120. std::vector<T> Napis<T>::whatIsInside()
  121. {
  122.     return this->writing;
  123. }
  124.  
  125.  
  126. template<typename T>
  127. char Napis<T>::at(int i)
  128. {
  129.     return this->writing.at(i);
  130. }
  131.  
  132.  
  133. template<typename T>
  134. std::basic_ostream<T>& operator<<(std::basic_ostream<T>& os, const Napis<T>& dt)
  135. {
  136.     for (size_t i = 0; i < dt.writing.size(); i++)
  137.     {
  138.         os << dt.writing.at(i);
  139.     }
  140.  
  141.     return os;
  142. }
  143.  
  144.  
  145. template<typename T>
  146. Napis<T> Napis<T>::operator+(const Napis& v)
  147. {
  148.     Napis result;
  149.  
  150.     for (size_t i = 0; i < this->writing.size(); i++)
  151.     {
  152.         result.writing.push_back(this->writing.at(i));
  153.     }
  154.  
  155.     for (size_t i = 0; i < v.writing.size(); i++)
  156.     {
  157.         result.writing.push_back(v.writing.at(i));
  158.     }
  159.  
  160.     return result;
  161. }
  162.  
  163.  
  164. template<typename T>
  165. Napis<T>& Napis<T>::operator++()
  166. {
  167.     char c = ' ';
  168.  
  169.     for (size_t i = 0; i < this->writing.size(); i++)
  170.     {
  171.         c = this->writing.at(i);
  172.         this->writing.at(i) = std::toupper(c);
  173.     }
  174.  
  175.     return *this;
  176. }
  177.  
  178.  
  179. template<typename T>
  180. Napis<T>& Napis<T>::operator--()
  181. {
  182.     char c = ' ';
  183.  
  184.     for (size_t i = 0; i < this->writing.size(); i++)
  185.     {
  186.         c = this->writing.at(i);
  187.         this->writing.at(i) = std::tolower(c);
  188.     }
  189.  
  190.     return *this;
  191. }
  192.  
  193.  
  194. template<typename T>
  195. Napis<T> Napis<T>::operator++(int)
  196. {
  197.     Napis result(this->writing);
  198.  
  199.     ++(*this);
  200.  
  201.     return result;
  202. }
  203.  
  204.  
  205. template<typename T>
  206. Napis<T> Napis<T>::operator--(int)
  207. {
  208.     Napis<T> result(this->writing);
  209.  
  210.     --(*this);
  211.  
  212.     return result;
  213. }
  214.  
  215.  
  216. template<typename T>
  217. bool Napis<T>::operator==(const Napis<T>& v)
  218. {
  219.     if (this->lenght == v.lenght)
  220.     {
  221.         for (int i = 0; i < this->lenght; i++)
  222.         {
  223.             if (this->writing.at(i) != v.writing.at(i))
  224.             {
  225.                 return false;
  226.             }
  227.  
  228.             return true;
  229.         }
  230.     }
  231.  
  232.     return false;
  233. }
  234.  
  235.  
  236. template<typename T>
  237. bool Napis<T>::operator!=(const Napis& v)
  238. {
  239.     return !(*this == v);
  240. }
  241.  
  242.  
  243. template<typename T>
  244. std::strong_ordering Napis<T>::operator<=>(const Napis& v)
  245. {
  246.  
  247.     if (this != &v)
  248.     {
  249.         if (this->lenght == v.lenght)
  250.         {
  251.  
  252.  
  253.             for (int i = 0; i < this->lenght; i++)
  254.             {
  255.                 if (this->writing.at(i) != v.writing.at(i))
  256.                 {
  257.                     return std::strong_ordering::equal;
  258.                 }
  259.             }
  260.  
  261.             return std::strong_ordering::equal;
  262.         }
  263.     }
  264.  
  265.     return std::strong_ordering::equal;
  266. }
  267.  
  268.  
  269. template<typename T>
  270. Napis<T>& Napis<T>::operator =(const Napis& v) {
  271.  
  272.     if (this != &v)
  273.     {
  274.         this->lenght = v.lenght;
  275.         this->writing = v.writing;
  276.     }
  277.  
  278.     return *this;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement