//1
//vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream>
namespace VECTOR
{
class vector
{
public:
enum Mode {RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
vector();
vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form = RECT);
~vector();
double xval()const { return x; };
double yval()const { return y; };
double magval() const { return mag; };
double angval() const { return ang; };
void polar_mode();
void rect_mode();
vector operator+(const vector & b)const;
vector operator-(const vector & b)const;
vector operator-()const;
vector operator*(double n)const;
friend vector operator *(double n, const vector &a);
friend std::ostream & operator<<(std::ostream&os, const vector &v);
};
}
#endif // !VECTOR_H_
//vector.cpp
#include<iostream>
#include<cmath>
#include"vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR {
const double Rad_to_deg = 57.2957795130823;
void vector::set_mag()
{
mag = sqrt(x*x + y*y);
}
void vector::set_ang()
{
if (x == 0.0&&y == 0.0)
ang = 0.0;
else
ang = atan2(y, x);
}
void vector::set_x()
{
x = mag*cos(ang);
}
void vector::set_y()
{
y = mag*sin(ang);
}
vector::vector()
{
x = y = mag = ang = 0.0;
mode = RECT;
}
vector::vector(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == POL)
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
void vector::reset(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == POL)
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
vector::~vector()
{
}
void vector::polar_mode()
{
mode = POL;
}
void vector::rect_mode()
{
mode = RECT;
}
vector vector::operator+(const vector &b) const
{
return vector(x + b.x, y + b.y);
}
vector vector::operator-(const vector &b)const
{
return vector(x - b.x, y - b.y);
}
vector vector::operator-()const
{
return vector(-x, -y);
}
vector vector::operator*(double n)const
{
return vector(n*x, n*y);
}
vector operator*(double n, const vector &a)
{
return a*n;
}
std::ostream & operator<<(std::ostream &os, const vector &v)
{
if (v.mode == vector::RECT)
os << "(x,y) = (" << v.x << " , " << v.y << ")";
else if (v.mode == vector::POL)
{
os << "(m,a) = ( " << v.mag << ", " << v.ang*Rad_to_deg << " °)";
}
else
os << "Vector object mode is invalid! ";
return os;
}
}
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include <ctime> //time() prototype
#include<fstream>
#include "vector.h"
int main()
{
using namespace std;
using VECTOR::vector;
srand(time(0));
double direction;
vector step;
vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream fout;
fout.open("thewalk.txt");
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
cout << "Enter the step length: ";
if (!(cin >> dstep))
break;
fout << "Target Distance: " << target << " , Step Size: " << dstep << "\n";
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, vector::POL);
result = result + step;
fout << steps << ": " << result << endl;
steps++;
}
fout << "After" << steps << " steps,the subject has the following location: \n";
fout << result << endl;
result.polar_mode();
fout << " or in polar coordinate: \n" << result << endl;
fout << "Average outward distance per step = " << result.magval() / steps << endl;
cout << "After" << steps << " steps,the subject has the following location: \n";
cout << result << endl;
result.polar_mode();
cout << " or in polar coordinate: \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): ";
}
fout.close();
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
system("pause");
return 0;
}
//2****************************************************
//vector.h
#define VECTOR_H_
#include<iostream>
namespace VECTOR
{
class vector
{
public:
enum Mode {RECT,POL};
private:
double x;
double y;
Mode mode;
void set_x(double mag,double ang);
void set_y(double mag, double ang);
public:
vector();
vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form = RECT);
~vector();
double xval()const { return x; };
double yval()const { return y; };
double magval() const;
double angval() const;
void polar_mode();
void rect_mode();
vector operator+(const vector & b)const;
vector operator-(const vector & b)const;
vector operator-()const;
vector operator*(double n)const;
friend vector operator *(double n, const vector &a);
friend std::ostream & operator<<(std::ostream&os, const vector &v);
};
}
#endif // !VECTOR_H_
//vector.cpp
#include<iostream>
#include<cmath>
#include"vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR {
const double Rad_to_deg = 57.2957795130823;
void vector::set_x(double mag,double ang)
{
x = mag*cos(ang);
}
void vector::set_y(double mag, double ang)
{
y = mag*sin(ang);
}
vector::vector()
{
x = y = 0.0;
mode = RECT;
}
vector::vector(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
set_x(n1,n2 / Rad_to_deg);
set_y(n1, n2 / Rad_to_deg);
}
else
{
cout << "Incorrect 3rd argument to vector() -- ";
cout << "vector set to 0\n";
x = y = 0.0;
mode = RECT;
}
}
void vector::reset(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
set_x(n1, n2 / Rad_to_deg);
set_y(n1, n2 / Rad_to_deg);
}
else
{
cout << "Incorrect 3rd argument to vector() -- ";
cout << "vector set to 0\n";
x = y = 0.0;
mode = RECT;
}
}
vector::~vector()
{
}
double vector::magval() const
{
return sqrt(x*x + y*y);
}
double vector::angval() const
{
if (x == 0.0&&y == 0.0)
return 0.0;
else
return atan2(y, x);
}
void vector::polar_mode()
{
mode = POL;
}
void vector::rect_mode()
{
mode = RECT;
}
vector vector::operator+(const vector &b) const
{
return vector(x + b.x, y + b.y);
}
vector vector::operator-(const vector &b)const
{
return vector(x - b.x, y - b.y);
}
vector vector::operator-()const
{
return vector(-x, -y);
}
vector vector::operator*(double n)const
{
return vector(n*x, n*y);
}
vector operator*(double n, const vector &a)
{
return a*n;
}
std::ostream & operator<<(std::ostream &os, const vector &v)
{
if (v.mode == vector::RECT)
os << "(x,y) = (" << v.x << " , " << v.y << ")";
else if (v.mode == vector::POL)
{
os << "(m,a) = ( " << v.magval() << ", " << v.angval()*Rad_to_deg << " °)";
}
else
os << "Vector object mode is invalid! ";
return os;
}
}
//3*******************************************
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include <ctime> //time() prototype
#include<fstream>
#include "vector.h"
const int maxstepsize = 10;
int main()
{
using namespace std;
using VECTOR::vector;
srand(time(0));
double direction;
vector step;
vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream fout;
int count_test = 0;
double steplogs[maxstepsize];
fout.open("thewalk.txt");
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
++count_test;
cout << "Enter the step length: ";
if (!(cin >> dstep))
break;
fout << "Target Distance: " << target << " , Step Size: " << dstep << "\n";
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, vector::POL);
result = result + step;
fout << steps << ": " << result << endl;
steps++;
}
fout << "After" << steps << " steps,the subject has the following location: \n";
fout << result << endl;
result.polar_mode();
fout << " or in polar coordinate: \n" << result << endl;
fout << "Average outward distance per step = " << result.magval() / steps << endl;
cout << "After" << steps << " steps,the subject has the following location: \n";
cout << result << endl;
result.polar_mode();
cout << " or in polar coordinate: \n" << result << endl;
cout << "Average outward distance per step = " << result.magval() / steps << endl;
steplogs[count_test - 1] = steps;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
double max = -99999990;
double min = 99999999;
double avg_step = 0;
double sum = 0;
for (int i = 0; i < count_test; ++i) {
sum += steplogs[i];
max = steplogs[i] > max ? steplogs[i] : max;
min = steplogs[i] < min ? steplogs[i] : min;
}
avg_step = sum / count_test;
fout << "among these tests you just did:\n";
fout << "the max step is: " << max << endl;
fout << "the min step is: " << min << endl;
fout << "the avg step is: " << avg_step << endl;
cout << "among these test you just did:\n";
cout << "the max step is: " << max << endl;
cout << "the min step is: " << min << endl;
cout << "the avg step is: " << avg_step << endl;
fout.close();
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
system("pause");
return 0;
}
//4*******************************************
//mytime.h
#ifndef MYTIME_H_
#define MYTIME_H_
#include<iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
~Time();
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
friend Time operator+(const Time &T1, const Time &T2);
friend Time operator-(const Time &T1, const Time &T2);
friend Time operator*(double m,Time &T) ;
friend Time operator*(Time &T,double m);
friend std::ostream & operator<<(std::ostream & os, const Time &T);
};
#endif // !MYTIME_H_
//mytime.cpp
#include<iostream>
#include<cmath>
#include"mytime.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
Time::~Time()
{
}
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 operator+(const Time & T1, const Time & T2)
{
Time result;
long totalminutes = T1.minutes + T2.minutes;
result.hours = T1.hours + T2.hours + totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
Time operator-(const Time & T1, const Time & T2)
{
Time diff;
int tot1, tot2;
tot1 = T1.hours * 60 + T1.minutes;
tot2 = T2.hours * 60 + T2.minutes;
diff.minutes = (tot1 - tot2) % 60;
diff.hours = (tot1 - tot2) / 60;
return diff;
}
Time operator*(double m,Time &T)
{
Time result;
long totalminutes = T.hours*m * 60 + T.minutes * m;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
Time operator*(Time & T, double m)
{
Time result;
long totalminutes = T.hours*m * 60 + T.minutes * m;
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;
}
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include <ctime> //time() prototype
#include<fstream>
#include "mytime.h"
using std::cout;
using std::endl;
int main()
{
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida and Tosca: \n";
cout << aida << "; " << tosca << endl;
temp = aida + tosca;
cout << "Aida + Tosca: " << temp << endl;
temp = aida*1.17;
cout << "Auda * 1.17: " << temp << endl;
cout << "10.0 * Tosca: " << 10.0 * tosca << endl;
system("pause");
return 0;
}
//这里将5,6的答案写在一起了************************************
//stone.h
#ifndef STONE_H_
#define STONE_H_
#include<iostream>
class Stonewt
{
public:
enum Mode{SP,INTP,FP};
private:
int stone; //SP State
double pds_left; //SP State
int intpound; // INTP State
double pounds; //FP State
Mode mode;
static const int Lbs_per_stn = 14;
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
void sp_mode();
void intp_mode();
void fp_mode();
~Stonewt();
Stonewt operator+(const Stonewt &s) const;
Stonewt operator-(const Stonewt &s) const;
Stonewt operator*( double n) const;
bool operator>(const Stonewt &s) const;
friend Stonewt operator*(double n, const Stonewt &s);
friend std::ostream & operator<<(std::ostream &os, const Stonewt &s);
};
#endif // !STONE_H_
//stone.cpp
#include<iostream>
#include<cmath>
#include"stone.h"
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn;
pounds = lbs;
intpound = int(pounds + 0.5);
mode = FP;
}
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn*Lbs_per_stn + lbs;
intpound = int(pounds + 0.5);
mode = FP;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = intpound = 0;
mode = FP;
}
void Stonewt::sp_mode()
{
mode = SP;
}
Stonewt::~Stonewt()
{
}
void Stonewt::intp_mode()
{
mode = INTP;
}
void Stonewt::fp_mode()
{
mode = FP;
}
Stonewt Stonewt::operator+(const Stonewt & s) const
{
if (mode == FP)
return Stonewt(pounds + s.pounds);
else if (mode == INTP)
return Stonewt(intpound + s.intpound);
else
return Stonewt(stone + s.stone, pds_left + s.pds_left);
}
Stonewt Stonewt::operator-(const Stonewt & s) const
{
if (mode == FP)
return Stonewt(pounds - s.pounds);
else if (mode == INTP)
return Stonewt(intpound - s.intpound);
else
return Stonewt(stone + s.stone, pds_left - s.pds_left);
}
Stonewt Stonewt::operator*(double n) const
{
if (mode == FP)
return Stonewt(n*pounds);
else if (mode == INTP)
return Stonewt(n*intpound);
else
return Stonewt(n*stone, n*pds_left);
}
bool Stonewt::operator>(const Stonewt & s) const
{
if (pounds >= s.pounds)
return true;
else
return false;
}
Stonewt operator*(double n, const Stonewt & s)
{
return s*n;
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
if (s.mode == Stonewt::FP)
os << "Weight in FP mode is: " << s.pounds << " pounds" << std::endl;
else if (s.mode == Stonewt::INTP)
os << "Weight in INTP mode is: " << s.intpound << " pounds(after int)" << std::endl;
else if (s.mode == Stonewt::SP)
os << "Weight in SP mode is: " << s.stone << " stones, " << s.pds_left << " pounds." << std::endl;
else
os << "Invalid Stonewt mode!\n";
return os;
}
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include <ctime> //time() prototype
#include<fstream>
#include "stone.h"
using std::cout;
using std::endl;
using std::cin;
int main()
{
Stonewt s[6] = { 200.12,100.5,45.6 };
double pnd[3];
cout << "please input the 4th-6th Stonewt Object value:\n ";
int count = 0;
while (count < 3 && cin >> pnd[count] )
{
s[count + 3] = Stonewt(pnd[count]);
++count;
}
if (!pnd[0])
cout << "Invalid input.Please enter a Nums.\n";
cout << "Now the Stonewt Object values are as follows:\n";
for (int i = 0; i < 6; ++i)
cout << " #" << i + 1 << s[i] << endl;
cout << "After reset the mode as SP,the values are as follows: \n";
cout << endl;
for (int i = 0; i < 6; ++i)
s[i].sp_mode(),
cout << " #" << i + 1 << s[i] << endl;
Stonewt s11(11,0);
Stonewt smax(0);
Stonewt smin(9999);
int max = 0;
int min = 0;
Stonewt temp(0);
for (int i = 0; i < 6; ++i)
{
if (s[i] > s11)
++max;
smax = smax > s[i] ? smax : s[i];
smin = smin > s[i] ? s[i] : smin;
}
cout << "the most Stonewt value is: " << smax << "\n";
cout << "the minimum Stonewt value is: " << smin << "\n";
cout << "the sums that bigger than s11 is: " << max << endl;
system("pause");
return 0;
}
//7**********************************************************
//complex1.h
#ifndef COMPLEX1_H_
#define COMPLEX1_H_
#include<iostream>
class complex
{
private:
double r;
int v;
public:
complex(double u1, double u2 = 0);
complex();
~complex();
complex operator+(const complex &c)const;
complex operator-(const complex &c)const;
complex operator*(double n)const;
complex operator*(const complex &c)const;
complex operator-();
friend complex operator*(double n, const complex &c);
friend std::istream & operator>>(std::istream &os, complex &c);
friend std::ostream & operator<<(std::ostream &os, complex &c);
};
#endif // !COMPLEX1_H_
//complex1.cpp
#include<iostream>
#include<cmath>
#include"complex1.h"
complex::complex(double u1, double u2)
{
r = u1;
v = u2;
}
complex::complex()
{
r = v = 0.0;
}
complex::~complex()
{
}
complex complex::operator+(const complex & c) const
{
return complex(r+c.r,v+c.v);
}
complex complex::operator-(const complex & c) const
{
return complex(r-c.r,v-c.v);
}
complex complex::operator*(double n) const
{
return complex(n*r, n*v);
}
complex complex::operator*(const complex & c) const
{
return complex(r*c.r-v*c.v,r*c.v+v*c.r);
}
complex complex::operator-()
{
return complex(r,-v);
}
complex operator*(double n, const complex & c)
{
return c*n;
}
std::istream & operator>>(std::istream & os, complex & c)
{
std::cout << "the real: ";
std::cin >> c.r;
std::cout << "the imaginary: ";
std::cin >> c.v;
return os;
}
std::ostream & operator<<(std::ostream & os, complex & c)
{
std::cout << " ( " << c.r << ", " << c.v << "i)\n";
return os;
}
//main.cpp
#include <iostream>
#include <cstdlib> //rand,srand() prototypes
#include <ctime> //time() prototype
#include<fstream>
#include "complex1.h"
using std::cout;
using std::endl;
using std::cin;
int main()
{
complex a(3.0, 4.0);
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjugate is " << -c << '\n';
cout << "a is " << a << "\n";
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number(q to quit):\n";
}
cout << "Done!\n";
system("pause");
return 0;
}
C++ Primer Plus 第六版 第十一章 练习题参考答案
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...