Tìm kiếm

Phân số (cơ bản)

? : Xây dựng 1 lớp phân số gồm:
 Hàm nhập, hàm rút gọn, hàm in, toán tử +,tính tổng (có thể sử dụng đa năng hóa toán tử !)

Code:
 #include <iostream.h>
#include <conio.h>
int uc(int a,int b)
 {
 if (a*b==0)
  return 1;
 else
  {
  while (a!=b)
{
if (a>b)
a=a-b;
else
b=b-a;
}
 return a;
  }
 }

class ps
 {
 private:
int tu;
int mau;
 public:
ps(int tt=0,int mm=0)
{
tu = tt;
mau = mm;
}
  friend ostream& operator<<(ostream& os,ps x)
{
os<<x.tu<<"/"<<x.mau;
return os;
}
  friend istream& operator>>(istream& is,ps &x)
{
cout<<"Nhap tu so: ";
is>>x.tu;
cout<<"Nhap mau so: ";
is>>x.mau;
return is;
}
  ps operator+(ps a)
{
ps b;
b.tu = tu*a.mau+mau*a.tu;
b.mau= mau*a.mau;
return rg(b);
}
  ps rg(ps x)
{
int s = uc(x.tu,x.mau);
x.tu = x.tu/s;
x.mau = x.mau/s;
return x;
}
 };
  void main()
{
clrscr();
ps a,b;
int n;
cout<<"Nhap so phan so: \n";
cin>>n;
ps *v = new ps[n];
for (int i=1;i<=n;i++)
{
cout<<"Nhap phan so thu "<<i <<" : \n";
cin>>v[i];
}
cout<<"----Mang phan so vua nhap ----\n\n";
for (i=1;i<=n;i++)
{
cout<<v[i];
cout<<"  ";
}
cout<<"\n\n";
cout<<"Nhap vao 2 phan so de tinh tong: \n";
cin>>a>>b;
cout<<"Tong 2 phan so vua nhap la : "<<a+b;
getch();
}

Read Users' Comments (0)

0 Response to "Phân số (cơ bản)"

Đăng nhận xét

Support

Liên hệ DMTuan-Uneti
Mọi thông tin góp ý các bạn liên hệ với mình ! Mail:
  1. manhtuan.leo@gmail.com
  2. manhtuan.itvp@gmail.com

Y!M: manhtuan.it92