Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Phanso.h
- class PhanSo
- {
- private:
- int tu;
- int mau;
- public:
- PhanSo(int a=0, int b=1);
- void Rutgon();
- int LayTuSo(){return tu;};
- int LayMauSo(){return mau;};
- void SetTuSo(int value){tu=value;};
- void SetMauSo(int value){mau=value;};
- void Nhap();
- void Xuat();
- PhanSo Cong(PhanSo ps2);
- PhanSo Tru(PhanSo ps2);
- };
- Phanso.cpp
- #include "stdafx.h"
- #include "PhanSo.h"
- #include "iostream"
- using namespace std;
- PhanSo::PhanSo(int a,int b)
- {
- tu=a;
- mau=b;
- }
- void PhanSo::Nhap()
- {
- cout<<"nhap vao tu so";
- cin>>tu;
- cout<<"nhap vao mau so";
- cin>>mau;
- }
- void PhanSo::Xuat()
- {
- cout<<"phan so la:"<<tu<<"/"<<mau<<endl;
- }
- PhanSo PhanSo::Cong(PhanSo ps2)
- {
- PhanSo result;
- result.tu=tu*ps2.mau+mau*ps2.tu;
- result.mau=mau*ps2.mau;
- result.Rutgon();
- return result;
- }
- PhanSo PhanSo::Tru(PhanSo ps2)
- {
- PhanSo result;
- result.tu=tu*ps2.mau-mau*ps2.tu;
- result.mau=mau*ps2.mau;
- result.Rutgon();
- return result;
- }
- int UCLN(int x,int y)
- {
- while (x!=y)
- {
- if(x>y)
- x=x-y;
- else y=y-x;
- }
- return x;
- }
- void PhanSo::Rutgon()
- {
- PhanSo result;
- int x;
- x=UCLN(tu,mau);
- result.tu=tu/x;
- result.mau=mau/x;
- cout<<"tong sau rut gon la: "<<result.tu<<"/"<<result.mau<<endl;
- }
- BT1.cpp
- // BT1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "conio.h"
- #include "PhanSo.h"
- #include "iostream"
- using namespace std;
- void main()
- {
- PhanSo a,b,c;
- a.Nhap();
- b.Nhap();
- c=a.Cong(b);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement