面向对象程序设计|类和构造

问题一:Point类

题目描述:

下面是一个平面上的点的类定义,请在类外实现它的所有方法,并生成点测试它。

面向对象程序设计|类和构造_第1张图片

输入要求:

测试数据的组数 t

第一组测试数据点p1的x坐标   第一组测试数据点p1的y坐标  第一组测试数据点p2的x坐标   第一组测试数据点p2的y坐标

..........

输出要求:

输出p1到p2的距离

输入样例:

2
1 2 3 4
-1 0.5 -2 5

 输出样例:

Distance of Point(1.00,2.00) to Point(3.00,4.00) is 2.83
Distance of Point(-1.00,0.50) to Point(-2.00,5.00) is 4.61
#include
#include
#include
#include 
#include
#include
using namespace std;

class Point
{
public:
	Point();
	Point(double x_value, double y_value);
	double getX();//利用函数调用x,y
	double getY();
	void setX(double x_value);//给私有数据x,y赋值
	void setY(double y_value);
	double distanceToAnotherPoint(const Point &p);
private:
	double x, y;
};

Point::Point() :x(0), y(0){}//无参构造

Point::Point(double x_value, double y_value)//带参构造
{
	x = x_value;
	y = y_value;
}

void Point::setX(double x_value)
{
	x = x_value;
}

void Point::setY(double y_value)
{
	y = y_value;
}

double Point::getX()
{
	return x;
}

double Point::getY()
{
	return y;
}
double Point::distanceToAnotherPoint(const Point &p)
{
	return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		double x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		Point p1(x1, y1);
		Point p2(x2, y2);
		cout << "Distance of Point(";
		cout << fixed << setprecision(2) << x1 << "," << y1 << ") to Point(" << x2 << "," << y2 << ") is " << p1.distanceToAnotherPoint(p2) << endl;
	}
	return 0;
}

问题二:Point类(数组)

题目描述:

同题目一

输入要求:

测试数据的组数 t

第一组点的个数

第一个点的 x 坐标   y坐标

第二个点的 x坐标  y坐标

............

输出要求:

输出第一组距离最大的两个点以及其距离

...........

输入样例:

2
4
0 0
5 0
5 5
2 10
3
-1 -8
0 9
5 0

输出样例:

Constructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 10.44,between p[1] and p[3].
Distructor.
Distructor.
Distructor.
Distructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 17.03,between p[0] and p[1].
Distructor.
Distructor.
Distructor.
#include
#include
#include
#include 
#include
#include
using namespace std;

class Point
{
public:
	Point();//无参构造
	Point(double x_value, double y_value);//带参构造
	double getX();
	double getY();
	void setXY(double x_value, double y_value);
	void setX(double x_value);
	void setY(double y_value);
	double distanceToAnotherPoint(Point& p);//传引用不调用析构函数
	~Point();//析构函数
private:
	double x, y;
};

Point::Point()
{
	x = 0;
	y = 0;
	cout << "Constructor." << endl;
}

Point::Point(double x_value, double y_value)
{
	x = x_value;
	y = y_value;
}

void Point::setXY(double x_value, double y_value)
{
	x = x_value;
	y = y_value;
}

void Point::setX(double x_value)
{
	x = x_value;
}

void Point::setY(double y_value)
{
	y = y_value;
}

double Point::getX()
{
	return x;
}

double Point::getY()
{
	return y;
}

double Point::distanceToAnotherPoint(Point& p)
{
	return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
}

Point::~Point()
{
	cout << "Distructor." << endl;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, x_val, y_val;
		cin >> n;
		Point* p = new Point[n];//定义Point类型的动态内存
		for (int i = 0; i < n; i++)
		{
			cin >> x_val >> y_val;
			p[i].setXY(x_val, y_val);
		}
		double maxdis = p[0].distanceToAnotherPoint(p[1]);//对最大距离进行初始化
		int index1 = 0, index2 = 1;
		for (int i = 0; i < n - 1; i++)
		{
			for (int j = i + 1; j < n; j++)
			{
				if (p[i].distanceToAnotherPoint(p[j]) > maxdis)
				{
					maxdis = p[i].distanceToAnotherPoint(p[j]);
					index1 = i;
					index2 = j;
				}
			}
		}
		cout << "The longeset distance is ";
		cout << fixed << setprecision(2) << maxdis;
		cout << ",between p[" << index1 << "] and p[" << index2 << "]." << endl;
		delete[]p;//释放内存空间
	}
	return 0;
}

问题三:分数类

题目描述:

完成下列分数类的实现:

class CFraction
{
private:
int fz, fm;
public:
CFraction(int fz_val, int fm_val) ;
CFraction add(const CFraction &r);
CFraction sub(const CFraction &r);
CFraction mul(const CFraction &r);
CFraction div(const CFraction &r);
int getGCD();   // 求对象的分子和分母的最大公约数
void print();
};

求两数a、b的最大公约数可采用辗转相除法,又称欧几里得算法,其步骤为:

1. 交换a, b使a > b;
2. 用a除b得到余数r,若r=0,则b为最大公约数,退出.
3. 若r不为0,则用b代替a, r代替b,此时a,b都比上一次的小,问题规模缩小了;
4. 继续第2步。

输入要求:

测试数据的组数 t

第一组第一个分数

第一组第二个分数

第二组第一个分数

第二组第二个分数

......

输出要求:

第一组两个分数的和

第一组两个分数的差

第一组两个分数的积

第一组两个分数的商

第二组两个分数的和

第二组两个分数的差

第二组两个分数的积

第二组两个分数的商

......

输入样例:

3
1/2
2/3
3/4
5/8
21/23
8/13

输出样例:

7/6
-1/6
1/3
3/4

11/8
1/8
15/32
6/5

457/299
89/299
168/299
273/184
#include
#include
#include
#include 
#include
#include
using namespace std;

class CFraction
{
public:
	CFraction() {};
	CFraction(int fz_val, int fm_val);
	CFraction add(const CFraction& r);
	CFraction sub(const CFraction& r);
	CFraction mul(const CFraction& r);
	CFraction div(const CFraction& r);
	int getGCD();
	void print();
private:
	int fz, fm;
};

CFraction::CFraction(int fz_val, int fm_val)
{
	fz = fz_val;
	fm = fm_val;
}

CFraction CFraction::add(const CFraction& r)//传引用
{
	return CFraction(fz * r.fm + fm * r.fz, fm * r.fm);
}

CFraction CFraction::sub(const CFraction& r)
{
	return CFraction(fz * r.fm - fm * r.fz, fm * r.fm);
}

CFraction CFraction::mul(const CFraction& r)
{
	return CFraction(fz * r.fz, fm * r.fm);
}

CFraction CFraction::div(const CFraction& r)
{
	return CFraction(fz * r.fm, fm * r.fz);
}

int CFraction::getGCD()//求分子与分母的最大公约数
{
	return __gcd(fz, fm);
}

void CFraction::print()//输出化简后的分子与分母
{
	int gcd = abs(getGCD());
	fz /= gcd;
	fm /= gcd;
	cout << fz << "/" << fm << endl;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int fz1, fz2, fm1, fm2;
		char ch;
		cin >> fz1 >> ch >> fm1 >> fz2 >> ch >> fm2;
		CFraction c1(fz1, fm1);
		CFraction c2(fz2, fm2);
		CFraction c3;//定义分c3来接受c1与c2运算后的结果
		c3 = c1.add(c2);
		c3.print();
		c3 = c1.sub(c2);
		c3.print();
		c3 = c1.mul(c2);
		c3.print();
		c3 = c1.div(c2);
		c3.print();
		cout << endl;
	}
	return 0;
}

问题四:图书借阅(数组)

题目描述:

假设图书馆的图书书名各不相同,相同书有若干本可借阅。

定义图书类CBook,数据成员包括:索取号、书名、馆藏数量、可借数量。方法包括:

构造函数:根据参数初始化数据成员。

借书: 若该书的可借数量大于等于1,则返回索取号;否则,返回空串。

主函数动态定义CBook数组,初始化图书馆馆藏信息。输入借书信息,对每次借书需求,根据样例给出结果。

输入要求:

图书馆馆藏书种类n

n个图书信息:索取号 书名 馆藏数量 可借数量

借书需求次数m

m本书名

输出要求:

对每个借书需求,根据样例输出结果。

输出空行。

输出所有馆藏书的信息。

输出借出图书数量 剩余馆藏图书数量

输入样例:

4
TP312JA-43/L99 Java语言程序设计教程 3 0
TP312PH/Q68b PHP7内核剖析 3 2
TP311.561-43/L93 Python大学教程 3 1
TP311.5-43/M18a1(2) 软件工程基础 3 3
4
Java语言程序设计教程
软件工程基础
Python大学教程
Python大学教程

输出样例:

Java语言程序设计教程 该书已全部借出
软件工程基础 索取号: TP311.5-43/M18a1(2)
Python大学教程 索取号: TP311.561-43/L93
Python大学教程 该书已全部借出

TP312JA-43/L99 Java语言程序设计教程 3 0
TP312PH/Q68b PHP7内核剖析 3 2
TP311.561-43/L93 Python大学教程 3 0
TP311.5-43/M18a1(2) 软件工程基础 3 2
借出图书: 8本  剩余馆藏图书: 4本
#include
#include
#include
#include 
#include
#include
using namespace std;

class CBook
{
public:
	CBook() {}
	void setLib(string clnum0, string bkname0, int totalnum0, int isbnum0)
	{
		clnum = clnum0;
		bkname = bkname0;
		totalnum = totalnum0;
		isbnum = isbnum0;
	}
	void Borrow()
	{
		cout << bkname;
		if (isbnum > 0)//如果能借出,则执行结束操作,打印数目索引号,可借数目减一
		{
			cout << " 索取号: " << clnum << endl;
			isbnum--;
		}
		else if (isbnum == 0)//如果剩余的数目为0,则输出全部借出
		{
			cout << " 该书已全部借出" << endl;
		}
	}
	string getClnum()
	{
		return clnum;
	}
	string getBkname()
	{
		return bkname;
	}
	int getTotalnum()
	{
		return totalnum;
	}
	int getIsbnum()
	{
		return isbnum;
	}
	void print()
	{
		cout << clnum << " " << bkname << " " << totalnum << " " << isbnum << endl;//打印数目信息
	}
private:
	string clnum, bkname;
	int totalnum, isbnum;
};

int main()
{
	int n;
	cin >> n;
	CBook* book = new CBook[n];//定义CBook类的动态内存
	string cln, bkn;
	int ttn, ibn;
	for (int i = 0; i < n; i++)
	{
		cin >> cln >> bkn >> ttn >> ibn;//分别输入检索号,书名,数量和可借数量
		book[i].setLib(cln, bkn, ttn, ibn);//传参构造
	}
	int x;
	cin >> x;
	for (int i = 0; i < x; i++)
	{
		string needbk;
		cin >> needbk;
		for (int j = 0; j < n; j++)
		{
			if (book[j].getBkname() == needbk)//如果有这本书就调用借书
			{
				book[j].Borrow();
			}
		}
	}
	cout << endl;
	int sum = 0, left = 0;
	for (int i = 0; i < n; i++)
	{
		book[i].print();
		sum += book[i].getTotalnum();//统计总共藏书的数目
		left += book[i].getIsbnum();//统计剩余藏书的数目
	}
	cout << "借出图书: " << sum - left << "本  剩余馆藏图书: " << left << "本" << endl;
	return 0;
}

你可能感兴趣的:(Simple,C++,算法,c++,图论)