首页
登录 | 注册

C++ pprimer plus 第六版 第十一章 编程练习答案

第十一章 编程练习答案

1.

//头文件,同11-13不用修改
#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 
//类实现文件,同11-14不用修改
#include <cmath>
#include "vect.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\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 << "Incorrect 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;
    }

    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;
    }   
}
//主程序文件
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "vect.h"
int main()
{
    using namespace std;
    using VECTOR::Vector;
    srand(time(0));//设置随机数

    ofstream outFile;//声明对象 
    outFile.open(" random walker.txt");//建立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:\n";
        cout << result << endl;
        result.polar_mode();
        cout << " or\n" << result << endl;
        cout << "Average outward distance per step = "
            << result.magval() / steps << endl;

        //写入文件中 
        outFile << "After " << steps  << " steps, the subject "
            "has the following location:\n";
        outFile << result << endl;
        result.polar_mode();
        outFile << " or\n" << 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!\n";
    cin.clear();
    while(cin.get() != '\n')
        continue;
    return 0;
}
//生成的txt文件
Target Distance: 20, Step Size: 2
0: (x,y) = (0, 0)
1: (x,y) = (-0.938943, -1.7659)
2: (x,y) = (-1.35477, 0.1904)
3: (x,y) = (0.0594471, -1.22381)
4: (x,y) = (1.44876, -2.66249)
5: (x,y) = (3.4293, -2.38415)
6: (x,y) = (4.89201, -1.02015)
7: (x,y) = (6.8483, -1.43597)
8: (x,y) = (8.31101, -0.0719769)
9: (x,y) = (8.65831, -2.04159)
10: (x,y) = (7.90909, -0.187225)
11: (x,y) = (8.52713, 1.71489)
12: (x,y) = (6.92986, 2.91852)
13: (x,y) = (6.79034, 0.92339)
14: (x,y) = (6.72054, -1.07539)
15: (x,y) = (4.76425, -1.49121)
16: (x,y) = (3.04991, -0.461139)
17: (x,y) = (1.2679, -1.36912)
18: (x,y) = (-0.0960954, -2.83183)
19: (x,y) = (-0.0262964, -0.833045)
20: (x,y) = (-0.577571, 1.08948)
21: (x,y) = (-2.37516, 0.212736)
22: (x,y) = (-3.0919, -1.65443)
23: (x,y) = (-1.41455, -0.565147)
24: (x,y) = (-3.16379, 0.404472)
25: (x,y) = (-1.1787, 0.648211)
26: (x,y) = (-0.865832, 2.62359)
27: (x,y) = (1.13386, 2.65849)
28: (x,y) = (2.70988, 1.42717)
29: (x,y) = (1.86465, -0.385446)
30: (x,y) = (3.37407, 0.926672)
31: (x,y) = (4.28205, 2.70868)
32: (x,y) = (3.40531, 4.50627)
33: (x,y) = (2.37523, 6.22061)
34: (x,y) = (3.49362, 4.56253)
35: (x,y) = (1.49636, 4.6672)
36: (x,y) = (0.878323, 6.56932)
37: (x,y) = (1.32822, 8.51806)
38: (x,y) = (2.94626, 9.69363)
39: (x,y) = (4.94626, 9.69363)
40: (x,y) = (6.84837, 10.3117)
41: (x,y) = (8.50645, 9.19328)
42: (x,y) = (9.28791, 7.35227)
43: (x,y) = (8.31829, 9.10151)
44: (x,y) = (8.03994, 7.12097)
45: (x,y) = (6.0912, 7.57087)
46: (x,y) = (7.48052, 9.00955)
47: (x,y) = (9.46106, 8.73121)
48: (x,y) = (9.87688, 6.77491)
49: (x,y) = (10.7849, 4.9929)
50: (x,y) = (10.4032, 6.95615)
51: (x,y) = (11.28, 8.75374)
52: (x,y) = (13.2287, 9.20364)
53: (x,y) = (15.0413, 8.35841)
54: (x,y) = (16.7374, 7.29857)
55: (x,y) = (17.2213, 9.23916)
56: (x,y) = (16.8055, 7.28286)
57: (x,y) = (16.2878, 9.21471)
58: (x,y) = (15.6038, 7.33533)
59: (x,y) = (17.2218, 8.5109)
60: (x,y) = (19.1443, 7.95963)
After 60 steps, the subject has the following location:
(m,a) = (20.7331, 22.576)
 or
(m,a) = (20.7331, 22.576)
Average outward distance per step = 0.345552

2.

//头文件
#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();
        double angval();
        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 "vect1.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\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 << "Incorrect 3rd argument to Vector() -- ";
            cout << "Vector set to 0\n";
            x = y = mag = ang = 0.0;
            mode = RECT;
        }
    }

    Vector::~Vector()
    {
    }

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

    double Vector::angval()
    {
        return atan2(y, x);
    }

    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;
    }   
}
//主程序文件,同11-15不用修改
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vect1.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;
    cout << "Enter terget 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 << endl;
        result.polar_mode();
        cout << " or\n" << result << endl;
        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;
    return 0;
}

3.

//头文件,同11-13不用修改
#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 
//类实现文件,同11-14不用修改
#include <cmath>
#include "vect.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\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 << "Incorrect 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;
    }

    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;
    }   
}
//主程序文件
#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 << "\nTimes #" << i+1 << ":\n";
            cout << "After " << steps << " steps, the subject has the following location:\n";
            cout << result <<endl;
            result.polar_mode();
            cout << " or\n" << 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 << "\nTest Times: " << num << " average steps: "<< totalSteps / num << endl;
        cout << "Max steps: " << maxSteps << "\nMin steps: " << minSteps << endl;
        cout << "Enter target distance (q to quit): ";
    }
    cout << "Bye!\n";
    cin.clear();
    while (cin.get() != '\n') {
        continue;
    }
    return 0;
}

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:\n";
    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;   
}

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\n\n";     
    }
    else if (s.mode == 2)
    {
        os << s.pounds << " pounds\n\n";    
    }
    else if (s.mode == 1)
    {

        os << int (s.pounds + 0.5) << " pounds\n\n";    
    }
    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\n";
    return 0;
}

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

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\n\n";     
    }
    else if (s.mode == 2)
    {
        os << s.pounds << " pounds\n\n";    
    }
    else if (s.mode == 1)
    {

        os << int (s.pounds + 0.5) << " pounds\n\n";    
    }
    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: \n";
    for (int i= 0; i < 6; i++)
    {
        if (incognito[i] > stand)
            cout << "incognito[" << i <<"]: " << incognito[i];
    }   
    return 0;
}

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):\n";
    while (cin >> c)
    {
        cout << "c is " << c << '\n';
        cout << "complex conjugate is " << ~c << '\n';
        cout << "a is " << a << '\n';       
        cout << "a + c is " << a + c << '\n';       
        cout << "a - c is " << a - c << '\n';       
        cout << "a * c is " << a * c << '\n';       
        cout << "2 * c is " << 2 * c << '\n';       
        cout << "Enter a complex number (q to quit):\n";
    }
    cout << "Done!\n";
    return 0;       
}


2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号