美文网首页
09 类的继承 和 分数化简,计算

09 类的继承 和 分数化简,计算

作者: LuckTime | 来源:发表于2016-06-18 09:56 被阅读21次

    // fraction -------------分数化简

    include <iostream>

    include <stdlib.h>

    using namespace std;

    class Math //------ 数学类为父类
    {
    private :
    public:
    double sum; //-----加法
    double dif; //--减法
    double product; //乘法
    double divide; //除法
    };

    class Fraction : public Math
    {
    int molecule;
    int denominator;
    public:
    void set(int a,int b) //----------赋值,一会试试引用
    {
    molecule = a;
    denominator = b;

    }
    void simplify();

    };

    void Fraction :: simplify()
    {
    int integer;//-----整数部分
    /*divide = (double)molecule / (double)denominator ;
    cout << divide << endl;
    */
    if((double)molecule / (double)denominator < 0) //----------判断两个数的商是正数还负数
    cout << " - " ;

    //----------------------------------------------------------
    molecule = molecule > 0 ? molecule : -molecule ; //----------将两个数变成绝对值
    denominator= denominator > 0 ? denominator : -denominator;

    if(molecule > denominator)
    {
    integer = molecule / denominator;
    molecule = molecule % denominator; //将假分数变成带分数
    cout << integer<<" ";
    }
    //-------------------------------------------------------------------
    for(int i = molecule; denominator != 0 && i >= 1; i--) //分母不能为零,并且约分
    {
    if( molecule % i == 0 && denominator % i == 0 )
    {
    molecule /= i;
    denominator /= i;
    }
    }
    cout << molecule<<"/" <<denominator<< endl;
    }

    int main()
    {
    int a,b;
    cin >>a>>b;
    Fraction A;
    A.set(a,b);
    A.simplify();
    }
    //===========================================
    //===========================================
    //============方法二: 继承 重载 构造函数================

    include <iostream>

    include <cstring>

    using namespace std;

    class Frac //-----------------父类
    {
    int n;
    int d;

    public:
    Frac():n(0),d(1){}
    Frac(int an, int ad):n(an),d(ad)
    {
    reduce(); //约分
    }

    void reduce()
    {
    if( d<0 ) {d= -d;n = -n;} //分母不能小于等于
    if( d==0 ) { cout << "d==0!! \n error!!" <<endl;}
    int absn = (n<0 ? -n: n); //分子变成正数
    for(int i =d; i>1; i--)
    {
    if(absn % i == 0&& d % i ==0)
    {
    n/=i; d/=i;
    break;
    }
    }
    }//计算机在运行时,先来看看各类函数。//成员函数中,可以直接访问成员函数。。在类外部时,需要传入参数。
    void show()
    {
    cout << n << "/" << d <<endl;
    }
    //------------------------------
    double value()
    {
    return (double)n/d;
    }

    };
    //========================================子类,带分数
    class Dai : public Frac
    {
    int i;
    public :
    Dai() :i(0){}//无参构造函数
    Dai(int ai,int an,int ad) : i(ai),Frac(an,ad)//有参构造函数
    {
    }
    void show()
    {
    cout << i << " ";
    Frac ::show();

    }

    double value()
    {
    return i+Frac::value();
    }

    };

    int main()
    {
    Frac f1(12,16);
    Frac f2;
    f1.show();
    cout << f1.value() << endl;

    f2.show();
    cout << f2.value() << endl;

    Dai f3(3,3,5);
    Dai f4;
    f3.show();
    cout << f3.value() << endl;

    f4.show();
    cout << f4.value() << endl;
    }

    //========================================
    //========================================
    //========================================

    include <iostream>

    using namespace std;

    class Vehilce
    {
    protected :
    int wheels; //车轮个数
    double weight; //车重
    public:
    Vehilce():wheels(0),weight(0){}
    Vehilce(int wh,double we):wheels(wh),weight(we){}
    ~Vehilce(){
    cout << "~Vehilce()" << endl;
    }
    virtual void show() = 0;
    } ;

    class Car :public Vehilce
    {
    int passengers; //承载人数
    public:
    Car(int pa,int wh,double we) : passengers(pa),Vehilce(wh,we){}

    Car() : passengers(0){}
    ~Car(){
    cout << "~Car()" <<endl;
    }
    void show()
    {
    cout << " Car-----" <<endl;
    }
    };

    class Truck :public Vehilce
    {
    int passengers; //承载人数
    double payload; //承载重量
    public:
    Truck(int pa,double pay ,int wh,double we) : passengers(pa),payload(pay),Vehilce(wh,we){}

    Truck() : passengers(0),payload(0){}
    ~Truck(){
    cout << "~Truck()" <<endl;
    }
    void show()
    {
    cout << " Truck()-----" <<endl;
    }
    };
    int main(int argc,char *argv[])
    {
    Truck t(4,8,3,4);
    t.show();
    Car c(3,NULL,5);
    c.show();
    Car (3,NULL,5);

    }

    相关文章

      网友评论

          本文标题:09 类的继承 和 分数化简,计算

          本文链接:https://www.haomeiwen.com/subject/aonvdttx.html