From aaa59d7ac8582ef89818988307d86acbba9a3574 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Fri, 31 Jan 2025 00:57:22 -0800 Subject: [PATCH] feat: complete c_4_lab_4 --- meson.build | 6 +++ src/chapter_4/lab_4/Distance.cpp | 54 +++++++++++++++++++++++ src/chapter_4/lab_4/Distance.h | 74 ++++++++++++++++++++++++++++++++ src/chapter_4/lab_4/main.cpp | 43 +++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 src/chapter_4/lab_4/Distance.cpp create mode 100644 src/chapter_4/lab_4/Distance.h create mode 100644 src/chapter_4/lab_4/main.cpp diff --git a/meson.build b/meson.build index 2039a22..5d04055 100644 --- a/meson.build +++ b/meson.build @@ -22,3 +22,9 @@ c_2_program_1 = executable('c_2_program_1', 'src/chapter_2/program_1/main.cpp', c_3_lab_1 = executable('c_3_lab_1', 'src/chapter_3/lab_1/main.cpp', install: true) c_3_program_1 = executable('c_3_program_1', 'src/chapter_3/program_1/main.cpp', install: true) + +c_4_lab_4 = executable( + 'c_4_lab_4', + ['src/chapter_4/lab_4/main.cpp', 'src/chapter_4/lab_4/Distance.cpp'], + install: true, +) diff --git a/src/chapter_4/lab_4/Distance.cpp b/src/chapter_4/lab_4/Distance.cpp new file mode 100644 index 0000000..4a85fd6 --- /dev/null +++ b/src/chapter_4/lab_4/Distance.cpp @@ -0,0 +1,54 @@ +#include "Distance.h" +#include + +Distance::Distance() { + this->_feet = 0; + this->_inches = 0; +} + +Distance::Distance(unsigned feet, double inches) { + this->_feet = feet; + this->_inches = inches; + this->init(); +} + +Distance::Distance(double inches) { + this->_inches = inches; + this->_feet = 0; + this->init(); +} + +void Distance::init() { + this->_inches = abs(this->_inches); + this->_feet += static_cast(this->_inches) / 12; + this->_inches -= ((static_cast(this->_inches) / 12) * 12); +} + +unsigned Distance::getFeet() const { return this->_feet; } + +double Distance::getInches() const { return this->_inches; } + +double Distance::distanceInInches() const { + return this->_inches + this->_feet * 12; +} + +double Distance::distanceInFeet() const { + return (static_cast(this->_feet) + this->_inches / 12); +} + +double Distance::distanceInMeters() const { + return (distanceInInches() * 0.0254); +} + +Distance Distance::operator-(const Distance &rhs) const { + return Distance(this->distanceInInches() - rhs.distanceInInches()); +} + +Distance Distance::operator+(const Distance &rhs) const { + return Distance(this->distanceInInches() + rhs.distanceInInches()); +} + +ostream &operator<<(ostream &out, const Distance &rhs) { + out << rhs.getFeet() << "' " << rhs.getInches() << "\""; + return out; +} diff --git a/src/chapter_4/lab_4/Distance.h b/src/chapter_4/lab_4/Distance.h new file mode 100644 index 0000000..9e8867b --- /dev/null +++ b/src/chapter_4/lab_4/Distance.h @@ -0,0 +1,74 @@ +#pragma once + +#define DISTANCE_H + +#include +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. + Calls the init() helper function to convert negative inches to positive + and any excess inches to feet. + */ + Distance(unsigned ft, double in); + + /* Constructs a distance of 0 ft and in inches. + Calls the init() helper function to convert negative inches to positive + and any excess inches to feet. + */ + explicit 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. + */ + Distance operator+(const Distance &rhs) const; + + /* Returns the difference between 2 Distances. + */ + 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); + +private: + /* Used by the 2 parameterized constructors to convert any negative values to + positive and inches >= 12 to the appropriate number of feet and inches. + Hint: No branch needed to convert to positive if you use absolute value + function. + */ + void init(); + + unsigned _feet; + double _inches; +}; diff --git a/src/chapter_4/lab_4/main.cpp b/src/chapter_4/lab_4/main.cpp new file mode 100644 index 0000000..d6c95d3 --- /dev/null +++ b/src/chapter_4/lab_4/main.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +#include "Distance.h" + +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) + cout << "d4 + d5: " << (d4 + d5) << endl; + // test add (>12 inches) + cout << "d2 + d4: " << (d2 + d4) << endl; + + // test sub (0 ft) + cout << "d3 - d1: " << (d3 - d1) << endl; + // test sub (0 ft, negative conversion) + cout << "d1 - d3: " << (d1 - d3) << endl; + + // test sub (positive ft & inch) + cout << "d4 - d2: " << (d4 - d2) << endl; + // test sub (negative ft & inch) + cout << "d2 - d4: " << (d2 - d4) << endl; + + // test sub (negative ft, positive inch) + cout << "d4 - d5: " << (d4 - d5) << endl; + // test sub (positive ft, negative inch) + cout << "d5 - d2: " << (d5 - d2) << endl; + + return 0; +}