A.分数的加减乘除(运算符重载)

题目描述

Fraction类的基本形式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 定义Fraction类
class Fraction
{
private:
int fz, fm;
int commonDivisor(); // 计算最大公约数
void contracted(); // 分数化简
public:
Fraction(int = 0, int = 1);
Fraction(Fraction&);
Fraction operator+(Fraction);
Fraction operator-(Fraction);
Fraction operator*(Fraction);
Fraction operator/(Fraction);
void set(int = 0, int = 1);
void display();
};

要求如下:

1.实现Fraction类;common_divisor()和contracted()函数体可为空,不实现具体功能。
2.编写main函数,初始化两个Fraction对象的,计算它们之间的加减乘除。

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
#include<iostream>
#include<cstring>
using namespace std;

class Fraction
{
private:
int fz, fm;
int commonDivisor() {};
void contracted() {};
public:
Fraction(int a = 0, int b = 1)
{
fz = a;
fm = b;
}
Fraction(Fraction& a)
{
fz = a.fz;
fm = a.fm;
}
Fraction operator+(Fraction& a)
{
Fraction A;
A.fz = fz * a.fm + a.fz * fm;
A.fm = fm * a.fm;
return A;
}
Fraction operator-(Fraction& a)
{
Fraction A;
A.fz = fz * a.fm - a.fz * fm;
A.fm = fm * a.fm;
return A;
}
Fraction operator*(Fraction& a)
{
Fraction A;
A.fz = fz * a.fz;
A.fm = fm * a.fm;
return A;
}
Fraction operator/(Fraction& a)
{
Fraction A;
A.fz = fz * a.fm;
A.fm = fm * a.fz;
return A;
}
void set(int a = 0, int b = 1)
{
fz = a;
fm = b;
}
void display()
{
cout << "fraction=" << fz << "/" << fm << endl;
}
};

int main()
{
int a, b;
cin >> a >> b;
Fraction x(a, b);
cin >> a >> b;
Fraction y(a, b);
Fraction z;
z = x + y;
z.display();
z = x - y;
z.display();
z = x * y;
z.display();
z = x / y;
z.display();


return 0;
}

B.复数的加减乘运算(运算符重载)

题目描述

定义一个复数类,通过重载运算符:+、-、*,实现两个复数之间的各种运算。

1
2
3
4
5
6
7
8
9
10
11
class Complex
{
private:
float real, image;
public:
Complex(float x = 0, float y = 0);
friend Complex operator+(Complex&, Complex&);
friend Complex operator-(Complex&, Complex&);
friend Complex operator*(Complex&, Complex&);
void show();
};

要求如下:

1.实现Complex类;
2.编写main函数,初始化两个Complex对象,计算它们之间的加减乘,并输出结果。
复数相乘的运算规则
设z1=a+bi,z2=c+di(a、b、c、d∈R)是任意两个复数,那么它们的积(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

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>
using namespace std;

class Complex
{
private:
float real, image;
public:
Complex(){}
Complex(float x , float y)
{
real = x;
image = y;
}
friend Complex operator+(Complex& a, Complex& b)
{
Complex c;
c.real = a.real + b.real;
c.image = a.image + b.image;
return c;
}
friend Complex operator-(Complex& a, Complex& b)
{
Complex c;
c.real = a.real - b.real;
c.image = a.image - b.image;
return c;
}
friend Complex operator*(Complex& a, Complex& b)
{
Complex c;
c.real = a.real * b.real - a.image * b.image;
c.image = a.image * b.real + a.real * b.image;
return c;
}
void show()
{
cout << "Real=" << real << " Image=" << image << endl;
}
};

int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
Complex c1(a, b);
Complex c2(c, d);
Complex c3;
c3 = c1 + c2;
c3.show();
c3 = c1 - c2;
c3.show();
c3 = c1 * c2;
c3.show();

return 0;
}

C.向量的加减(运算符重载)

题目描述

设向量X=(x1,x2,…,xn)和Y=(y1,y2…,yn),它们之间的加、减分别定义为:

X+Y=(x1+y1,x2+y2,…,xn+yn)
  X-Y=(x1-y1,x2-y2,…,xn-yn)

编程序定义向量类Vector ,重载运算符“+”、“-”,实现向量之间的加、减运算;并编写print函数作为向量的输出操作。

Vector类的基本形式如下:

1
2
3
4
5
6
7
8
9
10
11
12
class Vector
{
private:
int vec[5];
public:
Vector(int v[]);
Vector();
Vector(const Vector& obj);
Vector operator +(const Vector& obj);
Vector operator -(const Vector& obj);
void print();
};

要求如下:
1.实现Vector类;
2.编写main函数,初始化两个Vector对象的,计算它们之间的加减,并输出结果。

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
#include<iostream>
using namespace std;

class Vector
{
private:
int vec[5];
public:
Vector(int v[5])
{
for (int i = 0; i < 5; i++)
{
vec[i] = v[i];
}
}
Vector(){}
Vector(const Vector& obj)
{
for (int i = 0; i < 5; i++)
{
vec[i] = obj.vec[i];
}
}
Vector operator+(const Vector& obj)
{
Vector tmp;
for (int i = 0; i < 5; i++)
{
tmp.vec[i] = vec[i] + obj.vec[i];
}
return tmp;
}
Vector operator-(const Vector& obj)
{
Vector tmp;
for (int i = 0; i < 5; i++)
{
tmp.vec[i] = vec[i] - obj.vec[i];
}
return tmp;
}
void print()
{
for (int i = 0; i < 5; i++)
{
cout << vec[i] << " ";
}
cout << endl;
}

};

int main()
{
int x[5];
for (int i = 0; i < 5; i++)
{
cin >> x[i];
}
Vector a(x);
for (int i = 0; i < 5; i++)
{
cin >> x[i];
}
Vector b(x);
Vector c;
c = a + b;
c.print();
c = a - b;
c.print();

return 0;

}

D.矩阵相乘(运算符重载)

题目描述

定义一个矩阵类MyMatrix,并且在类中进行运算符重定义,用*实现矩阵相乘。要求必须对运算符进行重载,如果用函数如multiply(matrix,matrix)去实现矩阵之间的运算一律记0分。

必须包含以下析构函数,其中n是矩阵的阶数,data是存放矩阵数据的二维动态数组:

1
2
3
4
5
6
7
8
MyMatrix::~MyMatrix() 
{ // 释放空间
for (int i = 0; i < n; i++)
{
delete[] data[i];
}
delete[] data;
}
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
#include<iostream>
using namespace std;
class MyMatrix
{
private:
int a[10][10];
int n;
public:
MyMatrix(int _n):n(_n){}
friend MyMatrix operator*(MyMatrix& m1, MyMatrix& m2)
{
int n = m1.n;
MyMatrix x(n);
int sum;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
sum = 0;
for (int k = 0; k < n; k++)
{
sum += m1.a[i][k] * m2.a[k][j];
}
x.a[i][j] = sum;
}
}
return x;
}
void set()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
}
void show()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j != n - 1)
{
cout << a[i][j] << " ";
}
else
{
cout << a[i][j] << endl;
}
}

}
}
};

int main()
{
int c, n;
cin >> c >> n;
MyMatrix m1(n);
m1.set();
for (int i = 0; i < c - 1; i++)
{
MyMatrix m2(n);
m2.set();
m1 = m1 * m2;
}
m1.show();

return 0;
}

E.最小生日差值计算(运算符重载)

题目描述

定义一个学生类Student,包含该学生的姓名、出生年、月、日 ,重定义 “-”号实现两个学生之间相差多少天的比较。并利用重载的“-”运算符,求所有学生中年龄相差最小的两个人的名字以及相差天数。

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
135
136
137
138
139
140
141
142
143
144
145
146
#include<iostream>
#include<cstring>
using namespace std;


class Student
{
private:
string name;
int year;
int month;
int day;
public:
Student(){}
Student(string n,int y,int m,int d):name(n),year(y),month(m),day(d){}
void set(string n, int y, int m, int d)
{
year = y;
month = m;
day = d;
name = n;
}
string getname()
{
return name;
}
int operator-(Student& s)
{
int tmp = 0;
int emp = 0;
int m[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int n[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
for (int i = 0; i < month - 1; i++)
{
tmp += n[i];
}
}
else
{
for (int i = 0; i < month - 1; i++)
{
tmp += m[i];
}
}
if ((s.year % 4 == 0 && s.year % 100 != 0) || (s.year % 400 == 0))
{
for (int i = 0; i < s.month - 1; i++)
{
emp += n[i];
}
}
else
{
for (int i = 0; i < s.month - 1; i++)
{
emp += m[i];
}
}
tmp += day;
emp += s.day;
if (year == s.year)
{
if (tmp - emp < 0)
{
return emp - tmp;
}
else
{
return tmp - emp;
}
}
else if (year > s.year)
{
int max = year;
int min = s.year;
while (max - min)
{
if ((min % 4 == 0 && min % 100 != 0) || (min % 400 == 0))
{
tmp += 366;
}
else
{
tmp += 365;
}
min++;
}
return tmp - emp;
}
else
{
int min = year;
int max = s.year;
while (max - min)
{
if ((min % 4 == 0 && min % 100 != 0) || (min % 400 == 0))
{
emp += 366;
}
else
{
emp += 365;
}
min++;
}
return emp - tmp;
}
}
};

int main()
{
int t, d;
int min = 10000;
int s1 = 0;
int s2 = 0;
int year, month, day;
string name;
Student* s;
cin >> t;
s = new Student[t];
for (int i = 0; i < t; i++)
{
cin >> name >> year >> month >> day;
s[i].set(name, year, month, day);
}
for (int i = 0; i < t - 1; i ++ )
{
for (int j = 1; j < t; j++)
{
d = s[j] - s[i];
if (d < min && s[i].getname() != s[j].getname())
{
min = d;
s1 = i;
s2 = j;
}
}
}
cout << s[s1].getname() << "和" << s[s2].getname() << "年龄相差最小,为" << min << "天。" << endl;

return 0;

}