k4info
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Chương 7 bài tập 1 phần tự làm (các bạn tham khảo và đóng góp ý kiến cho mình nhe!!!)

2 posters

Go down

Cool Chương 7 bài tập 1 phần tự làm (các bạn tham khảo và đóng góp ý kiến cho mình nhe!!!)

Bài gửi by nguyenthithat Thu Oct 06, 2011 9:43 am

các toán tử cộng, trừ, nhân, chia tôi làm theo cách:
x/x + y/y
x/x - y/y
x/x * y/y
x/x / y/y

Code:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
class phanso{
      private:
      int tu, mau;
      public:
      phanso(int tu=0,int mau=1){
                this->tu=tu;
                this->mau=mau;
      };
      void inphanso(){
      cout<<"("<<tu<<"/"<<mau<<")";
      };
      friend phanso operator + (phanso,phanso);
      friend phanso operator - (phanso,phanso);
      friend phanso operator * (phanso,phanso);
      friend phanso operator / (phanso,phanso);
};
phanso operator + (phanso x,phanso y){
      phanso a;
      a.tu=((x.tu*y.mau)+(y.tu*x.mau));
      a.mau=(x.mau*y.mau);
      return a;
}
phanso operator - (phanso x,phanso y){
      phanso a;
      a.tu=((x.tu*y.mau)-(y.tu*x.mau));
      a.mau=(x.mau*y.mau);
      return a;
}
phanso operator * (phanso x,phanso y){
      phanso a;
      a.tu=(x.tu*y.tu);
      a.mau=(x.mau*y.mau);
      return a;
}
phanso operator / (phanso x,phanso y){
      phanso a;
      a.tu=(x.tu*y.mau);
      a.mau=(x.mau*y.tu);
      return a;
}
int main(){
    phanso a(7,2), b(3,2);
    cout<<"\n";
    phanso c=a+b;
    a.inphanso();
    cout<<"+";
    b.inphanso();
    cout<<"=";
    c.inphanso();
    cout<<"\n\n";
    phanso d=a-b;
    a.inphanso();
    cout<<"-";
    b.inphanso();
    cout<<"=";
    d.inphanso();
    cout<<"\n\n";
    phanso e=a*b;
    a.inphanso();
    cout<<"*";
    b.inphanso();
    cout<<"=";
    e.inphanso();
    cout<<"\n\n";
    phanso n=a/b;
    a.inphanso();
    cout<<"/";
    b.inphanso();
    cout<<"=";
    n.inphanso();
    getch();
}
avatar
nguyenthithat

Posts : 12
Thanked : 1
Gia Nhập 07/03/2011

Về Đầu Trang Go down

Cool Re: Chương 7 bài tập 1 phần tự làm (các bạn tham khảo và đóng góp ý kiến cho mình nhe!!!)

Bài gửi by itsnotoverlbh Thu Oct 06, 2011 4:34 pm

Bài của bạn làm đúng rồi mà
ah, khi bạn chạy vẫn thấy có dòng lỗi nhỏ ở khu báo lỗi nhưng chương trình vẫn thực thi.
Để loại bỏ lỗi này bạn có thể khai báo như sau:

Code:
 #include<iostream>
using namespace std;
class....
Vài góp ý chút thôi @
itsnotoverlbh
itsnotoverlbh

Posts : 367
Thanked : 21
Gia Nhập 18/02/2011

Tài Sản
Thú nuôi:

http://www.ittd.freevnn.com

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết