深圳大学计软《面向对象的程序设计》实验7 拷贝构造函数与复合类

A. Point&Circle(复合类与构造)

题目描述

在这里插入图片描述

类Point是我们写过的一个类,类Circle是一个新的类,Point作为其成员对象,请完成类Circle的成员函数的实现。

在主函数中生成一个圆和若干个点,判断这些点与圆的位置关系,如果点在圆内(包括在圆的边上),输出“inside”,否则输出"outside";然后移动圆心的位置,再次判断这些点与圆的位置关系。

输入

圆的x坐标 y坐标 半径

点的个数n

第一个点的x坐标 y坐标

第二个点的x坐标 y坐标

第n个点的x坐标 y坐标

圆心移动到的新的x坐标 y坐标

输出

第一个点与圆的关系

第二个点与圆的关系

第n个点与圆的关系

after move the centre of circle

圆心移动后第一个点与圆的关系

圆心移动后第二个点与圆的关系

圆心移动后第n个点与圆的关系

输入样例1

0 0 5
4
1 1
2 2
5 0
-6 0
-1 0

输出样例1

inside
inside
inside
outside
after move the centre of circle:
inside
inside
outside
inside

AC代码

#include<bits/stdc++.h>
using namespace std;

class Point {
    
    
	double x, y;
public:
	Point() {
    
    }

	Point(double x,double y):x(x),y(y){
    
    }

	double getX() {
    
     return x; }
	double getY() {
    
     return y; }

	double getDisTo(Point& p) {
    
    
		return sqrt(pow(x - p.x, 2) + pow(y - p.y,2));
	}

	void setXY(int x,int y){
    
    
		this->x = x;
		this->y = y;
	}

	~Point(){
    
    }
};

class Circle {
    
    
	Point centre;
	double radius;
public:
	Circle() {
    
    }

	Circle(double x, double y, double r) {
    
    
		centre.setXY(x, y);
		radius = r;
	}

	double getArea() {
    
    
		return 2.1415926 * radius * radius;
	}

	void moveCentreTo(double x1, double y1) {
    
    
		centre.setXY(x1, y1);
	}

	bool isContain(Point &p) {
    
    
		double distance = centre.getDisTo(p);
		return distance <= radius;
	}

};

int main() {
    
    
	int x, y, r;
	cin >> x >> y >> r;
	Circle c(x, y, r);
	int n;
	cin >> n;
	vector<Point>v(n);
	
	for (auto& it : v) {
    
    
		cin >> x >> y;
		it.setXY(x, y);
		if (c.isContain(it))
			cout << "inside" << endl;
		else
			cout << "outside" << endl;
	}
	cin >> x >> y;
	c.moveCentreTo(x, y);
	cout << "after move the centre of circle:" << endl;
	for (auto& it : v) {
    
    
		if (c.isContain(it))
			cout << "inside" << endl;
		else
			cout << "outside" << endl;
	}
	return 0;
}

B. Complex(类与对象+构造)

题目描述

编写一个复数类,能实现加、减运算,能输出复数的信息。 要求至少包含以下方法:

1、缺省(无参)构造函数,设置实部与虚部为1;

2、有参构造函数,给实部与虚部赋值;

3、加法运算,计算两个复数的和;

4、减法运算,计算两个复数的差;

5、输出方法,输出当前复数的值

输入

测试数据的组数t 第一组的两个复数的实部 虚部 实部 虚部 第二组的两个复数的实部 虚部 实部 虚部 …

输出

第一组两个复数的和 第一组两个复数的差

输入样例1

4
2 1 2 1
2 1 2 -1
3 1 2 -6
3 3 2 2

输出样例1

sum:4+2i
remainder:0
sum:4
remainder:2i
sum:5-5i
remainder:1+7i
sum:5+5i
remainder:1+i

AC代码

#include<bits/stdc++.h>
using namespace std;

class Complex
{
    
    
public:
	Complex();
	Complex(int, int);
	~Complex();
	Complex add(Complex&);
	Complex sub(Complex&);
	void display();


private:
	int a, b;
};

Complex::Complex()
{
    
    
	a = b = 0;
}

Complex::Complex(int a, int b) :a(a), b(b)
{
    
    
}

Complex::~Complex()
{
    
    
}

Complex Complex::add(Complex& c1)
{
    
    
	return Complex(a + c1.a, b + c1.b);
}

Complex Complex::sub(Complex& c1)
{
    
    
	return Complex(a - c1.a, b - c1.b);
}

void Complex::display()
{
    
    
	if (!(a || b)) {
    
    
		cout << 0 << endl;
		return;
	}

	if (a)
		cout << a;

	if (b > 0) {
    
    
		if(a)
		cout << "+";
		if (b != 1)
			cout << b;
		cout << "i" ;

	}
	else if (b < 0) {
    
    
		if (b != -1)
			cout << b << "i" ;
		else
			cout << "-i";
	}
	cout << endl;
}

int main() {
    
    
	int t;
	cin >> t;
	while (t--)
	{
    
    
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		Complex c1(a, b), c2(c, d);
		cout << "sum:";
		c1.add(c2).display();
		cout << "remainder:";
		c1.sub(c2).display();
	}
	return 0;
}

C. 电话号码升位(拷贝构造函数)

题目描述

定义一个电话号码类CTelNumber,包含1个字符指针数据成员,以及构造、析构、打印及拷贝构造函数。

字符指针是用于动态创建一个字符数组,然后保存外来输入的电话号码

构造函数的功能是为对象设置键盘输入的7位电话号码,

拷贝构造函数的功能是用原来7位号码的对象升位为8位号码对象,也就是说拷贝构造的对象是源对象的升级.电话升位的规则是原2、3、4开头的电话号码前面加8,原5、6、7、8开头的前面加2。

注意:合法的电话号码:1、长度为7位;2、电话号码的字符全部是数字字符;3、第一个字符只能是以下字符:2、3、4、5、6、7、8。与上述情况不符的输入均为非法

输入

测试数据的组数 t

第一个7位号码

第二个7位号码

输出

第一个号码升位后的号码

第二个号码升位后的号码

如果号码升级不成功,则输出报错信息,具体看示例

输入样例1

3
6545889
3335656
565655

输出样例1

26545889
83335656
Illegal phone number

AC代码

#include<bits/stdc++.h>
using namespace std;

class CTelNumber {
    
    
	string id;
public:
	CTelNumber() {
    
    
		cin >> id;
	}

	~CTelNumber()
	{
    
    
		id.clear();
	}

	void print() {
    
    
		cout << id << endl;
	}

	bool isLeagle() {
    
    
		if (id.length() != 7)
			return false;
		for (auto& it : id)
			if (!isdigit(it))
				return false;
		if (id[0] == '0' || id[0] == '1' || id[0] == '9')
			return false;
		return true;
	}

	CTelNumber(const CTelNumber& p) {
    
    
		id = p.id;
		if (id[0] >= '2' && id[0] <= '4')
			id = "8" + id;
		else if (id[0] >= '5' && id[0] <= '8')
			id = "2" + id;
	}

};

int main() {
    
    
	int t;
	cin >> t;
	while (t--){
    
    
		CTelNumber c;
		if (c.isLeagle()) {
    
    
			CTelNumber c1(c);
			c1.print();
		}
		else cout << "Illegal phone number" << endl;
	}
	return 0;
}

D. 身份证设定(复合类+拷贝构造)

题目描述

定义一个身份证类PID,包含私有属性:身份证类型、身份证号码、出生日期;另外包含方法:构造、拷贝构造打印等。

身份证类型表示一代身份证或者二代身份证,分别用1和2表示

身份证号码是一个字符串,长度为15或者18

出生日期是一个类,包含私有属性年、月、日,以及构造函数等(根据需要添加其他方法)

构造函数要注意是复合类,要考虑复合类成员的构造

打印函数把身份证的所有属性都输出,输出格式看示例

拷贝构造作用:如果身份证号码是15位的就升级为18位,包括把身份证类型改为2,然后把号码扩展,规则如下:

  1. 原15位身份证的第7位到12位表示出生年月日,每个两位;把年份的2位扩展为四位。

  2. 把扩展后的17个数字求和,取总和的末尾数字,如果末尾数字是0,则将0改为X,然后把这个数字作为第18位

3.如果身份证号码已经是18位,就无需升级

例如身份证123456910203000,表示91年2月3日出生,然后根据类属性出生日期知道是1991年,不是2091年。因此扩展为12345619910203000

接着把17个数字相加得到46,取末尾6,最终扩展为123456199102030006

输入

第一行输入t表示t个示例

第二行输入一个身份证的5个属性,顺序为:类型、号码、出生年、月、日

依次输入t行

输出

采用拷贝构造函数的方法对身份证号码升级,然后输出

输入样例1

3
1 123456910203000 1991 2 3
2 654321200001018889 2000 1 1
1 234567001217000 2000 12 17

输出样例1

type=2 birth=1991.02.03
ID=123456199102030006
type=2 birth=2000.01.01
ID=654321200001018889
type=2 birth=2000.12.17
ID=23456720001217000X

AC代码

#include<bits/stdc++.h>
using namespace std;

class Date {
    
    
	int y, m, d;
public:
	Date() {
    
      }
	Date(int y, int m, int d) :y(y), m(m), d(d) {
    
    }
	int getYear() {
    
     return y; }
	int getMonth() {
    
     return m; }
	int getDay() {
    
     return d; }
	void init() {
    
     cin >> y >> m >> d; }
};


class Pid {
    
    
	int type;
	string id;
	Date birthday;
public:
	Pid() {
    
    
		cin >> type >> id;
		birthday.init();
	}

	Pid(const Pid& pid) {
    
    
		type = pid.type;
		id = pid.id;
		birthday = pid.birthday;
		if (type == 1) {
    
    
			id.erase(6, 2);
			id.insert(6, to_string(birthday.getYear()));
			int sum = 0;
			for (auto& it : id) {
    
    
				sum += it - '0';
			}
			sum %= 10;
			if (sum == 0)
				id.push_back('X');
			else
				id.push_back(sum + '0');
			type = 2;
		}
	}

	void display() {
    
    
		cout << "type=" << type << " ";
		cout << "birth=" << birthday.getYear() << "." << setfill('0') << setw(2) << birthday.getMonth() << "." << setfill('0') << setw(2) << birthday.getDay() << endl;
		cout << "ID=" << id << endl;
	}


};

int main() {
    
    
	int t;
	cin >> t;
	while (t--) {
    
    
		Pid id;
		Pid update_id(id);
		update_id.display();
	}
	return 0;
}

E. 软件备份(拷贝构造函数)

题目描述

软件作为一种对象也可以用类来描述,软件的属性包括软件名称、类型(分别用O、T和B表示原版、试用版还是备份)、有效截至日期(用CDate类子对象表示)和存储介质(分别用D、H和U表示光盘、磁盘和U盘)等。软件拷贝可通过拷贝构造函数来实现,此时在拷贝构造函数中软件类型改成“B”, 存储介质改为"H",其它不变。试完成该类的拷贝构造、构造和打印(包括从2015年4月7日算起有效期还有多少天,是否过期)成员函数的实现。

当输入软件有效截止日期是0年0月0日,表示无日期限制,为unlimited;当输入日期在2015年4月7日之前,则是过期,表示为expired;如果输入日期在2015年4月7日之后,则显示之后的剩余天数。具体输出信息看输出范例。

附CDate类的实现:

class CDate
{
private:
int year, month, day;
public:
CDate(int y, int m, int d) { year = y; month = m; day = d; }
bool isLeapYear() { return (year%4 == 0 && year%100 != 0) || year%400 == 0; }
int getYear() { return year; }
int getMonth() { return month; }
int getDay() { return day; }
int getDayofYear() //计算日期从当年1月1日算起的天数
{
int i, sum=day;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};

if (isLeapYear())
for(i=0;i<month;i++) sum +=b[i];
else
for(i=0;i<month;i++) sum +=a[i];

return sum;
}
};

输入

测试数据的组数 t

第一个软件名称

第一个软件类型 第一个软件介质类型 第一个软件有效期年 月 日

第二个软件名称

第二个软件类型 第二个软件介质类型 第二个软件有效期年 月 日

输出

name: 第一个软件名称

type: 第一个软件类型

media: 第一个软件介质类型

第一个软件2015-4-7后的有效天数

name: 第一个软件名称

type: backup

media: hard disk

第一个软件2015-4-7后的有效天数

输入样例1

3
Photoshop_CS5
O D 0 0 0
Audition_3.0
B U 2015 2 3
Visual_Studio_2010
T H 2015 5 5

输出样例1

name:Photoshop_CS5
type:original
media:optical disk
this software has unlimited use

name:Photoshop_CS5
type:backup
media:hard disk
this software has unlimited use

name:Audition_3.0
type:backup
media:USB disk
this software has expired

name:Audition_3.0
type:backup
media:hard disk
this software has expired

name:Visual_Studio_2010
type:trial
media:hard disk
this software is going to be expired in 28 days

name:Visual_Studio_2010
type:backup
media:hard disk
this software is going to be expired in 28 days

AC代码

#include<bits/stdc++.h>
using namespace std;


class CDate
{
    
    
	int year, month, day;
public:
	CDate() {
    
    };
	CDate(int y, int m, int d) {
    
     year = y; month = m; day = d; }

	CDate(const CDate& c) {
    
    
		year = c.year;
		month = c.month;
		day = c.day;
	}

	bool isLeapYear() {
    
     return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; }
	int getYear() {
    
     return year; }
	int getMonth() {
    
     return month; }
	int getDay() {
    
     return day; }
	int getDayofYear()         //计算日期从当年1月1日算起的天数
	{
    
    
		int i, sum = day;
		int a[13] = {
    
     0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int b[13] = {
    
     0,31,29,31,30,31,30,31,31,30,31,30,31 };

		if (isLeapYear())
			for (i = 0; i < month; i++)   sum += b[i];
		else
			for (i = 0; i < month; i++)   sum += a[i];

		return sum;
	}

	void goTomorrow() {
    
    
		int a[13] = {
    
     0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (isLeapYear()) {
    
    
			a[2] += 1;
		}
		day++;
		if (day > a[month]) {
    
    
			day = 1;
			month++;
		}

		if (month > 12) {
    
    
			month = 1;
			year++;
		}
	}

	bool operator!=(CDate& date) {
    
    
		if (year != date.year)
			return true;
		if (month != date.month)
			return true;
		if (day != date.day)
			return true;
		return false;
	}

};

class Software {
    
    
	string name;
	char type, media;
	CDate ddl;
public:
	Software() {
    
    }

	Software(string name, char type, char hard, CDate ddl) {
    
    
		this->name = name;
		this->type = type;
		this->media = hard;
		this->ddl = ddl;
	}

	Software(const Software& s) {
    
    
		this->name = s.name;
		this->type = 'B';
		this->media = 'H';
		this->ddl = s.ddl;
	}

	string getName() {
    
    
		return name;
	}


	bool isOverdue() {
    
    
		if (ddl.getYear() < 2015)
			return true;
		if (ddl.getYear() > 2015)
			return false;
		if (ddl.getMonth() < 4)
			return true;
		if (ddl.getMonth() > 4)
			return false;
		if (ddl.getDay() < 7)
			return true;
		if (ddl.getDay() >= 7)
			return false;
	}

	int getRestDay() {
    
    
		CDate today(2015, 4, 7);
		int cnt = 0;
		while (today != ddl) {
    
    
			cnt++;
			today.goTomorrow();
		}
		return cnt;
	}


	void print() {
    
    
		cout << "name:" << name << endl;
		cout << "type:";
		if (type == 'B')
			cout << "backup" << endl;
		else if (type == 'T')
			cout << "trial" << endl;
		else
			cout << "original" << endl;
		cout << "media:";
		if (media == 'H')
			cout << "hard disk" << endl;
		else if (media == 'D')
			cout << "optical disk" << endl;
		else cout << "USB disk" << endl;



		if (!(ddl.getYear() || ddl.getMonth() || ddl.getDay()))
			cout << "this software has unlimited use" << endl;
		else if (isOverdue())
			cout << "this software has expired" << endl;
		else
			cout << "this software is going to be expired in " << getRestDay() << " days" << endl;
		cout << endl;
	}

};

int main() {
    
    
	int t;
	cin >> t;
	while (t--) {
    
    
		string name;
		char type, media;
		int  year, month, day;
		cin >> name >> type >> media >> year >> month >> day;
		Software s(name, type, media, CDate(year, month, day));
		s.print();
		Software copy_s(s);
		copy_s.print();

	}
	return 0;
}

F. 购物车(复合类)

题目描述

定义一个商品类,包含私有数据成员:商品编号、名称、颜色、尺码、单价、数量。成员函数有:计算总价(单价*数量)、输出商品信息。具体输出格式见样例输出。构造函数及其它函数可根据题目需要自行添加。

定义一个购物车类,包含私有数据成员:商品对象集合、商品总数、购物车所有商品总价。方法有:添加商品、删除商品、减少商品数量、增加商品数量,输出购物车中的商品清单。构造函数及其它函数可根据题目需要自行添加。

编写主函数,定义上述类对象,根据样例的输入、输出实现购物车的简单模拟。

购物车操作分别用ADD、DELETE、UP、DOWN表示,具体格式描述如下:

ADD 商品编号 商品名称 颜色 尺码 单价 数量 //添加1个或多个同类商品,若购物车已有指定编号商品,只需增加数量;若无,添加在购物车首部。

DELETE 商品编号 //删除购物车中给定商品编号的所有商品,不存在删除不成功的情况,即购物车中一定有给定编号的物品。

UP 商品编号 //购物车中商品编号的商品数量加1,不存在操作不成功的情况。

DOWN 商品编号 //购物车中商品编号的商品数量减1,且最小为1。

为更好理解题目,可在京东购物车里试操作。样例中的数据来源于该网站,包括颜色和尺码。为简化题目,假设同一商品不同颜色、尺码,不同编号。

输入

测试次数t

每组测试数据为:

购物车操作次数n,后跟n行操作。

输出

对每组测试数据,输出操作结束后的购物车商品清单,输出格式见样例,商品统计前输出10个-符号。所有测试数据操作结束后的购物车均非空。

输入样例1

1
8
ADD 2018040801 格力变频冷暖空调KFR-26GW 大1匹 变频挂机 2999 1
ADD 2018040802 长虹65D2P高清HDR平板LED液晶 1 1 4799 1
ADD 2018040803 康佳LED55X9人工智能平板电视机 null 55寸 4999 1
UP 2018040802
UP 2018040803
DOWN 2018040803
DELETE 2018040802
ADD 2018040802 长虹65D2P高清HDR平板LED液晶 1 1 4799 2

输出样例1

商品清单:
商品,颜色,尺码,单价,数量,小计
长虹65D2P高清HDR平板LED液晶,1,1,4799.00,2,9598.00
康佳LED55X9人工智能平板电视机,null,55寸,4999.00,1,4999.00
格力变频冷暖空调KFR-26GW,大1匹,变频挂机,2999.00,1,2999.00
----------
4件商品,总商品金额17596.00

AC代码

#include<bits/stdc++.h>
using namespace std;

class Good {
    
    
	string id, name, color, size;
	double price;
	int num;
public:
	Good() {
    
    
		cin >> id >> name >> color >> size >> price >> num;
	}

	double getSumPrice() {
    
    
		return num * price;
	}

	string getId() {
    
    
		return id;
	}

	int getNum() {
    
    
		return num;
	}

	void display() {
    
    
		cout << name << "," << color << "," << size << "," << fixed << setprecision(2) << price << "," << num << "," << fixed << setprecision(2) << getSumPrice() << endl;
	}

	void add(int n = 1) {
    
    
		num += n;
	}

	void down() {
    
    
		num--;
		if (num < 1)
			num = 1;
	}


};

class Cat {
    
    
	list<Good>set;
public:
	Cat() {
    
    }

	void ADD() {
    
    
		Good new_good;
		for (auto& good : set) {
    
    
			if (good.getId() == new_good.getId()) {
    
    
				good.add(new_good.getNum());
				return;
			}
		}
		set.push_front(new_good);
	}

	void UP() {
    
    
		string new_id;
		cin >> new_id;
		for (auto& good : set) {
    
    
			if (good.getId() == new_id) {
    
    
				good.add();
				return;
			}
		}
	}

	void DOWN() {
    
    
		string new_id;
		cin >> new_id;
		for (auto& good : set) {
    
    
			if (good.getId() == new_id) {
    
    
				good.down();
				return;
			}
		}
	}

	void DELETE() {
    
    
		string s;
		cin >> s;
		for (auto it = set.begin(); it != set.end(); it++)
		{
    
    
			if (it->getId() == s) {
    
    
				set.erase(it);
				return;
			}
		}
	}


	int getGoodNum() {
    
    
		int cnt = 0;
		for (auto& it : set)
			cnt += it.getNum();
		return cnt;
	}

	double getSumPrice() {
    
    
		double sum = 0;
		for (auto& it : set) {
    
    
			sum += it.getSumPrice();
		}
		return sum;
	}

	void print() {
    
    
		cout << "商品清单:" << endl;
		cout << "商品,颜色,尺码,单价,数量,小计" << endl;
		for (auto& it : set) {
    
    
			it.display();
		}
		cout << "----------" << endl;
		cout << getGoodNum() << "件商品,总商品金额" << fixed << setprecision(2) << getSumPrice() << endl;
	}

};


int main() {
    
    
	int t;
	cin >> t;
	while (t--) {
    
    
		Cat c;
		int n;
		cin >> n;
		while (n--) {
    
    
			string s;
			cin >> s;
			if (s == "ADD")
				c.ADD();
			else if (s == "UP")
				c.UP();
			else if (s == "DOWN")
				c.DOWN();
			else
				c.DELETE();
		}
		c.print();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46655675/article/details/129319908