C++Primer Plus习题记录-Chapter11

11-1

//headfile
#pragma once
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>

namespace VECTOR
{
    class Vector
    {
    public:
        enum Mode { RECT, POL };
        //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 // !VECTOR_H_

//realize file
#include "pch.h"
#include <cmath>
#include "vector.h"

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.0 / atan(1.0);
    //约为57.2958 

//私有函数  
    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
";
            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 << "Incorrect 3rd argument to Vector() -- ";
            cout << "Vector set to 0
";
            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)
    {
        //这里的operator<<()是友元函数,在名称空间中,但不在类定义中
        //使用要使用Vector::RECT     
        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;
    }
}

//main file
#include "pch.h"
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "vector.h"
using namespace std;

int main(void) {
    using VECTOR::Vector;
    srand(time(0));
    ofstream outFile;
    outFile.open("random walker.txt");
    double direction;
    Vector step;
    Vector result(0.0, 0.0);
    unsigned long steps = 0;
    double target;
    double dstep;
    cout << "Enter terget distance (q to quit): ";
    while (cin >> target)
    {
        cout << "Enter step length: ";
        if (!(cin >> dstep))
            break;

        //写入文件中 
        outFile << "Target Distance: " << target << ", Step Size: " << dstep << endl;
        outFile << 0 << ": " << result << endl;

        while (result.magval() < target)
        {
            direction = rand() % 360;
            step.reset(dstep, direction, Vector::POL);
            result = result + step;
            steps++;
            outFile << steps << ": " << result << endl;
        }

        //显示在屏幕上 
        cout << "After " << steps << " steps, the subject "
            "has the following location:
";
        cout << result << endl;
        result.polar_mode();
        cout << " or
" << result << endl;
        cout << "Average outward distance per step = "
            << result.magval() / steps << endl;

        //写入文件中 
        outFile << "After " << steps << " steps, the subject "
            "has the following location:
";
        outFile << result << endl;
        result.polar_mode();
        outFile << " or
" << result << endl;
        outFile << "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!
";
    cin.clear();
    while (cin.get() != '
')
        continue;
    return 0;
}
View Code

11-2

double Vector::magval()
    {
        return sqrt(x * x + y * y); 
    }

    double Vector::angval()
    {
        return atan2(y, x);
    }
//将11-1中magval和angval不在头文件中直接定义,放到实现文件中重新定义实现文件.

11-3

#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);//当前位置
    int steps = 0;//步数
    double target;//目标距离
    double dstep;//每一步的距离

    cout << "Enter target distance (q to quit): ";
    while (cin >> target)
    {
        cout << "Enter step length: ";
        if (!(cin >> dstep)) 
        {
            break;
        }
        cout << "Target Distance: " << target <<", Step Size: " << dstep <<endl;
        int num = 0;//测试次数
        cout << "Enter Test Times: ";//输入测试次数
        if (!(cin>> num) || num < 0) 
        {
            break;
        }

        int maxSteps = 0;
        int minSteps = 0;
        int totalSteps = 0;
        for (int i = 0; i < num; ++i)
        {
            //cout << 0 << ": " << result << endl;
            while(result.magval() < target)
            {
                //离起点的距离还没达到目标距离,就要继续执行
                direction = rand() % 360;//随机一个方向,角度
                step.reset(dstep, direction, Vector::POL);//当前的一步
                result = result + step;//当前的实际位置
                steps++;//计数
            //   cout << steps << ": " << result << endl;略去中间过程 
            }
            cout << "
Times #" << i+1 << ":
";
            cout << "After " << steps << " steps, the subject has the following location:
";
            cout << result <<endl;
            result.polar_mode();
            cout << " or
" << result << endl;
            cout << "Average outward distance per step = " << result.magval() / steps << endl;

            if (i == 0)
            {
                totalSteps = minSteps = maxSteps = steps;
            }
            else
            {
                totalSteps += steps;
                if (minSteps > steps) 
                    minSteps = steps;
                if (maxSteps < steps) 
                    maxSteps = steps;
            }
            steps = 0;
            result.reset(0, 0);
        }
        cout << "
Test Times: " << num << " average steps: "<< totalSteps / num << endl;
        cout << "Max steps: " << maxSteps << "
Min steps: " << minSteps << endl;
        cout << "Enter target distance (q to quit): ";
    }
    cout << "Bye!
";
    cin.clear();
    while (cin.get() != '
') {
        continue;
    }
    return 0;
}
//类定义和实现文件不改动,修改主函数如下即可

11-4

//头文件
#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);
        friend Time operator*(const Time & a, double b);                 
        friend Time operator*(double m, const Time & t)
            { return t * m; }   //内联函数      

        friend std::ostream & operator << (std::ostream & os, const Time & t);  
};

#endif
//类实现文件
#include "U11p4mytime.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;
}

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

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

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 = b.minutes + 60 * b.hours;    
    tot2 = a.minutes + 60 * a.hours;        
    diff.minutes = (tot2 - tot1) % 60;
    diff.hours = (tot2 - tot1) / 60;
    return diff;
}

Time operator*(const Time & a, double b)
{
    Time result;
    int total;
    total = a.hours * 60 * b + a.minutes * b;
    result.hours = total / 60;
    result.minutes = total % 60;
    return result;
}

std::ostream & operator<< (std::ostream & os, const Time & t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os;
}
//主程序文件
#include <iostream>
#include "U11p4mytime.h"

int main()
{
    using std::cout;
    using std::endl;
    Time aida(3, 35);
    Time tosca(2, 48);
    Time temp;

    cout << "Aida and Tosca:
";
    cout << aida << "; " << tosca << endl;
    temp = aida + tosca;//operator+()
    cout << "Aida + Tosca: " << temp << endl;
    temp = aida - tosca;//operator-()
    cout << "Aida - Tosca: " << temp << endl;
    temp = aida * 1.17;     
    cout << "Aida * 1.17: " << temp << endl;    
    cout << "10.0 * Aida: " << 10.0 * tosca << endl;    

    return 0;   
}

11-5

//头文件
#ifndef STONEWT_H_
#define STONEWT_H_

enum Mode{stoneps, intps, floatps};

class Stonewt
{   
    private:
        enum{Lbs_per_stn = 14};
        int stone;
        double pds_left;
        double pounds;
        int mode;
//这里是从0-2计数,第一个数为0 
//重载运算符时用int mode

    public:
        void setmode(Mode m);       
        Stonewt(double lbs);
        Stonewt(int stn, double lbs);
        Stonewt();
        ~Stonewt();

        friend Stonewt operator+(const Stonewt & a, const Stonewt & b);
        friend Stonewt operator-(const Stonewt & a, const Stonewt & b);
        friend Stonewt operator*(const Stonewt & a, double b);               
        friend Stonewt operator*(double a, const Stonewt & b); 
        friend std::ostream & operator << (std::ostream & os, const Stonewt & s);   
};

#endif
//类实现文件
#include <iostream>

#include "U11p5vector.h"
using std::cout;

void Stonewt::setmode(Mode m)
{
    mode = m;
}

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

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

Stonewt::Stonewt()
{
    mode = 2;
    stone = pounds = pds_left = 0;
}

Stonewt::~Stonewt()
{
}

Stonewt operator+(const Stonewt & a, const Stonewt & b)
{
    Stonewt t;
    if (a.mode == 0)
    {
        t.pds_left= a.pds_left + b.pds_left;        
        t.stone = a.stone + b.stone + int (t.pds_left) / Stonewt::Lbs_per_stn;
        t.pds_left = int (t.pds_left) % Stonewt::Lbs_per_stn + t.pds_left - int(t.pds_left);
        t.mode = 0;         
    }
    else if (a.mode == 1)
    {
        t.pounds = a.pounds + b.pounds;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1;
    }       
    else
    {
        t.pounds = a.pounds + b.pounds;
        t.mode = 2;
    }

    return t;       
}

Stonewt operator-(const Stonewt & a, const Stonewt & b)
{
    Stonewt t;
    if (a.mode == 0)
    {
        double t1, t2;
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
        t.stone = int ((t1 - t2) / Stonewt::Lbs_per_stn);
        t.pds_left = (int (t1- t2) % Stonewt::Lbs_per_stn) + t1 - t2 - int(t1 - t2);
        t.mode = 0;     
    }
    else if (a.mode == 2)
    {
        t.pounds = a.pounds - b.pounds;     
        t.mode = 2;         
    }
    else
    {
        t.pounds = a.pounds - b.pounds;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1; 
    }
    return t;       
}

Stonewt operator*(const Stonewt & a, double b)
{
    Stonewt t;
    if (a.mode == 0)
    {
        t.pounds = a.pounds * b;        
        t.stone = a.stone * b + int (t.pounds) / Stonewt::Lbs_per_stn;
        t.pds_left = int (t.pounds) % Stonewt::Lbs_per_stn + t.pounds - int(t.pounds);      
        t.mode = 0; 
    }
    else if (a.mode == 2)
    {
        t.pounds = a.pounds * b;
        t.mode = 2;
    }
    else
    {
        t.pounds = a.pounds * b;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1;
    }
    return t;   
}            

Stonewt operator*(double a, const Stonewt & b)
{
    Stonewt t;
    if (b.mode == 0)
    {
        t.pounds = b.pounds * a;        
        t.stone = b.stone * a + int (t.pounds) / Stonewt::Lbs_per_stn;
        t.pds_left = int (t.pounds) % Stonewt::Lbs_per_stn + t.pounds - int(t.pounds); 
        t.mode = 0; 

    }
    else if (b.mode == 2)
    {
        t.pounds = b.pounds * a;
        t.mode = 2; 
    }
    else
    {
        t.pounds = b.pounds * a;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1; 
    }
    return t;
}

std::ostream & operator << (std::ostream & os, const Stonewt & s)
{
    if (s.mode == 0)
    {
        os << s.stone << " stone, " << s.pds_left << " pounds

";     
    }
    else if (s.mode == 2)
    {
        os << s.pounds << " pounds

";    
    }
    else if (s.mode == 1)
    {

        os << int (s.pounds + 0.5) << " pounds

";    
    }
    else
    {
        os << "Stonewt object mode is invalid";
    }   
}
//主程序文件
#include <iostream>
using std::cout;
#include "U11p5vector.h"

void display(const Stonewt & st, int n);
int main()
{
    Stonewt incognito;
    Stonewt wolfe(285.9);
    Stonewt alex(334.4);
    cout << "The incognito weighed: ";  
    cout << incognito;
    cout << "The alex weighed: ";
    cout << alex;       
    cout << "The wolfe weighed: ";      
    cout << wolfe;
    cout << "Output in int:";
    wolfe.setmode(intps);
    cout << wolfe;
    cout << "Output in stone:";
    wolfe.setmode(stoneps);
    cout << wolfe;

//这里的输出格式由第一个参数的形式决定    
    cout << "The ( alex + wolfe ) weighed: ";
    cout << alex + wolfe; 

    cout << "The ( alex - wolfe ) weighed: ";
    cout << alex - wolfe;

    cout << "The wolfe * 2 weighed: ";  
    cout << wolfe * 2.0;//wolfe的形式已经变成英石磅的形式    

    cout << "The  2 * alex weighed: ";
    cout << 2.0 * alex;     

    incognito = 325.2;
    cout << "After dinner, the President weighed: ";
    cout << incognito;
    display(incognito, 2);
    cout << "No stone left unearned
";
    return 0;
}

void display(const Stonewt & st, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << "Wow! ";
        cout << st;
    }
}
View Code

11-6

//头文件
#ifndef STONEWT_H_
#define STONEWT_H_

enum Mode{stoneps, intps, floatps};

class Stonewt
{   
    private:
        enum{Lbs_per_stn = 14};
        int stone;
        double pds_left;
        double pounds;
        int mode;
//这里是从0-2计数,第一个数为0 
//重载运算符时用int mode

    public:
        void setmode(Mode m);       
        Stonewt(double lbs);
        Stonewt(int stn, double lbs);
        Stonewt();
        ~Stonewt();

        friend Stonewt operator+(const Stonewt & a, const Stonewt & b);
        friend Stonewt operator-(const Stonewt & a, const Stonewt & b);
        friend Stonewt operator*(const Stonewt & a, double b);
        friend Stonewt operator*(double a, const Stonewt & b);

        friend bool operator>(const Stonewt & a, const Stonewt & b);        
        friend bool operator<(const Stonewt & a, const Stonewt & b);
        friend bool operator>=(const Stonewt & a, const Stonewt & b);
        friend bool operator<=(const Stonewt & a, const Stonewt & b);
        friend bool operator==(const Stonewt & a, const Stonewt & b);
        friend bool operator!=(const Stonewt & a, const Stonewt & b);

        friend std::ostream & operator << (std::ostream & os, const Stonewt & s);   
};

#endif
//类实现文件
#include <iostream>

#include "U11p6vector.h"
using std::cout;

void Stonewt::setmode(Mode m)
{
    mode = m;
}

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

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

Stonewt::Stonewt()
{
    mode = 2;
    stone = pounds = pds_left = 0;
}

Stonewt::~Stonewt()
{
}

Stonewt operator+(const Stonewt & a, const Stonewt & b)
{
    Stonewt t;
    if (a.mode == 0)
    {
        t.pds_left= a.pds_left + b.pds_left;        
        t.stone = a.stone + b.stone + int (t.pds_left) / Stonewt::Lbs_per_stn;
        t.pds_left = int (t.pds_left) % Stonewt::Lbs_per_stn + t.pds_left - int(t.pds_left);
        t.mode = 0;         
    }
    else if (a.mode == 1)
    {
        t.pounds = a.pounds + b.pounds;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1;
    }       
    else
    {
        t.pounds = a.pounds + b.pounds;
        t.mode = 2;
    }

    return t;       
}

Stonewt operator-(const Stonewt & a, const Stonewt & b)
{
    Stonewt t;
    if (a.mode == 0)
    {
        double t1, t2;
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
        t.stone = int ((t1 - t2) / Stonewt::Lbs_per_stn);
        t.pds_left = (int (t1- t2) % Stonewt::Lbs_per_stn) + t1 - t2 - int(t1 - t2);
        t.mode = 0;     
    }
    else if (a.mode == 2)
    {
        t.pounds = a.pounds - b.pounds;     
        t.mode = 2;         
    }
    else
    {
        t.pounds = a.pounds - b.pounds;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1; 
    }
    return t;       
}

Stonewt operator*(const Stonewt & a, double b)
{
    Stonewt t;
    if (a.mode == 0)
    {
        t.pounds = a.pounds * b;        
        t.stone = a.stone * b + int (t.pounds) / Stonewt::Lbs_per_stn;
        t.pds_left = int (t.pounds) % Stonewt::Lbs_per_stn + t.pounds - int(t.pounds);      
        t.mode = 0; 
    }
    else if (a.mode == 2)
    {
        t.pounds = a.pounds * b;
        t.mode = 2;
    }
    else
    {
        t.pounds = a.pounds * b;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1;
    }
    return t;   
}            

Stonewt operator*(double a, const Stonewt & b)
{
    Stonewt t;
    if (b.mode == 0)
    {
        t.pounds = b.pounds * a;        
        t.stone = b.stone * a + int (t.pounds) / Stonewt::Lbs_per_stn;
        t.pds_left = int (t.pounds) % Stonewt::Lbs_per_stn + t.pounds - int(t.pounds); 
        t.mode = 0; 

    }
    else if (b.mode == 2)
    {
        t.pounds = b.pounds * a;
        t.mode = 2; 
    }
    else
    {
        t.pounds = b.pounds * a;
        t.pounds = int (t.pounds + 0.5);
        t.mode = 1; 
    }
    return t;
}

bool operator>(const Stonewt & a, const Stonewt & b)
{
    double t1, t2;

    if (a.mode == 0)
    {
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;   
    }
    else
    {
        t1 = a.pounds;          
    }

    if (b.mode == 0)
    {
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
    }
    else
    {
        t2 = b.pounds;          
    }
    return t1 > t2; 
}

bool operator<(const Stonewt & a, const Stonewt & b)
{
    double t1, t2;  
    if (a.mode == 0)
    {
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;   
    }
    else
    {
        t1 = a.pounds;          
    }

    if (b.mode == 0)
    {
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
    }
    else
    {
        t2 = b.pounds;          
    }
    return t1 < t2; 
}

bool operator>=(const Stonewt & a, const Stonewt & b)
{
    double t1, t2;  
    if (a.mode == 0)
    {
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;   
    }
    else
    {
        t1 = a.pounds;          
    }

    if (b.mode == 0)
    {
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
    }
    else
    {
        t2 = b.pounds;          
    }
    return t1 >= t2;    
}


bool operator<=(const Stonewt & a, const Stonewt & b){
    double t1, t2;  
    if (a.mode == 0)
    {
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;   
    }
    else
    {
        t1 = a.pounds;          
    }

    if (b.mode == 0)
    {
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
    }
    else
    {
        t2 = b.pounds;          
    }
    return t1 <= t2;    
}


bool operator==(const Stonewt & a, const Stonewt & b){
    double t1, t2;  
    if (a.mode == 0)
    {
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;   
    }
    else
    {
        t1 = a.pounds;          
    }

    if (b.mode == 0)
    {
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
    }
    else
    {
        t2 = b.pounds;          
    }
    return t1 == t2;    
}


bool operator!=(const Stonewt & a, const Stonewt & b)
{
    {
    double t1, t2;  
    if (a.mode == 0)
    {
        t1 = a.stone * Stonewt::Lbs_per_stn + a.pds_left;   
    }
    else
    {
        t1 = a.pounds;          
    }

    if (b.mode == 0)
    {
        t2 = b.stone * Stonewt::Lbs_per_stn + b.pds_left;   
    }
    else
    {
        t2 = b.pounds;          
    }
    return t1 != t2;    
}
}


std::ostream & operator << (std::ostream & os, const Stonewt & s)
{
    if (s.mode == 0)
    {
        os << s.stone << " stone, " << s.pds_left << " pounds

";     
    }
    else if (s.mode == 2)
    {
        os << s.pounds << " pounds

";    
    }
    else if (s.mode == 1)
    {

        os << int (s.pounds + 0.5) << " pounds

";    
    }
    else
    {
        os << "Stonewt object mode is invalid";
    }   
}
//主程序文件
#include <iostream>
#include "U11p6vector.h"

using std::cout;
using std::cin;

int main()
{
    Stonewt incognito[6];
    incognito[0] = 285.9;
    incognito[1] = 235.6;   
    incognito[2] = 262.4;   
    for (int i= 3; i < 6; i++)
    {
        double n;
        cout << "Please enter the number #" << i+1 << ": ";
        cin >> n;
        incognito[i] = n;   
    }

    Stonewt stand(11, 0);
    Stonewt max;
    Stonewt min;
    int imax;
    int imin;
    max = min = incognito[0];
    imax = imin = 0;

    for (int i= 0; i < 6; i++)
    {
        if (min > incognito[i])
        {
            min = incognito[i];         
            imin = i;
        }
        if (max < incognito[i])
        {
            max = incognito[i];
            imax = i;
        }       
    }

    cout << "The max is: " << "incognito[" << imax <<"]: " << max;
    cout << "The min is: " << "incognito[" << imin <<"]: " << min;      
    cout << "The weight more than 11 stone is: 
";
    for (int i= 0; i < 6; i++)
    {
        if (incognito[i] > stand)
            cout << "incognito[" << i <<"]: " << incognito[i];
    }   
    return 0;
}
View Code

11-7

//头文件
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include <iostream>

class complex
{
    private:
        double x;
        double y;       
    public:
        complex();
        complex(double n1, double n2);
        void reset(double n1, double n2);
        ~complex();
//友元函数重载 
        friend complex operator+(const complex & a, const complex & b);
        friend complex operator-(const complex & a, const complex & b);
        friend complex operator*(const complex & a, const complex & b);
        friend complex operator*(double a, const complex & b);
        friend complex operator~(const complex & a);
        friend std::ostream & operator << (std::ostream & os, const complex & c);       
        friend std::istream & operator >> (std::istream & ins, complex & c);//输入时,会产生改变,不用const
};  

#endif 
//类实现文件
#include <iostream>
#include "U11p7complex0.h"

using std::cout;

complex::complex()
{
    x = y = 0;
}

complex::complex(double n1, double n2)
{
    x = n1;
    y = n2;
}

void complex::reset(double n1, double n2)
{
    x = n1;
    y = n2;
}

complex::~complex()
{
}

//友元函数重载 
complex operator+(const complex & a, const complex & b)
{
    complex t;
    t.x = a.x + b.x;
    t.y = a.y + b.y;
    return t;   
}

complex operator-(const complex & a, const complex & b)
{
    complex t;
    t.x = a.x - b.x;
    t.y = a.y - b.y;
    return t;   
}

complex operator*(const complex & a, const complex & b)
{
    complex t;
    t.x = a.x * b.x - a.y * b.y;
    t.y = a.x * b.y + a.y * b.x;
    return t;   
}

complex operator*(double a, const complex & b)
{
    complex t;
    t.x = a * b.x;
    t.y = a * b.y;
    return t;
}

complex operator~(const complex & a)
{
    complex t;
    t.x = a.x;
    t.y = -a.y;
    return t;
}

std::istream & operator >> (std::istream & ins, complex & c)
{
    std::cout << "real: ";
    if(!(ins >> c.x))
        return ins;
    std::cout << "imaginary: ";
    ins >> c.y;
    return ins;
}

std::ostream & operator << (std::ostream & os, const complex & c)
{
    os << "(" << c.x << ", " << c.y << "i)";
    return os;          
}
//主文件
#include <iostream>
using namespace std;
#include "U11p7complex0.h"

int main()
{
    complex a(3.0, 4.0);
    complex c;

    cout << "Enter a complex number (q to quit):
";
    while (cin >> c)
    {
        cout << "c is " << c << '
';
        cout << "complex conjugate is " << ~c << '
';
        cout << "a is " << a << '
';       
        cout << "a + c is " << a + c << '
';       
        cout << "a - c is " << a - c << '
';       
        cout << "a * c is " << a * c << '
';       
        cout << "2 * c is " << 2 * c << '
';       
        cout << "Enter a complex number (q to quit):
";
    }
    cout << "Done!
";
    return 0;       
}
原文地址:https://www.cnblogs.com/lightmonster/p/10451270.html