Advertisement
Sofe1204

Untitled

May 13th, 2022
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class Distance{
  8.     private:
  9.     int yards,fleet,inches;
  10.     void copy(Distance &d){
  11.         this->yards = d.yards;
  12.         this->fleet = d.fleet;
  13.         this->inches = d.inches;
  14.     }
  15.  
  16.     public:
  17.     Distance(int yards = 0,int fleet = 0,int inches = 0){
  18.         this->yards = yards;
  19.         this->fleet = fleet;
  20.         this->inches = inches;
  21.     }
  22.     Distance(Distance &d){copy(d);}
  23.     ~Distance(){}
  24.  
  25.     void info(){
  26.         cout<<this->yards<<" yd "<<this->fleet<<"' "<<this->inches<<"''";
  27.     }
  28.  
  29.     double cm(){
  30.         return ((this->yards*3*12) + (this->fleet*12) + (this->inches))*2.54;
  31.     }
  32.  
  33.     int getYards(){
  34.         return this->yards;
  35.     }
  36.     int getFleet(){
  37.         return this->fleet;
  38.     }
  39.  
  40.     int getInches(){
  41.         return this->inches;
  42.     }
  43.  
  44.     void setYards(int x){
  45.         this->yards = x;
  46.     }
  47.     void setFleet(int x){
  48.         this->fleet = x;
  49.     }
  50.     void setInches(int x){
  51.         this->inches = x;
  52.     }
  53.  
  54.  
  55.     friend Distance operator+(const Distance &d1,const Distance &d2){
  56.         Distance d;
  57.         d.yards = d1.yards+d2.yards;
  58.         d.fleet = d1.fleet+d2.fleet;
  59.         d.inches = d1.inches+d2.inches;
  60.         return d;
  61.     }
  62.     friend Distance operator-(const Distance &d1,const Distance &d2){
  63.         Distance d;
  64.         d.yards = d1.yards-d2.yards;
  65.         d.fleet = d1.fleet-d2.fleet;
  66.         d.inches = d1.inches-d2.inches;
  67.         return d;
  68.     }
  69.  
  70. };
  71.  
  72. void rectify(Distance &d){
  73.  
  74.     if(d.getInches() >=12){
  75.         d.setInches(d.getInches()-12);
  76.         d.setFleet(d.getFleet()+1);
  77.     }
  78.     if(d.getFleet() >=3){
  79.         d.setFleet(d.getFleet()-3);
  80.         d.setYards(d.getYards()+1);
  81.     }
  82.     if(d.getInches()<=0){
  83.         d.setInches(d.getInches()+12);
  84.         d.setFleet(d.getFleet()-1);
  85.     }
  86.     if(d.getFleet()<=0){
  87.         d.setFleet(d.getFleet()+3);
  88.         d.setYards(d.getYards()-1);
  89.     }
  90.  
  91.  
  92. }
  93.  
  94. int main()
  95. {
  96.     int x,y,z;
  97.     int x1,y1,z1;
  98.     cout<<"Enter the (integer) yards, fleet, inches of a distance a: ";
  99.     cin>>x>>y>>z;
  100.     Distance d1(x,y,z);
  101.     cout<<"Enter the (integer) yards, fleet, inches of a distance b: ";
  102.     cin>>x>>y>>z;
  103.     Distance d2(x,y,z);
  104.  
  105.     cout<<endl;
  106.     cout<<endl;
  107.  
  108.     cout<<"The following distances have been input:"<<endl;
  109.     cout<<"a = "; d1.info();cout<<" = "<<d1.cm(); cout<<" cm"<<endl;
  110.     cout<<"b = "; d2.info();cout<<" = "<<d2.cm(); cout<<" cm"<<endl;
  111.  
  112.     cout<<endl;
  113.     cout<<endl;
  114.  
  115.     Distance c;
  116.     c = d1+d2;
  117.     cout<<"a + b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
  118.     c = d1-d2;
  119.     cout<<"a - b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
  120.  
  121.  
  122.     rectify(d1);
  123.     rectify(d2);
  124.  
  125.     cout<<endl;
  126.     cout<<endl;
  127.  
  128.     cout<<"The following distances have been input:"<<endl;
  129.     cout<<"a = "; d1.info();cout<<" = "<<d1.cm(); cout<<" cm"<<endl;
  130.     cout<<"b = "; d2.info();cout<<" = "<<d2.cm(); cout<<" cm"<<endl;
  131.  
  132.     cout<<endl;
  133.     cout<<endl;
  134.  
  135.     c = d1+d2;
  136.     cout<<"a + b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
  137.     c = d1-d2;
  138.     cout<<"a - b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement