Chapter 11 homework


1.
#include <iostream>
#include <fstream>
#include <ctime>
#include "vector.h"

int main()
{
    using namespace std;
    using VECTOR::Vector;
    ofstream of;
    of.open("result.txt");

    srand(time(0));
    double direction;
    Vector step;
    Vector result(0.0,0.0);
    unsigned int steps = 0;
    double target;
    double dstep;
    cout <<"Enter target distance ( q to quit ): ";
    cin >> target;
    cout << "Enter step length: ";
    cin >> dstep;
    of << "Target Distance : " << target << ", Step Size: " << dstep << endl;

    for(steps = 0; result.magval() < target; steps++)
    {
        direction = rand() % 360;
        step.reset (dstep, direction, Vector::POL);
        result = result + step;
        of << steps << ": "<< result << endl;
    }
    of << "After " << steps << " steps, the subject has the following location:\n";
    of << result <<endl;
    result.polar_mode();
    of << " or\n" << result << endl;
    of << "Average outward distance per step = "
             << result.magval()/steps << endl;
    steps = 0;
    result.reset(0.0, 0.0);
    cout << "Bye!\n";
    cin.clear();
    while(cin.get() != '\n')
        continue;
    of.close();
    return 0;
}

======================================== 答案 =========================================

#include <iostream>
#include <fstream>
#include <limits>
#include "TVector.h"

using namespace std;
using namespace n_vector;


int
main (void) 
{ 
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();


cout << endl;
return (0);
}

 


2.答案

double setp;
double distance;
int times;
int max_setps = 0;
int min_setps = numeric_limits<unsigned>::max();

int sum_setps = 0;
......
 for (int i = 0; i < times; ++i) {
            int cnt_steps = 0;
            TVector vec(0, 0, TVector::POL);

            cout << i << ":";
            while (vec.getLength() < distance) {
                vec += TVector(setp, rand()%360, TVector::POL);
                ++cnt_steps;
            }
            cout << cnt_steps << "steps to " << vec << endl;

            if (cnt_steps > max_setps) {
                max_setps = cnt_steps;    // get max step
            }
            if (cnt_steps < min_setps) {
                min_setps = cnt_steps;    // get min step
            }
            sum_setps += cnt_steps;       // total steps
        }

 7.  11_7_complex0.h


#ifndef E11_7_COMPLEX0_H
#define E11_7_COMPLEX0_H

#include <iostream>
class Complex
{
private:
    double m_a;
    double m_c;
public:
    Complex();  //default constructor
    ~Complex();
    Complex(double x1, double y1 );  // constructor
    //operator overloading
    Complex operator+(const Complex & t) const;
    Complex operator-(const Complex & t) const;
    Complex operator*(double n) const;
    Complex operator*(const Complex & t) const;
    Complex operator~() const;
    //friend
    friend std::ostream & operator<<(std::ostream & os, const Complex & v);
    friend std::istream & operator>>(std::istream & is, Complex & t);
    friend Complex operator*(double m, const Complex & t);
};
#endif //E11_7_COMPLEX0_H
complex0.h

11_7_comlpex0.cpp


#include "complex0.h"
using std::endl;
Complex::Complex()
{
    m_a = m_c = 0.0;
}

Complex::~Complex() {}

Complex::Complex(double x1, double y1)
{
    m_a = x1;
    m_c = y1;
}

Complex Complex::operator+(const Complex &t) const
{
    return Complex(t.m_a + m_a, t.m_c + m_c);
}

Complex Complex::operator-(const Complex & t) const
{
    return Complex(m_a - t.m_a, m_c - t.m_c);
}

Complex Complex::operator*(double n) const
{
    return Complex(m_a * n, m_c * n);
}

Complex Complex::operator*(const Complex & t) const
{
    return Complex(t.m_a * m_a - t.m_c * m_c, t.m_c * m_a + t.m_a * m_c);
}
Complex Complex::operator~() const
{
    return Complex(m_a, -m_c);
}
//friend
std::ostream &operator<<(std::ostream & os, const Complex & v)
{
    os << "( " << v.m_a <<", " << v.m_c <<"i )" << endl;
}

std::istream &operator>>(std::istream & is, Complex & t)
{
    std::cout << "Real: ";
    is >> t.m_a;
    std::cout << "Imaginary: ";
    is >> t.m_c;
}

Complex operator*(double m, const Complex & t)
{
    return Complex(t.m_a * m, t.m_c * m);
}
complex0.h

11_7_main.cpp


#include <iostream>
using namespace std;
#include "complex0.h"

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;
}
main.cpp

 

posted @ 2018-03-17 15:41  Bear_Guo  阅读(149)  评论(0编辑  收藏  举报