1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
using namespace std;
class distance
{
private:
int mi, ft, in;
void simplify();
public:
distance(int m=0, int f=0, int i=0)
{ mi=m; ft=f; in=i; simplify(); }
void setdata(int m,int f, int i)
{ mi=m; ft=f; in=i; simplify(); }
int getmi()
{return mi;}
int getft()
{return ft;}
int getin()
{return in;}
distance operator+(const distance &) const;
};
void distance::simplify()
{
in = 12*ft + in;
ft = in / 12;
in = in % 12;
ft = 5280*mi + ft;
mi = ft / 5280;
ft = ft % 5280;
}
distance distance::operator+(const distance &right) const
{
distance temp;
temp.in = in + right.in;
temp.ft = ft + right.ft;
temp.mi = mi + right.mi;
temp.simplify();
return temp;
}
int main()
{
distance d1, d2, d3;
int m, f, i;
cout << "enter a distance in mi, ft and in: ";
cin >> m >> f >> i;
d1.setdata(m,f,i);
cout << "enter another distance: ";
cin >> m >> f >> i;
d2.setdata(m,f,i);
d3 = d1 + d2;
cout << "d1 + d2 = " << d3.getft() << "ft " << d3.getin() << "in " << endl;
return 0;
}
|