上機作業1 2

baixiaosen1228發表於2020-12-22
1.
#include<iostream>
using namespace std;
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;
	}
public:
	int getnumerator() const { return m_numerator; }
	int getdenominator() const { return m_denominator; }
	double value() const; //計算分數值
	void reduce(); //約分
private:
	int gcd(int x, int y); //求分子分母最大公約數
	void makeCommond(Fraction f);//通分
	Fraction operator /(const Fraction& right) { //使用 operator/操作符過載實現兩個分數的除法運算
		Fraction result(m_numerator * right.getdenominator(), m_denominator * right.getdenominator());
		return result;
	}
public:
	~Fraction() {}
};
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;
}
inline double Fraction::value() const {
	return static_cast<double>(m_numerator) / m_denominator;
}
void Fraction::reduce() {
	int n = gcd(m_numerator, m_denominator); //獲取分子分母的最大公約數
	m_denominator /= n;
	m_numerator /= n;
}
int Fraction::gcd(int c, int d) {
	if (d == 0)
		return c;
	return gcd(d, c % d);
}
void Fraction::makeCommond(Fraction f)
{
	m_denominator = m_denominator * f.m_denominator;
	m_numerator = m_numerator * f.m_denominator;
}
2.
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main() {
	int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
	int c = 17;
	for (int i = 0; i < 5; i++) //順序查詢法
	{
		if (c == a[i]) {
			cout << "該資料在a陣列下標為" << i << endl;
			break;
		}
		else if (i == 4)
			cout << "a陣列中不存在該資料" << endl;
	}
	//折半查詢法
	if (c<b[0] || c>b[4])
		cout << "b陣列中不存在該資料" << endl;
	else if (c == b[2])
		cout << "該資料在b陣列下標為2" << endl;
	else if (c > b[2]) {
		for (int j = 3; j < 5; j++) {
			if (c == b[j]) {
				cout << "該資料在b陣列下標為" << j << endl;
				break;
			}
			else if (j == 4)
				cout << "b陣列中不存在該資料" << endl;
		}
	}
	else {
		for (int j = 0; j < 2; j++) {
			if (c == b[j]) {
				cout << "該資料在b陣列下標為" << j << endl;
				break;
			}
			else if (j == 2)
				cout << "b陣列中不存在該資料" << endl;
		}
	}
	vector<int> v;
	int p = 0;
	for (int i = 0;i < 5; i++) {
		int q = 1;
		for (int j = 2; j <= sqrt(a[i]); j++) {
			if ((a[i] % j) == 0) {
				q = 0;
				break;
			}
			else continue;
		}
		if (q) {
			v.push_back(a[i]);
			p++;
		}
	}
	for (int i = 0; i < 5; i++) {
		int q = 1;
		for (int j = 2; j <= sqrt(b[i]); j++) {
			if ((b[i] % j) == 0) {
				q = 0;
				break;
			}
			else continue;
		}

		if (q&&b[i]!=17) {
			v.push_back(b[i]);
			p++;
		}
	}
	for (int i = 0; i < p - 1; i++) {
		for (int j = i + 1; j < p; j++) {
			if (v[j] < v[i]) {
				int t = v[j];
				v[j] = v[i];
				v[i] = t;
			}
		}
	}
	for (int i = 0; i < p; i++) {
		cout << v[i] << endl;
	}
	return 0;
}

相關文章