[YTU]_2535( C++复数运算符重载(+与))
題目描述
定義一個(gè)復(fù)數(shù)類(lèi)Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)的加法運(yùn)算與輸出操作。
(1)參加運(yùn)算的兩個(gè)運(yùn)算量可以都是類(lèi)對(duì)象,也可以其中有一個(gè)是實(shí)數(shù),順序任意。例如,c1+c2,d+c1,c1+d均合法(設(shè)d為實(shí)數(shù),c1,c2為復(fù)數(shù))。
(2)輸出的算數(shù),在復(fù)數(shù)兩端加上括號(hào),實(shí)部和虛部均保留兩位小數(shù),如(8.23+2.00i)、(7.45-3.40i)、(-3.25+4.13i)等。
編寫(xiě)程序,分別求兩個(gè)復(fù)數(shù)之和、整數(shù)和復(fù)數(shù)之和,并且輸出。
請(qǐng)?jiān)谙旅娴某绦蚨位A(chǔ)上完成設(shè)計(jì):
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
Complex():real(0),imag(0) {}
Complex(double r,double i):real(r),imag(i) {}
Complex operator+(Complex &);
Complex operator+(double &);
friend Complex operator+(double&,Complex &);
friend ostream& operator << (ostream& output, const Complex& c);
private:
double real;
double imag;
};
//將程序需要的其他成份寫(xiě)在下面,只提交begin到end部分的代碼
//******************** begin ********************
//********************* end ********************
int main()
{
//測(cè)試復(fù)數(shù)加復(fù)數(shù)
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
Complex c3=c1+c2;
cout<<"c1+c2=";
cout<<c3;
//測(cè)試復(fù)數(shù)加實(shí)數(shù)
double d;
cin>>real>>imag;
cin>>d;
c3=Complex(real,imag)+d;
cout<<"c1+d=";
cout<<c3;
//測(cè)試實(shí)數(shù)加復(fù)數(shù)
cin>>d;
cin>>real>>imag;
c1=Complex(real,imag);
c3=d+c1;
cout<<"d+c1=";
cout<<c3;
return 0;
}
輸入
一個(gè)復(fù)數(shù)的實(shí)部和虛部,另一個(gè)復(fù)數(shù)的實(shí)部和虛部?
一個(gè)復(fù)數(shù)的實(shí)部和虛部,一個(gè)實(shí)數(shù)
一個(gè)實(shí)數(shù),一個(gè)復(fù)數(shù)的實(shí)部和虛部
輸出
兩個(gè)復(fù)數(shù)之和、復(fù)數(shù)和實(shí)數(shù)之和,實(shí)數(shù)和復(fù)數(shù)之和。
樣例輸入
3 4 5 -10 3 4 5 5 3 4樣例輸出
c1+c2=(8.00-6.00i) c1+d=(8.00+4.00i) d+c1=(8.00+4.00i)提示
只提交自己定義的函數(shù)部分,控制輸出小數(shù)點(diǎn)后兩位,用 setiosflags(ios::fixed);和setprecision(2);控制
#include <iostream> #include <iomanip> using namespace std;class Complex { public:Complex():real(0),imag(0) {}Complex(double r,double i):real(r),imag(i) {}Complex operator+(Complex &);Complex operator+(double &);friend Complex operator+(double&,Complex &);friend ostream& operator << (ostream& output, const Complex& c); private:double real;double imag; };Complex Complex::operator+(Complex &c) {return Complex(real+c.real,imag+c.imag);} Complex Complex::operator+(double &r) {return Complex(real+r,imag);} Complex operator+(double &r,Complex&c) {return Complex(r+c.real,c.imag);} ostream& operator<<(ostream &cout,const Complex &c) {cout<<setiosflags(ios::fixed);cout<<setprecision(2);cout<<'('<<c.real; if(c.imag>0) cout<<'+'; cout<<c.imag<<"i)"<<endl; }int main() {//測(cè)試復(fù)數(shù)加復(fù)數(shù)double real,imag;cin>>real>>imag;Complex c1(real,imag);cin>>real>>imag;Complex c2(real,imag);Complex c3=c1+c2;cout<<"c1+c2=";cout<<c3;//測(cè)試復(fù)數(shù)加實(shí)數(shù)double d;cin>>real>>imag;cin>>d;c3=Complex(real,imag)+d;cout<<"c1+d=";cout<<c3;//測(cè)試實(shí)數(shù)加復(fù)數(shù)cin>>d;cin>>real>>imag;c1=Complex(real,imag);c3=d+c1;cout<<"d+c1=";cout<<c3;return 0; }總結(jié)
以上是生活随笔為你收集整理的[YTU]_2535( C++复数运算符重载(+与))的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [YTU]_2354 (实现复数类中的
- 下一篇: [YTU]_2444(C++习题 对象转