C++ Primer Plus章节编程练习(第十一章)

1、修改程序清单11.15,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标示。另外,让该程序将初始化条件(母标距离和步长)以及结果小结写入到该文件中。该文件的内容与下面类似。

//Vector.h
#pragma once
#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);

        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);
        ~Vector();

    };
};
#endif // !VECTOR_H


//Vector.cpp
#include "stdafx.h"
#include "Vector.h"
#include <cmath>
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 = 45.0 / atan(1.0);
    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 (mode == 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 (mode == 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::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;
    }

    Vector::~Vector()
    {
    }
}


//main.cpp
// List11.3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Vector.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>

int main()
{
    using namespace std;
    using VECTOR::Vector;
    srand(time(0));        //time(0)函数返回当前时间,srand()函数允许覆盖默认的种子值,重新启动另一个随机数序列
    double direction;
    Vector step;
    Vector result(0.0, 0.0);
    unsigned long steps = 0;
    double target;
    double dstep;
    ofstream file;
    file.open("history.txt");
    
    if (!file.is_open()) {
        cout << "Couldn't open the file!";
        exit(EXIT_FAILURE);
    }

    cout << "Enter target distance (q to quit): ";
    while (cin >> target) {
        cout << "Enter step length: ";
        if (!(cin >> dstep))
            break;
        //将目标距离和步长写入文件
        file << "Target Distance: " << target << ", " << "Step Size: " << dstep << endl;
        while (result.magval() < target) {
            //将每步过程写入文件,用直角坐标表示
            file << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")" << endl;
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;
        }
        //将最后一步结果写入文件
        file << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")" << endl;
        file << "After " << steps << " steps, the subject has the following location:\n";
        file << "(x, y) = (" << result.xval() << ", " << result.yval() << ")" << "\nor\n";
        file << "(m, a) = (" << result.magval() << ", " << result.angval() << ")\n";
        file << "Average outward distance per step = " << result.magval() / steps << endl << endl;

        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;
    return 0;
}

 

posted @ 2018-10-19 12:35  SChenqi  阅读(411)  评论(0编辑  收藏  举报