Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. complex.cpp
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. template <typename T>
  10. class Complex
  11. {
  12. private:
  13.     vector<string> types;
  14.  
  15.     int Complex::GetTypeId(string type)
  16.     {
  17.         vector<string>::iterator iter = std::find(types.begin(), types.end(), type);
  18.  
  19.         if(iter==types.end())
  20.             return -1;
  21.  
  22.         return std::distance(types.begin(), iter);
  23.     }
  24.  
  25.     void FillStandartTypes(vector <string> & vect)
  26.     {
  27.         vect.push_back("m");
  28.         vect.push_back("kg");
  29.         vect.push_back("s");
  30.         vect.push_back("A");
  31.         vect.push_back("K");
  32.         vect.push_back("cd");
  33.         vect.push_back("mol");
  34.     }
  35.  
  36.     void InitFill(T value, string s)
  37.     {
  38.         FillStandartTypes(types);
  39.         val = value;
  40.         typeId = GetTypeId(s);
  41.     }
  42.  
  43. public:
  44.     int typeId;
  45.     T val;
  46.  
  47.     Complex()
  48.     {
  49.         InitFill(0, "");
  50.     }
  51.  
  52.     Complex(T value)
  53.     {
  54.         InitFill(value, "");
  55.     }
  56.  
  57.     Complex(T value, string s)
  58.     {
  59.         InitFill(value, s);
  60.     }
  61.  
  62.     string GetType(int typeId)
  63.     {
  64.         if(typeId == -1)
  65.             return "";
  66.         return types[typeId];
  67.     }
  68.  
  69.     int AddType(string type)
  70.     {
  71.         types.push_back(type);
  72.         return types.size()-1;
  73.     }
  74.  
  75.     bool operator ==(Complex & c)
  76.     {
  77.         if(this.typeId != c.typeId)
  78.             return false;
  79.  
  80.         return this.val == c.val;
  81.     }
  82.  
  83.     bool operator !=(Complex & c)
  84.     {
  85.         return !(this == c);
  86.     }
  87.  
  88.     friend ostream& operator<<(ostream& out, Complex<T> & c)
  89.     {
  90.         out << c.val << c.GetType(c.typeId);
  91.         return out;
  92.     }
  93.  
  94.     template <typename G>
  95.     friend Complex<T> operator+( Complex<T> & c1, Complex<G> & c2)
  96.     {
  97.         if(c1.typeId != c2.typeId)
  98.             throw 1;
  99.  
  100.         Complex <T> temp(c1.val + c2.val);
  101.         temp.typeId = c1.typeId;
  102.         return temp;
  103.     }
  104.  
  105.     template <typename G>
  106.     friend Complex<T> operator-( Complex<T> & c1, Complex<G> & c2)
  107.     {
  108.         if(c1.typeId != c2.typeId)
  109.             throw 1;
  110.  
  111.         Complex <T> temp(c1.val - c2.val);
  112.         temp.typeId = c1.typeId;
  113.         return temp;
  114.     }
  115.  
  116.     template <typename G>
  117.     friend Complex<T> operator*( Complex<T> & c1, Complex<G> & c2)
  118.     {
  119.         Complex <T> temp(c1.val * c2.val);
  120.         temp.typeId = temp.AddType(c1.GetType(c1.typeId) + "*" + c2.GetType(c2.typeId));
  121.         return temp;
  122.     }
  123.  
  124.     template <typename G>
  125.     friend Complex<T> operator/( Complex<T> & c1, Complex<G> & c2)
  126.     {
  127.         if(c2.val == 0)
  128.             throw 2;
  129.  
  130.         Complex <T> temp(c1.val / c2.val);
  131.         temp.typeId = temp.AddType(c1.GetType(c1.typeId) + "/" + c2.GetType(c2.typeId));
  132.         return temp;
  133.     }
  134. };
  135.  
  136. lab3.cpp
  137. #include <iostream>
  138. #include "complex.cpp"
  139.  
  140. using namespace std;
  141.  
  142. class TestClass
  143. {
  144. public:
  145.  
  146.     template <class T, class G>
  147.     void Run(T tval, G gval)
  148.     {
  149.         vector <string> types;
  150.         types.push_back("m");
  151.         types.push_back("kg");
  152.         types.push_back("s");
  153.         types.push_back("A");
  154.         types.push_back("K");
  155.         types.push_back("cd");
  156.         types.push_back("mol");
  157.  
  158.         for(int i=0; i < types.size();i++)
  159.         {
  160.             for(int j=0; j < types.size(); j++)
  161.             {
  162.                 for(int k=0; k < 4; k++)
  163.                 {
  164.                     Complex<T> c1(tval,types[i]);
  165.                     Complex<G> c2(gval,types[j]);
  166.  
  167.                     try
  168.                     {
  169.                         switch(k)
  170.                         {
  171.                         case 0:
  172.                             cout << c1 << "+" << c2 << "=";
  173.                             cout << c1 + c2 << endl;
  174.                             break;
  175.                         case 1:
  176.                             cout << c1 << "-" << c2 << "=";
  177.                             cout << c1 - c2 << endl;
  178.                             break;
  179.                         case 2:
  180.                             cout << c1 << "*" << c2 << "=";
  181.                             cout << c1 * c2 << endl;
  182.                             break;
  183.                         case 3:
  184.                             cout << c1 << "/" << c2 << "=";
  185.                             cout << c1 / c2 << endl;
  186.                             break;
  187.                         }                  
  188.                     }
  189.                     catch(int i)
  190.                     {
  191.                         if(i==1)
  192.                             cout << "Classes are not equal" << endl;
  193.                         else
  194.                             cout << "Division by zero" << endl;
  195.                     }
  196.                 }
  197.             }
  198.         }
  199.     }
  200. };
  201.  
  202. int main()
  203. {
  204.     TestClass test1;
  205.     test1.Run(10, 3.0);
  206.  
  207.     TestClass test2;
  208.     test2.Run(7.47,3.21f);
  209.  
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement