feat: complete c_4_lab_4
This commit is contained in:
parent
6fd1605429
commit
aaa59d7ac8
4 changed files with 177 additions and 0 deletions
|
@ -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_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_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,
|
||||||
|
)
|
||||||
|
|
54
src/chapter_4/lab_4/Distance.cpp
Normal file
54
src/chapter_4/lab_4/Distance.cpp
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
#include "Distance.h"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
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<int>(this->_inches) / 12;
|
||||||
|
this->_inches -= ((static_cast<int>(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<double>(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;
|
||||||
|
}
|
74
src/chapter_4/lab_4/Distance.h
Normal file
74
src/chapter_4/lab_4/Distance.h
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define DISTANCE_H
|
||||||
|
|
||||||
|
#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.
|
||||||
|
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;
|
||||||
|
};
|
43
src/chapter_4/lab_4/main.cpp
Normal file
43
src/chapter_4/lab_4/main.cpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in a new issue