第十一章编程练习答案
11.1修改程序清单11.5,将一系列连续的随机漫步者位置写入文件中,另外将初始条件和结果小结写入文件
//TVector.h
#include <iostream>
namespace n_vector
{
using std::ostream;
class TVector
{
public:
enum Mode {RECT, POL}; // 坐标类型,依次为直角坐标、极坐标
public:
explicit TVector (double a = 0, double b = 0, Mode mode = RECT); // (x, y) OR (length, angle)
void reset (double a = 0, double b = 0, Mode mode = RECT);
void setMode (Mode mode);
Mode getMode (void) const;
double getX (void) const;
double getY (void) const;
double getLength (void) const;
double getAngle (void) const;
TVector operator+ (const TVector& rvalue) const;
TVector& operator+= (const TVector& rvalue);
TVector operator- (void) const;
private:
void m_setPolar (void);
void m_setRect (void);
private:
double m_x; // 直角坐标的X坐标
double m_y; // 直角坐标的Y坐标
double m_length; // 极坐标的长度
double m_angle; // 极坐标的角度
Mode m_mode;
};
ostream& operator<< (ostream& os, const TVector& vec);
}
//TVector.cc
#include <iostream>
#include <cmath>
#include "TVector.h"
namespace n_vector
{
static const double k_rad_to_deg = 57.2958;
static double convertRadToDeg (double rad)
{
return (rad * k_rad_to_deg);
}
static double convertDegToRad (double deg)
{
return (deg / k_rad_to_deg);
}
TVector::TVector (double a, double b, Mode mode) : m_mode(mode)
{
if (RECT == mode) {
m_x = a;
m_y = b;
m_setPolar();
} else if (POL == mode) {
m_length = a;
m_angle = convertDegToRad(b);
m_setRect();
} else { ;}
}
void TVector::reset (double a, double b, Mode mode)
{
*this = TVector(a, b, mode);
}
void TVector::setMode (Mode mode)
{
m_mode = mode;
}
TVector::Mode TVector::getMode (void) const
{
return (m_mode);
}
double TVector::getX (void) const
{
return (m_x);
}
double TVector::getY (void) const
{
return (m_y);
}
double TVector::getAngle (void) const
{
return (convertRadToDeg(m_angle));
}
double TVector::getLength (void) const
{
return (m_length);
}
TVector TVector::operator+ (const TVector& rvalue) const
{
TVector tmp(m_x + rvalue.m_x, m_y + rvalue.m_y);
tmp.m_mode = this->m_mode;
return (tmp);
}
TVector& TVector::operator+= (const TVector& rvalue)
{
*this = *this + rvalue;
return (*this);
}
TVector TVector::operator- (void) const
{
TVector tmp(-m_x, -m_y);
tmp.m_mode = this->m_mode;
return (tmp);
}
void TVector::m_setPolar (void)
{
m_length = sqrt(m_x * m_x + m_y * m_y);
m_angle = (0 == m_x && 0 == m_y) ? 0 : atan2(m_y, m_x);
}
void TVector::m_setRect (void)
{
m_x = m_length * cos(m_angle);
m_y = m_length * sin(m_angle);
}
ostream& operator<< (ostream& os, const TVector& vec)
{
if (TVector::RECT == vec.getMode())
os << '(' << vec.getX() << ',' << vec.getY() << ')';
else
os << '(' << vec.getLength() << ',' << vec.getAngle() << "°)";
return (os);
}
}
//mian.cc
#include <iostream>
#include <fstream>
#include <limits>
#include "TVector.h"
using namespace std;
using namespace n_vector;
int main ()
{
ofstream of;
of.open("output.txt");
while (true) {
double setp;
double distance;
unsigned cnt_steps = 0;
cout << "输入步长:";
cin >> setp;
if (!cin || setp <= 0)
break;
cin.clear(); // 清空输入缓冲区错误标志位
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 清空输入缓冲区内容
cout << "输入距离:";
cin >> distance;
if (!cin || distance <= 0)
break;
cin.clear(); // 清空输入缓冲区错误标志位
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 清空输入缓冲区内容
of << "开始>>>>>>>>>>" << endl;
TVector vec(0, 0, TVector::POL);
srand((unsigned)time(nullptr));
while (vec.getLength() < distance) {
vec += TVector(setp, rand()%361, TVector::POL);
of << cnt_steps++ << ":" << vec << endl;
}
of << "结束<<<<<<<<<<" << endl;
of << "距离" << distance << ",步长" << setp << ",历经" << cnt_steps << "步走完,终点坐标" << vec << endl;
of << "==========\n" << endl;
}
of.close();
}
2,3,4,5,6题都为改编书中已有例子,实在是改不下去了。借先人例子来看
http://blog.csdn.net/qq844352155/article/details/24111645
http://blog.csdn.net/qq844352155/article/details/24112981
http://blog.csdn.net/qq844352155/article/details/24125767
http://blog.csdn.net/qq844352155/article/details/24128929
http://blog.csdn.net/qq844352155/article/details/24130805
11.7编写一个复数类,对复数进行运算
//11.7编写一个复数类,对复数进行运算,复数表示方法为(a,b)
#include <iostream>
using namespace std;
class Complex{
double a;
double b;
public:
Complex (const double x=0,const double y=0) : a(x),b(y) {}
friend ostream & operator << (ostream & os , const Complex & complex)
{
os << "(" << complex.a << " , " << complex.b << ")" ;
return (os);
}
friend istream & operator >> (istream & is , Complex & complex)
{
char ch;
is >> complex.a >> ch >> complex.b ;
return (is);
}
Complex operator +(const Complex & complex)
{
return Complex(a+complex.a,b+complex.b);
}
Complex operator -(const Complex & complex)
{
return Complex(a-complex.a,b-complex.b);
}
Complex operator *(const Complex & complex)
{
return Complex((a*complex.a - b*complex.b),(a*complex.b + b*complex.a));
}
friend Complex operator *(int x,const Complex & complex)
{
return Complex(x * complex.a , x * complex.b);
}
friend Complex operator ~(const Complex & complex)
{
return Complex(complex.a,-complex.b);
}
};
int main()
{
Complex a(3.0, 4.0); // initialize to (3,4i) Programming Exercises
Complex c;
cout << "Enter a complex number (eg:a,b)(q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << endl;
cout << "complex conjugate is " << ~c << '\n';
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 (eg:a,b)(q to quit):\n";
}
cout << "Done!" << endl;
}