C++ Primer Plus 第六版 第十一章课后编程练习答案

1.

vector.h

//清单11.13
#ifndef VECTOR_H
#define VECTOR_H
using namespace std;
#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 ostream &operator<<(ostream & os, const Vector & v);

    };
}

#endif

vector.cpp

//清单11.14
#include "vector.h"
#include <cmath>
using namespace std;
#include <iostream>

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.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 << "Incorrect 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 << "Incorret 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;
    }

    ostream & operator<<(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;
    }
}

play.cpp

//清单11.15
#include "vector.h"
#include <ctime>
using namespace std;
#include <iostream>
#include <fstream>//写入
int main()
{
    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 fout;
    fout.open("RandWalk.txt");//写入到txt
    cout << "Enter target distance(q to quit): ";
    while (cin >> target)
    {
        cout << "Enter step length: ";
        if(!(cin >> dstep))
            break;
        else
            fout << "Target Distance: " << target << ", Step Size: " << dstep << endl;
        int i = 0;
        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps ++;
            fout << i << ":(x,y) =(" << result.xval() << ", " << result.yval() << ")\n";
            i++;
        }
        cout << "After " << steps << " steps, the subject has the following location:\n";
        cout << result << endl;
        fout << "After " << steps << " steps, the subject has the following location:\n";
        fout << result << endl;
        result.polar_mode();
        cout << " or\n" << result << endl;
        cout << "Average outward distance per step = " << result.magval() / steps << endl;
        fout << " or\n" << result << endl;
        fout << "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";
    fout << "Bye!\n";
    cin.clear();
    while (cin.get() != '\n')
        continue;
    return 0;
}

2.  相较第1题,前两个文件稍改动

vector.h

//清单11.13
#ifndef VECTOR_H
#define VECTOR_H
using namespace std;
#include <iostream>
#include <cmath>

namespace VECTOR
{
    class Vector
    {
    public:
        enum Mode{RECT, POL};
    private:
        double x;
        double y;//不再存储mag,ang
        Mode mode;

        double set_mag();
        double set_ang();
        void set_x(double mag, double ang);//调用magval()、angval()时计算mag、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();

        double xval() const { return x;}
        double yval() const { return y;}
        double magval() const
        {
            double mag;
            mag = sqrt(x*x + y*y);
            return mag;
        }
        double angval() const
        {
            double ang;
            if(x == 0.0 && y == 0.0)
                ang = 0.0;
            else
                ang = atan2(y, x);
            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 ostream &operator<<(ostream & os, const Vector & v);

    };
}

#endif

vector.cpp

//清单11.14
#include "vector.h"
#include <cmath>
using namespace std;
#include <iostream>

namespace VECTOR
{
    const double Rad_to_deg = 45.0 / atan(1.0);

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

    double Vector::set_ang()
    {
        double ang;
        if(x==0.0 && y==0.0)
            ang = 0.0;
        else
            ang = atan2(y, x);
        return ang;
    }

    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;
            set_mag();
            set_ang();
        }
        else if (form == POL)
        {
            double mag,ang;
            mag = n1;
            ang = n2 / Rad_to_deg;
            set_x(mag, ang);
            set_y(mag, ang);
        }
        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;
            set_mag();
            set_ang();
        }
        else if (form == POL)
        {
            double mag;
            double ang;
            mag = n1;
            ang = n2 / Rad_to_deg;
            set_x(mag, ang);
            set_y(mag, ang);
        }
        else
        {
            cout << "Incorret 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(n*x, n*y);
    }

    Vector operator*(double n, const Vector &a) //友元
    {
        return a*n;
    }

    ostream & operator<<(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;
    }
}

play.cpp

//清单11.15
#include "vector.h"
#include <ctime>
using namespace std;
#include <iostream>
#include <fstream>//写入
int main()
{
    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 fout;
    fout.open("RandWalk.txt");//写入到txt
    cout << "Enter target distance(q to quit): ";
    while (cin >> target)
    {
        cout << "Enter step length: ";
        if(!(cin >> dstep))
            break;
        else
            fout << "Target Distance: " << target << ", Step Size: " << dstep << endl;
        int i = 0;
        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps ++;
            fout << i << ":(x,y) =(" << result.xval() << ", " << result.yval() << ")\n";
            i++;
        }
        cout << "After " << steps << " steps, the subject has the following location:\n";
        cout << result << endl;
        fout << "After " << steps << " steps, the subject has the following location:\n";
        fout << result << endl;
        result.polar_mode();
        cout << " or\n" << result << endl;
        cout << "Average outward distance per step = " << result.magval() / steps << endl;
        fout << " or\n" << result << endl;
        fout << "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";
    fout << "Bye!\n";
    cin.clear();
    while (cin.get() != '\n')
        continue;
    return 0;
}

3. 前2个文件与第1题相同

play.cpp

//清单11.15
#include "vector.h"
#include <ctime>
using namespace std;
#include <iostream>
#include <fstream>//写入
int main() {
    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 N;
    int maxsteps, minsteps, sumsteps;
    double avesteps;
    maxsteps = 0;
    minsteps = 65535;
    sumsteps = 0;

    cout << "How many times of test would you want: ";
    cin >> N;
    cout << "Enter target distance: ";
    cin >> target;
    cout << "Enter step length: ";
    cin >> dstep;
    for (int i = 0; i < N; i++) {
        while (result.magval() < target) {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;
        }
        sumsteps += steps;
        if (steps > maxsteps)
            maxsteps = steps;
        if (steps < minsteps)
            minsteps = steps;
        steps = 0;
        result.reset(0.0, 0.0);
    }
    avesteps = sumsteps / N;
    cout << "Walk Done!\n";
    cout << "Maxum step: " << maxsteps << ", Minimum step: " << minsteps << ", Average step: " << avesteps <<endl;
    cout << "Bye!";
    cin.clear();
    while (cin.get()!= '\n')
        continue;
    return 0;
}

4.

mytime3.h

//改自清单11.10
#include <iostream>
using namespace std;
#ifndef MYTIME3_H
#define MYTIME3_H
class Time
{
    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 & t1, const Time & t2);//友元
    friend Time operator-(const Time & t1, const Time & t2);
    friend Time operator*(const Time & t, double n);
    friend Time operator*(double m, const Time & t);
    friend ostream &operator << (ostream & os, const Time & t);
};

#endif

mytime3.cpp

//改自清单11.11
#include "mytime3.h"
using namespace std;

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 & t1, const Time & t2)
{
    Time sum;
    sum.minutes = t1.minutes + t2.minutes;
    sum.hours = t1.hours + t2.hours +sum.minutes/60;
    sum.minutes %= 60;
    return sum;
}

Time operator-(const Time & t1, const Time & t2)
{
    Time diff;
    int tot1, tot2;
    tot1 = t1.minutes + 60*t1.hours;
    tot2 = t2.minutes + 60*t2.hours;
    diff.minutes = (tot2 - tot1) % 60;
    diff.hours = (tot2 - tot1) / 60;
    return diff;
}

Time operator*(const Time & t, double mult)
{
    Time result;
    long totalminutes = t.hours * mult * 60 + t.minutes * mult;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}

Time operator*(double mult, const Time & t)
{
    Time result;
    long totalminutes = t.hours * mult * 60 + t.minutes * mult;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}

ostream & operator<<(ostream & os, const Time & t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os;
}

play.cpp

//清单11.12
#include "mytime3.h"
#include <iostream>
using namespace std;
int main()
{
    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.

stonewt.h

//改自清单11.16
#ifndef STONEWT_H
#define STONEWT_H
using namespace std;
#include <iostream>
class Stonewt
{
    enum {Lbs_per_stn = 14};
    int stone;
    int state;
    double pds_left;
    double pounds;
public:
    Stonewt(double lbs);
    Stonewt(int stn, double lbs);
    Stonewt();
    ~Stonewt();
    void setstate(int x);
    friend Stonewt operator+(const Stonewt & s1, const Stonewt & s2);
    friend Stonewt operator-(const Stonewt & s1, const Stonewt & s2);
    friend Stonewt operator*(const Stonewt & s, double n);
    friend Stonewt operator*(double m, const Stonewt & s);
    friend ostream &operator<<(ostream & os, const Stonewt & s);

};

#endif

stonewt.cpp

//改自清单11.17
#include <iostream>
using namespace std;
#include "stonewt.h"

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()
{
    stone = pounds = pds_left = 0;
    state = 3;
}

Stonewt::~Stonewt()
{
}

void Stonewt::setstate(int x)
{
    state = x;
}

Stonewt operator+(const Stonewt & s1, const Stonewt & s2)
{
    Stonewt sum;
    sum.pounds = s1.pounds + s2.pounds;
    sum.stone = int(sum.pounds) / 14;
    sum.pds_left = int(sum.pounds)%14 + sum.pounds - int(sum.pounds);
    return sum;
}

Stonewt operator-(const Stonewt & s1, const Stonewt & s2)
{
    double pounds;
    pounds = s1.pounds - s2.pounds;
    Stonewt diff(pounds);
    return diff;
}

Stonewt operator*(const Stonewt & s, double n)
{
    double pounds;
    pounds = s.pounds * n;
    Stonewt mult(pounds);
    return mult;
}

Stonewt operator*(double m, const Stonewt & s)
{
    double pounds;
    pounds = s.pounds * m;
    Stonewt mult(pounds);
    return mult;
}

ostream & operator<<(ostream & os, const Stonewt & s)
{
    if(s.state == 1)
        os << s.stone << " stone, " << s.pds_left << " pounds" << endl;
    if(s.state == 2)
        os << int(s.stone) << " pounds" << endl;
    if(s.state == 3)
        os << s.pounds << " pounds" << endl;
    return os;
}

play.cpp

#include "stonewt.h"
#include <iostream>
using namespace std;
int main()
{
    Stonewt s[3];
    Stonewt s1(275);
    Stonewt s2(285.7);
    Stonewt s3(21, 8);
    s[0] = s1;
    s[1] = s2;
    s[2] = s3;
    int state;

    for(int i=0; i<3; i++)
    {
        cout << "#" << i+1 << ": \n";
        cout << "Choose style to display('1' for stones, '2' for integer pounds, '3' for float pounds, others to quit): ";
        cin >> state;
        if(state != 1 && state != 2 && state != 3)
        {
            cout << "Error!\n";
            return 0;
        }
        s[i].setstate(state);
        cout << "s" << i+1 << ": " << s[i] << endl;
    }

    int st;
    cout << "Choose style to display the result('1' for stones, '2' for integer pounds, '3' for float pounds, others to quit): ";
    cin >> st;
    Stonewt sum;
    sum = s1 + s2;
    sum.setstate(st);
    cout << "s1 + s2 = " << sum << endl;
    Stonewt diff;
    diff = s3 - s2;
    diff.setstate(st);
    cout << "s3 - s2 = " << diff << endl;
    Stonewt mult1, mult2;
    mult1 = 10 * s1;
    mult2 = s3 * 1.5;
    mult1.setstate(st);
    mult2.setstate(st);
    cout << "10 * s1 = " << mult1 << endl;
    cout << "s3 * 1.5 = " << mult2 << endl;

    return 0;
}

6.

stonewt.h

//改自清单11.16
#ifndef STONEWT_H
#define STONEWT_H
using namespace std;
#include <iostream>
class Stonewt
{
    enum {Lbs_per_stn = 14};
    int stone;
    int state;
    double pds_left;
    double pounds;
public:
    Stonewt(double lbs);
    Stonewt(int stn, double lbs);
    Stonewt();
    ~Stonewt();
    void setstate(int x);
    friend bool operator<(const Stonewt & s1, const Stonewt & s2);//重载全部6个关系运算符
    friend bool operator>(const Stonewt & s1, const Stonewt & s2);
    friend bool operator==(const Stonewt & s1, const Stonewt & s2);
    friend bool operator<=(const Stonewt & s1, const Stonewt & s2);
    friend bool operator>=(const Stonewt & s1, const Stonewt & s2);
    friend bool operator!=(const Stonewt & s1, const Stonewt & s2);
    friend ostream &operator<<(ostream & os, const Stonewt & s);

};

#endif

stonewt.cpp

//改自清单11.17
#include <iostream>
using namespace std;
#include "stonewt.h"

Stonewt::Stonewt(double lbs)
{
    stone = int(lbs) / Lbs_per_stn;
    pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
    pounds = lbs;
    state = 1;
}

Stonewt::Stonewt(int stn, double lbs)
{
    stone = stn;
    pds_left = lbs;
    pounds = stn * Lbs_per_stn + lbs;
    state = 1;
}

Stonewt::Stonewt()
{
    stone = pounds = pds_left = 0;
    state = 1;
}

Stonewt::~Stonewt()
{
}

void Stonewt::setstate(int x)
{
    state = x;
}

bool operator<(const Stonewt & s1, const Stonewt & s2)
{
    if(s1.pounds < s2.pounds)
        return true;
    else
        return false;
}

bool operator>(const Stonewt & s1, const Stonewt & s2)
{
    if(s1.pounds > s2.pounds)
        return true;
    else
        return false;
}

bool operator==(const Stonewt & s1, const Stonewt & s2)
{
    if(s1.pounds == s2.pounds)
        return true;
    else
        return false;
}

bool operator<=(const Stonewt & s1, const Stonewt & s2)
{
    if(s1.pounds <= s2.pounds)
        return true;
    else
        return false;
}

bool operator>=(const Stonewt & s1, const Stonewt & s2)
{
    if(s1.pounds >= s2.pounds)
        return true;
    else
        return false;
}

bool operator!=(const Stonewt & s1, const Stonewt & s2)
{
    if(s1.pounds != s2.pounds)
        return true;
    else
        return false;
}

ostream & operator<<(ostream & os, const Stonewt & s)
{
    if(s.state == 1)
        os << s.stone << " stone, " << s.pds_left << " pounds" << endl;
    if(s.state == 2)
        os << int(s.stone) << " pounds" << endl;
    if(s.state == 3)
        os << s.pounds << " pounds" << endl;
    return os;
}

play.cpp

#include "stonewt.h"
#include <iostream>
using namespace std;
int main()
{
    Stonewt s[6] = {{275}, {288.22}, {26, 7}};
    for(int i=3; i<6; i++)
    {
        double pounds;
        cout << "Enter last 3 Stonewt members:\n";
        cout << "#" << i+1 << ": ";
        cin >> pounds;
        s[i] = pounds;
    }
    cout << "Done!\n";
    cout << "Whole 6 members: \n";
    cout << "#1: " << s[0] << endl;
    cout << "#2: " << s[1] << endl;
    cout << "#3: " << s[2] << endl;
    cout << "#4: " << s[3] << endl;
    cout << "#5: " << s[4] << endl;
    cout << "#6: " << s[5] << endl;

    Stonewt mins = 65535;
    Stonewt maxs = 0;
    int num = 0;
    Stonewt flag(11, 0);
    for(int i=0; i<6; i++)
    {
        if(s[i] < mins)
            mins = s[i];
        if(s[i] > maxs)
            maxs = s[i];
        if(s[i] >= flag)
            num++;
    }
    cout << "Maximum member: " << maxs << endl;
    cout << "Minimum member: " << mins << endl;
    cout << "There are " << num << " Stonewt class members bigger than 11 stones" << endl;
    cout << "Bye!\n";

    return 0;
}

7.

complex0.h

#ifndef COMPLEX_H
#define COMPLEX_H
using namespace std;
#include <iostream>
class Complex
{
    double real;
    double ima;
public:
    Complex();
    Complex(double x, double y);
    ~Complex();
    friend Complex operator+(const Complex & c1, const Complex & c2);
    friend Complex operator-(const Complex & c1, const Complex & c2);
    friend Complex operator*(double n, const Complex & c);//数乘
    friend Complex operator*(const Complex & c1, const Complex & c2);
    friend Complex operator~(const Complex & c);//共轭
    friend ostream &operator<<(ostream & os, const Complex & c);//不加const,在测试时就会报错,???
    friend istream &operator>>(istream & is, Complex & c);//重载运算符>>
};

#endif

complex0.cpp

#include <iostream>
using namespace std;
#include "complex0.h"

Complex::Complex()
{
    real = ima = 0.0;
}

Complex::Complex(double x, double y)
{
    real = x;
    ima = y;
}

Complex::~Complex()
{
}

Complex operator+(const Complex & c1, const Complex & c2)
{
    Complex sum;
    sum.real = c1.real + c2.real;
    sum.ima = c1.ima + c2.ima;
    return sum;
}

Complex operator-(const Complex & c1, const Complex & c2)
{
    Complex diff;
    diff.real = c1.real - c2.real;
    diff.ima = c1.ima - c2.ima;
    return diff;
}

Complex operator*(const Complex & c1, const Complex & c2)
{
    Complex mult;
    mult.real = c1.real*c2.real - c1.ima*c1.ima;
    mult.ima = c1.real*c2.ima + c1.ima*c2.real;
    return mult;
}

Complex operator*(double n, const Complex & c)
{
    Complex mult;
    mult.real = n * c.real;
    mult.ima = n * c.ima;
    return mult;
}

Complex operator~(const Complex & c)
{
    Complex conj;
    conj.real = c.real;
    conj.ima = -c.ima;
    return conj;
}

ostream & operator << (ostream & os, const Complex & c)//重载输出
{
    os << "(" << c.real << "," << c.ima << "i)";
    return os;
}

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

play.cpp

//检测代码用题中给出的即可
#include "complex0.h"
#include <iostream>
using namespace std;

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 << endl;
        cout << "complex conjugate is " <<  ~c << endl;
        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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值