C++-练习-71

题目:

修改以下随机漫步者代码,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用不好进行标示。另外,让该程序将初始化条件(目标距离和步长)以及结果小结写入该文件中。该文件的内容如下

 test.h

#ifndef STACK_H_

#define STACK_H_

#include <iostream>

namespace VECTOR

{

        class Vector

        {

        public:

                enum Mode { RECT, POL };

               //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);

                ~Vector();

                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);

        };

}

#endif

test_function.cpp

#include <cmath>

#include "test.h"

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 && y == 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;

        }

        Vector::Vector(double n1, double n2, Mode form)

        {

                if (form == RECT)

                {

                        x = n1;

                        y = n2;

                        set_mag();

                        set_ang();

                }

                else if (form == POL)

                {

                        mag = n1;

                        ang = n2;

                        x = y = mag = ang = 0.0;

                        mode = RECT;

                }

        }

        void Vector::reset(double n1, double n2, Mode form)

        {

                mode = form;

                if (form == RECT)

                {

                        x = n1;

                        y = n2;

                        set_mag();

                        set_ang();

                }

                else if (form == POL)

                {

                        mag = n1;

                        ang = n2;

                        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;

                }

        }

        Vector::~Vector(){}

        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;

        }

}

test.cpp

#include "test.h"

#include <iostream>

#include <cstdlib>

#include <ctime>

int main()

{

        using namespace std;

        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;

                while (result.magval() < target)

                {

                        direction = rand() % 360;

                        step.reset(dstep, direction, Vector::POL);

                        result = result + step;

                        steps++;

                }

                cout << "After " << steps << " steps,the subject has the following location:\n";

                result.rect_mode();

                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;

}

源代码:

这里主要修改test.cpp文件

test.cpp

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

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

    srand(time(0));
    double direction;	//角度
    Vector step;	//记录没次走完的坐标
    Vector result(0.0, 0.0);
    unsigned long steps = 0;
    double target;	//距离
    double dstep;	//步长

    ofstream of;
    of.open("hello.txt");

    cout << "Enter target distance(q to quit): ";

    while (cin >> target)
    {
        cout << "Enter step length: ";
        if (!(cin >> dstep))
            break;
        else
        {
            of << "Target Distance: " << target << " Step Size: " << dstep << endl;
        }

        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;

            result.rect_mode();
            of << steps << ": " <<  result << endl;

            steps++;
        }

        of << "After " << steps << " steps,the subject has the following location:\n";
        result.rect_mode();
        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 << "Enter target distance (q to quit): ";
    }

    cout << "Bye!\n";
    cin.clear();
    while (cin.get() != '\n')
        continue;
    return 0;
}

演示效果:


如果朋友你感觉文章的内容对你有帮助,可以点赞关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈

猜你喜欢

转载自blog.csdn.net/little_startoo/article/details/142449694