編程作業(3)

因爲作業代碼涉及隱私,所以文章只提供解題思路和有關拓展,若實在需要源碼可以私信

寫在前面:

1,類的方法裏如果想得到本類對象的話 有兩個方案:
	1,直接返回一個本類對象; (調用時需要額外創建一個對象)
	2,類的方法裏改變對象本身
2,this指針只能用於非靜態成員函數內部
3,在一個類中寫一個返回類型是該類對象的函數是很蠢的(除非很必要),因爲調用時得額外創建一個對象來調用這個函數。 直接寫在類的作用域外就挺好了。

作業一:
在這裏插入代碼片
作業二:實現一個2*2的矩陣
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;

//////////////////////////////////////////////////////////////////////
class Matrix
{
public:
	Matrix(int a11, int a12, 
		int a21, int a22);
	Matrix();
	~Matrix();

	//矩陣相乘
	static Matrix mutiply(Matrix A, Matrix B);
	//矩陣相加
	static Matrix add(Matrix A, Matrix B);
	//矩陣的逆

	double M[2][2];
};

///////////////////////////////////////////////////////////////////////

Matrix::Matrix()
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
			this->M[i][j] = 0;
	}
}

Matrix::Matrix(int a11,int a12,
	int a21, int a22)
{
	M[0][0] = a11; M[0][1] = a12;
	M[1][0] = a21; M[1][1] = a22;
}

Matrix::~Matrix(){}

Matrix Matrix::mutiply(Matrix A, Matrix B)
{
	Matrix Result;
	for (int m = 0; m < 2; m++)
	{
		for (int s = 0; s < 2; s++)
		{
			for (int n = 0; n < 2; n++)
			{
				Result.M[m][s] += A.M[m][n] * B.M[n][s];
			}
		}
	}
	return Result;
}


Matrix Matrix::add(Matrix A, Matrix B)
{
	Matrix Result;
	for (int m = 0; m < 2; m++)
	{
		for (int s = 0; s < 2; s++)
		{
			Result.M[m][s] = A.M[m][s] + B.M[m][s];
		}
	}
	return Result;
}
作業三:用一個類實現複數的加減乘除運算,使用print操作展示覆數/和不使用print操作的兩個版本

使用print

#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<vector>
using namespace std;

///////////////////////////////////////////////////////////////////////////
class Complex {
private:
	int m_x, m_y;

public:
	Complex() {}
	Complex(int x, int y) { this->m_x = x; this->m_y = y; }
	Complex(const Complex& c) { this->m_x = c.m_x; this->m_y = c.m_y; }
	void print() { cout << "(" << m_x << ", " << m_y << ")" <<endl; }
	void add(const Complex& c1, const Complex& c2);
	void mult(const Complex& c1, const Complex& c2);

};
///////////////////////////////////////////////////////////////////////////



void Complex::add(const Complex& c1, const Complex& c2) {
	this->m_x = c1.m_x + c2.m_x;
	this->m_y = c1.m_y + c2.m_y;
}

void Complex::mult(const Complex& c1, const Complex& c2) {
	this->m_x = (c1.m_x * c2.m_x) - (c1.m_y * c2.m_y);
	this->m_y = (c1.m_x + c2.m_y) + (c1.m_y * c2.m_x);
}

不使用print

#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<vector>
using namespace std;

///////////////////////////////////////////////////////////////////////////
class Complex {
private:
	int m_x, m_y;

public:
	//Complex() {}
	Complex(int x, int y) { this->m_x = x; this->m_y = y; }
	Complex(const Complex& c) { this->m_x = c.m_x; this->m_y = c.m_y; }
	//void print() { cout << "(" << m_x << ", " << m_y << ")" <<endl; }
	void add(const Complex& c1, const Complex& c2);
	void mult(const Complex& c1, const Complex& c2);

};
///////////////////////////////////////////////////////////////////////////



void Complex::add(const Complex& c1, const Complex& c2) {

	this->m_x = c1.m_x + c2.m_x;
	this->m_y = c1.m_y + c2.m_y;

	cout << "(" << this->m_x << ", " << this->m_y << ")" << endl;
	
}

void Complex::mult(const Complex& c1, const Complex& c2) {
	this->m_x = (c1.m_x * c2.m_x) - (c1.m_y * c2.m_y);
	this->m_y = (c1.m_x + c2.m_y) + (c1.m_y * c2.m_x);

	cout << "(" << this->m_x << ", " << this->m_y << ")" << endl;
}

int main() {
	int a = 1; int b = 2;
	Complex ccc1(a, b);
	Complex ccc2(a, b);
	Complex ccc(0,0);
	ccc.add(ccc1, ccc2);



	return 0;
}
作業四:實現複數的指數運算
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<vector>
using namespace std;

///////////////////////////////////////////////////////////////////////////
class Complex {
private:
	double m_x, m_y;

public:
	Complex() {}
	Complex(double x, double y) { this->m_x = x; this->m_y = y; }
	Complex(const Complex& c) { this->m_x = c.m_x; this->m_y = c.m_y; }
	void print() { cout << "(" << m_x << ", " << m_y << ")" <<endl; }
	void add(const Complex& c1, const Complex& c2);
	void mult(const Complex& c1, const Complex& c2);
	//Complex powerSomple(Complex& A, int B);

};
///////////////////////////////////////////////////////////////////////////



void Complex::add(const Complex& c1, const Complex& c2) {

	this->m_x = c1.m_x + c2.m_x;
	this->m_y = c1.m_y + c2.m_y;
	
}

void Complex::mult(const Complex& c1, const Complex& c2) {
	this->m_x = (c1.m_x * c2.m_x) - (c1.m_y * c2.m_y);
	this->m_y = (c1.m_x + c2.m_y) + (c1.m_y * c2.m_x);
}

Complex powerSomple(Complex& A, int B) {
	int Degree1[10] = { 1,2,4,8,16,32,64,128,256,512 };
	Complex Degree2[10];
	Degree2[0] = A;

	for (int i = 1; i <= 9; i++) {
		Degree2[i].mult(Degree2[i - 1], Degree2[i - 1]);
	}

	Complex Return(1, 0);
	for (int i = 9; i >= 0; i--) {
		if (B < Degree1[i]) continue;
		B -= Degree1[i];
		Return.mult(Return, Degree2[i]);
	}

	return Return;
}



int main() {
	double a = 1; double b = 2;
	Complex ccc1(a, b);
	Complex ccc = powerSomple(ccc1, 2);
	ccc.print();

	/*如果將返回值爲該類對象的函數寫在類中,則需要額外創建一個對象來調用這個函數*/
	//Complex ccc(0, 0);
	//Complex ccccc =ccc.powerSomple(ccc1, 2);
	//ccccc.print();
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章