1. 程式人生 > >YTUOJ——複數類--過載運算子+,-,*,/

YTUOJ——複數類--過載運算子+,-,*,/

#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
    Complex();
    Complex(double r,double i);
    Complex operator+(Complex &c2);
    Complex operator-(Complex &c2);
    Complex operator*(Complex &c2);
    Complex operator/(Complex &c2);
    void display();
private:
    double real;
    double imag;
};

Complex::Complex(){}

Complex::Complex(double r,double i){
	real = r;
	imag = i;
}

Complex Complex::operator+(Complex &c2) {
	Complex c; 
	c.real=real+c2.real; 
	c.imag=imag+c2.imag; 
	return c;
} 
  
Complex Complex::operator-(Complex &c2) {
	Complex c; 
	c.real=real-c2.real; 
	c.imag=imag-c2.imag; 
	return c;
}  
Complex Complex::operator*(Complex &c2) {
	Complex c; 
	c.real=real*c2.real-imag*c2.imag; 
	c.imag=imag*c2.real+real*c2.imag; 
	return c;
} 
 
Complex Complex::operator/(Complex &c2) {
	Complex c;  
	c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);  
	c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); 
	return c;
}
  
void Complex::display() {
	cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
//主函式已給定如下,提交時不需要包含,會自動新增到程式尾部
/* C++程式碼 */
int main()
{
    double real,imag;
    cin>>real>>imag;
    Complex c1(real,imag);
    cin>>real>>imag;
    Complex c2(real,imag);
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    Complex c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    return 0;
}

題目描述

定義一個複數類Complex,過載運算子“+”,“-”,“*”,“/”,使之能用於複數的加、減、乘、除。運算子過載函式作為Complex類的成員函式。編寫程式,分別求兩個複數之和、差、積和商。

輸入

兩個複數

輸出

兩個複數之和、差、積和商

樣例輸入

3 4
5 -10

樣例輸出

c1+c2=(8.00,-6.00i)
c1-c2=(-2.00,14.00i)
c1*c2=(55.00,-10.00i)
c1/c2=(-0.20,0.40i)

提示

前置程式碼及型別定義已給定如下,提交時不需要包含,會自動新增到程式前部  

/* C++程式碼 */
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
    Complex();
    Complex(double r,double i);
    Complex operator+(Complex &c2);
    Complex operator-(Complex &c2);
    Complex operator*(Complex &c2);
    Complex operator/(Complex &c2);
    void display();
private:
    double real;
    double imag;
};

主函式已給定如下,提交時不需要包含,會自動新增到程式尾部  

/* C++程式碼 */
int main()
{
    double real,imag;
    cin>>real>>imag;
    Complex c1(real,imag);
    cin>>real>>imag;
    Complex c2(real,imag);
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    Complex c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    return 0;
}

答案

#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
    Complex();
    Complex(double r,double i);
    Complex operator+(Complex &c2);
    Complex operator-(Complex &c2);
    Complex operator*(Complex &c2);
    Complex operator/(Complex &c2);
    void display();
private:
    double real;
    double imag;
};

Complex::Complex(){}

Complex::Complex(double r,double i){
	real = r;
	imag = i;
}

Complex Complex::operator+(Complex &c2) {
	Complex c; 
	c.real=real+c2.real; 
	c.imag=imag+c2.imag; 
	return c;
} 
  
Complex Complex::operator-(Complex &c2) {
	Complex c; 
	c.real=real-c2.real; 
	c.imag=imag-c2.imag; 
	return c;
}  
Complex Complex::operator*(Complex &c2) {
	Complex c; 
	c.real=real*c2.real-imag*c2.imag; 
	c.imag=imag*c2.real+real*c2.imag; 
	return c;
} 
 
Complex Complex::operator/(Complex &c2) {
	Complex c;  
	c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);  
	c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); 
	return c;
}
  
void Complex::display() {
	cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
//主函式已給定如下,提交時不需要包含,會自動新增到程式尾部
/* C++程式碼 */
int main()
{
    double real,imag;
    cin>>real>>imag;
    Complex c1(real,imag);
    cin>>real>>imag;
    Complex c2(real,imag);
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    Complex c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    return 0;
}