深圳大学计软《面向对象的程序设计》实验7 拷贝构造函数与复合类
创始人
2024-05-28 15:04:48
0

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
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;vectorv(n);for (auto& it : v) {cin >> x >> y;it.setXY(x, y);if (c.isContain(it))cout << "inside" << endl;elsecout << "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;elsecout << "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
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" ;elsecout << "-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
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
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');elseid.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 else
for(i=0;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
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];elsefor (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;elsecout << "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;elsecout << "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
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 {listset;
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();elsec.DELETE();}c.print();}return 0;
}

相关内容

热门资讯

Python|位运算|数组|动... 目录 1、只出现一次的数字(位运算,数组) 示例 选项代...
张岱的人物生平 张岱的人物生平张岱(414年-484年),字景山,吴郡吴县(今江苏苏州)人。南朝齐大臣。祖父张敞,东...
西游西后传演员女人物 西游西后传演员女人物西游西后传演员女人物 孙悟空 六小龄童 唐僧 徐少华 ...
名人故事中贾岛作诗内容简介 名人故事中贾岛作诗内容简介有一次,贾岛骑驴闯了官道.他正琢磨着一句诗,名叫《题李凝幽居》全诗如下:闲...
和男朋友一起优秀的文案? 和男朋友一起优秀的文案?1.希望是惟一所有的人都共同享有的好处;一无所有的人,仍拥有希望。2.生活,...
戴玉手镯的好处 戴玉手镯好还是... 戴玉手镯的好处 戴玉手镯好还是碧玺好 女人戴玉?戴玉好还是碧玺好点佩戴手镯,以和田玉手镯为佳!相嫌滑...
依然什么意思? 依然什么意思?依然(汉语词语)依然,汉语词汇。拼音:yī    rán基本解释:副词,指照往常、依旧...
高尔基的散文诗 高尔基的散文诗《海燕》、《大学》、《母亲》、《童年》这些都是比较出名的一些代表作。
心在飞扬作者简介 心在飞扬作者简介心在飞扬作者简介如下。根据相关公开资料查询,心在飞扬是一位优秀的小说作者,他的小说作...
卡什坦卡的故事赏析? 卡什坦卡的故事赏析?讲了一只小狗的故事, 我也是近来才读到这篇小说. 作家对动物的拟人描写真是惟妙...
林绍涛为简艾拿绿豆糕是哪一集 林绍涛为简艾拿绿豆糕是哪一集第三十二集。 贾宽认为是阎帅间接导致刘映霞住了院,第二天上班,他按捺不...
小爱同学是女生吗小安同学什么意... 小爱同学是女生吗小安同学什么意思 小爱同学,小安同学说你是女生。小安是男的。
内分泌失调导致脸上长斑,怎么调... 内分泌失调导致脸上长斑,怎么调理内分泌失调导致脸上长斑,怎么调理先调理内分泌,去看中医吧,另外用好的...
《魔幻仙境》刺客,骑士人物属性... 《魔幻仙境》刺客,骑士人物属性加点魔幻仙境骑士2功1体质
很喜欢她,该怎么办? 很喜欢她,该怎么办?太冷静了!! 太理智了!爱情是需要冲劲的~不要考虑着考虑那~否则缘...
言情小说作家 言情小说作家我比较喜欢匪我思存的,很虐,很悲,还有梅子黄时雨,笙离,叶萱,还有安宁的《温暖的玄》 小...
两个以名人的名字命名的风景名胜... 两个以名人的名字命名的风景名胜?快太白楼,李白。尚志公园,赵尚志。
幼儿教育的代表人物及其著作 幼儿教育的代表人物及其著作卡尔威特的《卡尔威特的教育》,小卡尔威特,他儿子成了天才后写的《小卡尔威特...
海贼王中为什么说路飞打凯多靠霸... 海贼王中为什么说路飞打凯多靠霸气升级?凯多是靠霸气升级吗?因为之前刚到时确实打不过人家因为路飞的实力...
运气不好拜财神有用吗运气不好拜... 运气不好拜财神有用吗运气不好拜财神有没有用1、运气不好拜财神有用。2、拜财神上香前先点蜡烛,照亮人神...