A.Equation(类与对象+构造)

题目描述

建立一个类Equation,表达方程ax2+bx+c=0。类中至少包含以下方法:

1、无参构造(abc默认值为1.0、1.0、0)与有参构造函数,用于初始化a、b、c的值;

2、set方法,用于修改a、b、c的值

3、getRoot方法,求出方程的根。

一元二次方程的求根公式如下:

x=b±b24ac2ax ={-b \pm \sqrt{b^2-4ac}\over 2a}

一元二次方程的求解分三种情况,如下:
一元二次方程ax2+bx+c=0(a0)ax^2+bx+c=0(a\ne0)的根与根的判别式有如下关系:(Δ=b24ac)(\Delta=b^2-4ac)

  1. Δ>0\Delta>0时,方程有两个不相等的实数根
  2. Δ=0\Delta=0时,方程有两个相等的实数根
  3. Δ<0\Delta<0时,方程无实数根,但有两个共轭复根
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<string.h>
#include<iomanip>
#include<cmath>

using namespace std;

class Equation
{
private:
double a, b, c;
public:
Equation()
{
a = 1.0, b = 1.0, c = 0;

}
Equation(double a1, double b1, double c1)
{
a = a1, b = b1; c = c1;
}
void set(double a1, double b1, double c1);
void getRoot();
};


void Equation::set(double a1, double b1, double c1)
{
a = a1, b = b1, c = c1;
}
void Equation::getRoot()
{
double x1, x2;
double solid, imaginary;
if (b * b - 4 * a * c > 0)
{
x1 = ((-b + sqrt(b * b - 4 * a * c)) / (2 * a));
x2 = ((-b - sqrt(b * b - 4 * a * c)) / (2 * a));
cout << fixed << setprecision(2) << "x1=" << x1 << " x2=" << x2 << endl;
}
if (b * b - 4 * a * c == 0)
{
x1 = -b / (2 * a);
cout << fixed << setprecision(2) << "x1=x2=" << x1 << endl;
}
if (b * b - 4 * a * c < 0)
{
solid = -b / (2 * a);
imaginary = sqrt(4 * a * c - b * b) / (2 * a);
cout << fixed << setprecision(2) << "x1=" << solid << "+" << imaginary << "i x2=" << solid << "-" << imaginary << "i" << endl;
}
}

int main()
{
int t;
cin >> t;
while (t--)
{
double a1, b1, c1;
cin >> a1 >> b1 >> c1;
Equation e(a1, b1, c1);
e.getRoot();
}
return 0;
}

B.对象是怎样构造的(拷贝构造函数)

题目描述

某个类包含一个整型数据成员.程序运行时若输入0表示用缺省方式定义一个类对象;输入1及一个整数表示用带一个参数的构造函数构造一个类对象;输入2及一个整数表示构造2个类对象,一个用输入的参数构造,另一个用前一个对象构造。试完成该类的定义和实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<string.h>
#include<iomanip>
#include<cmath>

using namespace std;

class Construction
{
private:
int x;
public:
Construction()
{
x = 0;
cout << "Constructed by default, value = 0" << endl;
}
Construction(int num)
{
x = num;
cout << "Constructed using one argument constructor, value = " << x << endl;
}
Construction(const Construction& c)
{
x = c.x;
cout << "Constructed using copy constructor, value = " << x << endl;
}
};

int main()
{
int t;
cin >> t;
while (t--)
{
int tmp;
cin >> tmp;
if (tmp == 0)
{
Construction mc;
}
else if (tmp == 1)
{
int num1;
cin >> num1;
Construction mc(num1);
}
else
{
int num2;
cin >> num2;
Construction mc1(num2);
Construction mc2(mc1);
}
}
return 0;
}

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

题目描述

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

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

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

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

注意:电话号码只能全部是数字字符,且与上述情况不符的输入均为非法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<stdio.h>

using namespace std;

class CTelNumber
{
private:
char* c;
public:
CTelNumber(char a[])
{
c = new char[8];
strcpy(c, a);
}
CTelNumber(const CTelNumber& CTN1)
{
c = new char[9];
if (CTN1.c[0] >= '2' && CTN1.c[0] <= '4') {
for (int i = 0; i <= 6; i++) {
c[i + 1] = CTN1.c[i];
}
c[0] = '8';
c[8] = '\0';
}
else if (CTN1.c[0] >= '5' && CTN1.c[0] <= '8') {
for (int i = 0; i <= 6; i++) {
c[i + 1] = CTN1.c[i];
}
c[0] = '2';
c[8] = '\0';
}

}
void print()
{
cout << c << endl;
}
~CTelNumber()
{
delete[]c;
}
};

int judge(char* p)
{
int tmp = 1;
if (p[0] >= '2' && p[0] <= '8')
{
tmp = 1;
}
else
{
return 0;
}
for (int i = 1; i < 7; i++)
{
if (p[i] >= '0' && p[i] <= '9')
{
tmp = 1;
}
else
{
tmp = 0;
break;
}
}
return tmp;
}
int main()
{
int i, t;
cin >> t;
for (i = 0; i < t; i++)
{
char ch[8];
cin >> ch;
if (judge(ch))
{
CTelNumber CM1(ch);
CTelNumber CM2(CM1);
CM2.print();
}
else
{
cout << "Illegal phone number" << endl;
}
}
return 0;
}

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

题目描述

软件作为一种对象也可以用类来描述,软件的属性包括软件名称、类型(分别用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类的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class CDate
{
private:
int year, month, day;
public:
CDate(int y, int m, int d);
bool isLeapYear();
int getYear();
int getMonth();
int getDay();
int getDayofYear();
};

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

bool CDate::isLeapYear()
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

int CDate::getYear()
{
return year;
}

int CDate::getMonth()
{
return month;
}

int CDate::getDay()
{
return day;
}

int CDate::getDayofYear()
{
int i, sum = day;
int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear())
{
a[2]++;
}
// 求日期的天数
for (i = 0; i < month; i++)
{
sum += a[i];
}
return sum;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<cstring>
using namespace std;
class CDate
{
private:
int year, month, day;
public:
CDate(int y, int m, int d);
bool isLeapYear(int year);
int getYear();
int getMonth();
int getDay();
int getDayofYear();
};
CDate::CDate(int y, int m, int d)
{
year = y, month = m, day = d;
}
bool CDate::isLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int CDate::getYear()
{
return year;
}
int CDate::getMonth()
{
return month;
}
int CDate::getDay()
{
return day;
}
int CDate::getDayofYear()
{
int i, sum = day;
if (year > 2015) {
for (int j = 2015; j < year; j++) {
if (isLeapYear(j))
sum += 366;
else
sum += 365;
}
}
int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeapYear(year))
{
a[2]++;
}
for (i = 0; i < month; i++)
{
sum += a[i];
}
return sum;
}
class soft
{
string name;
char type, medium;
CDate num;
public:
soft(string name1, char type1, char medium1, int year1, int month1, int day1) :num(year1, month1, day1) {
name = name1;
type = type1;
medium = medium1;
}
soft(soft& s) :num(s.num)
{
name = s.name;
type = 'B';
medium = 'H';
}
string getname()
{
return name;
}
string gettype()
{
switch (type)
{
case 'O':return "original";
case 'B':return "backup";
case 'T':return "trial";
}
}
string getmedium()
{
switch (medium)
{
case'D':return "optical disk";
case'U':return "USB disk";
case'H':return "hard disk";
}
}
void printdate()
{
CDate stand_num(2015, 4, 7);
if (num.getDayofYear() == 0) {
cout << "this software has unlimited use" << endl << endl;
}
else if (num.getDayofYear() < stand_num.getDayofYear()) {
cout << "this software has expired" << endl << endl;
}
else {
cout << "this software is going to be expired in " << num.getDayofYear() - stand_num.getDayofYear() << " days" << endl << endl;
}
}
void printsw()
{
cout << "name:" << getname() << endl;
cout << "type:" << gettype() << endl;
cout << "media:" << getmedium() << endl;
}
};
int main()
{
int t, y, m, d;
string name;
char type, medium;
cin >> t;
while (t--) {
cin >> name >> type >> medium >> y >> m >> d;
soft sw1(name, type, medium, y, m, d);
sw1.printsw();
sw1.printdate();
soft sw2(sw1);
sw2.printsw();
sw2.printdate();
}

return 0;
}

E.手机服务(构造+拷贝构造+堆)

题目描述

设计一个类来实现手机的功能。它包含私有属性:号码类型、号码、号码状态、停机日期;包含方法:构造、拷贝构造、打印、停机。

1、号码类型表示用户类别,只用单个字母,A表示机构,B表示企业、C表示个人

2、号码是11位整数,用一个字符串表示

3、号码状态用一个数字表示,1、2、3分别表示在用、未用、停用

4、停机日期是一个日期对象指针,在初始化时该成员指向空,该日期类包含私有属性年月日,以及构造函数和打印函数等


5、构造函数的作用就是接受外来参数,并设置各个属性值,并输出提示信息,看示例输出

6、拷贝构造的作用是复制已有对象的信息,并输出提示信息,看示例输出。
想一下停机日期该如何复制,没有停机如何复制??已经停机又如何复制??

7、打印功能是把对象的所有属性都输出,输出格式看示例

8、停机功能是停用当前号码,参数是停机日期,无返回值,操作是把状态改成停用,并停机日期指针创建为动态对象,并根据参数来设置停机日期,最后输出提示信息,看示例输出


要求:在主函数中实现号码备份的功能,对已有的虚拟手机号的所有信息进行复制,并将号码类型改成D表示备份;将手机号码末尾加字母X

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
class phone
{
private:
char type;
string num;
int state;
public:
phone() {};
phone(phone& Mainframe);
void set(char t1, string n1, int s1);
void print();
void stop(int y, int m, int d);
};
phone::phone(phone & Mainframe)
{
type = Mainframe.type;
num = Mainframe.num;
state = Mainframe.state;
cout << "Construct a copy of phone " << num << endl;
cout << "类型=备份||";
cout << "号码=" << num << "X";
switch (state)
{
case 1:cout << "||State=在用" << endl; break;
case 2:cout << "||State=未用" << endl; break;
case 3:cout << "||State=停用" << endl; break;
}
}
void phone::set(char t1, string n1, int s1)
{
type = t1;
num = n1;
state = s1;
cout << "Construct a new phone " << num << endl;
}
void phone::print()
{
switch (type)
{
case'A':cout << "类型=机构||"; break;
case'B':cout << "类型=企业||"; break;
case'C':cout << "类型=个人||"; break;
}
cout << "号码=" << num;
switch (state)
{
case 1:cout << "||State=在用" << endl; break;
case 2:cout << "||State=未用" << endl; break;
case 3:cout << "||State=停用" << endl; break;
}
}
void phone::stop(int y, int m, int d)
{
cout << "Stop the phone " << num << endl;
switch (type)
{
case'A':cout << "类型=机构||"; break;
case'B':cout << "类型=企业||"; break;
case'C':cout << "类型=个人||"; break;
}
cout << "号码=" << num << "||State=停用||停机日期=" << y << "." << m << "." << d << endl;
cout << "----" << endl;
}
int main()
{
char type;
string num;
int state, n, year, month, day;
phone P;
cin >> n;
while (n--)
{
cin >> type >> num >> state >> year >> month >> day;
P.set(type, num, state);
P.print();
phone p(P);
p.stop(year, month, day);
}
return 0;
}