2020,12,20

加粗样式第一题

#include<iostream>
using namespace std;

int gcd(int a, int b) {//利用递归的方法获取最大公因数
	if (a % b == 0)
	{
		return a;
	}
	else
	{
		return gcd(a, a % b);//两个数的最大公约数是小的数与两数余数的最大公因数
	}
}
//先找出最小公倍数因子
int lcm(int num1, int num2)
{
	return num1 * num2 / gcd(num1, num2);//两个数的最小公倍数等于两整数之积除以最大公约数
}
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() {
		cout << "析构" << endl;
	}

	int getnumerator()const {//获取分数的分子
		return m_numerator;
	}
	int getdenominator()const//获取分数的分母
	{
		return m_denominator;
	}
	void change_numerator(int new_numerator)//修改分子
	{
		this->m_numerator = new_numerator;
	}
	void change_denominator(int new_denominator)//修改分母
	{
		this->m_denominator = new_denominator;
	}
	



	Fraction Fraction_reduce()//将单个分式约分
	{
		//本质上要先找到分子和分母之间的最大公因数,然后分子分母同时除去最大公因数
		int diviser = gcd(m_numerator, m_denominator);//第一步,先找出最大公因子n
		 m_numerator /= diviser;//第二步,约分,即分子分母同时除以最大公因子n
	     m_denominator /= diviser;
		return *this;//返回分式
	}
};
Fraction divide1(const Fraction& divident, const Fraction& divisor)/*形参是引用类型,
													 直接使用d1,d2,无需创建新的类对象*/
{
	return Fraction(divident.getnumerator() * divisor.getdenominator(), \
		divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor)  /*形参是fraction类型,
要先创建形参对象divident和divisor,函数做形参实例化是从右左结合,先创建divisor后创divident*/
{
	Fraction result(divident.getnumerator() * divisor.getdenominator(), \
		divident.getdenominator() * divisor.getnumerator()); 
	return result;
}

//将两个分式通分函数
void tongfen(Fraction& num1, Fraction& num2)
{
	//本质上先找输入分式分母和自己分母的最小公倍数,然后自己分子分母同乘最小公倍数
	int diviser = lcm(num1.getdenominator(), num2.getdenominator());

	//对num1通分,即分子分母同时乘以最小公倍数因子

	
	num1.change_numerator(num1.getnumerator() * diviser);
	num1.change_denominator(num1.getdenominator() * diviser);

	//再对num2通分
	num2.change_numerator(num2.getnumerator() * diviser);
	num2.change_denominator(num2.getdenominator() * diviser);
}
//除号运算符重载
Fraction operator/ (const Fraction& divident, const Fraction& divisor)
{
	return Fraction(divident.getnumerator() * divisor.getdenominator(), //分子部分
		divident.getdenominator() * divisor.getnumerator());    //分母部分
}


说明执行下列语句后,分别执行的什么操作,会输出什么?
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);

1.Constructor called
2.Copy constructor called
3.Constructor called
4.Constructor called
Constructor called
5.Constructor called
6.Copy constructor called
Copy constructor called
Constructor called
Copy constructor called

第二题

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };

//顺序查找法
int find(int i) 
{
	for (int j = 0; j < 5; j++) {
		if (a[j] == i) {
			return j;
			cout << endl;
		}
	}
}

int found(int i) {//折半查找法,能进行折半查找的数组本身要求有序
	int h = size(b);
	int left = 0, right = h - 1;
	while (left <= right) {
		int middle = (left + right) / 2;        //开始折半
		if (b[middle] == i)
			return middle;
		else if (b[middle] > i)
			right = middle - 1;      //改变折半的范围
		else 
			left = middle + 1;
	}
	return -1;            //查找失败返回-1


}
//判断素数
int number(int j) {
	int k = sqrt(j);
	for (int i = 2; i <= k; i++) {
		if (j % i == 0)
			break;
		else 
		{
			if (i == k)
			return j;
		}
	}
	if (k <= 2)
		return j;          //考虑到数组元素中的2,3
}
//交换函数
void swap(int& x, int& y) {       //引用传递
	if (x == y)
		return ;
	int z(x);
	x = y;
	y = z;
}

int main() {
	int s;
	cout << "请输入要查找的数=";
	cin >> s;
	cout << '\n' << "a数组中要查找的下标为" << find(s) << endl;
	cout << "b数组中要查找的下标为" << found(s) << endl;
	vector<int> vi;
	for (auto i : a) {                        //阅览a数组中的元素,找到素数后,将其添加入容器中
		if (number(i) == i)
			vi.push_back(i);
	}
	for (auto i : b) {                       // 阅览b数组中的元素,找到素数后,将其添加入容器中
		if (number(i) == i)
			vi.push_back(i);
	}
	for (int i = vi.size() - 1; i >= 0; --i) {         //采用冒泡排序法
		for (int j = 0; j <= i - 1; ++j) {
			if (vi.at(j) > vi.at(j + 1))
				swap(vi.at(j), vi.at(j + 1));          //注:此处不能写swap(j,j+1),参数列表不对,同时编译器会报错
		}
	}
	for (auto it = vi.begin(); it != vi.end(); ++it) {    //此处运用迭代来阅览容器中的元素,注意此处要用!=
		cout << *it << endl;
	}

	return 0;
}

第三题

#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;
	}
	int getm_x() const { return m_x; }          //获取横坐标
	int getm_y() const { return m_y; }          //获取纵坐标
	double juli(double x, double y);
	~Point() {
		cout << "Destructor of Point" << endl;
	}
};
double Point::juli(double x, double y) {           //计算距离函数
	double t = sqrt((x - m_x) * (x - m_x) + (y - m_y) * (y - m_y));
	return t;
}
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;
	}
	double mianji() {                             //计算面积函数
		double i = 3.14 * m_radius * m_radius;
		return i;
	}
	double zhouchang() {                            //计算周长函数
		double j = 3.14 * 2 * m_radius;
		return j;
	}
	~Circle() {
		cout << "Destructor of Circle" << endl;
	}
};

int main()
{
	Circle a(2, Point(1, 1));
	cout << Point(1, 1).getm_x() << '\t' << Point(1, 1).getm_y() << endl;
	cout << a.mianji() << '\t' << a.zhouchang() << endl;
	cout << "end" << endl;
	double x, y;
	cin >> x >> y;
	cout << '\t' << Point(1, 1).juli(x, y) << endl;      //计算两点距离,point中的点可以改变,此处以(1,1)为例
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值