您的位置:首页 > 编程语言 > C语言/C++

【学习C++】C++ Primer Plus (第六版)第十一章编程练习1-7

2016-05-20 18:48 627 查看
1.

vector.h和vector.cpp见书 程序清单11.13,11.14

//main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "vector.h"
int main(){
using namespace std;
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("the walk.txt");
cout << "Enter target distance (q to quit):";
while (cin >> target){
cout << "Enter step length:";
if (!(cin >> dstep))
break;
fout << "Target Distance: " << target << ", Step Size: " << dstep<<"\n";
fout << steps << ": (x,y) = " << result << "\n";
while (result.magval() < target){
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
fout << steps << ": (x,y) = " << result << "\n";
}
fout << "After " << steps << " steps, the subject has the following location:\n";
fout << result << endl;
result.polar_mode();
fout << " or\n" << result << endl;
fout << "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";
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
return 0;
}


2.

//vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream>
using namespace std;
class Vector{
public:
enum Mode {RECT,POL};
private:
double x;
double y;
Mode mode;
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 ostream & operator<<(ostream & os, const Vector & v);
};
#endif
//vector.cpp
#include<iostream>
#include <cmath>
#include "vector.h"
using namespace std;
const double Rad_to_deg = 45.0 / atan(1.0);
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){
x = n1*cos(n2 / Rad_to_deg);
y = n1*sin(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){
x = n1*cos(n2 / Rad_to_deg);
y = n1*sin(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;
}
ostream & operator<<(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;
}
//main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector.h"
int main(){
using namespace std;
srand(time(0));
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, Vector::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";
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
return 0;
}

3.

vector.h和vector.cpp见书 程序清单11.13,11.14

//main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector.h"
int main(){
using namespace std;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
int N;
cout << "Enter times of our test:";
cin >> N;
int minsteps, maxsteps = 0,sumsteps=0;
for (int i = 0; i < N; i++){
cout << "Enter target distance (q to quit):";
cin >> target;
cout << "Enter step length:";
cin >> dstep;
while (result.magval() < target){
direction = rand() % 360;
step.reset(dstep, direction, Vector::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;
if (i == 0)
minsteps = steps;
else if (minsteps > steps)
minsteps = steps;
else
;
if (steps > maxsteps)
maxsteps = steps;
sumsteps += steps;
cout << "Average outward distance per step = " << result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);
}
cout << "Max steps: " << maxsteps << endl;
cout << "Min steps: " << minsteps << endl;
cout << "Everage steps:" << sumsteps / N << endl;
cout << "Bye!\n";
while (cin.get() != '\n')
continue;
cin.get();
return 0;
}

4.

//time.h
#ifndef TIME_H_
#define TIME_H_
#include<iostream>
using namespace std;
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);
friend Time operator+(const Time & t1, const Time & t2);
friend Time operator-(const Time & t1, const Time & t2);
friend Time operator*(double n, const Time & t);
friend Time operator*(const Time & t, double n);
friend ostream & operator<<(ostream & os, const Time & t);

};
#endif
//time.cpp
#include<iostream>
#include "time.h"
using namespace std;
Time::Time(){
hours = minutes = 0;
}
Time::Time(int h, int m){
hours = h;
minutes = m;
}
void Time::AddMin(int m){
minutes += m;
minutes = minutes % 60;
hours += 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 sum;
sum.minutes = t1.minutes + t2.minutes;
sum.hours = t1.hours + t2.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time operator-(const Time & t1, const Time & t2){
Time diff;
int tot1, tot2;
tot1 = t1.minutes + t2.hours * 60;
tot2 = t2.minutes + t2.hours * 60;
diff.minutes = (tot1 - tot2) % 60;
diff.hours = (tot1 - tot2) / 60;
return diff;
}
Time operator*(double n, const Time & t){
Time result;
long total = (t.minutes + t.hours * 60)*n;
result.hours = total / 60;
result.minutes = total % 60;
return result;
}
Time operator*(const Time & t, double n){
return n*t;
}
ostream & operator<<(ostream & os, const Time & t){
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
//main.cpp
#include <iostream>
#include "time.h"
using namespace std;
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 << "Aida*1.17: " << temp << endl;
cout << "10*Tosca: " << 10.0*tosca << endl;
cin.get();
cin.get();
return 0;
}

5.

//stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include<iostream>
using namespace std;
class Stonewt
{
public:
enum Mode{ Stone, int_p, double_p };
private:
enum { Lbs_per_stn = 14 };
int stone;
double pds_left;
double pounds;
Mode mode;
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt();
void set_mode(Mode m);
Stonewt operator+(const Stonewt & s) const;
Stonewt operator-(const Stonewt & s) const;
Stonewt operator*(const Stonewt & s) const;
friend ostream & operator<<(ostream & os, const Stonewt & s);
};
#endif
//stonewt.cpp
#include<iostream>
#include "stonewt.h"
using namespace std;
Stonewt::Stonewt(double lbs){
stone = (int)lbs / Lbs_per_stn;
pds_left = (int)lbs%Lbs_per_stn+lbs-(int)lbs;
pounds = lbs;
mode = double_p;
}
Stonewt::Stonewt(int stn, double lbs){
stone = stn;
pds_left = lbs;
pounds = stn*Lbs_per_stn + lbs;
mode = Stone;
}
Stonewt::Stonewt(){
stone = pounds = pds_left = 0;
mode = double_p;
};
Stonewt::~Stonewt(){
}
void Stonewt::set_mode(Mode m){
mode = m;
}
Stonewt Stonewt::operator+(const Stonewt & s) const{
return Stonewt(pounds + s.pounds);
}
Stonewt Stonewt::operator-(const Stonewt & s) const{
return Stonewt(pounds - s.pounds);
}
Stonewt Stonewt::operator*(const Stonewt & s) const{
return Stonewt(pounds * s.pounds);
}
ostream & operator<<(ostream & os, const Stonewt & s){
if (s.mode == Stonewt::Stone)
os << s.stone << " stone, " << s.pds_left << " pounds\n";
if (s.mode == Stonewt::double_p)
os << s.pounds << " pounds\n";
if (s.mode == Stonewt::int_p)
os << (int) s.pounds << " pounds\n";
return os;
}
//main.cpp
#include <iostream>
#include "stonewt.h"
using namespace std;
int main(){
Stonewt s1(12, 0);
Stonewt s2(145.2);
Stonewt s3 = s1 + s2;
cout << "s3 = " << s3 << endl;
s3.set_mode(Stonewt::Stone);
cout << "(stone) s3 = " << s3 << endl;
s3.set_mode(Stonewt::int_p);
cout << "(int) s3 = " << s3 << endl;
cout << "s1 - s2 = " << s1 - s2 << endl;
cout << "s1 * s2 = " << s1*s2 << endl;
cin.get();
cin.get();
return 0;
}

6.

//stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum { Lbs_per_stn = 14 };
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
bool operator>(const Stonewt & s);
bool operator>=(const Stonewt & s);
bool operator<(const Stonewt & s);
bool operator<=(const Stonewt & s);
bool operator==(const Stonewt & s);
bool operator!=(const Stonewt & s);

};
#endif
//stonewt.cpp
#include<iostream>
#include "stonewt.h"
using namespace std;
Stonewt::Stonewt(double lbs){
stone = (int)lbs / Lbs_per_stn;
pds_left = (int)lbs%Lbs_per_stn+lbs-(int)lbs;
pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs){
stone = stn;
pds_left = lbs;
pounds = stn*Lbs_per_stn + lbs;
}
Stonewt::Stonewt(){
stone = pounds = pds_left = 0;
};
Stonewt::~Stonewt(){
}
void Stonewt::show_stn() const{
cout << stone << " stone, " << pds_left << "pounds\n";
}
void Stonewt::show_lbs() const{
cout << pounds << " pounds\n";
}
bool Stonewt::operator>(const Stonewt & s){
if (pounds > s.pounds)
return true;
else
return false;
}
bool Stonewt::operator>=(const Stonewt & s){
if (pounds >= s.pounds)
return true;
else
return false;
}
bool Stonewt::operator<(const Stonewt & s){
if (pounds < s.pounds)
return true;
else
return false;
}
bool Stonewt::operator<=(const Stonewt & s){
if (pounds <= s.pounds)
return true;
else
return false;
}
bool Stonewt::operator==(const Stonewt & s){
if (pounds == s.pounds)
return true;
else
return false;
}
bool Stonewt::operator!=(const Stonewt & s){
if (pounds != s.pounds)
return true;
else
return false;
}
//main.cpp
#include <iostream>
#include "stonewt.h"
using namespace std;
int main(){
Stonewt s[6] = {Stonewt(11,0),Stonewt(15,1),Stonewt(16,1)};
int p;
double l;
for (int i = 3; i < 6; i++){
cout << "please initialize Stonewt[" << i << "]: ";
cin >> p;
cin >> l;
s[i]=Stonewt(p,l);
}
Stonewt mins=s[0],maxs=s[0];
Stonewt ele(11, 0);
int num = 0;
int min=0, max = 0;
for (int i = 0; i < 6; i++){
if (s[i] < mins){
mins = s[i];
min = i;
}
if (s[i] > maxs){
maxs = s[i];
max = i;
}
if (s[i] >= ele)
num++;
}
cout << "s[ " << min << "] is the smallest element"<<endl;
s[min].show_lbs();
cout << "s[ " << max << "] is the bigest element" << endl;
s[max].show_lbs();
cout << "number :" << num << endl;
cin.get();
cin.get();
return 0;
}

7.

//complex0.h
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include <iostream>
using namespace std;
class complex{
private:
double real;
double ima;
public:
complex(double r = 0, double m = 0);
complex operator+(const complex & a) const;
complex operator-(const complex & a) const;
complex operator*(const complex & a) const;
friend complex operator~(const complex & a);
friend complex operator*(double x, const complex & c);
friend ostream & operator<<(ostream & os, complex & c);
friend istream & operator>>(istream & is, complex &c);
};
#endif
//complex.cpp
#include "complex0.h"
complex::complex(double r, double m){
real = r;
ima = m;
}
complex complex::operator+(const complex & a) const{
complex sum;
sum.real = real + a.real;
sum.ima = ima + a.ima;
return sum;
}
complex complex::operator-(const complex & a) const{
complex dif;
dif.real = real - a.real;
dif.ima = ima - a.ima;
return dif;
}
complex complex::operator*(const complex & a) const{
complex result;
result.real = real*a.real - ima*a.ima;
result.ima = real*a.ima + ima*a.real;
return result;
}
complex operator*(double x, const complex & c){
complex result;
result.real = x*c.real;
result.ima = x*c.ima;
return result;
}
ostream & operator<<(ostream & os, complex & c){
os << "(" << c.real << "," << c.ima << "i)";
return os;
}
istream & operator>>(istream & is, complex &c){
cout << "real: ";
is >> c.real;
cout << "imaginary: ";
is >> c.ima;
return is;
}
complex operator~(const complex & a){
complex g;
g.real = a.real;
g.ima = -a.ima;
return g;

}
//main.cpp
#include <iostream>
#include "complex0.h"
using namespace std;
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";
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++