計程8-15

看板ZooStudy作者 (一個月的長盼)時間21年前 (2003/01/01 21:35), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串1/1
#include <iostream> using std::ostream; using std::istream; using std::cout; using std::cin; class complex{ friend ostream &operator<<(ostream &, const complex &); friend istream &operator>>(istream &, complex &); public: complex(double=0.0, double=0.0); complex operator+(const complex &)const; complex operator-(const complex &)const; complex operator*(const complex &)const; bool operator==(const complex &)const; bool operator!=(const complex &right)const { return !(*this==right); } const complex &operator=(const complex &); private: double real; double imaginary; }; complex::complex(double r, double i) :real(r) , imaginary (i){ } complex complex::operator+(const complex &operand2) const { return complex(real + operand2.real , imaginary + operand2.imaginary); } complex complex::operator-(const complex &operand2) const { return complex(real - operand2.real , imaginary - operand2.imaginary); } complex complex::operator*(const complex &operand2) const { return complex((real* operand2.real) - (imaginary* operand2.imaginary) , (imaginary* operand2.real) + (real* operand2.imaginary)); } bool complex::operator==(const complex &A)const { if((*this).real==A.real && (*this).imaginary==A.imaginary) return true; return false; } const complex& complex::operator=(const complex &right) { real = right.real; imaginary = right.imaginary; return *this; } ostream &operator<<(ostream &output, const complex &A) { output<<"(" <<A.real <<"," <<A.imaginary <<")" ; return output; } istream &operator>>(istream &input, complex &A) { input.ignore(); input>>A.real ; input.ignore(); input>>A.imaginary ; input.ignore(); return input; } int main() { cout<<"Key in TWO complex numbers, in the form (a,b)(c,d) \n"; complex c1,c2; complex x, y, z, w ; cin>> c1; cin>> c2; cout<<"c1="; cout<< c1 <<"\n"; cout<<"c2="; cout<< c2 <<"\n"; x = c1 + c2; y = c1 - c2; z = c1 * c2; w = c1; cout<<"x=" <<x <<"\n"; cout<<"y=" <<y <<"\n"; cout<<"z=" <<z <<"\n"; cout<<"w=" <<w <<"\n"; if(w==c1) cout<<"w==c1"<<"\n"; return 0; } -- 天下最難的事,就是享受最簡單平凡的日子 而最簡單平凡的日子,往往是天下最單純的幸福 -- ※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw) ◆ From: 61.226.112.68

推 61.224.56.186 01/01, , 1F
每次看到這些 我都覺得 這真是太可怕了...
推 61.224.56.186 01/01, 1F

推 61.226.112.68 01/01, , 2F
我也覺得阿>< 過ko之後有成就感ㄉ說:P
推 61.226.112.68 01/01, 2F
文章代碼(AID): #-4kwMIQ (ZooStudy)