C++ primer plus 第6版第11章编程练习答案(全)

本文提供了C++ Primer Plus第六版第11章的全部编程练习解答,深入探讨了C++中类和对象的相关概念与应用,帮助读者巩固面向对象编程的知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.
//头文件
#ifndef C__11_hpp
#define C__11_hpp
#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);
        ~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 /* C__11_hpp */
//函数定义
#include "C++11.hpp"
#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.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(form==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<<"Incorrrect 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(form==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<<"Incorrrect 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(x*n,y*n);
}
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;
        
}
}
//主函数
#include <iostream>
#include "C++11.hpp"
#include <cstdlib>
#include <fstream>
#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;
    ofstream ofile;
    ofile.open("C.docx");
    if(!ofile.is_open())
        cout<<"Can't open the text.\n";
    cout<<"Enter target distance (q to quit): ";
    if(cin>>target)
    {
   
        cout<<"Enter step length: ";
        if(!(cin>>dstep))
            return 0;
        ofile<<"Target Distance: "<<target<<",Step Size: "<<dstep<<endl;
        ofile<<steps<<": "<<result<<endl;;
        while(result.magval()<target)
        {
   
            direction=rand()%360;
            step.reset(dstep,direction,Vector::POL);
            result=result+step;
            steps++;
            ofile<<steps<<": "<<result<<endl;
            
        }
        ofile<<"After "<<steps<<" steps, the subject "
        "has the following location:\n";
        ofile<<result<<endl;
        result.polar_mode();
        ofile<<" or\n"<<result<<endl;
        ofile<<"Average outward distance per step = "
        <<result.magval()/steps<<endl;
    }
    cout<<"Bye\n";
    cin.clear();
    while(cin.get()!='\n')
        continue;
    return 0;
}

2.
//头文件
#ifndef C__11_hpp
#define C__11_hpp
#include <iostream>
#include <cmath>
namespace VECTOR
{
   
class Vector
{
   
public:
    enum Mode{
   RECT,POL};
private:
    double x;
    double y;
    Mode mode;
public:
    Vector();
    Vector(double n1,double n2,Mode form =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值