promotion image of download ymail app
Promoted
匿名使用者
匿名使用者 發問時間: 電腦與網際網路程式設計 · 6 年前

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 個解答

評分
  • 6 年前
    最佳解答

    #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;

    }

    • Commenter avatar登入以對解答發表意見
  • 6 年前

    到下面的網址看看吧

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

    • Commenter avatar登入以對解答發表意見
  • 6 年前

    到下面的網址看看吧

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

    • Commenter avatar登入以對解答發表意見
  • 6 年前

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

    • Commenter avatar登入以對解答發表意見
  • 您覺得這個回答如何?您可以登入為回答投票。
  • 6 年前

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

    • Commenter avatar登入以對解答發表意見
  • 6 年前

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

    • Commenter avatar登入以對解答發表意見
  • 6 年前

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

    • Commenter avatar登入以對解答發表意見
  • 6 年前

    參考下面的網址看看

    http://phi008780520.pixnet.net/blog

    • Commenter avatar登入以對解答發表意見
  • 6 年前

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

    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

    • Commenter avatar登入以對解答發表意見
  • 6 年前
    • Commenter avatar登入以對解答發表意見
還有問題?馬上發問,尋求解答。