2020-12-22

第五次上机作业
1、第一题问题中前半部分直接问的结果,所以我把他们全部打成注释了。后边部分没写题干的程序,题干程序缺少部分代码,所以我把他们也打成注释了(狗头)

#include<iostream>
using namespace std;

//1,Fraction a;    Constructor called;


//2,Fraction b(a)    
//Constructor called
//Copy constructor called

//3,
//Constructor called
//Constructor called

//4,
//Constructor called
//Constructor called


//5,
//10\12
//6,
//Constructor called
//Constructor called
//10\12

//~Fraction() {}


//int f = a.above;
//int g = a.below;


//约分

//最大公因数
//Fraction yuefen(int num,int don) {
//	if ( don%num == 0)
//		return num;
//	return(num, don % num);
//}
//int main() {
//	int numerator, denominator;
//	cin >> numerator >> denominator;
//	int c = yuefen(numerator, denominator);
//	numerator = numerator % c;
//	denominator = denominator % c;
//}


Fraction dicide3(const Fraction& divident, const Fraction& divisor){
//Fraction result((divident.getnumerator()* divisor.getdenominator() +
//	divident.getdenominator() * divisor.getnumerator()) / divisor.getdenominator() * divident.getdenominator());
//	return result;
//}



//operate重载
//Fraction opreator / (const Fraction & d1, Fraction & d2) {
   //Fraction e3 = (d1.getnumerator * d2.getdenominator, d1.getdenominator * d2.getnumerator);
   //return yuefen(e3);

2、第二题我手动写了中文注释,看起来比较轻松、

#include<iostream>
#include<vector>
using namespace std;
//判断素数
int sushu(int a) {
	int b = 1;
	if (a == 2)
		return 1;
	if (a > 2) {
		for (int i = 2; i < a; i++) {
			if (!(a % i)) 
				return 0;
		}
		return 1;
	}
}

int main() {
	int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };


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


	//折半查找法
	int x = 0, y = 4;
	int k = (x + y) / 2;
	for (int i = 0; i < 5; i++) {
		if (b[k] > 17) {
			y = k - 1;
		}
		if (b[k] == 17) {
			cout << k << endl;
			break;
		}
		if (b[k] < 17) {
			x = k + 1;
		}
		k = (x + y) / 2;
	}

	vector<int> c;
	int t = 0;
	for(int i = 0; i < 5; i++) {
		if (sushu(a[i])) {
			c.push_back(a[i]);
			t++;
		}
	}
	for (int i = 0; i < 5; i++) {
		if (sushu(b[i])) {
		c.push_back(b[i]);
			t++;
		}
	}
	for (int j = 0; j < (t - 2); j++) {
		for (int i = 0; i < (t - 2); i++) {
			if (c[i] > c[i + 1]) {
				int s = c[i + 1];
				c[i + 1] = c[i];
				c[i] = s;
			}
		}
	}


	//删除重复元素

	for (int i = 0; i < (t-1); i++) {
		for (int j = (i + 1); j < t; j++) {
			if (c[i] == c[j]) {
				c.erase(c.begin() + i);
				t--;
			}
		}
	}


	//冒泡排序法
	for (int i = 0; i < (t-1); i++) {
		for (int j = 0; j < (t - 1-i); j++) {
			if (c[j] > c[j + 1]) {
				int k = c[j];
				c[j] = c[j + 1];
				c[j + 1] = k;
			}
		}
	}


	//自我检验
	for (int i = 0; i < t; i++) {
		cout << c[i] << " ";
	}

}

3、第三题跟第一题类似,第一题没有考虑题干代码的问题,所以我做本题时也遇到了相同的问题,即不知如何从自定义类中提取出自己想要的元素,感谢舍友为我解惑,现已明白其中原理。

//point(1,1)
//  Constructor of Point

//Constructor of Circle
//~Point()
//  Destructor of Point
//end

#include<iostream>
#include<cmath>
using namespace std;
class Point {

	friend double juli(Point p1, Point p2);

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

	double getx() {
		return m_x;
	}
	double gety() {
		return m_y;
	}
	~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;
	}


	double mianji() {
		double s = 3.14 * (m_radius) * (m_radius);
		return s;
	}
	double zhouchang() {
		double l = 6.28 * m_radius;
		return l;
	}


	~Circle() {
		cout << "Destructor of Circle" << endl;
	}


};

double juli(Point p1, Point p2) {
	double x0 = (p1.getx() - p2.getx()) * (p1.getx() - p2.getx());
	double y0 = (p1.gety() - p2.gety()) * (p1.gety() - p2.gety());
	return(sqrt(x0 + y0));
}



   int main()
{
	Circle a(2, Point(1, 1));
	Point b(1, 1),c(1,2);


	double d = juli(b, c);
	double k1 = a.mianji();
	double k2 = a.zhouchang();


	cout << "end" << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值