上交OJ-1011. 復(fù)數(shù)類

1011. 復(fù)數(shù)類


題目描述

寫一個復(fù)數(shù)類,實(shí)現(xiàn)以下程序主函數(shù)中所需要的功能。

#include <iostream>
using namespace std;

class MyComplex
{
private:
  double x,y;
public:
  /* Implementation of MyComplex */
};

int main()
{
  MyComplex z1;
  MyComplex z2;

  cin >> z1 >> z2;

  cout << z1 + z2 <<endl;
  cout << z1 - z2 <<endl;
  cout << z1 * z2 <<endl;
  cout << z1 / z2 <<endl;
  cout << (z1 += z2) <<endl;
  cout << (z1 -= z2) <<endl;
  cout << (z1 *= z2) <<endl;
  cout << (z1 /= z2) <<endl;

  return 0;
}

輸入格式

輸入包括兩行,第一行是兩個整數(shù)a, b(0<|a|+1,|b|<10001),表示復(fù)數(shù)a+bi。

第二行是兩個整數(shù)c, d(0<|c|+1,|d|<10001),表示復(fù)數(shù)c+di。輸入數(shù)據(jù)保證不出現(xiàn)除以0的情況。

輸出格式

輸出包括八行,對應(yīng)所給程序中的輸出。注意輸出浮點(diǎn)數(shù)保留2位小數(shù)。

Sample Input 1

3 6
-3 5

Sample Output 1

0.00 11.00
6.00 1.00
-39.00 -3.00
0.62 -0.97
0.00 11.00
3.00 6.00
-39.00 -3.00
3.00 6.00

Sample Input 2

5 9
5 -9

Sample Output 2

10.00 0.00
0.00 18.00
106.00 0.00
-0.53 0.85
10.00 0.00
5.00 9.00
106.00 0.00
5.00 9.00

分析

這是對運(yùn)算符和輸入輸出重載的題目。

#include <iostream>
#include <iomanip>
using namespace std;

class MyComplex
{
private:
  double x,y;
public:
  /* Implementation of MyComplex */
  MyComplex(){x=0; y=0;}
  MyComplex(double r,double i){x=r; y=i;}
  
  friend ostream& operator << (ostream&, MyComplex);
  friend istream& operator >> (istream&, MyComplex&);
  
  MyComplex operator + (const MyComplex &c2);
  MyComplex operator - (const MyComplex &c2);
  MyComplex operator * (const MyComplex &c2);
  MyComplex operator / (const MyComplex &c2);
  MyComplex& operator += (const MyComplex &c2);
  MyComplex& operator -= (const MyComplex &c2);
  MyComplex& operator *= (const MyComplex &c2);
  MyComplex& operator /= (const MyComplex &c2);
};

ostream& operator << (ostream& output, MyComplex c) //定義運(yùn)算符“<<”重載函數(shù)
{
  output<<fixed<<setprecision(2)<<c.x<<" "<<c.y;
  return output;
}

istream& operator >> (istream& input, MyComplex& c) //定義運(yùn)算符“<<”重載函數(shù)
{
  input>>c.x>>c.y;
  return input;
}

MyComplex MyComplex::operator + (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  return MyComplex(x+c2.x, y+c2.y);
}

MyComplex MyComplex::operator - (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  return MyComplex(x-c2.x, y-c2.y);
}

MyComplex MyComplex::operator * (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  return MyComplex(x*c2.x-y*c2.y, x*c2.y+y*c2.x);
}

MyComplex MyComplex::operator / (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  return MyComplex((x*c2.x+y*c2.y)/(c2.x*c2.x+c2.y*c2.y), (-x*c2.y+y*c2.x)/(c2.x*c2.x+c2.y*c2.y));
}

MyComplex& MyComplex::operator += (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  *this = *this + c2;
  return *this;
}

MyComplex& MyComplex::operator -= (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  *this = *this - c2;
  return *this;
}

MyComplex& MyComplex::operator *= (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  *this = *this * c2;
  return *this;
}

MyComplex& MyComplex::operator /= (const MyComplex &c2)//定義運(yùn)算符“+”重載函數(shù)
{
  *this = *this / c2;
  return *this;
}

int main()
{
  MyComplex z1;
  MyComplex z2;

  cin >> z1 >> z2;

  cout << z1 + z2 <<endl;
  cout << z1 - z2 <<endl;
  cout << z1 * z2 <<endl;
  cout << z1 / z2 <<endl;
  cout << (z1 += z2) <<endl;
  cout << (z1 -= z2) <<endl;
  cout << (z1 *= z2) <<endl;
  cout << (z1 /= z2) <<endl;

  return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容