forked from ShujiaHuang/Cpp-Primer-Plus-6th
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaa2f37
commit 8c93d1c
Showing
22 changed files
with
1,313 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// mytime0.cpp -- implementing Time methods | ||
#include <iostream> | ||
#include "mytime0.h" | ||
|
||
Time::Time() | ||
{ | ||
hours = minutes = 0; | ||
} | ||
|
||
Time::Time(int h, int m ) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
void Time::AddMin(int m) | ||
{ | ||
minutes += m; | ||
hours += minutes / 60; | ||
minutes %= 60; | ||
} | ||
|
||
void Time::AddHr(int h) | ||
{ | ||
hours += h; | ||
} | ||
|
||
void Time::Reset(int h, int m) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
const Time Time::Sum(const Time & t) const | ||
{ | ||
Time sum; | ||
sum.minutes = minutes + t.minutes; | ||
sum.hours = hours + t.hours + sum.minutes / 60; | ||
sum.minutes %= 60; | ||
return sum; | ||
} | ||
|
||
void Time::Show() const | ||
{ | ||
std::cout << hours << " hours, " << minutes << " minutes"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// mytime0.h -- Time class before operator overloading | ||
#ifndef MYTIME0_H_ | ||
#define MYTIME0_H_ | ||
|
||
class Time | ||
{ | ||
private: | ||
int hours; | ||
int minutes; | ||
public: | ||
Time(); | ||
Time(int h, int m = 0); | ||
void AddMin(int m); | ||
void AddHr(int h); | ||
void Reset(int h = 0, int m = 0); | ||
const Time Sum(const Time & t) const; | ||
void Show() const; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// mytime1.cpp -- implementing Time methods | ||
#include <iostream> | ||
#include "mytime1.h" | ||
|
||
Time::Time() | ||
{ | ||
hours = minutes = 0; | ||
} | ||
|
||
Time::Time(int h, int m ) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
void Time::AddMin(int m) | ||
{ | ||
minutes += m; | ||
hours += minutes / 60; | ||
minutes %= 60; | ||
} | ||
|
||
void Time::AddHr(int h) | ||
{ | ||
hours += h; | ||
} | ||
|
||
void Time::Reset(int h, int m) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
Time Time::operator+(const Time & t) const | ||
{ | ||
Time sum; | ||
sum.minutes = minutes + t.minutes; | ||
sum.hours = hours + t.hours + sum.minutes / 60; | ||
sum.minutes %= 60; | ||
return sum; | ||
} | ||
|
||
void Time::Show() const | ||
{ | ||
std::cout << hours << " hours, " << minutes << " minutes"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// mytime1.h -- Time class before operator overloading | ||
#ifndef MYTIME1_H_ | ||
#define MYTIME1_H_ | ||
|
||
class Time | ||
{ | ||
private: | ||
int hours; | ||
int minutes; | ||
public: | ||
Time(); | ||
Time(int h, int m = 0); | ||
void AddMin(int m); | ||
void AddHr(int h); | ||
void Reset(int h = 0, int m = 0); | ||
Time operator+(const Time & t) const; | ||
void Show() const; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// mytime2.cpp -- implementing Time methods | ||
#include <iostream> | ||
#include "mytime2.h" | ||
|
||
Time::Time() | ||
{ | ||
hours = minutes = 0; | ||
} | ||
|
||
Time::Time(int h, int m ) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
void Time::AddMin(int m) | ||
{ | ||
minutes += m; | ||
hours += minutes / 60; | ||
minutes %= 60; | ||
} | ||
void Time::AddHr(int h) | ||
{ | ||
hours += h; | ||
} | ||
|
||
void Time::Reset(int h, int m) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
Time Time::operator+(const Time & t) const | ||
{ | ||
Time sum; | ||
sum.minutes = minutes + t.minutes; | ||
sum.hours = hours + t.hours + sum.minutes / 60; | ||
sum.minutes %= 60; | ||
return sum; | ||
} | ||
|
||
Time Time::operator-(const Time & t) const | ||
{ | ||
Time diff; | ||
int tot1, tot2; | ||
tot1 = t.minutes + 60 * t.hours; | ||
tot2 = minutes + 60 * hours; | ||
diff.minutes = (tot2 - tot1) % 60; | ||
diff.hours = (tot2 - tot1) / 60; | ||
return diff; | ||
} | ||
|
||
Time Time::operator*(double mult) const | ||
{ | ||
Time result; | ||
long totalminutes = hours * mult * 60 + minutes * mult; | ||
result.hours = totalminutes / 60; | ||
result.minutes = totalminutes % 60; | ||
return result; | ||
} | ||
|
||
void Time::Show() const | ||
{ | ||
std::cout << hours << " hours, " << minutes << " minutes"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// mytime2.h -- Time class after operator overloading | ||
#ifndef MYTIME2_H_ | ||
#define MYTIME2_H_ | ||
|
||
class Time | ||
{ | ||
private: | ||
int hours; | ||
int minutes; | ||
public: | ||
Time(); | ||
Time(int h, int m = 0); | ||
void AddMin(int m); | ||
void AddHr(int h); | ||
void Reset(int h = 0, int m = 0); | ||
Time operator+(const Time & t) const; | ||
Time operator-(const Time & t) const; | ||
Time operator*(double n) const; | ||
void Show() const; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// mytime3.cpp -- implementing Time methods | ||
#include "mytime3.h" | ||
|
||
Time::Time() | ||
{ | ||
hours = minutes = 0; | ||
} | ||
|
||
Time::Time(int h, int m ) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
void Time::AddMin(int m) | ||
{ | ||
minutes += m; | ||
hours += minutes / 60; | ||
minutes %= 60; | ||
} | ||
|
||
void Time::AddHr(int h) | ||
{ | ||
hours += h; | ||
} | ||
|
||
void Time::Reset(int h, int m) | ||
{ | ||
hours = h; | ||
minutes = m; | ||
} | ||
|
||
Time Time::operator+(const Time & t) const | ||
{ | ||
Time sum; | ||
sum.minutes = minutes + t.minutes; | ||
sum.hours = hours + t.hours + sum.minutes / 60; | ||
sum.minutes %= 60; | ||
return sum; | ||
} | ||
|
||
Time Time::operator-(const Time & t) const | ||
{ | ||
Time diff; | ||
int tot1, tot2; | ||
tot1 = t.minutes + 60 * t.hours; | ||
tot2 = minutes + 60 * hours; | ||
diff.minutes = (tot2 - tot1) % 60; | ||
diff.hours = (tot2 - tot1) / 60; | ||
return diff; | ||
} | ||
|
||
Time Time::operator*(double mult) const | ||
{ | ||
Time result; | ||
long totalminutes = hours * mult * 60 + minutes * mult; | ||
result.hours = totalminutes / 60; | ||
result.minutes = totalminutes % 60; | ||
return result; | ||
} | ||
|
||
std::ostream & operator<<(std::ostream & os, const Time & t) | ||
{ | ||
os << t.hours << " hours, " << t.minutes << " minutes"; | ||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// mytime3.h -- Time class with friends | ||
#ifndef MYTIME3_H_ | ||
#define MYTIME3_H_ | ||
#include <iostream> | ||
|
||
class Time | ||
{ | ||
private: | ||
int hours; | ||
int minutes; | ||
public: | ||
Time(); | ||
Time(int h, int m = 0); | ||
void AddMin(int m); | ||
void AddHr(int h); | ||
void Reset(int h = 0, int m = 0); | ||
Time operator+(const Time & t) const; | ||
Time operator-(const Time & t) const; | ||
Time operator*(double n) const; | ||
friend Time operator*(double m, const Time & t) | ||
{ return t * m; } // inline definition | ||
friend std::ostream & operator<<(std::ostream & os, const Time & t); | ||
|
||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// randwalk.cpp -- using the Vector class | ||
// compile with the vect.cpp file | ||
#include <iostream> | ||
#include <cstdlib> // rand(), srand() prototypes | ||
#include <ctime> // time() prototype | ||
#include "vect.h" | ||
int main() | ||
{ | ||
using namespace std; | ||
using VECTOR::Vector; | ||
srand(time(0)); // seed random-number generator | ||
double direction; | ||
Vector step; | ||
Vector result(0.0, 0.0); | ||
unsigned long steps = 0; | ||
double target; | ||
double dstep; | ||
cout << "Enter target distance (q to quit): "; | ||
while (cin >> target) | ||
{ | ||
cout << "Enter step length: "; | ||
if (!(cin >> dstep)) | ||
break; | ||
|
||
while (result.magval() < target) | ||
{ | ||
direction = rand() % 360; | ||
step.reset(dstep, direction, POL); | ||
result = result + step; | ||
steps++; | ||
} | ||
cout << "After " << steps << " steps, the subject " | ||
"has the following location:\n"; | ||
cout << result << endl; | ||
result.polar_mode(); | ||
cout << " or\n" << result << endl; | ||
cout << "Average outward distance per step = " | ||
<< result.magval()/steps << endl; | ||
steps = 0; | ||
result.reset(0.0, 0.0); | ||
cout << "Enter target distance (q to quit): "; | ||
} | ||
cout << "Bye!\n"; | ||
/* keep window open | ||
cin.clear(); | ||
while (cin.get() != '\n') | ||
continue; | ||
cin.get(); | ||
*/ | ||
return 0; | ||
} |
Oops, something went wrong.