https://wiki.vnoj.io.vn

Bài 3. Số phức

Bài 3. Số phức

Bài toán lớp số phức trong C++

Họ và tên: Vàng Văn Quyn
Nhóm: 01 (Bảo Thắng + Si Ma Cai)
Lớp: Tin A20-PH Lào Cai.
Môi trường: Visual Studio 2019 (Console)
Hãy lập chương trình xây dựng lớp số phức để thực hiện các công việc sau:
• Hàm tạo lập để gán các giá trị mặc định cho các dữ liệu thành phần là 0.
• Hàm tạo lập để gán các giá trị cho các dữ liệu thành phần là các số thực.
• Phép toán tải bội-nạp chồng toán tử: + - * / trên lớp các số phức.
• In ra các dữ liệu thành phần của lớp số phức và kết quả thực hiện các phép toán.
* Nạp chồng toán tử một ngôi

// Baikiemtra3.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include <iomanip>
using namespace std;

class SOPHUC {
private:
	double Re, Im;

public:
	SOPHUC() {
		this->Re = 0;
		this->Im = 0;
	}

	SOPHUC(double Re, double Im) {
		this->Re = Re;
		this->Im = Im;
	}

	void INRA() {
		cout << Re << (Im >= 0 ? '+' : '-')
			<< fabs(Im) << "i" << endl;
	}

	SOPHUC operator + (SOPHUC sp) {
		return SOPHUC(this->Re + sp.Re, this->Im + sp.Im);
	}

	SOPHUC operator - (SOPHUC sp) {
		return SOPHUC(this->Re - sp.Re, this->Im - sp.Im);
	}

	SOPHUC operator * (SOPHUC sp) {
		return SOPHUC(this->Re * sp.Re - this->Im * sp.Im,
			this->Re * sp.Im + this->Im * sp.Re);
	}

	SOPHUC operator / (SOPHUC sp) {
		return SOPHUC(
			(sp.Re * this->Re + sp.Im * this->Im) / (sp.Re * sp.Re + sp.Im * sp.Im),
			(sp.Re * this->Im - sp.Im * this->Re) / (sp.Re * sp.Re + sp.Im * sp.Im)
		);
	}
};

int main() {
	SOPHUC s1, s2, s3, s4, s5, s6;

	s1 = SOPHUC(2.5, 3.8);
	s2 = SOPHUC(2.1, 2.8);

	s3 = s1 + s2;
	s4 = s1 - s2;
	s5 = s1 * s2;
	s6 = s1 / s2;

	s3.INRA();
	s4.INRA();
	s5.INRA();
	s6.INRA();

	return 0;
}

* Nạp chồng toán tử hai ngôi (hàm bạn)

// Baikiemtra3.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include <iomanip>
using namespace std;

class SOPHUC {
private:
	double Re, Im;

public:
	SOPHUC() {
		this->Re = 0;
		this->Im = 0;
	}

	SOPHUC(double Re, double Im) {
		this->Re = Re;
		this->Im = Im;
	}

	void INRA() {
		cout << Re << (Im >= 0 ? '+' : '-')
			<< fabs(Im) << "i" << endl;
	}

	friend SOPHUC operator + (SOPHUC sp1, SOPHUC sp2) {
		return SOPHUC(sp1.Re + sp2.Re, sp1.Im + sp2.Im);
	}

	friend SOPHUC operator - (SOPHUC sp1, SOPHUC sp2) {
		return SOPHUC(sp1.Re - sp2.Re, sp1.Im - sp2.Im);
	}

	friend SOPHUC operator * (SOPHUC sp1, SOPHUC sp2) {
		return SOPHUC(
			sp1.Re * sp2.Re - sp1.Im * sp2.Im,
			sp1.Re * sp2.Im + sp1.Im * sp2.Re
		);
	}

	friend SOPHUC operator / (SOPHUC sp1, SOPHUC sp2) {
		return SOPHUC(
			(sp2.Re * sp1.Re + sp2.Im * sp1.Im) / (sp2.Re * sp2.Re + sp2.Im * sp2.Im),
			(sp2.Re * sp1.Im - sp2.Im * sp1.Re) / (sp2.Re * sp2.Re + sp2.Im * sp2.Im)
		);
	}
};

int main() {
	SOPHUC s1, s2, s3, s4, s5, s6;

	s1 = SOPHUC(2.5, 3.8);
	s2 = SOPHUC(2.1, 2.8);

	s3 = s1 + s2;
	s4 = s1 - s2;
	s5 = s1 * s2;
	s6 = s1 / s2;

	s3.INRA();
	s4.INRA();
	s5.INRA();
	s6.INRA();

	return 0;
}