Boolan STL與泛型編程 第五周作業(yè)

設計一個Measurement計量單位類型濒募,滿足如下要求坪郭,

  1. 當為距離單位吭产,當構(gòu)造米或者千米等不同距離單位的實例時侣监,統(tǒng)一以米為基本單位,實例調(diào)用description函數(shù)返回單位對應的meter類型(米類型)
  2. 當為時間單位臣淤,當構(gòu)造分鐘或者秒為單位的實例時达吞,統(tǒng)一以秒為基本單位,實例調(diào)用description函數(shù)返回單位對應的second類型(秒類型)
  3. 如果為除距離和時間的其他單位荒典,都打印值即可。

提示

請使用Traits來完成該題吞鸭,通過Traits獲取不同計量單位的轉(zhuǎn)換系數(shù)和基本單位寺董。
測試代碼

   measurement<meter> m1 = 20;
   measurement<kilometer> m2 = 11.2;
   meter me = m1.description();

   measurement<second> m3 = 20;
   measurement<minute> m4 = 10;

   measurement<double> m5 = 10;

   std::cout << me << std::endl;
   std::cout << m2.description() << std::endl;
   std::cout << m3.description() << std::endl;
   std::cout << m4.description() << std::endl;
   std::cout << m5.description() << std::endl;

為了讓Measurement返回的descriptioni函數(shù)知道它對應的單位究竟是meter還是second,我們需要將泛化的類傳給Traits去獲得它所對應的基本單位刻剥。
因此遮咖,在type_traits.h中,這樣寫

//
// Created by laixi on 2018/5/28.
//

#ifndef WEEK10HM_TYPE_TRAITS_H
#define WEEK10HM_TYPE_TRAITS_H

class meter;
class kilometer;
class second;
class minute;
class unit;

#include <type_traits>
#define const_value(x) std::integral_constant<int,x>

typedef std::integral_constant<bool, true> true_type;
typedef std::integral_constant<bool, false> false_type;
typedef const_value(1) one;

template <class type>
class type_traits {
public:
    typedef false_type __is_time;
    typedef false_type __is_length;
    typedef one coefficient;
    typedef type baseType;
};


template <>
class type_traits<meter> {
public:
    typedef false_type __is_time;
    typedef true_type __is_length;
    typedef one coefficient;
    typedef meter baseType;
};

template <>
class type_traits<kilometer> {
public:
    typedef false_type __is_time;
    typedef true_type __is_length;
    typedef const_value(1000) coefficient;
    typedef meter baseType;
};

template <>
class type_traits<second> {
public:
    typedef true_type __is_time;
    typedef false_type __is_length;
    typedef one coefficient;
    typedef second baseType;
};

template <>
class type_traits<minute> {
public:
    typedef true_type __is_time;
    typedef false_type __is_length;
    typedef const_value(60) coefficient;
    typedef second baseType;
};

#endif //WEEK10HM_TYPE_TRAITS_H

然后造虏,定義我的模板類measurement

//
// Created by laixi on 2018/5/28.
//

#ifndef WEEK10HM_MEASUREMENT_H
#define WEEK10HM_MEASUREMENT_H

#include "type_traits.h"

template <class U>
class measurement {
protected:
    typename type_traits<U>::coefficient coeff;
    U* _unit;

public:
    explicit measurement();
    explicit measurement(const measurement<U>& m);
    measurement operator =(U u);
    measurement operator =(measurement<U> m);
    measurement operator =(float val){
        _unit = new U(val);
    }
    U getUnit() const {return *this->_unit;};
    float value() const {
        return _unit->value;
    };
    typename type_traits<U>::baseType description();
    operator double(){return coeff;};
    measurement(float val){
        _unit = new U(val);
    };
    template <typename T>
            measurement operator= (T val){
        if (!_unit) {
            _unit = new U(val);
        } else
            *_unit = val;
    }
};

template<class U>
measurement<U>::measurement(const measurement<U> &m) {
    _unit = new U(*m._unit);
}


template<class U>
measurement<U> measurement<U>::operator=(U u) {
    if (!_unit) {
        _unit = new U(u);
        return *this;
    }
    if (*_unit != u)
        *_unit = u;
    return *this;
}

template<class U>
measurement<U> measurement<U>::operator=(measurement<U> m) {
    if (!_unit) {
        _unit = new U(m.description());
        return *this;
    }
    if (*_unit != m.description())
        *_unit = m.description();
    return *this;
}


template<class U>
measurement<U>::measurement() {
    _unit = new U();
}


#endif //WEEK10HM_MEASUREMENT_H

為了讓measurement有具體的unit的概念御吞,我定義了一個基類unit

//
// Created by laixi on 2018/5/28.
//

#ifndef WEEK10HM_UNIT_H
#define WEEK10HM_UNIT_H

#include "type_traits.h"

#include <string>
#include "measurement.h"

typedef std::integral_constant<bool, true> true_type;
typedef std::integral_constant<bool, false> false_type;
typedef const_value(1) one;

class unit {
public:
    float value;
    std::string units;
public:
    explicit unit(float val,std::string uni=""):units(uni),value(val){};
    unit():units(""),value(0){};
    unit(const unit& u){
        value = u.value;
        units = u.units;
    };
    //virtual unit& operator =(const unit& u) = 0;
    unit&operator=(float val) {
        value = val;
    }
};


void output(std::ostream& os, unit u, true_type, false_type) {
    os << u.value << u.units;
};

void output(std::ostream& os, unit u, false_type, true_type) {
    os << u.value << u.units;
};

void output(std::ostream& os, unit u, false_type, false_type) {
    os << u.value;
}

std::ostream& operator<<(std::ostream& os, unit u){
    output(os, u, type_traits<unit>::__is_length(), type_traits<unit>::__is_time());
    return os;
}

#endif //WEEK10HM_UNIT_H

然后通過基類來衍生出基本的時間類和長度類second和meter,為了方便漓藕,所有的子類都寫在meter.h中

//
// Created by laixi on 2018/5/28.
//

#ifndef WEEK10HM_METER_H
#define WEEK10HM_METER_H

#include "unit.h"

class meter: public unit {
public:
    meter(float val=0):unit(val,"m"){};
    //meter(unit& u):unit("m"){};
    meter&operator=(const meter& m){
        value = m.value;
    };
    //meter(float val):unit("m"),value(val){};
};

class kilometer: public unit {
public:
    kilometer(float val=0):unit(val,"km"){};
    //kilometer(float val):unit("km"),value(val){};
};

class second: public unit {
public:
    second(float val=0):unit(val,"s"){};
    //second(float val):unit("s"),value(val){};
};

class minute: public unit {
public:
    minute(float val=0):unit(val,"m"){};
    //minute(float val):unit("m"),value(val){};
};


template <class U>
meter getLengthBase(measurement<U> &m){
    meter me = meter(m.value());
    me.value = me.value * m;
    return me;
}

template <class U>
second getTimeBase(measurement<U> &m){
    second se = second(m.value());
    se.value = se.value * m;
    return se;
}

template <class U, class unit>
unit base_unit(measurement<U> &m,false_type,false_type){
    return m.getUnit();
}

template <class U>
meter base_unit(measurement<U> &m,false_type,true_type){
    return getLengthBase(m);
}

template <class U>
second base_unit(measurement<U> &m,true_type,false_type){
    return getTimeBase(m);
}

template <class U>
typename type_traits<U>::baseType base_unit(measurement<U> &m,false_type,false_type){
    return m.getUnit();
}

template <class U>
typename type_traits<U>::baseType measurement<U>::description() {
    return base_unit(*this,typename type_traits<U>::__is_time(),typename type_traits<U>::__is_length());
}

template <class U>
std::ostream& operator<<(std::ostream& os, U u){
    output(os, u, type_traits<U>::__is_length(), type_traits<U>::__is_time());
    return os;
}

#endif //WEEK10HM_METER_H

最后陶珠,在main.cpp中按照題意輸入代碼,即可運行通過了享钞。

#include <iostream>

#include "meter.h"

int main() {
    //std::cout << "Hello, World!" << std::endl;
    measurement<meter> m1 = 20;
    measurement<kilometer> m2 = 11.2;
    meter me = m1.description();

    measurement<second> m3 = 20;
    measurement<minute> m4 = 10;

    measurement<double> m5 = 10;

    std::cout << me << std::endl;
    std::cout << m2.description() << std::endl;
    std::cout << m3.description() << std::endl;
    std::cout << m4.description() << std::endl;
    std::cout << m5.description() << std::endl;
    return 0;
}

運行結(jié)果為

20m
11200m
20s
600s
10
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末揍诽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子栗竖,更是在濱河造成了極大的恐慌暑脆,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狐肢,死亡現(xiàn)場離奇詭異添吗,居然都是意外死亡,警方通過查閱死者的電腦和手機份名,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門碟联,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人僵腺,你說我怎么就攤上這事玄帕。” “怎么了想邦?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵裤纹,是天一觀的道長。 經(jīng)常有香客問我,道長鹰椒,這世上最難降的妖魔是什么锡移? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮漆际,結(jié)果婚禮上淆珊,老公的妹妹穿的比我還像新娘。我一直安慰自己奸汇,他們只是感情好施符,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著擂找,像睡著了一般戳吝。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贯涎,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天听哭,我揣著相機與錄音,去河邊找鬼塘雳。 笑死陆盘,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的败明。 我是一名探鬼主播隘马,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼妻顶!你這毒婦竟也來了祟霍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤盈包,失蹤者是張志新(化名)和其女友劉穎沸呐,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體呢燥,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡崭添,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了叛氨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片呼渣。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖寞埠,靈堂內(nèi)的尸體忽然破棺而出屁置,到底是詐尸還是另有隱情,我是刑警寧澤仁连,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布蓝角,位于F島的核電站阱穗,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏使鹅。R本人自食惡果不足惜揪阶,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望患朱。 院中可真熱鬧鲁僚,春花似錦、人聲如沸裁厅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽执虹。三九已至拓挥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間声畏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工姻成, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留插龄,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓科展,卻偏偏與公主長得像均牢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子才睹,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容