Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. template <class T>
  2. class Operand;
  3.  
  4. template <>
  5. class Operand <std::string>
  6. {
  7.         std::string value;
  8. public:
  9.         Operand() {}
  10.         Operand(std::string initValue)
  11.         {
  12.                 value = initValue;
  13.         }
  14.  
  15.         Operand <std::string> operator+(Operand<std::string> const &b) const
  16.         {
  17.                 Operand<std::string> str(*this);
  18.                 str.setValue(str.getValue() + b.getValue());
  19.                 return str;
  20.         }
  21.  
  22.         Operand <std::string> operator+(Operand<int> const &b) const;
  23.  
  24.         Operand <std::string> operator*(Operand<int> const &b) const;
  25.  
  26.         std::string getValue() const
  27.         {
  28.                 return value;
  29.         }
  30.  
  31.         void setValue(std::string initValue)
  32.         {
  33.                 value = initValue;
  34.         }
  35. };
  36.  
  37. template <class T>
  38. class Operand
  39. {
  40.         T value;
  41. public:
  42.         Operand() {}
  43.         Operand(T initValue)
  44.         {
  45.                 value = initValue;
  46.         }
  47.  
  48.         Operand<T> operator-(Operand<T> const &b) const
  49.         {
  50.                 Operand<T> integ(*this);
  51.                 integ.setValue(integ.getValue() - b.getValue());
  52.                 return integ;
  53.         }
  54.  
  55.         Operand<T> operator/(Operand<T> const &b) const
  56.         {
  57.                 Operand<T> integ(*this);
  58.                 integ.setValue(integ.getValue() / b.getValue());
  59.                 return integ;
  60.         }
  61.  
  62.         Operand<T> operator+(Operand<T> const &b) const
  63.         {
  64.                 Operand<T> integ(*this);
  65.                 integ.setValue(integ.getValue() + b.getValue());
  66.                 return integ;
  67.         }
  68.  
  69.         /*Operand<std::string> operator+(Operand<std::string> const &b) const
  70.         {
  71.                 Operand<std::string> str(std::to_string(this->getValue()));
  72.                 str.setValue(str.getValue() + b.getValue());
  73.                 return str;
  74.         }*/
  75.  
  76.         Operand<T> operator*(Operand<T> const &b) const
  77.         {
  78.                 Operand<T> integ(*this);
  79.                 integ.setValue(integ.getValue() * b.getValue());
  80.                 return integ;
  81.         }
  82.  
  83.         Operand<std::string> operator*(Operand<std::string> const &b) const
  84.         {
  85.                 Operand<std::string> str(b);
  86.                 int n = this->getValue();
  87.                 std::string strPart = str.getValue();
  88.  
  89.                 while (n--)
  90.                 {
  91.                         str.setValue(str.getValue() + strPart);
  92.                 }
  93.  
  94.                 return str;
  95.         }
  96.  
  97.         T getValue() const
  98.         {
  99.                 return value;
  100.         }
  101.  
  102.         void setValue(T initValue)
  103.         {
  104.                 value = initValue;
  105.         }
  106. };
  107.  
  108.  
  109. Operand<std::string> Operand<std::string>::operator+(const Operand<int>& b) const
  110. {
  111.     Operand<std::string> str(*this);
  112.     str.setValue(str.getValue() + std::to_string(b.getValue()));
  113.     return str;
  114. }
  115.  
  116. Operand<std::string> Operand<std::string>::operator*(const Operand<int>& b) const
  117. {
  118.     Operand<std::string> str(*this);
  119.     int n = b.getValue();
  120.     std::string strPart = str.getValue();
  121.  
  122.     while (n--)
  123.     {
  124.         str.setValue(str.getValue() + strPart);
  125.     }
  126.  
  127.     return str;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement