第三次上機

Hikd77發表於2020-12-24

第三次上機
題目 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;
}

說明執行下列語句後,分別執行的什麼操作,會輸出什麼?
Fraction a;執行預設建構函式,輸出 Constructor called

Fraction b(a);b為物件a的副本,執行復制建構函式,輸出 Copy constructor called

Fraction c = Fraction(3, 2);複製初始化,先構造一個臨時物件,然後將這個臨時物件以複製構造的方式來初始化物件c,輸出 Constructor called Copy constructor called
c++編譯器使用優化技術,直接初始化方式完成這一操作, 實際輸出為Constructor called

Fraction d1(2, 3), d2(4, 5);直接初始化兩個物件d1,d2,執行兩次建構函式
輸出Constructor called Constructor called

Fraction e1 = divide1(d1, d2);執行divide1函式返回一個臨時物件,執行復制建構函式對e1進行賦值
輸出 Copy constructor called

Fraction e2 = divide2(d1, d2);執行divide2函式返回一個臨時物件,執行復制建構函式對e2進行賦值
輸出 Copy constructor called

#include <iostream>
using namespace std;
class Fraction {
public://公有成員函式
	int m_numerator = 0; // 分子預設為0; 
	int m_denominator = 1; //分母預設為1;
	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() {
		cout << "Detructor of Fraction";
	}
	int getnumerator()const { return m_numerator; }//獲取分子
	int getdenominator() const { return m_denominator; }//獲取分母
	int gcd(int x, int y) {
		if (y != 0)
			return reduce(y, x % y);
		else return x;
	}//求最大公約數函式
	double reduce(int x, int y) {
		int i = gcd(m_numerator, m_denominator);
		double n = m_numerator / i;
		double d = m_denominator / i;
		double result = n / d;
		return result;
	}//約分
	Fraction tf(Fraction x, Fraction y) 
	{
		int i = x.m_denominator * y.m_denominator;
		int o = x.m_numerator * y.m_denominator;
		int p = y.m_numerator * x.m_denominator;
		x.m_numerator = o;
		x.m_denominator = i;
		y.m_denominator = i;
		y.m_numerator = p;		
		return x, y;
	}//通分
	
};
Fraction operator/(const Fraction& x, const Fraction& y)
{
	return Fraction(x.m_numerator * y.m_denominator, x.m_denominator * y.m_numerator);
}

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;
}
int main() {
	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);

	return 0;
}

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

  1. 編寫順序查詢法函式和折半查詢法函式,分別在陣列a和陣列b中查詢元素17所在的下標
    並輸出。
  2. 編寫判斷素數函式和排序函式,並對容器c中的結果進行輸出。
#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
vector<int>v;
 //順序查詢法函式
int sx(int x[5]) {
	for (int i = 0; i < 5; ++i)
	{
		if (x[i] == 17)return i;
	}

}
//折半查詢函式
int zb(int x[5])
{
	int i = 0, j = 4, h;
	h = (i + j) / 2;
	if (x[h] == 17) return h;
	if (x[h] > 17) 
	{
		j = h - 1;
		if (x[j] == 17)return j;
		else { return i; }
	}
	if (x[h] < 17)
	{
		i = h + 1;
		if(x[i] == 17)return i;
		else { return j; }
	}
}
//排序函式
int px(int x[5]) {
	for (int i = 1; i < 5; i++)
	{
		for (int j = 0; j < 5 - i; j++)
		{
			if (x[j + 1] < x[j])
			{
				int t = x[j + 1];
				x[j + 1] = x[j];
				x[j] = t;
			}
		}
	}
	for (int i = 0; i < 10; i++)
	{
		v[i] += x[i];
	}
	return x[5];
}
//判斷素數函式
int sh(int x)
{
	int m = sqrt(x);
		for (int i = 2; i <= m; i++)
		{
			if (x % i != 0)return x;
			else { break; }			
		}
}
int main() {
	vector <int> c;
	int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
		cout << sx(a) << endl;
		cout<< zb(b) << endl;		

		for (int i = 0; i < 5; i++) 
		{   
			if (sh(a[i]) != 0) { c.push_back(a[i]); }
			if (sh(b[i]) != 0&&b[i]!=a[i]) { c.push_back(b[i]); }
		}
		
		for(int i = 0; i <= size(c) - 1; i++)
		{
			for(int j=0;j<
		}
	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§, m_radius® {
cout << “Constructor of Circle” << endl;
}
~Circle() {
cout << “Destructor of Circle” << endl;
}
};
int main()
{
Circle a(2, Point(1, 1));
cout << “end” << endl;
return 0;
}

  1. 說明上述程式執行流程和輸出結果;
  2. 在Point類中完善獲取點的橫座標、獲取點的縱座標成員函式,並在主函式中測試;
  3. 通過友元函式實現平面上任意兩個點的距離計算輔助函式;
  4. 在Circle類中完善圓的面積計算與圓的周長計算成員函式,並在主函式中測試;

1.執行流程:首先呼叫point建構函式、circle建構函式、再呼叫point解構函式、circle解構函式
輸出:Constructor of Point
Copy constructor of Point
Constructor of Circle
Destructor of Point
Destructor of Circle
end

#include <iostream>
#include<cmath>
using namespace std;
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;
	}
	double getpointx() { return m_x;}	
	double getpointy() { return m_y; }
	friend double len(const Point&a,const Point&b )
};
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;
	}
};

double len(const Point& a, const Point& b)
{
	double s = a.m_x - b.m_x, m = a.m_y - b.m_y;
	double length = sqrt(s * s + m * m);
	return length;
}


int main()
{
	Circle a(2, Point(1, 1));
	cout << "end" << endl;
	Point b(5, 7);
	cout << b.getpointx() << endl << b.getpointy();
	return 0;
}