1011. 復(fù)數(shù)類
題目描述
寫一個(gè)復(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;
}
輸入格式
輸入包括兩行,第一行是兩個(gè)整數(shù)a, b(0<|a|+1,|b|<10001)脱篙,表示復(fù)數(shù)a+bi娇钱。
第二行是兩個(gè)整數(shù)c, d(0<|c|+1,|d|<10001),表示復(fù)數(shù)c+di绊困。輸入數(shù)據(jù)保證不出現(xiàn)除以0的情況文搂。
輸出格式
輸出包括八行,對(duì)應(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
分析
這是對(duì)運(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;
}