第十章 编程练习4-7

编程练习4

//MyTime.h
#ifndef MYTIME_H_
#define MYTIME_H_
#include <iostream>
class Time
{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h, int m);
    ~Time();
    void AddMin(int m);
    void AddHr (int h);
    void Reset (int h = 0, int m = 0);
    friend Time operator+(const Time & t1, const Time & t2);
    friend Time operator-(const Time & t1, const Time & t2);
    friend Time operator*(const Time & t,double n);
    friend Time operator*(double n, const Time & t) { return t * n;}
    friend std::ostream & operator << (std::ostream & os, const Time & t);
};
#endif
//Time.cpp
#include "MyTime.h"
#include <iostream>

Time::Time()
{
    hours = minutes = 0;
}

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

Time::~Time(void)
{
}

void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes/60;
    minutes %= 60;
}
void Time::AddHr (int h)
{
    hours += h;
}
void Time::Reset (int h, int m)
{
    hours = h;
    minutes = m;
}
Time operator+(const Time & t1, const Time & t2)
{
    Time sum;
    sum.minutes = t1.minutes + t2.minutes;
    sum.hours = t1.hours + t2.hours + sum.minutes/60;
    sum.minutes %= 60;
    return sum;
}

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

Time operator*(const Time & t,double n)
{
    Time result;
    long totalminutes = t.hours * n * 60 + t.minutes * n;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}
std::ostream & operator << (std::ostream & os, const Time & t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os;
}
//main.cpp
#include <iostream>
#include "MyTime.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;
    cout << "Aida + Tosca: " << temp << endl;
    temp = aida - tosca;
    cout << "Aida - Tosca: " << temp << endl;
    temp = aida * 1.17;
    cout << "Aida * 1.17: " << temp << endl;
    cout << "10 * Tosca: " << 10*tosca << endl;
    system("pause");
    return 0;
}

编程练习5

//Stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>
class Stonewt
{
private:
    enum {Lbs_per_stn = 14};
    int stone;
    double pds_left;
    double pounds;
public:
    Stonewt(void);
    ~Stonewt(void);
    Stonewt(double lbs);
    Stonewt(int stn, double lbs);
    void show_lbs() const;
    void show_stn() const;
    Stonewt operator+(const Stonewt &s) const;
    Stonewt operator-(const Stonewt &s) const;
    Stonewt operator*(double n) const;
    friend Stonewt operator*(double n, const Stonewt & s) { return s * n;}
    friend std::ostream& operator<<(std::ostream &os,const Stonewt &s);
};
#endif
//Stonewt.cpp
#include "Stonewt.h"
#include <iostream>
using namespace std;
Stonewt::Stonewt()
{
    stone = pounds = pds_left = 0;
}

Stonewt::~Stonewt()
{
}

Stonewt::Stonewt(double lbs)
{
    stone = int(lbs) / Lbs_per_stn;
    pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
    pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs)
{
    stone = stn;
    pds_left = lbs;
    pounds = stn * Lbs_per_stn + lbs;
}

void Stonewt::show_lbs() const
{
    cout << stone << " stone, " << pds_left << " pounds\n";
}
void Stonewt::show_stn() const
{
    cout << pounds << " pounds\n";
}

Stonewt Stonewt::operator+(const Stonewt &s) const
{
    double newpounds=pounds+s.pounds;  
    return Stonewt(newpounds); 
}
Stonewt Stonewt::operator-(const Stonewt &s) const
{
    double newpounds=pounds-s.pounds;  
    return Stonewt(newpounds);  
}
Stonewt Stonewt::operator*(double n) const
{
    double newpounds=pounds*n;  
    return Stonewt(newpounds);  
}

std::ostream& operator<<(std::ostream &os,const Stonewt &s)  
{       
    os<<s.stone<<"  stone!"<<endl;  
    os<<s.pounds+s.pds_left<<"  pounds!"<<endl;  
    return os;  
} 
//main.cpp
#include <iostream>  
#include "stonewt.h"  
using namespace std;  
int main()  
{  
    Stonewt s1;  
    Stonewt s2(10,5);  
    Stonewt s3(1.5);  

    cout<<s1<<endl;  
    cout<<s2<<endl;  
    cout<<s3<<endl;  

    Stonewt s4=s2-s3;  
    cout<<s4<<endl;  
    cout << s4*2 << endl;
    cout << 2*s2 << endl;
    cout << s2+s3<< endl;

    system("pause");
    return 0; 
}  

编程练习6
这道题其实很简单,就把重载函数部分贴到这吧

bool operator>(const Stonewt & s) {return pounds>s.pounds;}
bool operator<(const Stonewt & s) {return pounds<s.pounds;}
bool operator==(const Stonewt & s){return pounds==s.pounds;}
bool operator>=(const Stonewt & s){return pounds>=s.pounds;}
bool operator<=(const Stonewt & s){return pounds<=s.pounds;}
bool operator!=(const Stonewt & s) {return pounds!=s.pounds;}

编程练习7
主函数跟书上一样,这里就只贴类函数来

//Mycomplex.h
#ifndef MYCOMPLEX_H_
#define MYCOMPLEX_H_
#include <iostream>
class Mycomplex
{
private:
    double real;
    double imaginary;
public:
    Mycomplex();
    ~Mycomplex();
    Mycomplex(double r, double i);

    Mycomplex operator+(const Mycomplex &c) const;
    Mycomplex operator-(const Mycomplex &c) const;
    Mycomplex operator*(const Mycomplex &c) const;
    friend Mycomplex operator*(double x, const Mycomplex &c);
    friend Mycomplex operator~(const Mycomplex &c);
    friend std::ostream& operator<<(std::ostream &os,const Mycomplex &c);  
    friend std::istream& operator>>(std::istream &is,Mycomplex &c); 
};
#endif
//Mycomplex.h
#include "Mycomplex.h"

Mycomplex::Mycomplex()
{
    real = imaginary = 0.0;
}


Mycomplex::~Mycomplex(void)
{
}

Mycomplex::Mycomplex(double r, double i)
{
    real = r;
    imaginary = i;
}

Mycomplex Mycomplex::operator+(const Mycomplex &c) const
{
    return Mycomplex(real+c.real, imaginary+c.imaginary);
}
Mycomplex Mycomplex::operator-(const Mycomplex &c) const
{
    return Mycomplex(real-c.real, imaginary-c.imaginary);
}
Mycomplex Mycomplex::operator*(const Mycomplex &c) const
{
    return Mycomplex(real*c.real-imaginary*c.imaginary, real*c.imaginary+imaginary*c.real);
}

Mycomplex operator*(double x, const Mycomplex &c)
{
    return Mycomplex(x*c.real, x*c.real);
}
Mycomplex operator~(const Mycomplex &c)
{
    return Mycomplex(c.real, -c.imaginary);
}

std::ostream& operator<<(std::ostream &os,const Mycomplex &c)  
{       
    os << "(" << c.real << ", " << c.imaginary << "i)";  
    return os;  
} 

std::istream& operator>>(std::istream &is,Mycomplex &c)
{
    using std::cout;
    using std::endl;
    cout << "real: ";  
    double r;  
    is >> r;  
    cout << "imaginary: ";  
    double i;  
    is >> i;  
    c.real = r;  
    c.imaginary = i;  
    return is; 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值