BJFU

BJFU,第1张

仅供参考,仅供参考,仅供参考,仅供参考

作业管理系统上面的题目。



Question 1:数位拆分
#include 
#include 
class Divide
{
private:
	int number;
	int first;
	int last;
public:
	Divide(int number);
	void Chai();
	void Suan();
};
Divide::Divide(int number)
{
	this->number = number;
}
void Divide::Chai()
{
	int temp = number;
	first = temp / 100;
	last = temp % 100;
}
void Divide::Suan()
{
	std::cout << first + last << std::endl;
	std::cout << first - last << std::endl;
	std::cout << first * last << std::endl;
	std::cout << std::fixed << std::setprecision(2) << first*1.0/last << std::endl;
	std::cout << first % last << std::endl;

}

int main()
{
	Divide a(4321);
	a.Chai();
	a.Suan();
	return 0;
}
Question 2:三角形面积
#include 
#include 
#include 

class Triangle
{
private:
	double a, b, c;
public:
	Triangle(double a, double b, double c);
	double S();
	void show();
};

Triangle::Triangle(double a, double b, double c)
{
	this->a = a;
	this->b = b;
	this->c = c;
}
double Triangle::S()
{
	double temp,s;
	temp = (a + b + c) / 2.0;
	s = sqrt(temp * (temp - a) * (temp - b) * (temp - c));
	return s;
}

void Triangle::show()
{
	std::cout << std::fixed<> a >> b >> c;
	Triangle test(a, b, c);
	test.show();
	return 0;
}
Question 3:验证1000以内的哥德巴赫猜想
#include 
#include 

bool Correct(int k)
{
	for (auto i = 2; i <= sqrt(k); i++)
		if (k % i == 0)return false;
	return true;
}

bool Conjecture_Range(int n)
{
	bool result = true;//初始赋值为真
	for (auto i = 4; i < n; i++)
	{
		if (i % 2 == 1)continue;//不是偶数不进入下一阶段
		for (auto k = 2; k < i-1; k++)
		{
			if (Correct(k) == false)continue;//不是素数不进入下一阶段
			int temp = i - k;
			if (Correct(temp) == true)break;//判断另一个数是否为素数,若是,这个i找到了两个素数
			if (k == i - 2) result = false;//当执行最后一次时还没找到,则赋值为假
		}
	}
	return result;
}


int main()
{
	int n = 1000;
	bool result = Conjecture_Range(n);
	std::cout << result << std::endl;
	return 0;
}
Question 4:圆类
#include 
#include 
const double PI = acos(-1);

class Circle
{
private:
	double r;
public:
	Circle();
	Circle(double r);
	void Set();
	void Get();
	double length();
	double area();
};

Circle::Circle():r(1.0){}
Circle::Circle(double r) { this->r = r; }
void Circle::Set()
{
	std::cout << "输入半径" << std::endl;
	std::cin >> r;
}
void Circle::Get()
{
	std::cout << "半径:" << r << std::endl;
	std::cout<< "周长:" << length() << "  面积:" << area() << std::endl;
}
double Circle::length(){ return 2 * PI * r; }
double Circle::area(){ return PI * r * r; }

int main()
{
	Circle s;//测试无参构造函数
	s.Set();
	s.Get();
	std::cout << "___________________" << std::endl;
	Circle p(5.0);//测试有参构造函数
	p.Get();
	return 0;
}
Question 5:CRect类
#include 
#include 

class CRect
{
private:
	double lenth;
	double width;
public:
	CRect();
	CRect(double lenth , double width);
	void Set();
	void Get();
	double far();
	double area();
};

CRect::CRect() :lenth(1.0), width(1.0) {};
CRect::CRect(double lenth, double width)
{
	this->lenth = lenth;
	this->width = width;
}
void CRect::Set()
{
	std::cout << "请输入长与宽" << std::endl;
	std::cin >> lenth >> width;
}
void CRect::Get() 
{ 
	std::cout << "长:" << lenth << " 宽:" << width << std::endl;
	std::cout << "周长:" << far() << " 面积:" << area() << std::endl; 
}
double CRect::far() { return 2 * (lenth + width); }
double CRect::area() { return lenth * width; }

int main()
{
	CRect s;//测试无参构造函数
	s.Set();
	s.Get();
	std::cout << "__________________" << std::endl;
	CRect p(50.0,30.0);//测试有参构造函数
	p.Get();
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/564767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-06
下一篇 2022-04-06

发表评论

登录后才能评论

评论列表(0条)

保存