首页 > 编程语言 >C++PrimerPlus中文第六版第11章编程练习答案

C++PrimerPlus中文第六版第11章编程练习答案

时间:2023-02-14 12:55:29浏览次数:45  
标签:11 Stone PrimerPlus Complex0 double 第六版 Vector operator const

1、

// 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; // length of vector
        double ang; // direction of vector in degrees
        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& v) const;
        Vector operator-(const Vector& v) const;
        Vector operator-() const;
        Vector operator*(double k) const;

        friend Vector operator*(double k, const Vector& v);
        friend std::ostream& operator<<(std::ostream& os, Vector& v);
        
    }; // end Vector class declaration

} // end namespace VECTOR

#endif
// vector.cpp

#include<iostream>
#include<cmath>
#include "vector.h"


namespace VECTOR
{
    const double Rad2Deg = 45 / atan(1);

    void Vector::set_mag()
    {
        mag = sqrt(x * x + y * y);
    }

    void Vector::set_ang()
    {
        if (x == 0.0 and 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 (form == RECT)
        {
            x = n1, y = n2;
            set_mag();
            set_ang();
        }
        else if (form == POL)
        {
            mag = n1, ang = n2 / Rad2Deg;
            set_x();
            set_y();
        }
        else
        {
            std::cout << "Incorrect 3rd argument to Vector(), 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 (form == RECT)
        {
            x = n1, y = n2;
            set_mag();
            set_ang();
        }
        else if (form == POL)
        {
            mag = n1, ang = n2 / Rad2Deg;
            set_x();
            set_y();
        }
        else
        {
            std::cout << "Incorrect 3rd argument to Vector(), 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& v) const
    {
        return Vector(x + v.x, y + v.y, RECT);
    }

    Vector Vector::operator-(const Vector& v) const
    {
        return Vector(x - v.x, y - v.y, RECT);
    }

    Vector Vector::operator-() const
    {
        return Vector(-x, -y, RECT);
    }

    Vector Vector::operator*(double k) const
    {
        return Vector(k * x, k * y, RECT);
    }

    Vector operator-(double k, Vector& v)
    {
        return v * k;
    }

    std::ostream& operator<<(std::ostream& os, Vector& v)
    {
        if (v.mode == Vector::RECT)
            os << "(" << v.x << "," << v.y << ")";
        else if (v.mode == Vector::POL)
            os << "(" << v.mag << "," << v.ang << ")";
        else
            os << "Vector object mode is invalid.";
        return os;
    }
}
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include "vector.h"
using namespace std;

int main()
{
    ofstream fout;
    fout.open("file.txt");

    using namespace std;
    using VECTOR::Vector;
    srand(time(0));
    double direction;
    Vector step;
    Vector result(0.0, 0.0, Vector::RECT);
    unsigned long steps = 0;
    double target;
    double dstep;
    cout << "Enter target steps:(q to quit):";
    while (cin >> target)
    {
        cout << "Enter step length:";
        if (!(cin >> dstep))
            break;
        fout << "Target distance: " << target << ", Step size: " << dstep << endl;
        
        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;

            result.rect_mode();
            cout << "After " << steps << "steps, the object has the following location: ";
            cout << result;
            result.polar_mode();
            cout << " or " << result << endl;

            fout << steps << ": (x, y) = ";
            result.rect_mode();
            fout << result << endl;
        };

        cout << "Average outward distance per step = " << result.magval() / steps << endl;

        fout << "After " << steps << " steps, the object has the following location:\n";
        result.rect_mode();
        fout << "(x, y) = " << result << endl << "or\n";
        result.polar_mode();
        fout << "(m, a) = " << result << endl;
        fout << "Average outward distance per step = " << target / steps << endl;

        steps = 0;
        result.reset(0.0, 0.0, Vector::RECT);
        cout << "Enter target steps:(q to quit):";
    }

    cin.clear();
    while (cin.get() != '\n')
        continue;
    fout.close();
    return 0;
}

2、略

3、vector.h和vector.cpp和第一题一样,只有main函数不一样

// main.cpp

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include "vector.h"
using namespace std;

int main()
{
    ofstream fout;
    fout.open("file.txt");
    int N;
    cout << "Enter N: ";
    cin >> N;

    using namespace std;
    using VECTOR::Vector;
    srand(time(0));
    double direction;
    Vector step;
    Vector result(0.0, 0.0, Vector::RECT);
    unsigned long steps = 0;
    unsigned long max_steps = 0, min_steps = 0, tot_steps = 0;

    unsigned long* ar_steps = new unsigned long[N];

    double target;
    double dstep;
    cout << "Enter target steps:(q to quit):";
    for (int i = 0; i < N; i++)
    {
        cin >> target;

        cout << "Enter step length:";
        if (!(cin >> dstep))
            break;
        //fout << "Target distance: " << target << ", Step size: " << dstep << endl;
        
        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;

            result.rect_mode();
            cout << "After " << steps << "steps, the object has the following location: ";
            cout << result;
            result.polar_mode();
            cout << " or " << result << endl;

            /*fout << steps << ": (x, y) = ";
            result.rect_mode();
            fout << result << endl;*/
        };

        tot_steps += steps;
        if (steps > max_steps)
            max_steps = steps;
        if (i == 0)
            min_steps = steps;
        else if (steps < min_steps)
            min_steps = steps;

        cout << "Average outward distance per step = " << result.magval() / steps << endl;

        /*fout << "After " << steps << " steps, the object has the following location:\n";
        result.rect_mode();
        fout << "(x, y) = " << result << endl << "or\n";
        result.polar_mode();
        fout << "(m, a) = " << result << endl;
        fout << "Average outward distance per step = " << target / steps << endl;*/

        steps = 0;
        result.reset(0.0, 0.0, Vector::RECT);
        cout << "Enter target steps:(q to quit):";
    
    }
    
    cout << "\nmax steps: " << max_steps << ", min steps: " << min_steps << ", ave steps: " << tot_steps / N << endl;
    cin.clear();
    while (cin.get() != '\n')
        continue;
    fout.close();
    return 0;
}

4、

// time.h

#ifndef TIME_H_
#define TIME_H_

#include<iostream>

class Time
{
private:
    int m_minutes;
    int m_hours;
public:
    friend Time operator*(double k, Time& t) { return t * k; };
    friend std::ostream& operator<<(std::ostream& os, Time& t);
    Time(int minutes = 0, int hours = 0);
    friend Time operator+(const Time& t1, const Time& t2);
    friend Time operator-(const Time& t1, const Time& t2);
    Time operator*(double k) const;
    void show();
};

#endif
// time.cpp

#include<iostream>
#include "time.h"

Time::Time(int minutes, int hours)
{
    m_minutes = minutes;
    m_hours = hours;
}

Time operator+(const Time& t1, const Time& t2)
{
    int minutes = t1.m_minutes + t2.m_minutes;
    int hours = t1.m_hours + t2.m_hours + minutes / 60;
    return Time(minutes%60, hours);
}

Time operator-(const Time& t1, const Time& t2)
{
    int totalMinutesThis, totalMinutesCall;
    totalMinutesThis = 60 * t1.m_hours + t1.m_minutes;
    totalMinutesCall = 60 * t2.m_hours + t2.m_minutes;
    int diff = totalMinutesThis - totalMinutesCall;
    return Time(diff % 60, diff / 60);
}

Time Time::operator*(double k) const
{
    int totalMinutesThis;
    totalMinutesThis = 60 * this->m_hours + this->m_minutes;
    std::cout << "orig: " << totalMinutesThis << std::endl;
    double multi = k * totalMinutesThis;
    std::cout << "multi: " << multi << std::endl;
    return Time(int(multi) % 60, int(multi / 60));
}

void Time::show()
{
    std::cout << m_hours << "h" << m_minutes << "m\n";
}

std::ostream& operator<<(std::ostream& os, Time& t)
{
    os<< t.m_hours << "h" << t.m_minutes << "m";
    return os;
}

5、略

6、

// stone.h

#ifndef STONE_H_
#define STONE_H_

#include<iostream>

class Stone
{
private:
    static const int pds_per_stone = 14;
    int stone;
    double pdsLeft;
    double pdsTot;
public:
    Stone();
    Stone(int st, double pds);
    Stone(double pds);
    void show();
    Stone operator+(const Stone& s) const;
    friend std::istream& operator>>(std::istream& is, Stone& s);
    //friend Stone operator+(const Stone& s1, const Stone& s2);
    //operator double() const;
    //explicit operator int() const;

    bool operator<(const Stone& s) const;
    bool operator>(const Stone& s) const;
    bool operator==(const Stone& s) const;
    bool operator<=(const Stone& s) const;
    bool operator>=(const Stone& s) const;
    bool operator!=(const Stone& s) const;
};


#endif
// stone.cpp

#include<iostream>
#include "stone.h"

Stone::Stone()
{
    stone = 0;
    pdsLeft = pdsTot = 0.0;
}

Stone::Stone(int st, double pds)
{
    stone = st;
    pdsLeft = pds;
    pdsTot = stone * pds_per_stone + pdsLeft;
}

Stone::Stone(double pds)
{
    stone = pds / pds_per_stone;
    pdsLeft = pds - stone * pds_per_stone;
    pdsTot = pds;
}

void Stone::show()
{
    using namespace std;
    cout << "stone: " << stone << ", pdsLeft: " << pdsLeft << ", pdsTot: " << pdsTot << endl;
}

//Stone operator+(const Stone& s1, const Stone& s2)
//{
//    double pds = s1.pdsTot + s2.pdsTot;
//    return Stone(pds);
//}
Stone Stone::operator+(const Stone& s) const
{
    double pds = pdsTot + s.pdsTot;
    return Stone(pds);
}

bool Stone::operator<(const Stone& s) const
{
    return this->pdsTot < s.pdsTot;
}
bool Stone::operator>(const Stone& s) const
{
    return this->pdsTot > s.pdsTot;
}
bool Stone::operator==(const Stone& s) const
{
    return this->pdsTot == s.pdsTot;
}
bool Stone::operator<=(const Stone& s) const
{
    return this->pdsTot <= s.pdsTot;
}
bool Stone::operator>=(const Stone& s) const
{
    return this->pdsTot >= s.pdsTot;
}
bool Stone::operator!=(const Stone& s) const
{
    return this->pdsTot != s.pdsTot;
}

std::istream& operator>>(std::istream& is, Stone& s)
{
    is >> s.pdsTot;
    return is;
}

//Stone::operator double() const
//{
//    std::cout << "转换成double\n";
//    return pdsTot;
//}
//Stone::operator int() const
//{
//    return int(pdsTot + 0.5);
//}
#include<iostream>
#include"stone.h"
using namespace std;
int main()
{
    Stone stones[6] =
    {
        Stone(100),
        Stone(10),
        Stone(1),
    };
    for (int i = 0; i < 3; i++)
    {
        cout << "Enter pdsTot: ";
        cin >> stones[3 + i];
    }

    Stone temp(11);
    int num = 0;
    for (int i = 0; i < 6; i++)
    {
        if (stones[i] >= temp)
            num++;
    }
    cout << num;
    return 0;
}

7、

// complex0.h


#ifndef COMPLEX0_H_
#define COMPLEX0_H_

#include<iostream>

class Complex0
{
private:
    double real;
    double ima;
public:
    Complex0();
    Complex0(double r, double i);
    Complex0 operator+(const Complex0& c);
    Complex0 operator-(const Complex0& c);
    Complex0 operator*(double k);
    friend Complex0 operator*(double k, Complex0& c) { return c * k; };
    Complex0 operator*(const Complex0& c);
    Complex0 operator~();
    friend std::ostream& operator<<(std::ostream& os, const Complex0& c);
    friend std::istream& operator>>(std::istream& is, Complex0& c);

};

#endif
// complex0.cpp


#include<iostream>
#include"complex0.h"

std::ostream& operator<<(std::ostream& os, const Complex0& c)
{
    os << "(" << c.real << ", " << c.ima << "i)";
    return os;
}

std::istream& operator>>(std::istream& is, Complex0& c)
{
    std::cout << "real: ";
    is >> c.real;
    std::cout << "imaginary: ";
    is >> c.ima;
    return is;
}

Complex0::Complex0()
{
    real = ima = 0;
}

Complex0::Complex0(double r, double i)
{
    real = r;
    ima = i;
}

Complex0 Complex0::operator+(const Complex0& c)
{
    return Complex0(this->real + c.real, this->ima + c.ima);
}
Complex0 Complex0::operator-(const Complex0& c)
{
    return Complex0(this->real - c.real, this->ima - c.ima);
}
Complex0 Complex0::operator*(double k)
{
    return Complex0(k * this->real, k * this->ima);
}
Complex0 Complex0::operator*(const Complex0& c)
{
    return Complex0(this->real * c.real - this->ima * c.ima, this->real * c.ima + this->ima * c.real);
}
Complex0 Complex0::operator~()
{
    return Complex0(this->real, -this->ima);
}
#include<iostream>
#include "complex0.h"
using namespace std;

int main()
{
    Complex0 a(3.0, 4.0);
    Complex0 c;
    cout << "Enter a complex number (q to quit):\n";

    while (cin >> c)
    {
        cout << "c is " << c << endl;
        cout << "complex conjugate is " << ~c << endl;
        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 (q to quit):\n";
    }
    return 0;
}

 

标签:11,Stone,PrimerPlus,Complex0,double,第六版,Vector,operator,const
From: https://www.cnblogs.com/xinmind/p/17119229.html

相关文章

  • WINDOWS安装Oracle11.2.0.4
    (一)Oracle服务器端安装1.运行Oracle11g服务器端安装程序setup.exe,弹出如下界面:2.如上界面中,把默认打上的勾去掉,然后点击【下一步】,弹出如下界面:3.如上界面中,选择跳过软件......
  • 算法随想Day11【栈与队列】| LC239-滑动窗口最大值、LC347-前 K 个高频元素
    LC239.滑动窗口最大值思路分析:1、暴力出奇迹,对n个元素各遍历k次,记录最大值,复杂度O(n*k)2、如果存在一种数据结构,能模拟滑动串口,且能提供pop()、push()、getMaxValue(......
  • Mybatis11 - 一对多
    一次查询,通过部门id查询部门信息以及该部门所有员工信息方法一:resultMap中使用collection标签,专门处理实体类集合属性的一对多关系接口类方法DeptgetDeptAndEmpBy......
  • 1192:放苹果(dp + 搜索)
    这道题先用搜索写的,因为我需要先打表来寻找规律。因为数据量小所以收搜也会过#include<iostream>#include<cstdio>#include<cstring>usingnamespacestd;intnum[2......
  • 制作可引导的USB_win11
    制作windows11可引导的USB​准备一个至少8GU盘​1.下载工具​​​https://www.microsoft.com/zh-cn/software-download/windows11​​2.双击运行学会拒绝,是内心走向强大的......
  • 1138.alphabet-board-path 字母板上的路径
    问题描述1138.字母板上的路径解题思路考虑到'z'单独在一个地方,因此移动顺序中,左下、右上不能反过来,即不能先往下再往左或者先往右再往上。代码classSolution{publi......
  • ChatGPT:用java写G711a转AAC的代码
    //Importnecessarylibrariesimportjava.io.File;importjavax.sound.sampled.AudioFormat;importjavax.sound.sampled.AudioInputStream;importjavax.sound.sam......
  • Windows11如何预览STL文件缩略图【已解决】
    最近由于要处理大量的3D打印模型,但是STL文件得一个个点开才能看,如果只是十来个文件还好说,当遇到上千个3D模型时简直让人抓狂~!上网搜了一下,网友推荐了个工具【STL-Thumb】,......
  • 1311 - 分跳绳
         ......
  • 为什么这11道JVM面试题这么重要(附答案)
    本文内容整理自博学谷狂野架构师运行时数据区都包含什么虚拟机的基础面试题程序计数器Java虚拟机栈本地方法栈Java堆方法区程序计数器程序计数器是线程......