Advertisement
Guest User

Cmass

a guest
Nov 24th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. class Cmass {
  9.  
  10.     private:                 //make it private when finished
  11.         float mass;
  12.         string index;
  13.  
  14.     public:
  15.         Cmass()          //konstruktor domyslny; masa 0.0 index: kg
  16.         {
  17.             mass = 0.0;
  18.             index = "kg";
  19.         }
  20.         Cmass(const string &new_mass_const)  //konstruktor przyjmuje string i ustawua mass i index w zaleznosci od tego co w stringu
  21.         {
  22.             string new_mass = new_mass_const;
  23.             int i = new_mass.length();
  24.             if (new_mass.at(i-1) == 't')
  25.                 {
  26.                     new_mass.erase(i);
  27.                     istringstream iss(new_mass);
  28.                     iss >> mass;
  29.                     index = "t";
  30.                 }
  31.             else
  32.                 {
  33.             if (new_mass.at(i-1) == 'g')
  34.                 {
  35.                     if(new_mass.at(i-2) == 'k')
  36.                     {
  37.                     new_mass.erase(i);
  38.                     new_mass.erase(i-1);
  39.                     istringstream iss(new_mass);
  40.                     iss >> mass;
  41.                     index = "kg";
  42.                     }
  43.                     else
  44.                 {
  45.                     new_mass.erase(i-1);
  46.                     istringstream iss(new_mass);
  47.                     iss >> mass;
  48.                     index = "g";
  49.                 }
  50.                 }
  51.             else
  52.                 {
  53.                     istringstream iss(new_mass);
  54.                     iss >> mass;
  55.                     index = "kg";
  56.                 }
  57.  
  58.                 }
  59.         }
  60.         Cmass(float new_mass = 0.0)
  61.         {
  62.             mass = new_mass;
  63.             index = "kg";
  64.         }
  65.  
  66.         float conv2kg(Cmass obj);
  67.         float conv2kg(Cmass* pobj);
  68.  
  69.  //PRZECIAZANE OPERATORY
  70.  
  71.         operator float()
  72.         {
  73.              return mass;
  74.         }
  75.         operator string();
  76.         bool operator < (const Cmass & first);
  77.         bool operator + (Cmass &second);
  78.         friend bool operator - (const Cmass &second);
  79.         friend ostream &operator << (ostream &output, const Cmass &mass);
  80.  
  81. };
  82.  
  83. ostream &operator << (ostream &output, const Cmass &mass)
  84.     {
  85.        return output <<mass.mass <<mass.index<<endl;
  86.     }
  87.  
  88.  
  89. bool Cmass::operator + (Cmass &second)
  90.             {
  91.  
  92.             this->mass = conv2kg(this) + conv2kg(second);
  93.                 if (this->mass < 1)
  94.                     {
  95.                         this->mass = this->mass / 0.001;
  96.                         this->index = "g";
  97.                         return true;
  98.                     }
  99.                 if  (this->mass > 1000)
  100.                     {
  101.                         this->mass = this->mass / 1000;
  102.                         this->index = "t";
  103.                         return true;
  104.                     }
  105.  
  106.             this->index = "kg";
  107.             return true;
  108.         }
  109.  
  110. float Cmass::conv2kg(Cmass obj)
  111.         {
  112.             float obj_mass = obj.mass;
  113.                 if(obj.index == "g")
  114.                     {
  115.                         obj_mass = obj_mass * 0.001;
  116.                         return obj_mass;
  117.                     }
  118.                 if (obj.index == "kg")
  119.                     {
  120.                         return obj_mass;
  121.                     }
  122.                 if (obj.index == "t")
  123.                     {
  124.                         obj_mass = obj_mass * 1000;
  125.                         return obj_mass;
  126.                     }
  127.                 return 0;
  128.         }
  129.  
  130. float Cmass::conv2kg(Cmass* pobj)
  131.         {
  132.             Cmass obj = *pobj;
  133.                 if(obj.index == "g")
  134.                     {
  135.                         obj.mass = obj.mass * 0.001;
  136.                         return obj.mass;
  137.                     }
  138.                 if (obj.index == "kg")
  139.                     {
  140.                         return obj.mass;
  141.                     }
  142.                 if (obj.index == "t")
  143.                     {
  144.                         obj.mass = obj.mass * 1000;
  145.                         return obj.mass;
  146.                     }
  147.                 return 0;
  148.         }
  149.  
  150. int main()
  151. {
  152.     Cmass obj1(string("120kg"));
  153.     Cmass obj2(string("2t"));
  154.     Cmass obj3(string("0kg"));
  155.     obj3 = obj1 + obj2;
  156.     cout<< obj1;
  157.     cin.get();
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement