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

本文提供了C++ Primer Plus第六版第11章的全部编程练习解答,深入探讨了C++中类和对象的相关概念与应用,帮助读者巩固面向对象编程的知识。
摘要由CSDN通过智能技术生成
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 = 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 sqrt(x*x+y*y);}
    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 /* 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);
double Vector::angval()const
{
    if(x==0.0&&y==0.0)
        return 0.0;
    else
        return atan2(y,x);
}
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)
    {
        x=n1*cos(n2);
        y=n1*sin(n2);
    }
    else
    {
        cout<<"Incorrrect 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)
    {
        x=n1*cos(n2);
        y=n1*sin(n2);
    }
    else
    {
        cout<<"Incorrrect 3rd argument to Vector()--";
        cout<<"vector set to 0\n";
        x=y=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.magval()<<","<<v.angval()*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;
}

3.
//头文件
#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;
    int trails;
    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;
        cout<<"How many trails: ";
        if(!(cin>>trails))
            return 0;
        double n_step[trails];
        ofile<<"Target Distance: "<<target<<",Step Size: "<<dstep<<endl;
        for(int i=0;i<trails;++i)
        {
        ofile<<"Tril: "<<i+1<<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;
            
        }
            n_step[i]=steps;
        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<<endl;
            steps=0;
            result.reset(0.0,0.0);
        }
        int high=n_step[0];
        int low=n_step[0];
        int sum=0;
        for(int k=0;k<trails;++k)
        {
            sum+=n_step[k];
            high=(high>n_step[k])?high:n_step[k];
            low=(low<n_step[k])?low:n_step[k];
        }
        ofile<<"\nThe highest steps : "<<high<<endl;
        ofile<<"The lowest steps: "<<low<<endl;
        ofile<<"Average: "<<(double)sum/trails<<endl;
    }
    cout<<"Bye\n";
    cin.clear();
    while(cin.get()!='\n')
        continue;
    return 0;
}

4.
//头文件
#ifndef C__11_hpp
#define C__11_hpp

#include <iostream>
class Time
{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h,int m=0);
    void addMin(int m);
    void addHr(int h);
    void Reset(int h=0,int m=0);
    friend Time operator+(const Time & t,const Time & k);
    friend Time operator-(const Time & t,const Time & k);
    friend Time operator*(const Time & t,double k);
    friend Time operator*(double m,const Time & t){return t*m;};
    friend std::ostream & operator<<(std::ostream & os,const Time & t);
    
};
#endif /* C__11_hpp */
//定义

#include "C++11.hpp"
Time::Time()
{
    hours=minutes=0;
}
Time::Time(int h,int m)
{
    hours=h;
    minutes=m;
}
void Time::addMin(int m)
{
    minutes+=m;
    hours+=minutes/60;
    minutes%=60;
}
void Time::addHr(int h)
{
    hours+=h;
}
void Time::Reset(int h,int m)
{
    hours=h;
    minutes=m;
}
Time operator+(const Time & t,const Time & k)
{
    Time time;
    time.minutes=t.minutes+k.minutes;
    time.hours=t.hours+k.hours+time.minutes/60;
    time.minutes%=60;
    return time;
}
Time operator-(const Time & t,const Time & k)
{
    Time time;
    int total1,total2;
    total1=t.minutes+t.hours*60;
    total2=k.minutes+k.hours*60;
    time.hours=(total1-total2)/60;
    time.minutes=(total1-total2)%60;
    return time;
}
Time operator*(const Time & t,double k)
{
    Time time;
    long totalminutes;
    totalminutes=t.hours*60*k+t.minutes*k;
    time.hours=totalminutes/60;
    time.minutes=totalminutes%60;
    return time;
}
std::ostream & operator<<(std::ostream & os,const Time & t)
{
    os<<t.hours<<" hours, "<<t.minutes<<" minutes";
    return os;
}
//主函数
#include <iostream>
#include "C++11.hpp"
int main() {
    using std::cout;
    using std::endl;
    Time aida(3,35);
    Time tosca(2,48);
    Time temp;
    cout<<"Aida and Tosca:\n";
    cout<<aida<<"; "<<tosca<<endl;
    temp=aida+tosca;
    cout<<"Aida + Tosca: "<<temp<<endl;
    temp=aida*1.17;
    cout<<"Aida * 1.17: "<<temp<<endl;
    cout<<"10.0 * TOsca: "<<10.0 * tosca<<endl;
    return 0;
}

5.
//头文件
#ifndef C__11_hpp
#define C__11_hpp
#include <iostream>
class Stonewt
{
private:
    enum{Lbs_per_stn = 14};
    enum Mode{Stone,Int_pounds,Float_pounds};
    Mode mode;
    int stone;
    double pds_left;
    double pounds;
public:
    Stonewt();
    Stonewt(int stn,double lbs,Mode m=Float_pounds);
    ~Stonewt();
    void Intp_mode(){mode=Int_pounds;};
    void Flop_mode(){mode=Float_pounds;};
    void Stop_mode(){mode=Stone;};
    Stonewt operator+(const Stonewt & t)const;
    Stonewt operator-(const Stonewt & t)const;
    Stonewt operator*(double t)const;
    friend Stonewt operator*(double m,Stonewt & t){return t*m;};
    friend std::ostream & operator<<(std::ostream & os,const Stonewt & t);
};

#endif /* C__11_hpp */
//定义
#include "C++11.hpp"
#include <iostream>
Stonewt::Stonewt()
{
    stone=pds_left=pounds=0.0;
    mode=Float_pounds;
}
Stonewt::~Stonewt()
{
}
 Stonewt::Stonewt(int stn,double lbs,Mode m)
{
    mode=m;
    stone=stn;
    pds_left=lbs;
    if(m==Stone||m==Float_pounds)
    pounds=stn*Lbs_per_stn+lbs;
    else if(m==Int_pounds)
        pounds=int(stn*Lbs_per_stn+lbs+0.5);
    else
        std::cout<<"Wrong mode!\n";
    
}
Stonewt Stonewt::operator+(const Stonewt & t)const
{
    Stonewt sto;
    sto.mode=mode;
    sto.stone=stone+t.stone+int(pds_left+t.pds_left)/Lbs_per_stn;
    sto.pds_left=int(pds_left+t.pds_left)%Lbs_per_stn+pds_left+t.pds_left-int(pds_left+t.pds_left);
    if(mode==Stone||mode==Float_pounds)
    sto.pounds=(stone+t.stone)*Lbs_per_stn+pds_left+t.pds_left;
    else
        sto.pounds=int((stone+t.stone)*Lbs_per_stn+pds_left+t.pds_left+0.5);
    return sto;
}
Stonewt Stonewt::operator-(const Stonewt & t)const
{
    Stonewt sto;
    sto.mode=mode;
    float sum1,sum2;
    sum1=stone*Lbs_per_stn+pds_left;
    sum2=t.stone*Lbs_per_stn+t.pds_left;
    sto.stone=int(sum1-sum2)/Lbs_per_stn;
    sto.pds_left=int(sum1-sum2)%Lbs_per_stn+sum1-sum2-int(sum1-sum2);
    if(mode==Stone||mode==Float_pounds)
        sto.pounds=sum1-sum2;
    else
    {
        if(sum1>=sum2)
        sto.pounds=int(sum1-sum2+0.5);
        else
            sto.pounds=int(sum1-sum2-0.5);
    }
    return sto;
}
Stonewt Stonewt::operator*(double t)const
{
    Stonewt sto;
    sto.mode=mode;
    double sum;
    sum=stone*t*Lbs_per_stn+t*pds_left;
    sto.stone=int(sum)/Lbs_per_stn;
    sto.pds_left=int(sum)%Lbs_per_stn+sum-int(sum);
    if(mode==Stone||mode==Float_pounds)
        sto.pounds=sum;
    else
        sto.pounds=int(sum+0.5);
    return sto;
}
std::ostream & operator<<(std::ostream & os,const Stonewt & t)
{
    if(t.mode==Stonewt::Stone)
        os<<t.stone<<" stones "<<t.pds_left<<" lbs.\n";
    else if(t.mode==Stonewt::Int_pounds)
        os<<t.pounds<<" pounds.\n";
    else if(t.mode==Stonewt::Float_pounds)
        os<<t.pounds<<" pounds.\n";
    return os;
}
//主函数
#include <iostream>
#include "C++11.hpp"
int main() {
    using namespace std;
    Stonewt a;
    cout<<a;
    Stonewt b{11,3.2};
    Stonewt c{2,1.8};
    cout<<b+c;
    cout<<b-c;
    cout<<2*b;
    cout<<b*2;
    b.Stop_mode();
    cout<<2*b;
    cout<<c-b;
    Stonewt d=c-b;
    d.Stop_mode();
    cout<<d;
    return 0;
}
6.
//头文件
#ifndef C__11_hpp
#define C__11_hpp
#include <iostream>
class Stonewt
{
private:
    enum{Lbs_per_stn = 14};
    int stone;
    double pds_left;
    double pounds;
public:
    Stonewt(){stone=pds_left=pounds=0;};
    Stonewt (double lbs);
    Stonewt(int stn,double lbs);
    ~Stonewt();
    operator double(){return pounds;};
    bool operator<(Stonewt & t)const;
    bool operator<=(Stonewt & t)const;
    bool operator>(Stonewt & t)const;
    bool operator>=(Stonewt & t)const;
    bool operator!=(Stonewt & t)const;
    bool operator==(Stonewt & t)const;
};
#endif /* C__11_hpp */
//定义
#include "C++11.hpp"

Stonewt::Stonewt (double lbs)
{
    stone=int(lbs)/Lbs_per_stn;
    pds_left=int(lbs)%Lbs_per_stn+lbs-int(lbs);
    pounds=lbs;
}
Stonewt::Stonewt(int stn,double lbs)
{
    stone=stn;
    pds_left=lbs;
    pounds=stn*Lbs_per_stn+lbs;
}
Stonewt::~Stonewt()
{
}
bool Stonewt::operator<(Stonewt & t)const
{
    if(pounds<t.pounds)
        return true;
    else
        return false;
}
bool Stonewt::operator<=(Stonewt & t)const
{
    if(pounds<=t.pounds)
        return true;
    else
        return false;
}
bool Stonewt::operator>(Stonewt & t)const
{
    if(pounds>t.pounds)
        return true;
    else
        return false;
}
bool Stonewt::operator>=(Stonewt & t)const
{
    if(pounds>=t.pounds)
        return true;
    else
        return false;
}
bool Stonewt::operator!=(Stonewt & t)const
{
    if(pounds!=t.pounds)
          return true;
      else
          return false;
}
bool Stonewt::operator==(Stonewt & t)const
{
    if(pounds==t.pounds)
          return true;
      else
          return false;
}
//主函数
#include <iostream>
#include "C++11.hpp"
int main() {
    using std::cout;
    using std::endl;
    using std::cin;
    Stonewt sto[6]{157.2,110,34.5};
    cout<<"Enter three double :";
    int k=0;
    double m;
    while(k<3&&cin>>m)
    {
        sto[k+3]=m;
        ++k;
    }
    double small=sto[0];
    double large=sto[0];
    Stonewt com(11,0);
    int n=0;
    for(int i=0;i<6;++i)
    {
        small=(small<sto[i])?(double)small:(double)sto[i];
        large=(large>sto[i])?(double)large:(double)sto[i];
        if(sto[i]>=com)
            ++n;
    }
    cout<<"The smallest element is "<<double(small)<<endl;
    cout<<"The largest element is "<<double(large)<<endl;
    cout<<n<<" bigger then 11 stone.\n";
    return 0;
}
7.
//头文件
ifndef C__11_hpp
#define C__11_hpp
#include <iostream>
class Complex
{
private:
    double real;
    double imag;
public:
    Complex();
    Complex(double a,double b);
    ~Complex();
    Complex operator+(Complex & a);
    Complex operator-(Complex & a);
    Complex operator*(Complex & a);
    Complex operator~()const;
    friend Complex operator*(double a,Complex & b);
    friend std::ostream & operator<<(std::ostream & os, const Complex & a );
    friend std::istream & operator>>(std::istream & is,Complex & a);
};

#endif /* C__11_hpp */
//定义
#include "C++11.hpp"
Complex::Complex()
{
    real=imag=0.0;
}
Complex::Complex(double a,double b)
{
    real=a;
    imag=b;
}
Complex::~Complex()
{
    
}
Complex Complex::operator+(Complex & a)
{
    return Complex(real+a.real,imag+a.imag);
}
Complex Complex::operator-(Complex & a)
{
    
  return Complex(real-a.real,imag-a.imag);
}
Complex Complex::operator*(Complex & a)
{
    return Complex(real*a.real-imag*a.imag,real*a.imag+imag*a.real);
}
Complex operator*(double a,Complex & b)
{
    return Complex(a*b.real,a*b.imag);
}
Complex Complex::operator~()const
{
    return Complex(real,-imag);
}

std::ostream & operator<<(std::ostream & os,const Complex & a )
{
    os<<"("<<a.real<<","<<a.imag<<"i)";
    return os;
}
std::istream & operator>>(std::istream & is,Complex & a)
{
    std::cout<<"real: ";
    if(!(is>>a.real))
        return is;
    std::cout<<"imaginary: ";
    is>>a.imag;
    return is;
}
//主函数

#include <iostream>
using namespace std;
#include "C++11.hpp"
int main() {
    Complex a(3.0,4.0);
    Complex c;
    cout<<"Enter a complex number (q to quit):\n";
    while(cin>>c)
    {
        cout<<"c is "<<c<<'\n';
        cout<<"complex conjugate is "<<~c<<'\n';
        cout<<"a is "<<a<<'\n';
        cout<<"a + c is "<<a+c<<'\n';
        cout<<"a - c is "<<a-c<<'\n';
        cout<<"a * c is "<<a*c<<'\n';
        cout<<"2 * c is "<<2*c<<'\n';
        cout<<"Enter a complex number (q to quit):\n";
    }
    cout<<"Done!\n";
    return 0;
}
//
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值