Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- class Cmass {
- private: //make it private when finished
- float mass;
- string index;
- public:
- Cmass() //konstruktor domyslny; masa 0.0 index: kg
- {
- mass = 0.0;
- index = "kg";
- }
- Cmass(const string &new_mass_const) //konstruktor przyjmuje string i ustawua mass i index w zaleznosci od tego co w stringu
- {
- string new_mass = new_mass_const;
- int i = new_mass.length();
- if (new_mass.at(i-1) == 't')
- {
- new_mass.erase(i);
- istringstream iss(new_mass);
- iss >> mass;
- index = "t";
- }
- else
- {
- if (new_mass.at(i-1) == 'g')
- {
- if(new_mass.at(i-2) == 'k')
- {
- new_mass.erase(i);
- new_mass.erase(i-1);
- istringstream iss(new_mass);
- iss >> mass;
- index = "kg";
- }
- else
- {
- new_mass.erase(i-1);
- istringstream iss(new_mass);
- iss >> mass;
- index = "g";
- }
- }
- else
- {
- istringstream iss(new_mass);
- iss >> mass;
- index = "kg";
- }
- }
- }
- Cmass(float new_mass = 0.0)
- {
- mass = new_mass;
- index = "kg";
- }
- float conv2kg(Cmass obj);
- float conv2kg(Cmass* pobj);
- //PRZECIAZANE OPERATORY
- operator float()
- {
- return mass;
- }
- operator string();
- bool operator < (const Cmass & first);
- bool operator + (Cmass &second);
- friend bool operator - (const Cmass &second);
- friend ostream &operator << (ostream &output, const Cmass &mass);
- };
- ostream &operator << (ostream &output, const Cmass &mass)
- {
- return output <<mass.mass <<mass.index<<endl;
- }
- bool Cmass::operator + (Cmass &second)
- {
- this->mass = conv2kg(this) + conv2kg(second);
- if (this->mass < 1)
- {
- this->mass = this->mass / 0.001;
- this->index = "g";
- return true;
- }
- if (this->mass > 1000)
- {
- this->mass = this->mass / 1000;
- this->index = "t";
- return true;
- }
- this->index = "kg";
- return true;
- }
- float Cmass::conv2kg(Cmass obj)
- {
- float obj_mass = obj.mass;
- if(obj.index == "g")
- {
- obj_mass = obj_mass * 0.001;
- return obj_mass;
- }
- if (obj.index == "kg")
- {
- return obj_mass;
- }
- if (obj.index == "t")
- {
- obj_mass = obj_mass * 1000;
- return obj_mass;
- }
- return 0;
- }
- float Cmass::conv2kg(Cmass* pobj)
- {
- Cmass obj = *pobj;
- if(obj.index == "g")
- {
- obj.mass = obj.mass * 0.001;
- return obj.mass;
- }
- if (obj.index == "kg")
- {
- return obj.mass;
- }
- if (obj.index == "t")
- {
- obj.mass = obj.mass * 1000;
- return obj.mass;
- }
- return 0;
- }
- int main()
- {
- Cmass obj1(string("120kg"));
- Cmass obj2(string("2t"));
- Cmass obj3(string("0kg"));
- obj3 = obj1 + obj2;
- cout<< obj1;
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement