计算机程序设计C++(第9周基础练习)

本周的C++基础练习聚焦于类的构建,包括点类、圆类、矩形类、复数类和分数类的设计与实现。
摘要由CSDN通过智能技术生成

计算机程序设计C++ MOOC

测试与作业C++基础练习100题

##第九周基本练习

本周内容为类的构建

  1. 点类
    在这里插入图片描述
#include "iostream"
#include "cmath"

using namespace std;

class POINT
{
private:
	double x, y;
public:
	void set(double,double);
	double distance(POINT);
	void show();
};

void POINT::set(double a, double b)
{
	x = a; y = b;
}

void POINT::show()
{
	cout<<'('<<x<<','<<y<<')';
}

double POINT::distance(POINT a)
{
	return sqrt((x - a.x)*(x - a.x) + (y - a.y)*(y - a.y));
}

int main()
{
	POINT a, b;
	double x, y;
	cin >> x >> y;
	a.set(x, y);
	cin >> x >> y;
	b.set(x, y);
	a.show(); cout << endl;
	b.show(); cout << endl;
	cout << a.distance(b) << endl;
	return 0;
}
  1. 圆类
    在这里插入图片描述
#include "iostream"

using namespace std;

class CIRCLE
{
private:
	double x, y, r;
public:
	void set(double, double, double);
	double area(){ return 3.14*r*r; }
	double perimeter(){ return 2 * 3.14*r; }
	void show();
};

void CIRCLE::set(double a, double b, double c)
{
	x = a; y = b; r = c;
}

void CIRCLE::show()
{
	cout << '(' << x << ',' << y << ',' << r << ')';
}

int main()
{
	CIRCLE a;
	double x, y, r;
	cin >> x >> y >> r;
	a.set(x, y, r);
	a.show(); cout << endl;
	cout << a.perimeter() << endl;
	cout << a.area() << endl;
	return 0;
}
  1. 矩形类
    在这里插入图片描述
#include "iostream"

using namespace std;

class RECT
{
private:
	int x1, y1, x2, y2;
public:
	void set(int, int, int, int);
	int area(){ return abs((x1 - x2))*abs((y1 - y2)); }
	int perimeter(){ return 2 * abs((x1 - x2)) + 2 * abs((y1 - y2)); }
	void showinfo();
	void show();
};

void RECT::set(int a, int b, int c, int d)
{
	x1 = a; y1 = b; x2 = c; y2 = d;
}

void RECT::showinfo()
{
	cout << '(' << x1 << ',' << y1 << ',' << x2 <<','<< y2 << ')';
}

void RECT::show()
{
	int m = abs((x1 - x2)), n = abs((y1 - y2));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}

int main()
{
	RECT a;
	double x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	a.set(x1, y1, x2, y2);
	a.showinfo(); cout << endl;
	cout << a.perimeter() << endl;
	cout << a.area() << endl;
	a.show();
	return 0;
}
  1. 复数类
    在这里插入图片描述
#include "iostream"
#include "cmath"

using namespace std;

class COMPLEX
{
private:
	double real, imag;
public:
	void set(double, double);
	double getReal(){return real; }
	double getImag(){ return imag; }
	double module(){ return sqrt(real*real + imag*imag); }
	COMPLEX add(COMPLEX);
	void show();
};

void COMPLEX::set(double a, double b)
{
	real = a; imag = b;
}

COMPLEX COMPLEX::add(COMPLEX a)
{
	COMPLEX temp;
	temp.real = real+a.real;
	temp.imag = imag+a.imag;
	return temp;
}

void COMPLEX::show()
{
	if (imag >= 0)
		cout << real << "+" << imag << 'j';
	else
		cout << real << imag << 'j';
}

int main()
{
	COMPLEX a, b, c;
	double x, y;
	cin >> x >> y;
	a.set(x, y);
	cin >> x >> y;
	b.set(x, y);
	c = a.add(b);
	c.show();
	cout << endl;
	cout << c.getReal() << endl;
	cout << c.getImag() << endl;
	cout << c.module() << endl;
	return 0;
}
  1. 分数类
    在这里插入图片描述
#include "iostream"
#include "cmath"

using namespace std;

class FRACTION
{
private:
	int mol, den;
	void reduce();
public:
	void set(int,int);
	double real(){ return double(mol)/double(den); }
	void show();
};

void FRACTION::reduce()
{
	if (mol == 0) return;
	int m = (abs(mol) < abs(den)) ? abs(mol) : abs(den);
	while (m)
	{
		if (mol%m == 0 && den%m == 0) break;
		m--;
	}
	mol = mol / m;
	den = den / m;
}

void FRACTION::set(int a, int b)
{
	mol = a;
	den = b;
	reduce();
	if (den < 0)
	{
		mol *= -1;
		den *= -1;
	}
}

void FRACTION::show()
{
	cout << mol << '/' << den;
}

int main()
{
	FRACTION a;
	int x, y;
	cin >> x >> y;
	a.set(x, y);
	a.show();
	cout << endl;
	cout << a.real() << endl;
	return 0;
}

以上为第九周的基本练习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值