Chủ đề: Chạy sai kết quả lớp phân số
-
25-10-2009, 09:11 AM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Chạy sai kết quả lớp phân số
em làm nhưng kết quả sai
đây là code của em
Mã:/*Cau 6a.Xay dung 1 lop PS mô ta các doi tuong phân so, lop gom cac thanh phan:- Cac thuoc tinh mô ta tu so và mau so cua phân so.- Ham thiet lap.- Hàm nhap phân so.- Hàm in phân so dang tu so/mau so.- Hàm rut gon phân so.- Toán tu + dùng de tính tong 2 phân so, ket qua tra ve mot phân so toi gian.b. Viet chuong trình nhap mot mang n doi tuong phan so, sap xep mang theo thu tu tang dan va in ra mang da sap xep */#include<iostream.h>#include<conio.h>#include<math.h>class ps{ private: int tu,mau; public: ps(int ttu=0,int mmau=1) { tu=ttu; mau=mmau; } // ham nhap friend istream & operator>>(istream&nhap,ps&p); // ham in: friend ostream &operator<<(ostream&in,ps&p); friend ps rutgon(ps&p); friend int operator >(ps &a,ps &b); friend int ucln(int a,int b);};//---------------------------------istream &operator>>(istream &nhap,ps &p){ cout<<" Nhap Mang cac phan so:"; cout<<"Tu="; nhap>>p.tu; cout<<" Mau="; nhap>>p.mau; return nhap;}//----------------------------ostream &operator<<(ostream &in,ps &p){ //rutgon a[i]; if(p.mau==0) in<<"vo nghia"; if(p.mau==1||p.tu==0) in<<p.tu; else in<<p.tu<<"/"<<p.mau; return in;}//---------------------------int operator >(ps &a,ps &b){ if((a.tu*b.mau-a.mau*b.tu)*a.mau*b.mau>0) return 1; else return 0;}//-----------------------------int ucln(int a,int b){ if(a==b) return 1; if(a>b) return ucln(a-b,b); else return ucln(a,b-a);}//----------------------------ps rutgon(ps &p){ int u=ucln(abs(p.tu),abs(p.mau)); p.tu/=u; p.mau/=u; return rutgon(p);}//-------------------void main(){ //clrscr(); ps *a; int n,i,j; cout<<"Nhap vao:n="; cin>>n; cout<<"Nhap vao mang cac phan so: "; a=new ps[n]; for(i=0;i<n;i++) { cout<<"Phan so thu"<<i+1<<":"; cin>>a[i]; } cout<<"Mang cac phan so da nhap: "; for(i=0;i<n;i++) cout<<a[i]<<" "; ps tg; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { tg=a[i]; a[i]=a[j]; a[j]=tg; } cout<<" Mang sau khi sap xep:"; for(i=0;i<n;i++) cout<<a[i]<<" "; //getch();}
View more random threads:
-
25-10-2009, 11:09 AM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Mã:#include "stdafx.h" #include "iostream" #include <conio.h> #include <math.h> using namespace std; class PS { private: int tu,mau; public: friend ostream& operator<<( ostream& os,const PS &p) { os<<p.tu<<"/"<<p.mau; return os; } friend istream& operator>>(istream& is, PS &p) { cout<<" Nhap Tu so:";is>>p.tu; do { cout<<" Nhap Mau so :"; is>>p.mau; } while(p.mau==0); return is; } int uscln(int x,int y); void rutgon(); PS operator+(PS); PS operator-(PS); PS operator*(PS); PS operator/(PS); }; int PS::uscln(int x,int y) { x=abs(x);y=abs(y); if(x*y==0) return 1; while(x!=y) if(x>y) x-=y; else y-=x; return x; } void PS::rutgon() { int x; x=uscln(tu,mau); tu=tu/x; mau=mau/x; } PS PS::operator+(PS q) { PS t; t.tu=tu*q.mau+mau*q.tu; t.mau=mau*q.mau; t.rutgon(); return t; } PS PS::operator-(PS q) { PS t; t.tu=tu*q.mau-mau*q.tu; t.mau=mau*q.mau; t.rutgon(); return t; } PS PS::operator*(PS q) { PS t; t.tu=tu*q.tu; t.mau=mau*q.mau; t.rutgon(); return t; } PS PS::operator/(PS q) { PS t; t.tu=tu*q.mau; t.mau=mau*q.tu; t.rutgon(); return t; } void main() { PS p,q,cong,tru,nhan,chia; cout<<" Nhap PS p: ";cin>>p; cout<<" PS p la : "<<p; cout<<" Nhap PS q: ";cin>>q; cout<<" PS q la : "<<q; cong=p+q; cout<<" Phep p + q :"<<p<<"+"<<q<<"="<<cong; tru=p-q; cout<<" Phep p - q :"<<p<<"-"<<q<<"="<<tru; nhan=p*q; cout<<" Phep p * q :"<<p<<"*"<<q<<"="<<nhan; chia=p/q; cout<<" Phep p : q :"<<p<<"/"<<q<<"="<<chia; getch(); }
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ