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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include <iostream>
using namespace std;
class Distance
{
public:
/* Constructs a default Distance of 0 (0 feet and 0.0 inches)
*/
Distance();
/* Constructs a distance of ft feet and in inches,
unless in >= 12, in which case the values of feet and inches
are adjusted accordingly. A Distance will always be positive.
*/
Distance(int, double);
/* Constructs a distance of 0 ft and in inches,
unless in >= 12, in which case the values of feet and inches
are adjusted accordingly. A Distance will always be positive.
*/
Distance(double in);
/* Returns just the feet portion of the Distance
*/
unsigned getFeet() const;
/* Returns just the inches portion of the Distance
*/
double getInches() const;
/* Returns the entire distance as the equivalent amount of inches.
(e.g., 4 feet 3.5 inches would be returned as 51.5 inches)
*/
double distanceInInches() const;
/* Returns the entire distance as the equivalent amount of feet.
(e.g., 3 feet 6 inches would be returned as 3.5 feet)
*/
double distanceInFeet() const;
/* Returns the entire distance as the equivalent amount of meters.
1 inch equals 0.0254 meters.
(e.g., 2 feet 8.12 inches would be returned as 0.815848 meters)
*/
double distanceInMeters() const;
/* Returns the sum of 2 Distances.
*/
const Distance operator+(const Distance &rhs) const;
/* Returns the difference between 2 Distances.
*/
const Distance operator-(const Distance &rhs) const;
/* Outputs to the stream out the Distance in the format:
feet' inches'' (i.e. 3' 3.41'')
*/
friend ostream & operator<<(ostream &out, const Distance &rhs);
/* Used by the 2 parameterized constructors to convert any negative values to positive and
inches >= 12 to the appropriate number of feet and inches.
*/
//this could be a mutator function?
friend void init(int& , double& );
private:
int feet;
double inches;
};
Distance::Distance() {
feet = 0;
inches = 0.0;
}
Distance::Distance(int ft, double in) {
if ((in >= 12) || (in < 0)) {
void init(int& , double& );
init(ft, in);
feet = ft;
inches = in;
}
else
{
inches = in;
feet = ft;
}
}
Distance::Distance(double in) {
if ((in >= 12) || (in < 0)) {
int ft = 0;
void init(int& , double& );
init(ft, in);
feet = ft;
inches = in;
}
else
{
inches = in;
feet = 0;
}
}
unsigned Distance::getFeet() const {
return feet;
}
double Distance::getInches() const {
return inches;
}
double Distance::distanceInInches() const {
return (inches + (feet * 12));
}
double Distance::distanceInFeet() const {
return (feet + (inches / 12));
}
double Distance::distanceInMeters() const {
return ((feet + (inches / 12))*0.3048);
}
const Distance Distance::operator+(const Distance &rhs) const {
Distance newSum;
newSum.feet = (feet + rhs.feet);
newSum.inches = (inches + rhs.inches);
if (newSum.inches >= 12.0) {
int tempoft = newSum.feet;
double tempoin = newSum.inches;
void init(int& , double& );
init(tempoft, tempoin);
newSum.feet = tempoft;
newSum.inches = tempoin;
}
return newSum;
}
const Distance Distance::operator-(const Distance &rhs) const {
Distance newDiff;
newDiff.feet = (feet - rhs.feet);
newDiff.inches = (inches - rhs.inches);
if (!(newDiff.feet >= 0)) {
newDiff.feet = (rhs.feet - feet);
//cout << "hey";
}
else if ((!(newDiff.inches >= 0.0)) || (newDiff.inches >= 12.0)) {
int tempoft2 = newDiff.feet;
double tempoin2 = newDiff.inches;
void init(int& , double& );
init(tempoft2, tempoin2);
newDiff.feet = tempoft2;
newDiff.inches = tempoin2;
//cout << "hey";
}
return newDiff;
}
void init(int& ft, double& in) {
if(in < 0.0) {
in = -in;
cout << "hey";
}
else if (in >= 12.0) {
ft = ft + (in / 12.0);
double decimDigits = (in - static_cast <int>(in));
in = (static_cast <int>(in) % 12);
in = in + decimDigits;
}
}
ostream & operator<<(ostream &out, const Distance &rhs) {
out << rhs.feet << "' " << rhs.inches << "''" << endl;
return out;
}
int main()
{
Distance d1;
cout << "d1: " << d1 << endl;
Distance d2 = Distance(2, 5.9);
Distance d3 = Distance(3.75);
cout << "d2: " << d2 << endl;
cout << "d3: " << d3 << endl;
//test init helper function
Distance d4 = Distance(5, 19.34);
Distance d5 = Distance(100);
cout << "d4: " << d4 << endl;
cout << "d5: " << d5 << endl;
//test add (<12 inches)
Distance sum1, sum2, tot1, tot2, tot3, tot4, tot5, tot6;
sum1 = (d4 + d5);
//Distance newSum2(sum2.feet, sum2.inches);
cout << "d4 + d5: " << sum1 << endl;
sum2 = (d2 + d4);
//test add (>12 inches)
cout << "d2 + d4: " << sum2 << endl;
tot1 = (d3 - d1);
//test sub (0 ft)
cout << "d3 - d1: " << tot1 << endl;
//test sub (0 ft, negative conversion)
tot2 = (d1 - d3);
cout << "d1 - d3: " << tot2 << endl;
tot3 = (d4 - d2);
//test sub (positive ft & inch)
cout << "d4 - d2: " << tot3 << endl;
tot4 = (d2 - d4);
//test sub (negative ft & inch)
cout << "d2 - d4: " << tot4 << endl;
tot5 = (d4 - d5);
//test sub (negative ft, positive inch)
cout << "d4 - d5: " << tot5 << endl;
tot6 = (d5 - d2);
//test sub (positive ft, negative inch)
cout << "d5 - d2: " << tot6 << endl;
return 0;
}
|