// 22.5HV2 Software Engineering II // Unit 3 Exercise 2 #include "imperial.h" void Imperial::reduce() { pounds += ounces/16; stones += pounds/14; pounds %= 14; ounces %= 16; } ostream& operator<<(ostream& ostr, const Imperial& w) { return ostr << w.stones << "st. " << w.pounds << "lb. " << w.ounces << "oz."; } istream& operator>>(istream& istr, Imperial& w) { cout << "Enter stones: "; istr >> w.stones; cout << "Enter pounds: "; istr >> w.pounds; cout << "Enter ounces: "; istr >> w.ounces; return istr; } bool operator<(const Imperial &a, const Imperial &b) { return (a.stones*224+a.pounds*16+a.ounces < b.stones*224+b.pounds*16+b.ounces); } bool operator>(const Imperial &a, const Imperial &b) { return (a.stones*224+a.pounds*16+a.ounces > b.stones*224+b.pounds*16+b.ounces); } bool operator<=(const Imperial &a, const Imperial &b) { return (a.stones*224+a.pounds*16+a.ounces <= b.stones*224+b.pounds*16+b.ounces); } bool operator>=(const Imperial &a, const Imperial &b) { return (a.stones*224+a.pounds*16+a.ounces >= b.stones*224+b.pounds*16+b.ounces); } bool operator==(const Imperial &a, const Imperial &b) { return (a.stones*224+a.pounds*16+a.ounces == b.stones*224+b.pounds*16+b.ounces); } bool operator!=(const Imperial &a, const Imperial &b) { return (a.stones*224+a.pounds*16+a.ounces != b.stones*224+b.pounds*16+b.ounces); } Imperial operator+(const Imperial &a, const Imperial &b) { return Imperial(a.stones+b.stones,a.pounds+b.pounds,a.ounces+b.ounces); // Call to constructor will call reduce() } Imperial operator-(const Imperial &a, const Imperial &b) { int oz = (a.stones*224+a.pounds*16+a.ounces) - (b.stones*224+b.pounds*16+b.ounces); if (oz<0) { cerr << "Error can't handle -ve weights!" << endl; return Imperial(0,0,0); } else return Imperial(0,0,oz); // Call to constructor will call reduce() }