1. 程式人生 > 其它 >19C20C 石俊傑 第三次上機作業

19C20C 石俊傑 第三次上機作業

第三次上機作業

19C20C-石俊傑-20191003535

題1: 類的定義與基本操作

class Fraction {
//資料成員,訪問控制屬性預設是私有
int m_numerator = 0; // 分子預設為0; C++11
int m_denominator = 1; //分母預設為1;
public://公有成員函式
Fraction(int above = 0, int below = 1) : 
m_numerator(above), m_denominator(below) {
cout << "Constructor called" << endl;
}
Fraction
(const Fraction& rhs) : m_numerator(rhs.m_numerator), \ m_denominator(rhs.m_denominator) { cout << "Copy constructor called" << endl; } }; Fraction divide1(const Fraction& divident, const Fraction& divisor) { return Fraction(divident.getnumerator() * divisor.getdenominator
(), \ divident.getdenominator() * divisor.getnumerator()); } Fraction divide2(Fraction divident, Fraction divisor) { Fraction result(divident.getnumerator() * divisor.getdenominator(), \ divident.getdenominator() * divisor.getnumerator()); return result; }

1.1 執行下列語句後,分別執行的什麼操作,會輸出什麼

Fraction a;
Fraction b(a);

Fraction c = Fraction(3, 2);
Fraction d1(2, 3), d2(4, 5);
Fraction e1 = divide1(d1, d2);
Fraction e2 = divide2(d1, d2);

執行操作:
a呼叫建構函式
b呼叫拷貝建構函式
(3,2)呼叫建構函式
d1呼叫建構函式
d2呼叫建構函式
divide1函式呼叫建構函式
divide2函式divisor呼叫拷貝建構函式
divide2函式divident呼叫拷貝建構函式
divide2函式result呼叫建構函式
divide2函式返回值呼叫拷貝建構函式
解構函式

輸出結果:
Constructor called
Copy constructor called
Constructor called
Constructor called
Constructor called
Constructor called
Copy constructor called
Copy constructor called
Constructor called
Copy constructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called

1.2 在上述類的定義基礎上,完善下列操作

1) 顯示定義解構函式

    ~Fraction() { cout << "Destructor called" << endl; }

2) 獲取分數的分子

    int getnumerator()const {//獲取分數的分子
	    return this ->m_numerator;
    }

3) 獲取分數的分母

    int getdenominator()const {//獲取分數的分母
    	return this->m_denominator;
    }

4) 實現分數的約分

	Fraction reduction() {
		int maxNum = gcd(m_numerator, m_denominator);
		m_numerator /= maxNum;
		m_denominator /= maxNum;
		return *this;
	}

5) 實現對兩個分數物件進行通分

Fraction common(const Fraction& divident, const Fraction& divisor) {
	Fraction a(divident.getnumerator() * divisor.getnumerator(), divident.getdenominator() * divisor.getdenominator());
	return a.reduction();
}

6) 使用 operator/操作符過載實現兩個分數的除法運算

	Fraction operator/ (const Fraction& right) {
		Fraction result(m_numerator * right.getdenominator(), m_denominator * right.getnumerator());
		result.reduction();
		return result;
	}

題2: 陣列與函式的綜合應用

已知:int a[5] = {19, 67,24,11,17}, b[5] = {2,3,9,17,59};
編寫程式查詢陣列中是否存在某個指定元素;將陣列a和陣列b中的素數不重不漏地合併到一個vector容器c中,然後按照下標訪問的方式手動對容器c中的資料,按從小到大順序重新

2.1 編寫順序查詢法函式和折半查詢法函式,分別在陣列a和陣列b中查詢元素17所在的下標並輸出

//順序查詢
void find1(int a[], int demand, int n)
{
	bool x = 1;
	for (int i = 0; i < n; i++) {
		if (demand == a[i]) {
			x = 0;
			cout << i << endl;
		}
	}
	if (x) cout << "不存在該元素" << endl;
}
//交換
void swap(int& c1, int& c2) {
	int c = c1; c1 = c2; c2 = c;
}
//交換排序
void sort(int a[], int n)
{
	for (int i = 0; i < n - 1; i++) {
		for (int j = i; j < n; j++) {
			if (a[i] > a[j]) {
				swap(a[i], a[j]);
			}
		}
	}
}
//折半查詢
void find2(int a[], int demand, int n) {
	sort(a, n);
	int min = 0;
	int max = n;
	while (min <= max) {
		int mid = (min + max) / 2;
		if (a[mid] == demand) {
			cout<<mid<<endl;
			return;
		}
		else if (a[mid] < demand) {
			min = mid + 1;
		}
		else {
			max = mid - 1;
		}
	}
	cout << "不存在該元素" << endl;
}

在這裡插入圖片描述

2.2 編寫判斷素數函式和排序函式,並對容器c中的結果進行輸出

//判斷素數
bool is_prime(int a) {
	if (a <= 1)return false;
	int b = int(sqrt(a * 1.0));//開方
	for (int i = 2; i <= b; i++) {
		if (a % i == 0)return false;
	}
	return true;
}
//交換排序
void sort(vector<int>&a)
{
	int n = a.size();
	for (int i = 0; i < n - 1; i++) {
		for (int j = i; j < n; j++) {
			if (a[i] > a[j]) {
				swap(a[i], a[j]);
			}
		}
	}
}

主函式:

#include <iostream>
#include <vector>
using namespace std;

int main(){
	int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
	//合併素數
	vector<int> c;
	for (int i = 0; i < 5; ++i) {
		if (is_prime(a[i]))c.push_back(a[i]);
		if (is_prime(b[i]))c.push_back(b[i]);
	}
	
	//排序
	sort(c);
	//刪除重複值
	c.erase(unique(c.begin(),c.end()), c.end());
	//輸出
	int len = c.size();
	for (int i = 0; i < len; ++i) {
		cout << c[i] << ends;
	}
	
    return 0;
}

輸出結果
在這裡插入圖片描述

題目3:類的定義與基本操作

class Point {
 double m_x = 0, m_y = 0;
public:
 Point(double x=0, double y=0) : m_x(x), m_y(y) {
 cout << "Constructor of Point" << endl;
 }
 Point(const Point &p) :m_x(p.m_x), m_y(p.m_y) {
 cout << "Copy constructor of Point" << endl;
 }
 ~Point() {
 cout << "Destructor of Point" << endl;
 }
};
class Circle {
 Point m_center; double m_radius = 1.0;
public:
 Circle(double r=1, const Point &p=Point()) :m_center(p), m_radius(r) {
 cout << "Constructor of Circle" << endl;
 }
 ~Circle() { 
 cout << "Destructor of Circle" << endl; 
 }
};
int main()
{
 Circle a(2, Point(1, 1));
 cout << "end" << endl;
return 0;
}

3.1 說明上述程式執行流程和輸出結果
執行流程:
Point(1,1)呼叫建構函式
a呼叫拷貝建構函式
a呼叫建構函式
Point解構函式
輸出end
Circle解構函式
Point解構函式

輸出結果:
Constructor of Point
Copy constructor of Point
Constructor of Circle
Destructor of Point
end
Destructor of Circle
Destructor of Point

3.2 在Point類中完善獲取點的橫座標、獲取點的縱座標成員函式,並在主函式中測試

	double get_x() const{return this -> m_x;}//獲取點的橫座標
	double get_y() const{return this -> m_y; }//獲取點的縱座標

主函式:

#include <iostream>
#include <vector>
using namespace std;

int main(){
	//獲取點的橫縱座標
	Point p1(1, 2);
	cout << "p1的橫座標為:"<<p1.get_x() << endl;
	cout << "p1的縱座標為:" << p1.get_y() << endl;
	
	return 0;
}

執行結果:
在這裡插入圖片描述

3.3 通過友元函式實現平面上任意兩個點的距離計算輔助函式

friend double distance(Point& p1, Point& p2);
double distance(Point& a, Point& b) {
	return sqrt((a.m_x - b.m_x) * (a.m_x - b.m_x) + (a.m_y - b.m_y) * (a.m_y - b.m_y));
}

3.4 在Circle類中完善圓的面積計算與圓的周長計算成員函式,並在主函式中測試

	double area() {
		return 3.14 * m_radius * m_radius;
	}
	double circumference() {
		return 2 * 3.14 * m_radius;
	}

主函式:

#include <iostream>
#include <vector>
using namespace std;

int main(){
	Circle a(2, Point(1, 1));
	//計算圓的面積與周長
	cout << "a的面積為:"<<a.area() << endl;
    cout<<"a的周長為:"<< a.circumference() << endl;
	
	return 0;
}

執行結果:
在這裡插入圖片描述