oop_javabest Admin
Tổng số bài gửi : 6 Registration date : 24/09/2008
| Tiêu đề: Xây dựng lớp phân số! Thu Mar 26, 2009 9:23 pm | |
| Đề: Xây dựng lớp phân số sử dụng phép chồng toán tử thực hiện các yêu cầu sau: - Toán tử nhập xuất. - Toán tử cộng trừ nhân chia 2 phân số. - Rút gọn phân số. - So sánh 2 phân số, =, <, >, <=, >=, != Nhập vào một mảng các phân số, tính và in ra: - Phân số lớn nhất, phân số nhỏ nhất. - Tổng và tích các phân số đã nhập vào. - Sắp xếp các phân số theo thứ tự tăng dần. BÀI LÀM - Code:
-
#include <iostream> #include<conio.h> using namespace std; int ucln(int a, int b) { int tam = a; while(!(a%tam==0&&b%tam==0&&tam>0)) tam --; return tam; } class Phanso { int ts, ms; public: Phanso(); Phanso(int ts1, int ms1); friend istream & operator >> (istream &, Phanso &); friend ostream & operator << (ostream &, Phanso &); Phanso operator + (Phanso &); Phanso operator - (Phanso &); Phanso operator * (Phanso &); void rutgon(); bool operator == (Phanso &); bool operator != (Phanso &); bool operator > (Phanso &); bool operator < (Phanso &); bool operator >= (Phanso &); bool operator <= (Phanso &); }; Phanso::Phanso() { ts = 0; ms =1; } Phanso::Phanso(int ts1, int ms1) { ts = ts1; ms = ms1; } ostream & operator << (ostream &os, Phanso &r) { os << r.ts << "/"<< r.ms; return os; } istream & operator >> (istream &is, Phanso &r) { cout << "Nhap tu so:"; is >> r.ts; cout << "Nhap mau so:"; is >> r.ms; return is; } Phanso Phanso::operator+(Phanso &r) { int ts1, ms1; ts1 = ts*r.ms + ms*r.ts; ms1 = ms * r.ms; int uc = ucln(ts1, ms1); ts1 = ts1/uc; ms1 = ms1/uc; return Phanso(ts1, ms1); } Phanso Phanso::operator-(Phanso &r) { int ts1, ms1; ts1 = ts*r.ms - ms*r.ts; ms1 = ms * r.ms; int uc = ucln(ts1, ms1); ts1 = ts1/uc; ms1 = ms1/uc; return Phanso(ts1, ms1); } Phanso Phanso::operator*(Phanso &r) { int ts1, ms1;
ts1 = ts*r.ts; ms1 = ms * r.ms; int uc = ucln(ts1, ms1); ts1 = ts1/uc; ms1 = ms1/uc; return Phanso(ts1, ms1); } bool Phanso::operator > (Phanso &r) { int ts1, ms1; ts1 = ts*r.ms - ms*r.ts; ms1 = ms * r.ms; return (ts1*ms1>0); } bool Phanso::operator == (Phanso & r) { if(!(*this>r) && !(r>*this)) return true; return false; } bool Phanso::operator != (Phanso & r) { if(!(*this==r)) return true; return false; } bool Phanso::operator < (Phanso & r) { if(r>*this) return true; return false; } void Phanso::rutgon() { int uc = ucln(ts, ms); ts = ts/uc; ms = ms/uc; } void sapxep(Phanso a[], int n) { Phanso tam; int i, j; for (i=0;i<n;++i) for(j=i+1;j<n;++j) if(a[j]<a[i]) { tam = a[i]; a[i] = a[j]; a[j] = tam; } } int main() { Phanso * a; int n, i; Phanso tong, tich(1,1); int m1, m2; cout << "Nhap n="; cin >> n; a = new Phanso[n]; for(i=0;i<n;++i) { cout << "Nhap phan so thu "<< i << endl; cin >> a[i]; } cout << "Mang cac phan so vua nhap vao:\n"; for(i=0;i<n;++i) cout << a[i] <<endl; m1 = m2 = 0; for(i=0;i<n;++i) { tong = tong + a[i]; tich = tich * a[i]; if(a[i]>a[m1]) m1 = i; if(a[i]<a[m2]) m2 = i; } cout << "Phan so tong:" << tong << endl; cout << "Phan so tich:"<< tich << endl; cout << "Phan so Lon nhat:"<< a[m1] << endl; cout << "Phan so Nho nhat:" << a[m2] << endl; sapxep(a, n); cout << "Mang sau khi sap xep:"<< endl; for(i=0;i<n;++i) cout << a[i] << endl; delete [] a; return 0; }
| |
|
soledad Admin
Tổng số bài gửi : 60 Age : 35 Registration date : 18/10/2008
| Tiêu đề: hàm friend Sat Mar 28, 2009 12:52 am | |
| - Code:
-
#include <iostream.h>
class phanso { private : long tu,mau; public : uocluoc(); void nhap(); void xuat(); void gantu(long x); void ganmau(long y); friend phanso operator*(phanso a,phanso b); friend phanso operator+(phanso a,phanso b); friend phanso operator+(phanso a,long t); bool operator==(phanso b); }; int uscln(long t,long m) { int i,h; if (t>m) h=m; else h=t; for(i=h;i>0;i--) if((t%i==0)&&(m%i==0)) {break;} return i; } phanso::uocluoc() { long tam; tam=long(uscln(tu,mau)); tu=tu/tam; mau=mau/tam; return 0;} void phanso::nhap() { cout<<"nhap tu "; cin>>tu; cout<<"nhap mau "; cin>>mau; } void phanso::xuat() { cout<<tu<<"/"<<mau; } void phanso::gantu(long x) { tu=x;} void phanso::ganmau(long y) {mau=y;} phanso operator*(phanso a,phanso b) {phanso tam; tam.gantu(a.tu*b.tu); tam.ganmau(a.mau*b.mau); return tam; } phanso operator+(phanso a,phanso b) { phanso tam; tam.gantu(a.tu*b.mau+a.mau*b.tu); tam.ganmau(a.mau*b.mau); return tam; } phanso operator+(phanso a,long b) { phanso tam; tam.gantu(a.tu+b*a.mau); tam.ganmau(a.mau); return tam; } void main()
{ phanso a,b,c,d,kq;int t=1; cout<<"nhap phan so a"<<'\n'; a.nhap(); cout<<"nhap phan so b"<<'\n'; b.nhap(); cout<<"nhap phan so c"<<'\n'; c.nhap(); cout<<"3 phan so vua nhap la : "; a.xuat();cout<<" "; b.xuat();cout<<" "; c.xuat();cout<<" "; kq=a*b+c+t; kq.uocluoc(); cout<<'\n'<<"ket qua la ";kq.xuat();cout<<'\n'; }
| |
|
oop_javabest Admin
Tổng số bài gửi : 6 Registration date : 24/09/2008
| Tiêu đề: Re: Xây dựng lớp phân số! Sat Mar 28, 2009 11:15 am | |
| Bài của soledad sao nó báo lỗi nè: | |
|
soledad Admin
Tổng số bài gửi : 60 Age : 35 Registration date : 18/10/2008
| Tiêu đề: trả lời Sat Mar 28, 2009 11:28 pm | |
| ủa,sao Soledad chạy đc mà,không có báo lỗi gì hết,ok luôn | |
|
Sponsored content
| Tiêu đề: Re: Xây dựng lớp phân số! | |
| |
|