Advertisement
gacnvnmovie

Untitled

Mar 11th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. Phanso.h
  2.  
  3. class PhanSo
  4. {
  5.     private:
  6.         int tu;
  7.         int mau;
  8.     public:
  9.         PhanSo(int a=0, int b=1);
  10.         void Rutgon();
  11.         int LayTuSo(){return tu;};
  12.         int LayMauSo(){return mau;};
  13.         void SetTuSo(int value){tu=value;};
  14.         void SetMauSo(int value){mau=value;};
  15.         void Nhap();
  16.         void Xuat();
  17.         PhanSo Cong(PhanSo ps2);
  18.         PhanSo Tru(PhanSo ps2);
  19. };
  20.  
  21.  
  22. Phanso.cpp
  23.  
  24. #include "stdafx.h"
  25. #include "PhanSo.h"
  26. #include "iostream"
  27. using namespace std;
  28.     PhanSo::PhanSo(int a,int b)
  29.     {
  30.         tu=a;
  31.         mau=b;
  32.     }
  33.     void PhanSo::Nhap()
  34.     {
  35.         cout<<"nhap vao tu so";
  36.         cin>>tu;
  37.         cout<<"nhap vao mau so";
  38.         cin>>mau;
  39.     }
  40.     void PhanSo::Xuat()
  41.     {
  42.         cout<<"phan so la:"<<tu<<"/"<<mau<<endl;
  43.     }
  44.     PhanSo PhanSo::Cong(PhanSo ps2)
  45.     {
  46.         PhanSo result;
  47.         result.tu=tu*ps2.mau+mau*ps2.tu;
  48.         result.mau=mau*ps2.mau;
  49.         result.Rutgon();
  50.         return result;
  51.     }
  52.     PhanSo PhanSo::Tru(PhanSo ps2)
  53.     {
  54.         PhanSo result;
  55.         result.tu=tu*ps2.mau-mau*ps2.tu;
  56.         result.mau=mau*ps2.mau;
  57.         result.Rutgon();
  58.         return result;
  59.     }
  60.     int  UCLN(int x,int y)
  61.     {
  62.         while (x!=y)
  63.         {
  64.             if(x>y)
  65.                 x=x-y;
  66.             else y=y-x;
  67.         }
  68.         return x;
  69.     }
  70.     void PhanSo::Rutgon()
  71.     {
  72.         PhanSo result;
  73.         int x;
  74.         x=UCLN(tu,mau);
  75.         result.tu=tu/x;
  76.         result.mau=mau/x;
  77.         cout<<"tong sau rut gon la: "<<result.tu<<"/"<<result.mau<<endl;
  78.     }
  79.  
  80. BT1.cpp
  81.  
  82. // BT1.cpp : Defines the entry point for the console application.
  83. //
  84. #include "stdafx.h"
  85. #include "conio.h"
  86. #include "PhanSo.h"
  87. #include "iostream"
  88. using namespace std;
  89. void main()
  90. {
  91.     PhanSo a,b,c;
  92.     a.Nhap();
  93.     b.Nhap();
  94.     c=a.Cong(b);
  95.     _getch();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement