Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cmath>
- using namespace std;
- class Distance{
- private:
- int yards,fleet,inches;
- void copy(Distance &d){
- this->yards = d.yards;
- this->fleet = d.fleet;
- this->inches = d.inches;
- }
- public:
- Distance(int yards = 0,int fleet = 0,int inches = 0){
- this->yards = yards;
- this->fleet = fleet;
- this->inches = inches;
- }
- Distance(Distance &d){copy(d);}
- ~Distance(){}
- void info(){
- cout<<this->yards<<" yd "<<this->fleet<<"' "<<this->inches<<"''";
- }
- double cm(){
- return ((this->yards*3*12) + (this->fleet*12) + (this->inches))*2.54;
- }
- int getYards(){
- return this->yards;
- }
- int getFleet(){
- return this->fleet;
- }
- int getInches(){
- return this->inches;
- }
- void setYards(int x){
- this->yards = x;
- }
- void setFleet(int x){
- this->fleet = x;
- }
- void setInches(int x){
- this->inches = x;
- }
- friend Distance operator+(const Distance &d1,const Distance &d2){
- Distance d;
- d.yards = d1.yards+d2.yards;
- d.fleet = d1.fleet+d2.fleet;
- d.inches = d1.inches+d2.inches;
- return d;
- }
- friend Distance operator-(const Distance &d1,const Distance &d2){
- Distance d;
- d.yards = d1.yards-d2.yards;
- d.fleet = d1.fleet-d2.fleet;
- d.inches = d1.inches-d2.inches;
- return d;
- }
- };
- void rectify(Distance &d){
- if(d.getInches() >=12){
- d.setInches(d.getInches()-12);
- d.setFleet(d.getFleet()+1);
- }
- if(d.getFleet() >=3){
- d.setFleet(d.getFleet()-3);
- d.setYards(d.getYards()+1);
- }
- if(d.getInches()<=0){
- d.setInches(d.getInches()+12);
- d.setFleet(d.getFleet()-1);
- }
- if(d.getFleet()<=0){
- d.setFleet(d.getFleet()+3);
- d.setYards(d.getYards()-1);
- }
- }
- int main()
- {
- int x,y,z;
- int x1,y1,z1;
- cout<<"Enter the (integer) yards, fleet, inches of a distance a: ";
- cin>>x>>y>>z;
- Distance d1(x,y,z);
- cout<<"Enter the (integer) yards, fleet, inches of a distance b: ";
- cin>>x>>y>>z;
- Distance d2(x,y,z);
- cout<<endl;
- cout<<endl;
- cout<<"The following distances have been input:"<<endl;
- cout<<"a = "; d1.info();cout<<" = "<<d1.cm(); cout<<" cm"<<endl;
- cout<<"b = "; d2.info();cout<<" = "<<d2.cm(); cout<<" cm"<<endl;
- cout<<endl;
- cout<<endl;
- Distance c;
- c = d1+d2;
- cout<<"a + b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
- c = d1-d2;
- cout<<"a - b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
- rectify(d1);
- rectify(d2);
- cout<<endl;
- cout<<endl;
- cout<<"The following distances have been input:"<<endl;
- cout<<"a = "; d1.info();cout<<" = "<<d1.cm(); cout<<" cm"<<endl;
- cout<<"b = "; d2.info();cout<<" = "<<d2.cm(); cout<<" cm"<<endl;
- cout<<endl;
- cout<<endl;
- c = d1+d2;
- cout<<"a + b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
- c = d1-d2;
- cout<<"a - b = ";c.info(); cout<<" = "<<c.cm(); cout<<" cm"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement