C++ Primer Plus,Chapter11, excercise11.1

来源:互联网 发布:考单片机的高级证书 编辑:程序博客网 时间:2024/06/18 12:29

1.随机漫步位置写入文件

这里写图片描述
这里写图片描述

//vecor.h#include <iostream>#ifndef VECTOR_H_#define VECTOR_H_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);        ~Vector();        void reset(double n1, double n2, Mode form = RECT);        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*(double n) const;        friend Vector operator*(double n, const Vector &v);        friend std::ostream & operator<<(std::ostream &os, const Vector &v);        operator double();    };}#endif//vector.cpp#include "stdafx.h"#include "vector.h"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 (mode == RECT)        {            x = n1;            y = n2;            set_mag();            set_ang();        }        else if (mode == POL)        {            mag = n1;            ang = n2 / Rad_to_deg;            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::reset(double n1, double n2, Mode form)    {        mode = form;        if (mode == RECT)        {            x = n1;            y = n2;            set_mag();            set_ang();        }        else if (mode == POL)        {            mag = n1;            ang = n2 / Rad_to_deg;            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::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);    }    Vector Vector::operator-(const Vector &v) const    {        return Vector(x - v.x, y - v.y);    }    Vector Vector::operator*(double n) const    {        return Vector(x * n, y * n);    }    Vector operator*(double n, const Vector &v)    {        //return Vector(n * v.x, n * v.y);        return v * n;    }    std::ostream & operator<<(std::ostream &os, const Vector &v)    {        if (v.mode == Vector::RECT)            os << "(x, y) = (" << v.x << ", " << v.y << ").\n";        else if (v.mode == Vector::POL)            os << "(m, a) = (" << v.mag << ", " << v.ang << ").\n";        else            os << "Vector object mode is invalid";        return os;    }    Vector::operator double()    {        return mag;    }}//main.cpp#include "stdafx.h"#include "vector.h"#include <ctime>#include <fstream>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("randwalk.txt");        if (!(fout.is_open()))            exit(EXIT_FAILURE);    cout << "Enter target distance (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;            fout << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")\n";            steps++;        }        fout << "After " << steps << "steps, the subject has the following location:\n";        fout << "(x, y) = (" << result.xval() << ", " << result.yval() << ")\n or\n(m, a) = (" << result.magval() << ", " << result.angval() << ")\n";        fout << "Average outward distance per step = " << result.magval() / steps << endl;        cout << "After " << steps << "steps, the subject has the following location:\n";        cout << result;        result.polar_mode();        cout << "or\n" << result;        cout << "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";    cin.clear();    while (cin.get() != '\n')        continue;    fout.close();    cin.get();    return 0;}

2.vector类,由x,y计算mag,ang ,公有接口不变

这里写图片描述

//vector.h#include <iostream>#ifndef VECTOR_H_#define VECTOR_H_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);        ~Vector();        void reset(double n1, double n2, Mode form = RECT);        double xval() const {return x;}        double yval() const {return y;}        double magval() const;        double angval() const;        void polar_mode();        void rect_mode();        Vector operator+(const Vector &v) const;        Vector operator-(const Vector &v) const;        //Vector operator-() const;        Vector operator*(double n) const;        friend Vector operator*(double n, const Vector &v);        friend std::ostream & operator<<(std::ostream &os, const Vector &v);        operator double();    };}#endif//vector.cpp#include "stdafx.h"#include "vector.h"namespace VECTOR{    const double Rad_to_deg = 45.0 / atan(1.0);    Vector::Vector()    {        x = y = 0.0;        mode = RECT;    }    Vector::Vector(double n1, double n2, Mode form)    {        mode = form;        if (mode == RECT)        {            x = n1;            y = n2;        }        else if (mode == POL)        {            x = n1 * cos(n2 / Rad_to_deg);            y = n1 * sin(n2 / Rad_to_deg);        }        else        {            std::cout << "Incorrect 3rd argument to Vector(), Vector set to 0.\n";            x = y = 0.0;            mode = RECT;        }    }    Vector ::~Vector()    {    }    void Vector::reset(double n1, double n2, Mode form)    {        mode = form;        if (mode == RECT)        {            x = n1;            y = n2;        }        else if (mode == POL)        {            x = n1 * cos(n2 / Rad_to_deg);            y = n1 * sin(n2 / Rad_to_deg);        }        else        {            std::cout << "Incorrect 3rd argument to Vector(), Vector set to 0.\n";            x = y = 0.0;            mode = RECT;        }    }    double Vector::magval() const    {        double mag = sqrt(x * x + y * y);        return mag;    }    double Vector::angval() const    {        double ang = atan2(y, x);        return ang;    }    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);    }    Vector Vector::operator-(const Vector &v) const    {        return Vector(x - v.x, y - v.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 &v)    {        //return Vector(n * v.x, n * v.y);        return v * n;    }    std::ostream & operator<<(std::ostream &os, const Vector &v)    {        if (v.mode == Vector::RECT)            os << "(x, y) = (" << v.x << ", " << v.y << ").\n";        else if (v.mode == Vector::POL)            os << "(m, a) = (" << v.magval() << ", " << v.angval() << ").\n";        else            os << "Vector object mode is invalid";        return os;    }    Vector::operator double()    {        return magval();    }}//main.cpp#include "stdafx.h"#include "vector.h"#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;    cout << "Enter target distance (q to quit): ";    while (cin >> target)    {        cout << "Enter step length: ";        if (!(cin >> dstep))            break;        while (result.magval() < target)        {            direction = rand() % 360;            step.reset(dstep, direction, Vector::POL);            result = result + step;            steps++;        }        cout << "After " << steps << "steps, the subject has the following location:\n";        cout << result;        result.polar_mode();        cout << "or\n" << result;        cout << "Average outward distance per step = " << result.magval() / steps << endl;        double n = result;        cout << n << endl;        cout << double(step) << endl;        steps = 0;        result.reset(0.0, 0.0);        cout << "Enter target distance (q to quit): ";    }    cout << "bye!\n";    cin.clear();    while (cin.get() != '\n')        continue;    cin.get();    return 0;}

3.N次测试中最大、最小、平均步数

这里写图片描述

//vector.h#include <iostream>#ifndef VECTOR_H_#define VECTOR_H_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);        ~Vector();        void reset(double n1, double n2, Mode form = RECT);        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 n) const;        friend Vector operator*(double n, const Vector &v);        friend std::ostream & operator<<(std::ostream &os, const Vector &v);        operator double();    };}#endif//vector.cpp#include "stdafx.h"#include "vector.h"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 (mode == RECT)        {            x = n1;            y = n2;            set_mag();            set_ang();        }        else if (mode == POL)        {            mag = n1;            ang = n2 / Rad_to_deg;            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::reset(double n1, double n2, Mode form)    {        mode = form;        if (mode == RECT)        {            x = n1;            y = n2;            set_mag();            set_ang();        }        else if (mode == POL)        {            mag = n1;            ang = n2 / Rad_to_deg;            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::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);    }    Vector Vector::operator-(const Vector &v) const    {        return Vector(x - v.x, y - v.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 &v)    {        //return Vector(n * v.x, n * v.y);        return v * n;    }    std::ostream & operator<<(std::ostream &os, const Vector &v)    {        if (v.mode == Vector::RECT)            os << "(x, y) = (" << v.x << ", " << v.y << ").\n";        else if (v.mode == Vector::POL)            os << "(m, a) = (" << v.mag << ", " << v.ang << ").\n";        else            os << "Vector object mode is invalid";        return os;    }    Vector::operator double()    {        return mag;    }}//main.cpp#include "stdafx.h"#include "vector.h"#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;    unsigned long arr[30];    cout << "How many times do you want to test? ";    int n;    cin >> n;    for(int i = 0; i < n; i++)    {        cout << "#" << i + 1 << endl;        cout << "Enter target distance (q to quit): ";        while (cin >> target)        {            cout << "Enter step length: ";            if (!(cin >> dstep))                break;            while (result.magval() < target)            {                direction = rand() % 360;                step.reset(dstep, direction, Vector::POL);                result = result + step;                steps++;            }            cout << "After " << steps << "steps, the subject has the following location:\n";            cout << result;            result.polar_mode();            cout << "or\n" << result;            cout << "Average outward distance per step = " << result.magval() / steps << endl;            arr[i] = steps;            steps = 0;            result.reset(0.0, 0.0);            break;        }    }    unsigned long max, min;    unsigned long sum = 0;    double average;    max = arr[0];    for (int i = 1; i < n; i++)    {        if (arr[i] > max)            max = arr[i];    }    min = arr[0];    for (int i = 1; i < n; i++)    {        if (arr[i] < min)            min = arr[i];    }    for (int i = 0; i < n; i++)        sum += arr[i];    average = sum / n;    cout<< "\nThe maximum of steps: " << max << ", The minimum of steps: " << min << ", Average of steps: " << average << endl;    cout << "bye!\n";    cin.clear();    while (cin.get() != '\n')        continue;    cin.get();    return 0;}

4. Time类,用友元函数重载运算符

这里写图片描述

//mytime.h#include <iostream>#ifndef MYTIME_H_#define MYTIME_H_class Mytime{private:    int hours;    int minutes;public:    Mytime();    Mytime(int h, int m = 0);    void addmin(int m);    void addhr(int h);    void reset(int h, int m);    friend Mytime operator*(double n, const Mytime &mt);    friend std::ostream & operator<<(std::ostream & os, const Mytime & mt);    friend Mytime operator+(const Mytime &mt, const Mytime & mt1);    friend Mytime operator-(const Mytime &mt, const Mytime & mt1);    friend Mytime operator*(const Mytime &mt, double n);};#endif//mytime.cpp#include "stdafx.h"#include "mytime.h"#include <iostream>Mytime::Mytime(){    hours = minutes = 0;}Mytime::Mytime(int h, int m){    hours = h;    minutes = m;}void Mytime::addmin(int m){    minutes += m;    hours += (minutes / 60);    minutes %= 60;}void Mytime::addhr(int h){    hours += h;}Mytime operator*(double n, const Mytime &mt){    Mytime mult;    long timeminutes = mt.hours * n * 60 + mt.minutes * n;    mult.hours = timeminutes / 60;    mult.minutes = timeminutes % 60;    return mult;}std::ostream & operator<<(std::ostream & os, const Mytime & mt){    os << mt.hours << " hours, " << mt.minutes << " minutes";    return os;}Mytime operator+(const Mytime &mt, const Mytime & mt1){    Mytime sum;    sum.minutes = mt1.minutes + mt.minutes;    sum.hours = mt1.hours + mt.hours + sum.minutes / 60;    sum.minutes %= 60;    return sum;}Mytime operator-(const Mytime &mt, const Mytime & mt1){    Mytime diff;    int tot1, tot2;    tot1 = mt1.minutes + mt1.hours * 60;    tot2 = mt.minutes + mt.hours * 60;    diff.minutes = (tot1 - tot2) % 60;    diff.hours = (tot1 - tot2) /60;    return diff;}Mytime operator*(const Mytime &mt, double n){    Mytime mult;    long timeminutes = mt.hours * n * 60 + mt.minutes * n;    mult.hours = timeminutes / 60;    mult.minutes = timeminutes % 60;    return mult;}//main.cpp#include "stdafx.h"#include "mytime.h"#include <iostream>int main(){    using std::cout;    Mytime aida(3, 35);    Mytime tosca(2, 48);    Mytime temp;    cout << "Aida and Tosca: \n";    cout << aida << "; " << tosca << "\n";    temp = aida + tosca;    cout << "Aida + Tosca: " << temp << "\n";    temp = aida * 1.17;    cout << "Aida * 1.17: " << temp << "\n";    temp = 10.0 * tosca;    cout << "10.0 * Tosca: " << temp << "\n";    std::cin.get();    return 0;}

5. Stonewt类,状态成员,重载运算符

这里写图片描述

//stone.h#include <iostream>#ifndef STONEWT_H_#define STONEWT_H_class Stonewt{private:    enum {Lbs_per_stone = 14};    enum Mode{STN, LBS};    int stone;    double pds_lft;    double pounds;    Mode mode;public:    Stonewt();    Stonewt(double value, Mode form = STN);    ~Stonewt();    Stonewt operator*(double n);    friend Stonewt operator*(double n, const Stonewt &st);    friend std::ostream & operator<<(std::ostream &os, const Stonewt & st);    Stonewt operator+(const Stonewt & st);    Stonewt operator-(const Stonewt & st);    void stn_mode();    void lbs_mode();};#endif//stone.cpp#include "stdafx.h"#include "stone.h"#include <iostream>Stonewt::Stonewt(){    stone = pds_lft = pounds = 0;    mode = STN;}Stonewt::Stonewt(double value, Mode form){    mode = form;    if (form == STN)    {        stone = int(value);        pds_lft = (value - int(value)) * Lbs_per_stone;        pounds = value * Lbs_per_stone;    }    else if (form == LBS)    {        pounds = value;        stone = int(value) / Lbs_per_stone;        pds_lft = int(value) % Lbs_per_stone + value - int(value);    }    else    {        std::cout << "Incorrect 2rd argument to Stonewt(), Stonewt set to 0.\n";        stone = pounds = pds_lft = 0;        mode = LBS;    }}Stonewt::~Stonewt(){}Stonewt Stonewt::operator*(double n){    Stonewt mult;    double allpds = stone * Lbs_per_stone + pds_lft;    mult.stone = int(allpds * n) / Lbs_per_stone;    mult.pds_lft = int(allpds * n) % Lbs_per_stone + (allpds * n) - int(allpds * n);    mult.pounds = pounds * n;    return mult;}Stonewt operator*(double n, const Stonewt &st){    Stonewt mult;    double allpds = st.stone * Stonewt::Lbs_per_stone + st.pds_lft;    mult.stone = int(allpds * n) / Stonewt::Lbs_per_stone;    mult.pds_lft = int(allpds * n) % Stonewt::Lbs_per_stone + (allpds * n) - int(allpds * n);    mult.pounds = st.pounds * n;    return mult;}std::ostream & operator<<(std::ostream &os, const Stonewt & st){    if (st.mode == Stonewt::STN)        os << st.stone << " stones, " << st.pds_lft << " pounds\n";    else if (st.mode == Stonewt::LBS)        os << st.pounds << " pounds\n";    else        os << "Stonewt object mode is invalid\n";    return os;}Stonewt Stonewt::operator+(const Stonewt & st){    Stonewt add;    add.pounds = pounds + st.pounds;    add.stone = int(add.pounds) / Lbs_per_stone;    add.pds_lft = int(add.pounds) % Lbs_per_stone + add.pounds - int(add.pounds);    return add;}Stonewt Stonewt::operator-(const Stonewt & st){    Stonewt diff;    diff.pounds = pounds - st.pounds;    diff.stone = int(diff.pounds) / Lbs_per_stone;    diff.pds_lft = int(diff.pounds) % Lbs_per_stone + diff.pounds - int(diff.pounds);    return diff;}void Stonewt::stn_mode(){    mode = STN;}void Stonewt::lbs_mode(){    mode = LBS;}//main.cpp#include "stdafx.h"#include "stone.h"#include <iostream>void display(const Stonewt &st, int n);int main(){    using namespace std;    Stonewt st1(26.8);    Stonewt st2 (23.4);    Stonewt st3;    cout << "st1:\n";    cout << st1;    cout << "st2:\n";    st2.stn_mode();    cout << st2;    cout << "st1 + st2 = ";    st3 = st1 + st2;    cout << st3;    cout << "st1 - st2 = ";    st3 = st1 - st2;    cout << st3;    cout << "st1 * 1.7 = ";    st3 = st1 * 1.7;    cout << st3;    cout << "1.7 * st1 = ";    st3 = 1.7 * st1;    cout << st3;    cin.get();    return 0;}void display(const Stonewt &st, int n){    for (int i = 0; i < n; i++)    {        std::cout << "Wow! ";        std::cout << st;    }}

6.Stonewt类,重载关系运算符,对象之间比较

这里写图片描述

//stone.h#include <iostream>#ifndef STONEWT_H_#define STONEWT_H_class Stonewt{private:    double pounds;public:    Stonewt();    Stonewt(double pds);    ~Stonewt();    void show_pds() const;    bool operator>(const Stonewt & st);    bool operator<(const Stonewt & st);    bool operator>=(const Stonewt & st);    bool operator<=(const Stonewt & st);    bool operator==(const Stonewt & st);    bool operator!=(const Stonewt & st);    friend std::istream & operator>>(std::istream & is, Stonewt & st);};#endif//stone.cpp#include "stdafx.h"#include "stone.h"#include <iostream>Stonewt::Stonewt(){    pounds = 0;}Stonewt::Stonewt(double pds){    pounds = pds;}Stonewt::~Stonewt(){}void Stonewt::show_pds() const{    std::cout << pounds << " pounds\n";}bool Stonewt::operator>(const Stonewt & st){    if (pounds > st.pounds)        return true;    else        return false;}bool Stonewt::operator<(const Stonewt & st){    if (pounds < st.pounds)        return true;    else        return false;}bool Stonewt::operator>=(const Stonewt & st){    if (pounds >= st.pounds)        return true;    else        return false;}bool Stonewt::operator<=(const Stonewt & st){    if (pounds <= st.pounds)        return true;    else        return false;}bool Stonewt::operator==(const Stonewt & st){    if (pounds == st.pounds)        return true;    else        return false;}bool Stonewt::operator!=(const Stonewt & st){    if (pounds != st.pounds)        return true;    else        return false;}std::istream & operator>>(std::istream & is, Stonewt & st) //不能有const{    is >> st.pounds;    return is;}//main.cpp#include "stdafx.h"#include "stone.h"#include <iostream>int main(){    using namespace std;    Stonewt arr[6] = {10.1, 23.2, 11.3};    for (int i = 3; i < 6; i++)    {        cout << "#" << i + 1 << ": ";        cin >> arr[i];    }    Stonewt max = arr[0];    for (int i = 1; i < 6; i++)    {        if (arr[i] > max)            max = arr[i];    }    Stonewt min = arr[0];    for (int i = 1; i < 6; i++)    {        if (arr[i] < min)            min = arr[i];    }    Stonewt st1(11);    int count = 0;    for (int i = 0; i < 6; i++)    {        if (arr[i] >= st1)            count++;    }    cout << "Maximum: ";    max.show_pds();    cout << "Minimum: ";    min.show_pds();    cout << "numbers: " << count << endl;    cin.get();    cin.get();    return 0;}   

7.复数运算,输入,输出

这里写图片描述

//complex0.h#include <iostream>#ifndef COMPLEX0_H_#define COMPLEX0_H_class Complex0{private:    double real;    double imag;public:    Complex0();    Complex0(double re, double im);    ~Complex0();    Complex0 operator+(const Complex0 & cpx);    Complex0 operator-(const Complex0 & cpx);    Complex0 operator*(const Complex0 & cpx);    Complex0 operator-() const;    friend Complex0 operator*(double n, const Complex0 & cpx);    friend std::istream & operator>>(std::istream & is, Complex0 & cpx);    friend std::ostream & operator<<(std::ostream & os, const Complex0 & cpx);};#endif//complex0.cpp#include "stdafx.h"#include "complex0.h"Complex0::Complex0(){    real = imag = 0.0;}Complex0::Complex0(double re, double im){    real = re;    imag = im;}Complex0::~Complex0(){}Complex0 Complex0::operator+(const Complex0 & cpx){    /*Complex0 add;    add.real = real + cpx.real;    add.imag = imag + cpx.imag;    return add;*/    return Complex0(real + cpx.real, imag + cpx.imag);}Complex0 Complex0::operator-(const Complex0 & cpx){    /*Complex0 diff;    diff.real = real + cpx.real;    diff.imag = imag + cpx.imag;    return diff;*/    return Complex0(real - cpx.real, imag - cpx.imag);}Complex0 Complex0::operator*(const Complex0 & cpx){    /*Complex0 mult;    mult.real = real * cpx.real - imag * cpx.imag;    mult.imag = real * cpx.imag + imag * cpx.real;    return mult;*/    return Complex0(real * cpx.real - imag * cpx.imag, real * cpx.imag + imag * cpx.real);}Complex0 Complex0::operator-() const{    return Complex0(-real, -imag);}Complex0 operator*(double n, const Complex0 & cpx){    return Complex0(n * cpx.real, n * cpx.imag);}std::istream & operator>>(std::istream & is, Complex0 & cpx){    std::cout << "real: ";    is >> cpx.real;    std::cout << "imag: ";    is >> cpx.imag;    return is;}std::ostream & operator<<(std::ostream & os, const Complex0 & cpx){    os << "(" << cpx.real << ", " << cpx.imag << "i)\n";    return os;}//main.cpp#include "stdafx.h"#include "complex0.h"int main(){    using namespace std;    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";    }    cout << "Done!\n";    cin.get();    return 0;}

欢迎使用Markdown编辑器写博客

本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦:

  • Markdown和扩展Markdown简洁的语法
  • 代码块高亮
  • 图片链接和图片上传
  • LaTex数学公式
  • UML序列图和流程图
  • 离线写博客
  • 导入导出Markdown文件
  • 丰富的快捷键

快捷键

  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y

Markdown及扩展

Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档,然后转换成格式丰富的HTML页面。 —— [ 维基百科 ]

使用简单的符号标识不同的标题,将某些文字标记为粗体或者斜体,创建一个链接等,详细语法参考帮助?。

本编辑器支持 Markdown Extra ,  扩展了很多好用的功能。具体请参考Github.

表格

Markdown Extra 表格语法:

项目 价格 Computer $1600 Phone $12 Pipe $1

可以使用冒号来定义对齐方式:

项目 价格 数量 Computer 1600 元 5 Phone 12 元 12 Pipe 1 元 234

定义列表

Markdown Extra 定义列表语法:
项目1
项目2
定义 A
定义 B
项目3
定义 C

定义 D

定义D内容

代码块

代码块语法遵循标准markdown代码,例如:

@requires_authorizationdef somefunc(param1='', param2=0):    '''A docstring'''    if param1 > param2: # interesting        print 'Greater'    return (param2 - param1 + 1) or Noneclass SomeClass:    pass>>> message = '''interpreter... prompt'''

脚注

生成一个脚注1.

目录

[TOC]来生成目录:

  • 随机漫步位置写入文件
  • vector类由xy计算magang 公有接口不变
  • N次测试中最大最小平均步数
  • Time类用友元函数重载运算符
  • Stonewt类状态成员重载运算符
  • Stonewt类重载关系运算符对象之间比较
  • 复数运算输入输出
  • 欢迎使用Markdown编辑器写博客
    • 快捷键
    • Markdown及扩展
      • 表格
      • 定义列表
      • 代码块
      • 脚注
      • 目录
      • 数学公式
      • UML 图
    • 离线写博客
    • 浏览器兼容

数学公式

使用MathJax渲染LaTex 数学公式,详见math.stackexchange.com.

  • 行内公式,数学公式为:Γ(n)=(n1)!nN
  • 块级公式:

x=b±b24ac2a

更多LaTex语法请参考 这儿.

UML 图:

可以渲染序列图:

Created with Raphaël 2.1.0张三张三李四李四嘿,小四儿, 写博客了没?李四愣了一下,说:忙得吐血,哪有时间写。

或者流程图:

Created with Raphaël 2.1.0开始我的操作确认?结束yesno
  • 关于 序列图 语法,参考 这儿,
  • 关于 流程图 语法,参考 这儿.

离线写博客

即使用户在没有网络的情况下,也可以通过本编辑器离线写博客(直接在曾经使用过的浏览器中输入write.blog.csdn.net/mdeditor即可。Markdown编辑器使用浏览器离线存储将内容保存在本地。

用户写博客的过程中,内容实时保存在浏览器缓存中,在用户关闭浏览器或者其它异常情况下,内容不会丢失。用户再次打开浏览器时,会显示上次用户正在编辑的没有发表的内容。

博客发表后,本地缓存将被删除。 

用户可以选择 把正在写的博客保存到服务器草稿箱,即使换浏览器或者清除缓存,内容也不会丢失。

注意:虽然浏览器存储大部分时候都比较可靠,但为了您的数据安全,在联网后,请务必及时发表或者保存到服务器草稿箱

浏览器兼容

  1. 目前,本编辑器对Chrome浏览器支持最为完整。建议大家使用较新版本的Chrome。
  2. IE9以下不支持
  3. IE9,10,11存在以下问题
    1. 不支持离线功能
    2. IE9不支持文件导入导出
    3. IE10不支持拖拽文件导入


  1. 这里是 脚注内容. ↩
0 0