第十一章 编程练习1-3

编程练习1
这里Vector的类定义没有修改,只需修改主函数,所以这里就只把更改后的主函数贴出来了

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

int main()
{
    using namespace std;
    ofstream fin;
    fin.open("steps.txt");

    using VECTOR::Vector;
    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;
        fin << "Target Distance: " << target << ", Step Size: " << dstep << "\n";
        while (result.magval() < target)
        {
            fin << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")\n";
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;
        }
        fin << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")\n";
        cout << "After " << steps << " steps, the subject "
            "has the following location:\n";
        cout << result << endl;
        fin << "After " << steps << " steps, the subject "
            "has the following location:\n";
        fin << result << endl;
        result.polar_mode();
        cout << " or\n" << result << endl;
        cout << "Average outward distance per step = "
            << result.magval()/steps << endl;
        fin << " or\n" << result << endl;
        fin << "Average outward distance per step = "
            << result.magval()/steps << endl;
        steps = 0;
        result.reset(0, 0);
        cout << "Enter target distance (q to quite): ";
    }
    cout << "Bye!\n";
    cin.clear();
    while(cin.get()!='\n')
        continue;
    system("pause");
    return 0;
}

编程练习2
这题只需要修改类Vector的头文件和定义,所以主函数就不贴出来了

//vector.h
#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(double mag, double ang);
        void set_y(double mag, double ang);
    public:
        Vector();
        Vector(double n1, double n2, Mode form = RECT);
        void reset(double n1, double n2, Mode form = RECT);
        ~Vector(void);
        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 std::ostream & operator<<(std::ostream & os, const Vector & v);
    };
}

#endif
//vector.cpp
#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/atan(1);

    void Vector::set_x(double mag, double ang)
    {
        x = mag*cos(ang);
    }
    void Vector::set_y(double mag, double ang)
    {
        y = mag*sin(ang);
    }


    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)
        {
            set_x(n1, n2); 
            set_y(n1, n2);
        }
        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)
        {
            set_x(n1, n2); 
            set_y(n1, n2);
        }
        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
    {
        double ang;
        if (x == 0 && y == 0)
            ang = 0;
        else
            ang = atan2(y,x);
        return ang;
    }
    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.magval() << ", "
                << v.angval() * Rad_to_deg << ")";
        }
        else
            os << "Vector object mode is invalid";
        return os;
    }
}

编程练习3
只需修改主函数

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector.h"

int main()
{
    using namespace std;
    using VECTOR::Vector;

    cout << "Enter the number of test: ";
    int num;
    cin >> num;
    double direction;
    Vector step;
    Vector result(0.0, 0.0);
    unsigned long steps = 0;
    unsigned long minsteps = 0;
    unsigned long maxsteps = 0;
    unsigned long totalsteps = 0;
    double target;
    double dstep;
    for (int i = 0; i < num; i++)
    {
        srand(time(0));

        cout << "Enter target distance for time " << i+1 << ": ";
        if(!(cin >> target))
            break;      
        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++;
        }
        result.polar_mode();
        totalsteps += steps;
        if(i == 0)
        {
            minsteps = steps;
            maxsteps = steps;
        }
        else
        {
            if (minsteps > steps)
                minsteps = steps;
            if (maxsteps < steps)
                maxsteps = steps;
        }

        cout << steps << endl;
        steps = 0;
        result.reset(0, 0);
        cin.clear();
        while(cin.get()!='\n')
            continue;
    }
    cout << "average steps is " << totalsteps/num << endl;
    cout << "min steps is " << minsteps << endl;
    cout << "max steps is " << maxsteps << endl;

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值