C++ primer 第十一章课后练习

在这里插入图片描述 在这里插入图片描述

#ifndef VECTOR_H_
#define VECTOR_H_
#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 
#include<cmath>
#include"11_1.h"
using namespace std;
namespace VECTOR
{
	const double Rad_to_deg = 45.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;
			set_x();
			set_y();
		}
		else
		{
			cout << "Incorrect 3rd arggument 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;
			set_x();
			set_y();
		}
		else
		{
			cout << "Incorrect 3rd arggument 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 objects mode is invalid";
		return os;
	}
	};
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "11_1.h"

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

    fout.open("temp.txt");
    cout << "Enter target distance (q to quit): ";
    while (cin >> target)
    {
        cout << "Enter step length: ";
        if (!(cin >> dstep))
        {
            break;
        }
        fout << "Target Distance: " << target;
        fout << ", Step Size: " << dstep << endl;
        fout << "0: " << result << endl;
        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;
            fout << steps << ": " << result << endl;
        }
        fout << "After " << steps << " steps, the subject ";
        fout << "has the following location:\n";
        fout << result << endl;
        result.polar_mode();
        fout << " or\n";
        fout << result << endl;
        fout << "Average outward distance per step = ";
        fout << result.magval() / steps << endl;
        fout << endl;

        cout << "After " << steps << " steps, the subject ";
        cout << "has the following location:\n";
        cout << result << endl;
        result.polar_mode();
        cout << " or\n";
        cout << result << endl;
        cout << "Average outward distance per step = ";
        cout << result.magval() / steps << endl;
        steps = 0;
        result.reset(0.0, 0.0);
        cout << "Enter target distance (q to quit): ";
    }
    cin.clear();
    while (cin.get() != '\n')
        continue;
    fout.close();
    cout << "Bye!\n";

    return 0;
}

在这里插入图片描述

#ifndef VECT_H_
#define VECT_H_
#include <iostream>

namespace VECTOR
{
    class Vector
    {
    public:
        enum Mode{ RECT, POL };

    private:
        double x;
        double y;
        Mode mode;
        double set_mag() const;             //修改了原来设置矢量长度的函数;
        double set_ang() const;             //修改了原来设置矢量角度的函数;
        void set_x(double mag, double ang); //修改了原来设置矢量x坐标的函数;
        void set_y(double mag, double ang); //修改了原来设置矢量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 set_mag(); }
        double angval() const { return set_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

#include <cmath>
#include "vect.h"
using std::atan;
using std::atan2;
using std::cos;
using std::cout;
using std::sin;
using std::sqrt;

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

    double Vector::set_mag() const
    {
        return sqrt(x * x + y * y); //计算向量的长度;
    }

    double Vector::set_ang() const
    {
        if (x == 0.0 && y == 0.0)
        {
            return 0.0; //若是向量的x坐标和y坐标为0则角度也为0;
        }
        else
        {
            return atan2(y, x); //否则计算向量的角度并返回至调用对象;
        }
    }

    void Vector::set_x(double mag, double ang)
    {
        x = mag * cos(ang);
        return;
    }

    void Vector::set_y(double mag, double ang)
    {
        y = mag * sin(ang);
        return;
    }

    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)
        {
            set_x(n1, n2 / Rad_to_deg); //使用修改的设置x坐标的函数来更新x坐标值;
            set_y(n1, n2 / Rad_to_deg); //使用修改的设置y坐标的函数来更新y坐标值;
        }
        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;
        }
        else if (form == POL)
        {
            set_x(n1, n2 / Rad_to_deg); //同上一个函数说明;
            set_y(n1, n2 / Rad_to_deg); //同上一个函数说明;
        }
        else
        {
            cout << "Incorrect 3rd argument to Vector() -- ";
            cout << "vector set to 0\n";
            x = y = 0.0;
            mode = RECT;
        }
        return;
    }

    Vector::~Vector()
    {
    }

    void Vector::polar_mode()
    {
        mode = POL;
        return;
    }

    void Vector::rect_mode()
    {
        mode = RECT;
        return;
    }

    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.magval() << ", ";
            os << v.angval() * Rad_to_deg << ")";
        }
        else
        {
            os << "Vector object mode is invalid";
        }
        return os;
    }
}

在这里插入图片描述

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vect.h"

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 = 50.0;
    double dstep = 2.0;
    int i, n, max, min;
    double average = 0.0;

    cout << "Target distance: " << target << endl;
    cout << "Step length: " << dstep << endl;
    cout << "Please you enter running time: ";
    while (!(cin >> n))
    {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        cout << "Please enter an number: ";
    }
    i = 0;
    while (i < n)
    {
        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;
        }
        if (0 == i) //初始化;
        {
            max = min = steps;
        }
        else //进行最大值与最小值的存储;
        {
            max = max < steps ? steps : max;
            min = min > steps ? steps : min;
        }
        average += steps; //累加每次测试的步数;
        cout << "\nTime #" << i + 1 << ':' << endl;
        cout << "After " << steps << " steps, the subject ";
        cout << "has the following location:\n";
        cout << result << endl;
        result.polar_mode();
        cout << " or\n";
        cout << result << endl;
        cout << "Average outward distance per step = ";
        cout << result.magval() / steps << endl;
        steps = 0;
        result.reset(0.0, 0.0);
        ++i;
    }
    cout << "\nThe biggest steps are: " << max << endl;
    cout << "The smallest steps are: " << min << endl;
    cout << "The average steps are: " << average / n << endl;
    cout << "Bye!\n";

    return 0;
}

在这里插入图片描述

#ifndef MYTIME3_H_
#define MYTIME3_H_
#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 &a, const Time &b); //'+'运算符的友元函数原型;
    friend Time operator-(const Time &a, const Time &b); //'-'运算符的友元函数原型;
    Time operator*(double n) const;
    friend Time operator*(double m, const Time &t) { return t * m; } //内联友元函数;
    friend std::ostream &operator<<(std::ostream &os, const Time &t);
};

#endif

#include "mytime3.h"

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;
    return;
}

void Time::AddHr(int h)
{
    hours += h;
    return;
}

void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
    return;
}

Time operator+(const Time &a, const Time &b) //友元函数实现的'+'运算符重载;
{
    Time sum;
    sum.minutes = a.minutes + b.minutes;
    sum.hours = a.hours + b.hours + sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}

Time operator-(const Time &a, const Time &b) //友元函数实现的'-'运算符重载;
{
    Time diff;
    int tot1, tot2;
    tot1 = a.minutes + 60 * a.hours;
    tot2 = b.minutes + 60 * b.hours;
    diff.minutes = (tot2 - tot1) % 60;
    diff.hours = (tot2 - tot1) / 60;
    return diff;
}

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

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

在这里插入图片描述

#ifndef STONEWT_H_
#define STONEWT_H_

class Stonewt
{
public:
    enum Mode { STONE, INT_POUND, DOUBLE_POUND };

private:
    enum { Lbs_per_stn = 14 }; //1英石等于14英镑;
    int stone;
    double pds_left;
    double pounds;
    int pounds_int;
    Mode mode;
    void set_stone();      //设置stone英石变量;
    void set_pounds();     //设置pounds英镑变量;
    void set_pounds_int(); //设置pounds整型英镑变量;

public:
    Stonewt(double lbs, Mode form);
    Stonewt(int stn, double lbs, Mode form);
    Stonewt();
    ~Stonewt();
    void set_stone_mode();                                 //设置英石格式;
    void set_pounds_mode();                                //设置整数磅格式;
    void set_int_pounds_mode();                            //设置浮点磅格式;
    explicit operator int() const;                         //防止隐式转换并将对象转换为int类型;
    explicit operator double() const;                      //防止隐式转换并将对象转换为double类型;
    Stonewt operator+(const Stonewt &st) const;            //重载'+'运算符;
    Stonewt operator-(const Stonewt &st) const;            //重载'-'运算符;
    Stonewt operator*(double n) const;                     //重载'*'运算符;
    friend Stonewt operator*(double n, const Stonewt &st); //友元函数重载'*'运算符可以令对象在'*'右边进行操作;
    friend std::ostream &operator<<(std::ostream &os, const Stonewt &st);
};

#endif

#include <iostream>
#include "stonewt.h"
using std::cout;
using std::endl;

void Stonewt::set_stone()
{
    stone = int(pounds) / Lbs_per_stn;                           //设置stone英石变量;
    pds_left = int(pounds) % Lbs_per_stn + pounds - int(pounds); //设置pounds磅数剩余变量;
}

void Stonewt::set_pounds()
{
    pounds = stone * Lbs_per_stn + pds_left; //设置pounds英镑变量;
}

void Stonewt::set_pounds_int()
{
    pounds_int = int(pounds + 0.5); //设置pounds英镑整型四舍五入变量;
}

Stonewt::Stonewt(double lbs, Mode form)
{
    mode = form;
    if (mode == STONE)
    {
        stone = int(lbs) / Lbs_per_stn;
        pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
        set_pounds();     //调用函数设置Stonewt类剩余数据;
        set_pounds_int(); //同上;
    }
    else if (mode == INT_POUND)
    {
        pounds_int = int(lbs);
        pounds = lbs;
        set_stone();
    }
    else if (mode == DOUBLE_POUND)
    {
        pounds = lbs;
        set_pounds_int();
        set_stone();
    }
    else
    {
        cout << "Incorrect mode!" << endl;
        cout << "Stonewt set to 0" << endl;
        stone = pounds = pds_left = 0.0;
        mode = STONE;
    }
}

Stonewt::Stonewt(int stn, double lbs, Mode form)
{
    mode = form;
    if (mode == STONE)
    {
        stone = stn;
        pds_left = lbs;
        set_pounds();
        set_pounds_int();
    }
    else if (mode == INT_POUND)
    {
        pounds_int = int(stn * Lbs_per_stn + lbs);
        pounds = stn * Lbs_per_stn + lbs;
        set_stone();
    }
    else if (mode == DOUBLE_POUND)
    {
        pounds = stn * Lbs_per_stn + lbs;
        set_pounds_int();
        set_stone();
    }
    else
    {
        cout << "Incorrect mode!" << endl;
        cout << "Stonewt set to 0" << endl;
        stone = pounds = pds_left = 0.0;
        mode = STONE;
    }
}

Stonewt::Stonewt()
{
    stone = pounds = pds_left = 0.0;
    mode = STONE;
}

Stonewt::~Stonewt()
{
}

void Stonewt::set_stone_mode()
{
    mode = STONE;
    return;
}

void Stonewt::set_pounds_mode()
{
    mode = DOUBLE_POUND;
    return;
}

void Stonewt::set_int_pounds_mode()
{
    mode = INT_POUND;
    return;
}

Stonewt::operator int() const
{
    return int(pounds + 0.5); //返回四舍五入后的pounds英镑整型值;
}

Stonewt::operator double() const
{
    return pounds;
}

Stonewt Stonewt::operator+(const Stonewt &st) const
{
    return Stonewt(pounds + st.pounds, st.mode); //调用构造函数进行加法操作并重新设置mode模式;
}

Stonewt Stonewt::operator-(const Stonewt &st) const
{
    return Stonewt(pounds - st.pounds, st.mode); //调用构造函数进行减法操作并重新设置mode模式;
}

Stonewt Stonewt::operator*(double n) const
{
    return Stonewt(pounds * n, mode); //调用构造函数进行乘法操作并重新设置mode模式;
}

Stonewt operator*(double n, const Stonewt &st)
{
    return Stonewt(n * st.pounds, st.mode); //调用非友元函数进行乘法操作;
}

std::ostream &operator<<(std::ostream &os, const Stonewt &st)
{
    if (st.mode == Stonewt::STONE)
    {
        os << st.stone << " stone, " << st.pds_left << " pounds" << endl;
    }
    else if (st.mode == Stonewt::INT_POUND)
    {
        os << st.pounds_int << " pounds(int)" << endl;
    }
    else if (st.mode == Stonewt::DOUBLE_POUND)
    {
        os << st.pounds << " pounds(double)" << endl;
    }
    else
    {
        os << "Incorrect mode!" << endl;
    }
    return os;
}

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

int main()
{
    using std::cout;
    using std::endl;
    Stonewt incognito(275, Stonewt::DOUBLE_POUND);
    Stonewt wolfe(285.7, Stonewt::STONE);
    Stonewt taft(21, 8, Stonewt::INT_POUND);

    cout << "Here are the tsets:" << endl;
    cout << "The celebrity weighed ";
    cout << incognito;
    cout << "The detective weighed ";
    cout << wolfe;
    cout << "The President weighed ";
    cout << taft;
    cout << "incognito + wolfe = " << incognito + wolfe;
    cout << "wolfe - incognito = " << wolfe - incognito;
    cout << "taft * 10.0 = " << taft * 10.0;
    cout << "10.0 * taft = " << 10.0 * taft;

    return 0;
}

在这里插入图片描述

#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>

class Stonewt
{
private:
    enum{ Lbs_per_stn = 14 };
    int stone;
    double pds_left;
    double pounds;

public:
    Stonewt(double lbs);
    Stonewt(int stn, double lbs);
    Stonewt();
    ~Stonewt();
    bool operator<(const Stonewt &st) const;  //重载'<'运算符;
    bool operator>(const Stonewt &st) const;  //重载'>'运算符;
    bool operator<=(const Stonewt &st) const; //重载'<='运算符;
    bool operator>=(const Stonewt &st) const; //重载'>='运算符;
    bool operator==(const Stonewt &st) const; //重载'=='运算符;
    bool operator!=(const Stonewt &st) const; //重载'!='运算符;
    friend std::ostream &operator<<(std::ostream &os, const Stonewt &st);
};

#endif

#include <iostream>
using std::cout;
#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;
}

Stonewt::~Stonewt()
{
}

bool Stonewt::operator<(const Stonewt &st) const
{
    return this->pounds < st.pounds ? true : false; //重载'<'运算符;
}

bool Stonewt::operator>(const Stonewt &st) const
{
    return this->pounds > st.pounds ? true : false; //重载'>'运算符;
}

bool Stonewt::operator<=(const Stonewt &st) const
{
    return this->pounds <= st.pounds ? true : false; //重载'<='运算符;
}

bool Stonewt::operator>=(const Stonewt &st) const
{
    return this->pounds >= st.pounds ? true : false; //重载'>='运算符;
}

bool Stonewt::operator==(const Stonewt &st) const
{
    return this->pounds == st.pounds ? true : false; //重载'=='运算符;
}

bool Stonewt::operator!=(const Stonewt &st) const
{
    return this->pounds != st.pounds ? true : false; //重载'!='运算符;
}

std::ostream &operator<<(std::ostream &os, const Stonewt &st)
{
    os << st.pounds << " pounds.\n";
    return os;
}

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

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    double val;
    int i, count;
    Stonewt min, max;
    Stonewt temp(11.0);
    Stonewt wt[6] = {Stonewt(285.7), Stonewt(21, 8), Stonewt(12.0)};

    count = 0;
    for (i = 3; i < 6; i++)
    {
        cout << "Please enter an number for pounds: ";
        while (!(cin >> val))
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Illegal input! Enter an number: ";
        }
        wt[i] = Stonewt(i + 1, val); //循环赋值;
    }
    min = max = wt[0];
    for (i = 0; i < 6; i++)
    {
        if (wt[i] < min)
        {
            min = wt[i];
        }
        if (max < wt[i])
        {
            max = wt[i];
        }
        if (temp <= wt[i])
        {
            ++count;
        }
    }
    cout << "The minimum pounds are: " << min;
    cout << "The maximum pounds are: " << max;
    cout << "There are " << count << " elements more than 11 pounds." << endl;

    return 0;
}

在这里插入图片描述在这里插入图片描述

#ifndef COMPLEX_H_
#define COMPLEX_H_
#include<iostream>
class complex
{
public:
	complex();
	complex(double x,double y);
	~complex();
	complex operator~();
	friend complex operator+(complex& a, complex& b);
	friend complex operator-(complex& a, complex& b);
	friend complex operator*(complex& a, complex& b);
	friend complex operator*(double n, const complex& a);
	friend std::ostream& operator<<(std::ostream& os, const complex& a);
	friend std::istream& operator>>(std::istream& is,  complex& a);
private:
	double real;
	double imag;

};

#endif // !COMPLEX_H_
#include"11_7.h"
#include<iostream>
complex::complex()
{
	real = 0.0;
	imag = 0.0;
}
complex::complex(double x, double y)
{
	real = x;
	imag = y;
}
complex::~complex()
{}
complex complex:: operator~()
{
	imag = -imag;
	return complex(real, imag);
}
complex operator+(complex& a, complex& b)
{
	double x = a.real + b.real;
	double y = a.imag + b.imag;
	return complex(x, y);
}
complex operator-(complex& a, complex& b)
{
	double x = a.real - b.real;
	double y = a.imag - b.imag;
	return complex(x, y);
}
complex operator*(double n,const complex& a)
{
	double x = n * a.real;
	double y = n * a.imag;
	return complex(x, y);
}
complex operator*(complex& a,  complex& b)
{
	double x = a.real * b.real - a.imag * b.imag;
	double y = a.real * b.imag + a.imag * b.real;
	return complex(x, y);
}

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) 
	{
		std::cout << "imaginary: ";
		 is >> a.imag;
	}
	return is;

}
#include <iostream>
using namespace std;
#include "11_7.h"
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):";
	}
	cout << "Done!\n";
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值