Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 7 years ago

c++operator overloading座標運算

請問一下個為大大

我算出來的a有錯 可否請問是哪邊??

要怎麼改??

#include <iostream>

#include <cstdio>

#include <cstdlib>

using namespace std;

class position

{

private:

float x,y;

public:

position(float a,float b)

{

x=a;y=b;

}

position operator = (position rhs)

{

x=rhs.x; y=rhs.y;

return *this;

}

position operator - (position rhs)

{

position pass(0,0);

x=rhs.x-x; y=rhs.y-y;

return pass;

}

position operator / (position rhs)

{

position cool(0,0);

x=rhs.x/x; y=rhs.y/y;

return cool;

}

position operator * (position rhs)

{

position ans(0,0);

ans.x=rhs.x*x;ans.y=rhs.y*y;

return ans;

}

position operator + (position rights)

{

position answer(0,0);

answer.x=rights.x+x;answer.y=rights.y+y;

return answer;

}

void print()

{

cout<<"("<<x<<","<<y<<")"<<endl;

}

};

int main()

{

position a(1,1),b(2,0),c(2,1);

a=((b*c)-a+c)/b;

cout<<"a的座標為"<<endl;

a.print();

cout<<"b的座標為"<<endl;

b.print();

cout<<"c的座標為"<<endl;

c.print();

system("pause");

return 0;

}

10 Answers

Rating
  • 7 years ago
    Favorite Answer

    #include <iostream>

    #include <cstdio>

    #include <cstdlib>

    using namespace std;

    template <class T>

    class position {

    private:

    T x,y;

    public:

    position(const position &a):x(a.x),y(a.y){}

    position(T a,T b):x(a),y(b){}

    position operator==(position r){return x==r.x && y==r.y;}

    position operator=(position r){x=r.x; y=r.y; return *this; }

    position operator-(position r){position p(*this); p.x -= r.x; p.y -= r.y; return p; }

    position operator/(position r){position p(r); p.x/=x; p.y/=y; return p; }

    position operator*(position r){position p(r); p.x*=x; p.y*=y; return p; }

    position operator+(position r){position p(r); p.x+=x; p.y+=y; return p; }

    void print() { cout<<'('<<x<<','<<y<<')'<<endl; }

    };

    int main() {

    position<double> a(1.0,1.0),b(2.23,0.34),c(2.2,1.1);

    a=((b*c)-a+c)/b;

    cout<<"a的座標為"; a.print();

    cout<<"b的座標為"; b.print();

    cout<<"c的座標為"; c.print();

    // system("pause");

    return 0;

    }

    2014-05-22 10:17:37 補充:

    >而且 position operator==應是return bool

    agree. And I did not test it for the main did not call it.

    > operator/ operator* operator+ 算式有誤.

    I just copy from the original code - I don't care what they were meant to do...

    2014-05-22 10:19:30 補充:

    if you looked at the operator/

    your will see both I and the original code use

    a/b = (b.x/a.x) NOT as how you computed...

    2014-05-22 10:20:39 補充:

    see the original code below.

    position operator / (position rhs)

    {

    position cool(0,0);

    x=rhs.x/x; y=rhs.y/y; // return = rhs/lhs

    return cool;

    }

  • 7 years ago

    到下面的網址看看吧

    ▶▶http://candy5660601.pixnet.net/blog

  • 7 years ago

    到下面的網址看看吧

    ▶▶http://candy5660601.pixnet.net/blog

  • 7 years ago

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

  • How do you think about the answers? You can sign in to vote the answer.
  • 7 years ago

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

  • 7 years ago

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

  • 7 years ago

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

  • Anonymous
    7 years ago

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

  • 7 years ago

    公主大大的好像寫錯了ㄟ,

    a.x = ((b*c) - a + c) / b = ((2.23*2.2) - 1.0 + 2.2) / 2.23

    = 2.73812

    可是程式跑出來是0.365215

    原因在於

    operator/

    operator*

    operator+

    算式有誤.

    2014-05-22 09:55:51 補充:

    而且 position operator==應是return bool

    2014-05-22 11:07:23 補充:

    完整版

    http://ideone.com/ZOmHFW

Still have questions? Get your answers by asking now.