📄 selsolutions5.txt
字号:
cout << "vector set to 0\n";
x = y = 0.0;
mode = 'r';
}
}
// set vector from rectangular coordinates if form is r (the
// default) or else from polar coordinates if form is p
void Vector:: set(double n1, double n2, char form)
{
mode = form;
if (form == 'r')
{
x = n1;
y = n2;
}
else if (form == 'p')
{
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 = 'r';
}
}
Vector::~Vector() // destructor
{
}
double Vector::magval() const // report magnitude
{
return sqrt(x*x +y*y);
}
double Vector::angval() const // report angle
{
if (x == 0.0 && y == 0.0)
return 0;
else
return atan2(y, x);
}
void Vector::polar_mode() // set to polar mode
{
mode = 'p';
}
void Vector::rect_mode() // set to rectangular mode
{
mode = 'r';
}
// operator overloading
// add two Vectors
Vector Vector::operator+(const Vector & b) const
{
return Vector(x + b.x, y + b.y);
}
// subtract Vector b from a
Vector Vector::operator-(const Vector & b) const
{
return Vector(x - b.x, y - b.y);
}
// reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
// multiple vector by n
Vector Vector::operator*(double n) const
{
return Vector(n * x, n * y);
}
// friend methods
// multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return a * n;
}
// display rectangular coordinates if mode is r,
// else display polar coordinates if mode is p
ostream & operator<<(ostream & os, const Vector & v)
{
if (v.mode == 'r')
os << "(x,y) = (" << v.x << ", " << v.y << ")";
else if (v.mode == 'p')
{
os << "(m,a) = (" << v.magval() << ", "
<< v.angval() * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
} // end namespace VECTOR
// pe11-2walk.cpp -- use the modified Vector class
// compile with the vect.cpp file
#include <iostream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "pe11-2.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.set(dstep, direction, 'p');
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.set(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
return 0;
}
PE 11-5
// pe11ston.h -- definition for Stonewt class (for pe 11-5)
#ifndef PE11STONEWT_H_
#define PE11STONEWT_H_
#include <iostream>
class Stonewt
{
private:
enum {Lbs_per_stn = 14}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
char mode; // display mode for weight
// 's' = stone, 'f' = float, 'w' = whole pounds
public:
Stonewt(double lbs); // constructor for double pounds
Stonewt(int stn, double lbs); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
void set_mode(char m) {mode = m; }
Stonewt operator+(const Stonewt & sw) const;
Stonewt operator-(const Stonewt & sw) const;
Stonewt operator*(double m) const;
friend Stonewt operator*(double m, const Stonewt & sw)
{ return sw * m; }
friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw);
};
#endif
// pe11ston.h -- definition for Stonewt class (for pe 11-5)
#ifndef PE11STONEWT_H_
#define PE11STONEWT_H_
#include <iostream>
class Stonewt
{
private:
enum {Lbs_per_stn = 14}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
char mode; // display mode for weight
// 's' = stone, 'f' = float, 'w' = whole pounds
public:
Stonewt(double lbs); // constructor for double pounds
Stonewt(int stn, double lbs); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
void set_mode(char m) {mode = m; }
Stonewt operator+(const Stonewt & sw) const;
Stonewt operator-(const Stonewt & sw) const;
Stonewt operator*(double m) const;
friend Stonewt operator*(double m, const Stonewt & sw)
{ return sw * m; }
friend std::ostream & operator<<(std::ostream & os, const Stonewt & sw);
};
#endif
// pe11-5.cpp
#include <iostream>
#include "pe11ston.h"
// link with pe11ston.cpp
int main(void)
{
using std::cout;
Stonewt fullback(245.5);
Stonewt cornerback(13, 5.2);
cout << fullback;
cout << cornerback;
cornerback.set_mode('w');
cout << cornerback;
Stonewt lump;
lump = fullback + cornerback;
cout << lump;
fullback = fullback * 1.1;
cout << fullback;
lump = lump - fullback;
cout << lump;
lump = 1.3 * lump;
lump.set_mode('s');
cout << lump;
return 0;
}
PE 11-7
// pe11-7.cpp
#include <iostream>
#include "complex0.h" // to avoid confusion with complex.h
int main()
{
using std::cout;
using std::endl;
using std::cin;
complex a(3.0, 4.0); // initialize to (3,4i)
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << endl;
cout << "complex conjugate is " << ~c << endl;
cout << "a is " << a << endl;
cout << "a + c is " << a + c << endl;
cout << "a - c is " << a - c << endl;
cout << "a * c is " << a * c << endl;
cout << "2 * c is " << 2 * c << endl;
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";
return 0;
}
// complex0.h
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include <iostream>
class complex
{
private:
double r;
double i;
public:
complex();
complex(double real);
complex(double real, double imag);
double magnitude();
complex operator+(const complex & z) const;
complex operator-(const complex & z) const;
complex operator~() const;
friend complex square(const complex & z);
friend complex operator*(const complex & z, const complex & w);
friend std::ostream & operator<<(std::ostream & os, const complex & z);
friend std::istream & operator>>(std::istream & is, complex & z);
};
#endif
// complex0.cpp
#include <iostream>
#include <cmath>
#include "complex0.h"
complex::complex()
{
r = i = 0.0;
}
complex::complex(double real)
{
r = real;
i = 0.0;
}
complex::complex(double real, double imag)
{
r = real;
i = imag;
}
double complex::magnitude()
{
return std::sqrt(r*r + i*i);
}
complex complex::operator+(const complex & z) const
{
complex sum;
sum.r = r + z.r;
sum.i = i + z.i;
return sum;
}
complex complex::operator-(const complex & z) const
{
complex sum;
sum.r = r + z.r;
sum.i = i + z.i;
return sum;
}
complex complex::operator~() const
{
complex conjugate;
conjugate.r = r;
conjugate.i = -i;
return conjugate;
}
complex square (const complex & z)
{
complex sq;
sq.r = z.r * z.r - z.i * z.i;
sq.i = 2.0 * z.r * z.i;
return sq;
}
complex operator*(const complex & z, const complex & w)
{
complex sq;
sq.r = w.r * z.r - w.i * z.i;
sq.i = w.r * z.i + w.i * z.r;
return sq;
}
std::ostream & operator<<(std::ostream & os, const complex & z)
{
os << '(' << z.r << ',' << z.i << "i)";
return os;
}
std::istream & operator>>(std::istream & is, complex & z)
{
std::cout << "real: ";
if (is >> z.r)
{
std::cout << "imaginary: ";
is >> z.i;
}
return is;
}
Chapter 12
PE 12-2
// pe12-2.cpp
#include <iostream>
//#include "string2.h"
#include "pe12strg.h" // alternative name
int main()
{
using std::cout;
using std::cin;
String s1(" and I am a C++ student.");
String s2 = "Please enter your name: ";
String s3;
cout << s2; // overloaded << operator
cin >> s3; // overloaded >> operator
s2 = "My name is " + s3; // overloaded =, + operators
cout << s2 << ".\n";
s2 = s2 + s1;
s2.stringup(); // converts string to uppercase
cout << "The string\n" << s2 << "\ncontains " << s2.has('A')
<< " 'A' characters in it.\n";
s1 = "red"; // String(const char *),
// then String & operator=(const String&)
String rgb[3] = { String(s1), String("green"), String("blue")};
cout << "Enter the name of a primary color for mixing light: ";
String ans;
bool success = false;
while (cin >> ans)
{
ans.stringlow(); // converts string to lowercase
for (int i = 0; i < 3; i++)
{
if (ans == rgb[i]) // overloaded == operator
{
cout << "That's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
return 0;
}
// pe12strg.h
#ifndef PE12STRG_H_
#define PE12STRG_H_
#include <iostream>
class String {
private:
char * str; // pointer to a string
int chars; // number of characters
static int strings; // total number of strings
public:
String();
String(const char * ps); // converts C++ string to String
String(const String & s);
~String();
int numstrings();
int len();
void stringup();
void stringlow();
int has(char ch);
String & operator=(const String & s);
friend std::ostream & operator<<(std::ostream & os, const String & s);
friend std::istream & operator>>(std::istream & os, String & s);
friend String operator+(const String & s1, const String & s2);
friend int operator==(const String & s1, const String & s2);
friend int operator<(const String & s1, const String & s2);
friend int operator>(const String & s1, const String & s2);
};
#endif
// pe12strg.cpp
#include <iostream>
#include <cctype>
//#include "string2.h"
#include "pe12strg.h" // alternative name
int String::strings = 0;
String::String()
{
str = NULL;
chars = 0;
strings++;
}
String::String(const char * ps)
{
chars = std::strlen(ps);
str = new char [chars + 1];
std::strcpy(str, ps);
strings++;
}
String::String(const String & s)
{
chars = s.chars;
str = new char [chars + 1];
std::strcpy(str, s.str);
strings++;
}
String::~String()
{
strings--;
delete [] str;
}
int String::numstrings()
{
return strings;
}
int String::len()
{
return chars;
}
void String::stringup()
{
for (int i = 0; i < chars; i++)
str[i] = std::toupper(str[i]);
}
void String::stringlow()
{
for (int i = 0; i < chars; i++)
str[i] = std::tolower(str[i]);
}
String & String::operator=(const String & s) // allows chaining
{
if (this == &s) // assignment to self
return * this;
delete [] str; // free old contents, if any
chars = s.chars;
str = new char [chars + 1];
std::strcpy(str, s.str);
return * this;
}
std::ostream & operator<<(std::ostream & os, const String & s)
{
os << s.str;
return os;
}
std::istream & operator>>(std::istream & is, String & s)
{
char temp[80];
is.getline(temp,80);
s = temp;
return is;
}
String operator+(const String & s1, const String & s2)
{
int len = s1.chars + s2.chars;
char * ps = new char [len + 1];
std::strcpy(ps, s1.str);
std::strcat(ps, s2.str);
String temp(ps);
return temp;
}
int String::has(char ch)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -