Chào các bạn! Truyen4U chính thức đã quay trở lại rồi đây!^^. Mong các bạn tiếp tục ủng hộ truy cập tên miền Truyen4U.Com này nhé! Mãi yêu... ♥

Phan so

#include<conio.h>

#include<iostream.h>

#include<math.h>

class phanso

{

    private:

        int tu, mau;

    public:

        phanso()

        {

         tu =0;

         mau=1;

        }

        void nhap()

        {

            cout<<"nhap tu so:";cin>>tu;cout<<"

";

            cout<<"nhap mau so:";cin>>mau;cout<<"

";

        }

        void toigian()

        {

        int t1=abs(tu),t2=abs(mau);

       for (;t1!=t2;)

        if (t1>t2)

            t1-=t2;

        else

            t2-=t1;

       tu=tu/t1;

       mau=mau/t1;}

        phanso operator +(phanso a)

        { phanso b;

          b.tu=tu*a.mau+mau*a.tu;

          b.mau=mau*a.mau;

          return b;

        }

        phanso operator -(phanso a)

        { phanso b;

          b.tu=tu*a.mau-mau*a.tu;

          b.mau=mau*a.mau;

          return b;

        }

        phanso operator *(phanso a)

        { phanso b;

          b.tu=tu*a.tu;

          b.mau=mau*a.mau;

          return b;

        }

        phanso operator /(phanso a)

        { phanso b;

          b.tu=tu*a.mau;

          b.mau=mau*a.tu;

          return b;

        }

        void xuat()

        {

         cout<<"Tu so:"<<tu;

         cout<<"

Mau so:"<<mau;

        }

};

  /*    int ucln(int a,int b)

    {a=abs(a);

     b=abs(b);

     while(a!=b)

     {

      if (a>b) a=a-b;

      else b=b-a;

     }

     return a;

    }

    void phanso::toigian()

    {

        cout<<"Tu so:"<<tu/ucln(tu,mau)<<"

";

        cout<<"Mau so:"<<mau/ucln(tu,mau)<<"

";

    }*/

void main()

{

    phanso a;

    phanso b;

    phanso c;

    a.nhap();

    b.nhap();

    c=a-b;

    c.toigian();

        c.xuat();

    getch();

}

Bạn đang đọc truyện trên: Truyen4U.Com

Tags: