C++ Primer Plus v6 Chapeter 11 exercise 5&&6

write in advance

因为五六题用的是同一个类,且彼此之间并不冲突。所以我在书上提供代码的基础上,同时进行五六题。

要点在head file和method file, main file文件中主要是使用自己定义好的类和方法,使用循环(for/while)进行方法的验证。在main file文件中,我定义了两个数组,实际上可以使用new 动态分配内存空间,更节省内存,更高效,此处仅仅作为测试类的正确程度,所以使用比较笨拙的方法。

在head file中,我将 static int nums放在了 public部分,因为我的的运算符重载主要用以比较两个Stonewt类的大小,并不是每次比较都需要递增nums。而除去方法和友元,我们无法访问private部分。

注意static 修饰符修饰的变量在head 文件中声明, 在method 文件中定义,定义时不需要static 关键字,需要类解析符。

因为比较的是double 类型的数值,计算机存储浮点数的原因,不能直接用> , < 进行比较。需要设置一个值,此处该值取 1e-6。在不同精度要求下,该值需要根据具体精度要求更改。

重载比较运算符的部分, 在>= <= 部分,我在内部调用了已经写好的 > < ==, 但需要注意==部分的正负号。

head file

#ifndef STONEWT_H_
#define STONEWT_H_
#include<iostream>
class Stonewt
{
private:
    enum{stone_form ,pounds_form ,Lbs_per_stn = 14};
    int stone;
    double pds_left;
    double pounds;
    int state;        // 0:int pounds, 1:
public:
    static int nums;    // static variable declaration
    Stonewt(double lbs);    // constructors take a single argument convert a value of the argument type to the class type
    Stonewt(int stn, double lbs);
    Stonewt();    //default constructor
    ~Stonewt();
    void show_lbs() const;
    void show_stn() const;
    operator int() const;  // conversion function, convert a object to a particular type, the type here is <int> 
    operator double() const;

    Stonewt operator+(const Stonewt& st2) const;
    Stonewt operator-(const Stonewt& st2) const;
    Stonewt operator*(const Stonewt& st2) const;
    Stonewt operator/(const Stonewt& st2) const;
    bool operator>(const Stonewt& st2) const;
    bool operator<(const Stonewt& st2) const;
    bool operator>=(const Stonewt& st2) const;
    bool operator<=(const Stonewt& st2) const;
    bool operator==(const Stonewt& st2) const;
    bool operator!=(const Stonewt& st2) const;
    Stonewt& operator+=(const Stonewt& st2);
    Stonewt& operator-=(const Stonewt& st2);
    Stonewt& operator*=(const Stonewt& st2);
    Stonewt& operator/=(const Stonewt& st2);

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

#endif

method file

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

int Stonewt::nums = 0; // static value definition

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

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

Stonewt::Stonewt()
{
    stone = pds_left = pounds = 0;
    state = stone_form;
}

Stonewt::~Stonewt()
{

}

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

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

Stonewt::operator int() const
{
    return int(pounds + 0.5); // good idea, not truncation, return the closet inter value
}

Stonewt::operator double() const
{
    return pounds;
}
Stonewt Stonewt::operator+(const Stonewt& st2) const
{
    Stonewt result(pounds+ st2.pounds);
    return result;
}
Stonewt Stonewt::operator-(const Stonewt& st2) const
{
    Stonewt temp(pounds - st2.pounds);
    return temp;
}
Stonewt Stonewt::operator*(const Stonewt& st2) const
{
    Stonewt temp(pounds * st2.pounds);
    return temp;
}
Stonewt Stonewt::operator/(const Stonewt& st2) const
{
    if (double(st2) - 0 < 1e-6)
    {    
        std::cout << "cannot / value 0!\n";
        return *this;
    }
    Stonewt temp(pounds / st2.pounds);
    return temp;
}


bool Stonewt::operator<(const Stonewt& st2) const
{
    if (stone > st2.stone)
        return false;
    return (pds_left - st2.pds_left < 1e-6);
}
bool Stonewt::operator>(const Stonewt& st2) const
{
    if (stone < st2.stone)
        return false;
    return (pounds - st2.pounds) > 1e-6;
}
bool Stonewt::operator<=(const Stonewt& st2) const
{
    if (stone > st2.stone)
        return false;
    return (pds_left - st2.pds_left) <= 1e-6;
    //return this->operator<(st2) || this->operator==(st2);
}
bool Stonewt::operator>=(const Stonewt& st2) const
{
    return this->operator>(st2) || this->operator==(st2);
}
bool Stonewt::operator==(const Stonewt& st2) const
{
    return (stone == st2.stone) && abs(pounds - st2.pounds) < 1e-6;
}
bool Stonewt::operator!=(const Stonewt& st2) const
{
    return stone != st2.stone || pounds - st2.pounds > 1e-6;
}

Stonewt& Stonewt::operator+=(const Stonewt& rg)
{
    *this = *this + rg;
    return *this;
}
Stonewt& Stonewt::operator-=(const Stonewt& rg)
{
    *this = *this - rg;
    return *this;
}
Stonewt& Stonewt::operator*=(const Stonewt& rg)
{
    *this = *this * rg;
    return *this;
}
Stonewt& Stonewt::operator/=(const Stonewt& rg)
{
    *this = *this / rg;
    return *this;
}

std::ostream& operator<<(std::ostream& os,  const Stonewt& st)
{
    switch (st.state) 
    {
        case 0: os << st.stone << " stone," << st.pds_left << " pounds";    break;
        case 1: os << st.pounds << " pounds"; break;
        default:;
    }
    return os;
}

main file

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

int main()
{
    using std::cout;
    Stonewt poppins(9, 2.8);
    Stonewt lalala(6.66);
    Stonewt line(4);    // how many is bigger than it
    Stonewt largest(0);
    Stonewt smallest(0);

    Stonewt arr[6] = {3.14, 6.28,10.12,};
    Stonewt matharr[4] = { 1,1,1,1 };

    for (int i = 3; i < 6; i++)
        arr[i] = Stonewt::Stonewt(i);    // assignment operation
    for (int i = 0; i < 6; i++)
    {
        largest = largest > arr[i] ? largest : arr[i];
        smallest = smallest < arr[i] ? smallest : arr[i];
        if (arr[i] >= line)
            Stonewt::nums++;
        std::cout << arr[i] << " ";
    }
    std::cout << "\nshow array done!\n\n";
    std::cout << "largest:" << largest << ", smallest:" << smallest
        << ", " << Stonewt::nums << " elements >= line 4\n";

    for (int i = 0; i < 6; i++)
    {
        matharr[0] += arr[i];
        matharr[1] -= arr[i];
        matharr[2] *= arr[i];
        matharr[3] /= arr[i];
    }
    for (int i = 0; i < 4; i++)
        std::cout << matharr[i] << ";    ";
    std::cout << std::endl;

    double pwt = poppins;
    cout << "convert to double => ";
    cout << "poppins: " << pwt << " pounds\n";
    cout << "convert to int=> ";
    cout << "poppins " << int(poppins) << std::endl;
    return 0;
}

summary

that's all, thanks u guy.

  • 32
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值