Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- complex.cpp
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- template <typename T>
- class Complex
- {
- private:
- vector<string> types;
- int Complex::GetTypeId(string type)
- {
- vector<string>::iterator iter = std::find(types.begin(), types.end(), type);
- if(iter==types.end())
- return -1;
- return std::distance(types.begin(), iter);
- }
- void FillStandartTypes(vector <string> & vect)
- {
- vect.push_back("m");
- vect.push_back("kg");
- vect.push_back("s");
- vect.push_back("A");
- vect.push_back("K");
- vect.push_back("cd");
- vect.push_back("mol");
- }
- void InitFill(T value, string s)
- {
- FillStandartTypes(types);
- val = value;
- typeId = GetTypeId(s);
- }
- public:
- int typeId;
- T val;
- Complex()
- {
- InitFill(0, "");
- }
- Complex(T value)
- {
- InitFill(value, "");
- }
- Complex(T value, string s)
- {
- InitFill(value, s);
- }
- string GetType(int typeId)
- {
- if(typeId == -1)
- return "";
- return types[typeId];
- }
- int AddType(string type)
- {
- types.push_back(type);
- return types.size()-1;
- }
- bool operator ==(Complex & c)
- {
- if(this.typeId != c.typeId)
- return false;
- return this.val == c.val;
- }
- bool operator !=(Complex & c)
- {
- return !(this == c);
- }
- friend ostream& operator<<(ostream& out, Complex<T> & c)
- {
- out << c.val << c.GetType(c.typeId);
- return out;
- }
- template <typename G>
- friend Complex<T> operator+( Complex<T> & c1, Complex<G> & c2)
- {
- if(c1.typeId != c2.typeId)
- throw 1;
- Complex <T> temp(c1.val + c2.val);
- temp.typeId = c1.typeId;
- return temp;
- }
- template <typename G>
- friend Complex<T> operator-( Complex<T> & c1, Complex<G> & c2)
- {
- if(c1.typeId != c2.typeId)
- throw 1;
- Complex <T> temp(c1.val - c2.val);
- temp.typeId = c1.typeId;
- return temp;
- }
- template <typename G>
- friend Complex<T> operator*( Complex<T> & c1, Complex<G> & c2)
- {
- Complex <T> temp(c1.val * c2.val);
- temp.typeId = temp.AddType(c1.GetType(c1.typeId) + "*" + c2.GetType(c2.typeId));
- return temp;
- }
- template <typename G>
- friend Complex<T> operator/( Complex<T> & c1, Complex<G> & c2)
- {
- if(c2.val == 0)
- throw 2;
- Complex <T> temp(c1.val / c2.val);
- temp.typeId = temp.AddType(c1.GetType(c1.typeId) + "/" + c2.GetType(c2.typeId));
- return temp;
- }
- };
- lab3.cpp
- #include <iostream>
- #include "complex.cpp"
- using namespace std;
- class TestClass
- {
- public:
- template <class T, class G>
- void Run(T tval, G gval)
- {
- vector <string> types;
- types.push_back("m");
- types.push_back("kg");
- types.push_back("s");
- types.push_back("A");
- types.push_back("K");
- types.push_back("cd");
- types.push_back("mol");
- for(int i=0; i < types.size();i++)
- {
- for(int j=0; j < types.size(); j++)
- {
- for(int k=0; k < 4; k++)
- {
- Complex<T> c1(tval,types[i]);
- Complex<G> c2(gval,types[j]);
- try
- {
- switch(k)
- {
- case 0:
- cout << c1 << "+" << c2 << "=";
- cout << c1 + c2 << endl;
- break;
- case 1:
- cout << c1 << "-" << c2 << "=";
- cout << c1 - c2 << endl;
- break;
- case 2:
- cout << c1 << "*" << c2 << "=";
- cout << c1 * c2 << endl;
- break;
- case 3:
- cout << c1 << "/" << c2 << "=";
- cout << c1 / c2 << endl;
- break;
- }
- }
- catch(int i)
- {
- if(i==1)
- cout << "Classes are not equal" << endl;
- else
- cout << "Division by zero" << endl;
- }
- }
- }
- }
- }
- };
- int main()
- {
- TestClass test1;
- test1.Run(10, 3.0);
- TestClass test2;
- test2.Run(7.47,3.21f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement