第三次上机作业

题目一

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;
}

```cpp
说明执行下列语句后 ,分别执行的什么操作 , 会 输出 什么? ?
Fraction a;//定义Fraction类对象a,并调用构造函数,输出Constructor called
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,将返回值赋予e1
Fraction e2 = divide2(d1, d2);//调用divide2,将返回值赋予e2

输出结果
Constructor called
Copy constructor called
Constructor called
Constructor called
Constructor called
Constructor called
Copy constructor called
Copy constructor called
Constructor called
Copy constructor called
显示定义析构函数;

~Fraction(){
cout << "Fraction析构函数的调用" << endl;
}

获取分数的分子

//类内定义
public://公有成员函数
	int getnumerator() const{
		return m_numerator;
	}

获取分数的分母

//类内定义
publicint getdenominator()const{
		return m_denominator;
	}

约分

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;
}

通分

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);
}

查找指定元素是否存在

#include<iostream>
using namespace std;
int main() {
	int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 },t;
	cout << "请输入想查找的数字:" << endl;
	cin >> t;
	for (int i = 0; i < 5; i++) {
		if (a[i]==t) {
			cout << "存在" << endl;
			break;
			 }
		

		while (i = 4) {
			cout << "不存在" << endl;
			break;
		}
		}
	return 0;
}
## 素数
``#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>vab;
	for (int i = 0; i < 5; i++) {
		vab.push_back(a[i]);
		vab.push_back(b[i]);
		for (int j = 0; j < i; j++) {
			if (b[i] == a[j]) {
				vab.pop_back();
			}
		}
    }
	 
	int s = sizeof(vab) / sizeof(vab[0]);

	vector<int>c;
	
	for (int i = 0; i < s; i++) {
		
		for (int j = 2; j < vab.at(i); j++) {
			if (vab.at(i) % j == 0) {
				break;
			}
			while (j = vab.at(i) - 1) {
				c.push_back(vab[i]);
				
			}
        }
		int n;
		n = sizeof(c) / sizeof(c[0]);
		for (int i = 0; i < n; i++) {
			if (c[i] == 17) {
				cout << c[i] << endl;
				break;
			}
		}
		return 0;
	}

	
	
	return 0;

}

```cpp
#include<iostream>
using namespace std;

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

int zheban(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 << shunxu(a)<< endl;
	cout <<zheban(b, 0, 4) << endl;
	return 0;
}

point()调用了Point(double x=0, double y=0) 输出“Constructor of Point”
调用了拷贝构造函数,输出“Copy constructor of Point”
接着,circle类调用了构造函数,输出“Constructor of Circle”
临时对象对a初始化完毕后被销毁输出“Destructor of Point”
输出end
按照初始化的逆序,销毁每个成员,依次输出“Destructor of Circle”与“Destructor of 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;
}

2,

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);
}

3,

publicdouble 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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值