c++---两个小练习

一、立方体类设计

设计一个立方体类,并判断两个立方体是否相等

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
/* 设计一个立方体类
 * 包含长宽高以及设置长宽高
 * 计算体积面积
 */

class Cube {
public:
	//设置长
	void setL(int L)
	{
		m_L = L;
	}
	//设置宽
	void setW(int W)
	{
		m_W = W;
	}
	//设置高
	void setH(int H)
	{
		m_H = H;
	}
	int getL()
	{
		return m_L;
	}
	int getW()
	{
		return m_W;
	}
	int getH()
	{
		return m_H;
	}
	//求面积
	int getS()
	{
		return 2 * m_W * m_L + 2 * m_W * m_H + 2 * m_H * m_L;
	}
	//求体积
	int getV()
	{
		return m_L * m_W * m_H;
	}
	bool compareCubebyClass(Cube &c)
	{
		return m_L == c.getL() && m_H == c.getH() && m_W == c.getW();
	}
private:
	int m_L;	//长
	int m_W;	//宽
	int m_H;	//高
};
//利用全局函数比较两个立方体
bool compareCube(Cube &c1, Cube &c2)
{
	/*if (c1.getL() == c2.getL() && c1.getH() == c2.getH() && c1.getW() == c2.getW())
	{
		return true;
	}
	else
	{
		return false;
	}*/
	return c1.getL() == c2.getL() && c1.getH() == c2.getH() && c1.getW() == c2.getW();
}

void test01()
{
	Cube c1;
	c1.setL(20);
	c1.setW(10);
	c1.setH(30);
	cout << "c1立方体的面积:" << c1.getS() << endl;
	cout << "c1立方体的体积:" << c1.getV() << endl;
	Cube c2;
	c2.setL(20);
	c2.setW(10);
	c2.setH(30);
	cout << "c2立方体的面积:" << c2.getS() << endl;
	cout << "c2立方体的体积:" << c2.getV() << endl;
	bool ret = compareCube(c1, c2);
	if (ret)
	{
		cout << "全局函数--两个立方体相等" << endl;
	}
	else
	{
		cout << "全局函数--两个立方体不相等" << endl;
	}
	//利用成员函数判断
	bool ret2 = c1.compareCubebyClass(c2);
	if (ret2)
	{
		cout << "成员函数--两个立方体相等" << endl;
	}
	else
	{
		cout << "成员函数--两个立方体不相等" << endl;
	}
}

int main()
{
	test01();


	system("pause");
	return EXIT_SUCCESS;
}

二、点和圆的关系案例

判断点和圆的位置关系

这里用了模块化编程

spot.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Spot {
public:
	//设置点
	void SetSpot(int a, int b);
	
	//获取x
	int getX();
	
	int getY();
	
private:
	int m_X;
	int m_Y;
};

spot.cpp

#include "spot.h"


//设置点
void Spot::SetSpot(int a, int b)
{
	m_X = a;
	m_Y = b;
}
//获取x
int Spot::getX()
{
	return m_X;
}
int Spot::getY()
{
	return m_Y;
}

circular.h

#include "spot.h"
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class circular {
public:
	//设置半径
	void setR(int r);

	//获取半径
	int getR();

	//设置圆心
	void setCenter(Spot a);

	//获取圆心
	Spot getCenter();

	void compareDistance(Spot a);
	

private:
	int m_R;	//半径
	Spot m_C;	//圆心
};

circular.cpp

#include "circular.h"
#include "spot.h"

//设置半径
void circular::setR(int r)
{
	m_R = r;
}
//获取半径
int circular::getR()
{
	return m_R;
}
//设置圆心
void circular::setCenter(Spot a)
{
	m_C = a;
}
//获取圆心
Spot circular::getCenter()
{
	return m_C;
}
void circular::compareDistance(Spot a)
{
	int distance = (a.getX() - m_C.getX()) * (a.getX() - m_C.getX()) \
		+ (a.getY() - m_C.getY()) * (a.getY() - m_C.getY());
	int r_Distance = m_R * m_R;
	if (distance == r_Distance)
	{
		cout << "点在圆上" << endl;
	}
	else if (distance > r_Distance)
	{
		cout << "点在圆外" << endl;
	}
	else
	{
		cout << "点在圆内" << endl;
	}
}

main.c

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "circular.h"
#include "spot.h"
void test02()
{
	//点
	Spot s;
	s.SetSpot(1, 2);
	//圆心
	Spot cs;
	cs.SetSpot(1, 1);
	//圆
	circular c;
	c.setR(1);			//半径
	c.setCenter(cs);	//圆心
	c.compareDistance(s);
}

int main()
{
	//test01();
	test02();

	system("pause");
	return EXIT_SUCCESS;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值