第三次上机作业

题目 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;//定义Fraction类型对象a,并调用构造函数
Fraction b(a);//调用拷贝构造函数,通过括号法将a的属性拷贝给b
Fraction c = Fraction(3, 2);//调用构造函数,通过显示法对c的属性进行赋值
Fraction d1(2, 3), d2(4, 5);//调用构造函数,通过括号法对d1,d2的属性进行赋值
Fraction e1 = divide1(d1, d2);//调用divide1函数使d1与d2相除,并将返回值赋值给e1的属性,调用构造函数
Fraction e2 = divide2(d1, d2)//调用devide2函数使d1与d2相除,并将返回的对象result拷贝给e2,调用拷贝构造函数

输出结果

Constructor called//对象a调用
Copy constructor called//对象b调用
Constructor called//对象c调用
Constructor called//对象d1调用
Constructor called//对象d2调用
Constructor called//对象e1调用
Copy constructor called//实参d1拷贝给函数divide2中形参divident时调用
Copy constructor called//实参d2拷贝给函数divide2中形参divisor时调用
Constructor called//对象result调用
Copy constructor called//将devide2函数返回结果result拷贝给e2时调用

在上述类的定义基础上,完善下列操作:

  1. 显示定义析构函数;
~Fraction(){
cout << "Fraction析构函数的调用" << endl;
}
  1. 获取分数的分子;
//类内定义
public://公有成员函数
	int getnumerator() const{
		return m_numerator;
	}
  1. 获取分数的分母;
//类内定义
publicint getdenominator()const{
		return m_denominator;
	}
  1. 实现分数的约分;
int gcd(int m_numerator, int m_denominator)
{
	if (m_numerator % m_denominator == 0)return m_denominator;
	if (m_numerator% m_denominator != 0)return gcd(m_denominator, m_numerator%m_denominator);
}//定义全局函数gcd求分子与分母的最大公约数

void func1()//类内定义成员函数func,使分子分母除以最大公约数
{
	int n = gcd(m_numerator, m_denominator);
	m_numerator /= n;
	m_denominator /= n;
}
  1. 实现对两个分数对象进行通分;
void func2(Fraction& d1, Fraction& d2) {
	int n = gcd(d1.m_denominator, d2.m_denominator);
	int m = d1.m_denominator;
	d1.m_numerator *= (d2.m_denominator / n);
	d1.m_denominator *= (d2.m_denominator / n);
	d2.m_numerator *= (m / n);
	d2.m_denominator *=( m / n);
}
  1. 使用 operator/操作符重载实现两个分数的除法运算。
Fraction operator/(const Fraction& divident, const Fraction& divisor) {
	 Fraction result(divident.getnumerator() * divisor.getdenominator(), \
		divident.getdenominator() * divisor.getnumerator());
	 return result;
}//返回的对象result即为两个分数相除所得的新分数

题目 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所在的下标
    并输出。
#include<iostream>
using namespace std;

int search(int a[])//顺序查找法函数 
{
	for (int i = 0; i <= 4; i++)
	{
		if (a[i] == 17)return i;
	}
	return 0;
}

int mesearch(int a[], int low, int high)//折半查找法函数 
{
	while (low <= high)
	{
		int pos = (low + high) / 2;
		if (17 < a[pos])high = pos - 1;
		else if (17 > a[pos])low = pos + 1;
		else return pos;
	}
	if (low > high)return -1;
}

int main() {
	int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
	cout << search(a)<< endl;
	cout << mesearch(b, 0, 4) << endl;
	return 0;
}
  1. 编写判断素数函数和排序函数,并对容器c中的结果进行输出。
#include<iostream>
#include<vector>
using namespace std;

//判断数组中每个元素是否为素数,并将其中的素数放入已定义的容器c中
void func1(int a[],vector<int>&c) 
{
	for (int i = 0; i <= 4; i++)
	{
		int num = 0;
		for (int j = 1; j <= a[i]; j++) 
		{
			if (a[i] % j == 0)num++;
		}
		if (num == 2) c.push_back(a[i]);
	}
}

//(冒泡)排序函数,将容器c中的元素进行从小到大排序
void func2(vector<int>& c)
{
	for(int i=1;i<c.size();i++)
	{
		for (int j = 0; j < c.size()-i; j++)
		{
			if (c[j] > c[j + 1])
			{
				int n = c[j];
				c[j] = c[j + 1];
				c[j + 1] = n;
			}
		}
	}
}

int main() 
{
	vector<int> c;
	int a[5] = { 19,67,24,11,17 }int b[5] = { 2,3,9,17,59 };
	func1(a, c);
	func1(b, c);
	func2(c);
	for(int i=0;i<c.size();i++)
	{
		cout << c[i] << endl;
	}//对容器c中的结果进行输出
	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;
}

1) 说明上述程序执行流程和输出结果;
1.Circle a(2,Point(1,1))中Point(1,1)调用了Point类的有参构造函数,输出“Constructor of Point”
2.Point(1,1)的属性通过Point类中的拷贝构造函数赋值给Circle类中的成员m_center,输出“Copy constructor of Point”
3.Circle类对象a的初始化调用了Circle类中的有参构造函数,输出“Constructor of Circle”
4.Point(1,1)为临时对象,对a的属性进行初始化后立即销毁,调用了Point类中的析构函数,输出“Destructor of Point”
5.程序终止前Circle类对象a与Point类对象m_center还未销毁,因此先输出“end”
6.程序结束,按照初始化顺序的逆序依次销毁每个成员,因此先调用Circle类中的析构函数再调用Point类中的析构函数,依次输出“Destructor of Circle”与“Destructor of Point”

Constructor of Point
Copy constructor of Point
Constructor of Circle
Destructor of Point
end
Destructor of Circle
Destructor of Point

2) 在Point类中完善获取点的横坐标、获取点的纵坐标成员函数,并在主函数中测试;

Public://类内定义的成员函数
	double getx() {
		return m_x;
	}//获取点的横坐标
	double gety() {
		return m_y;
	}//获取点的纵坐标

int main()
{
	Point p;
	cout << p.getx() << endl;
	cout << p.gety() << endl;
	return 0;
}//在主函数中测试

3) 通过友元函数实现平面上任意两个点的距离计算辅助函数;

friend double calculates(Point p1, Point p2);
//类的辅助函数声明为友元

double calculates(Point p1, Point p2) 
{
	double num = (p1.getx() - p2.getx()) * (p1.getx() - p2.getx())/
	+ (p1.gety() - p2.gety()) * (p1.gety() - p2.gety());
	return sqrt(num);
}//计算两点间的距离

4) 在Circle类中完善圆的面积计算与圆的周长计算成员函数,并在主函数中测试;

public//类内定义成员函数
    double calculateS() {
		return 3.14 * m_radius * m_radius;
	}//计算圆的面积
	double calculateC() {
		return 2 * 3.14 * m_radius;
	}//计算圆的周长

int main()
{
	Circle a(2, Point(1, 1));
	cout << a.calculateS() << endl;
	cout << a.calculateC() << endl;
	return 0;
}//在主函数中测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值