题目一

【问题描述】

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
#include<iostream>
using namespace std;
template <class ElemType>
class MyLinkList; //链表类声明
template <class ElemType>
class Node{
public:
Node()
{
next=NULL;
}

Node(ElemType val)
{
data=val;
next=NULL;
}
friend class MyLinkList<ElemType>; //友元说明
private:
ElemType data;//值
Node<ElemType> * next;//指向下一个节点的指针
};
template <class ElemType>
class MyLinkList{
private:
unsigned int listlength;
Node<ElemType> * pTemp;//临时节点
Node<ElemType> * pTail;//头结点
Node<ElemType> * pHead;//尾节点
public:
MyLinkList();//初始化
void addAtTail(ElemType x);//表尾添加元素
void traversal();//遍历整个链表并打印
//该函数请同学完成
void Delete(ElemType x);//删除第一个值为x的节点
void insertHead(ElemType x);//在链表的头部插入节点
};
template <class ElemType>
MyLinkList<ElemType>::MyLinkList()
{
pTemp=NULL;
pTail=NULL;
pHead=NULL;
listlength=0;
}
template <class ElemType>
void MyLinkList<ElemType>::addAtTail(ElemType x)
{
pTemp=new Node<ElemType>(x);//申请一个新的节点
if(pTail==NULL)//如果没有尾节点则链表为空,pTemp既为头结点,又是尾节点
{
pHead=pTemp;
pTail=pTemp;
}
else//如果链表非空
{
pTail->next=pTemp;//pTemp既为尾节点的下一个节点
pTail=pTemp;//pTemp变成了尾节点,把尾节点赋值为pTemp
}
++listlength;//元素个数+1
}
template <class ElemType>
void MyLinkList<ElemType>::traversal()
{
pTemp=pHead;//用临时节点指向头结点
while(pTemp!=NULL)//遍历链表并输出
{
cout<<pTemp->data<<" "; //""内三个空格
pTemp=pTemp->next;
}
cout<<endl;
}
//该函数功能为删除值为x的第一个结点,请同学完成,可参考之前的改错作业

template <class ElemType>
void MyLinkList<ElemType>::insertHead(ElemType x)
{
pTemp=new Node<ElemType>(x);
if(pTail==NULL)
pTail=pTemp;
pTemp->next=pHead;
pHead=pTemp;
}
class Student
{
public:
Student(int id = 0, int height = 0)
{
this->ID = id;
this->Height = height;
}
bool operator ==(Student &a2);
friend ostream &operator <<(ostream &out, const Student &s)
{
out << "ID:" << s.ID << " Height:" << s.Height;
return out;
}
private:
int ID;
int Height;
};
//重载==运算符函数,注意删除的是ID相同的结点


int main()
{
MyLinkList<int> list1;
for(int i=0;i<10;i++)
list1.insertHead(i);
list1.insertHead(9);
list1.insertHead(9);
list1.insertHead(9);
list1.traversal();
list1.Delete(9);
list1.traversal();
MyLinkList<Student> list2;
list2.insertHead(Student(1,174));
list2.addAtTail(Student(2,176));
list2.addAtTail(Student(3,173));
list2.traversal();
list2.Delete(Student(1,174));
list2.traversal();
return 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
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
147
148
#include<iostream>
using namespace std;
template <class ElemType>
class MyLinkList; //链表类声明
template <class ElemType>
class Node{
public:
Node()
{
next=NULL;
}

Node(ElemType val)
{
data=val;
next=NULL;
}
friend class MyLinkList<ElemType>; //友元说明
private:
ElemType data;//值
Node<ElemType> * next;//指向下一个节点的指针
};
template <class ElemType>
class MyLinkList{
private:
unsigned int listlength;
Node<ElemType> * pTemp;//临时节点
Node<ElemType> * pTail;//头结点
Node<ElemType> * pHead;//尾节点
public:
MyLinkList();//初始化
void addAtTail(ElemType x);//表尾添加元素
void traversal();//遍历整个链表并打印
//该函数请同学完成
void Delete(ElemType x);//删除第一个值为x的节点
void insertHead(ElemType x);//在链表的头部插入节点
};
template <class ElemType>
MyLinkList<ElemType>::MyLinkList()
{
pTemp=NULL;
pTail=NULL;
pHead=NULL;
listlength=0;
}
template <class ElemType>
void MyLinkList<ElemType>::addAtTail(ElemType x)
{
pTemp=new Node<ElemType>(x);//申请一个新的节点
if(pTail==NULL)//如果没有尾节点则链表为空,pTemp既为头结点,又是尾节点
{
pHead=pTemp;
pTail=pTemp;
}
else//如果链表非空
{
pTail->next=pTemp;//pTemp既为尾节点的下一个节点
pTail=pTemp;//pTemp变成了尾节点,把尾节点赋值为pTemp
}
++listlength;//元素个数+1
}
template <class ElemType>
void MyLinkList<ElemType>::traversal()
{
pTemp=pHead;//用临时节点指向头结点
while(pTemp!=NULL)//遍历链表并输出
{
cout<<pTemp->data<<" "; //""内三个空格
pTemp=pTemp->next;
}
cout<<endl;
}
//该函数功能为删除值为x的第一个结点,请同学完成,可参考之前的改错作业
template <class ElemType>
void MyLinkList<ElemType>::Delete(ElemType x)
{
Node<ElemType> *p = pHead;
Node<ElemType> *q = NULL;
while (p != NULL && !(p->data == x)) {
q = p;
p = p->next;
}
if (p == NULL) {
return; // 找不到匹配的节点,直接返回
}
if (p == pHead) { //如果删除的是头结点
pHead = pHead->next;
}
else {
q->next = p->next;
}
if (p == pTail) { //如果删除的是尾节点
pTail = q;
}
delete p;
--listlength; //元素个数-1
}
template <class ElemType>
void MyLinkList<ElemType>::insertHead(ElemType x)
{
pTemp=new Node<ElemType>(x);
if(pTail==NULL)
pTail=pTemp;
pTemp->next=pHead;
pHead=pTemp;
}
class Student
{
public:
Student(int id = 0, int height = 0)
{
this->ID = id;
this->Height = height;
}
bool operator ==(Student &a2);
friend ostream &operator <<(ostream &out, const Student &s)
{
out << "ID:" << s.ID << " Height:" << s.Height;
return out;
}
private:
int ID;
int Height;
};
//重载==运算符函数,注意删除的是ID相同的结点
bool Student::operator ==(Student &a2){
return ID == a2.ID;
}
int main()
{
MyLinkList<int> list1;
for(int i=0;i<10;i++)
list1.insertHead(i);
list1.insertHead(9);
list1.insertHead(9);
list1.insertHead(9);
list1.traversal();
list1.Delete(9);
list1.traversal();
MyLinkList<Student> list2;
list2.insertHead(Student(1,174));
list2.addAtTail(Student(2,176));
list2.addAtTail(Student(3,173));
list2.traversal();
list2.Delete(Student(1,174));
list2.traversal();
return 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
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
#include  <iostream>
#include <iomanip>
using namespace std;
template <typename ElemType>
class myArrayList
{
private:
int mSize;
int mLen;
ElemType *mpArr;
public:
myArrayList(int n);
myArrayList(ElemType *a, int n);
void show();
ElemType getMax();
void sort();
//*************************

};
//*****************************

template<typename ElemType>
myArrayList<ElemType>::myArrayList(myArrayList<ElemType> &other)

{

this->mLen = other.mLen;

this->mSize = other.mSize;

this->mpArr = new ElemType[this->mLen];

for (int i = 0; i < this->mLen; i++)

this->mpArr[i] = other.mpArr[i];

}

template<typename ElemType>

myArrayList<ElemType>::myArrayList(int n)

{

this->mSize = n;

this->mLen = 0;

this->mpArr = new ElemType[mSize];

}
//*************************************

template <typename ElemType>

void myArrayList<ElemType>::show()

{

for (int i = 0; i < mLen; i++)

cout << mpArr[i]<<" ";//三个空格

cout << endl;

}

template <typename ElemType>
ElemType myArrayList<ElemType>::getMax()
{
ElemType max;
max = mpArr[0];
for (int i = 1; i < mLen; i++)
if (max < mpArr[i])
max = mpArr[i];
return max;
}



//Student.h

class Student
{
private:
int mId;
float height;
int score;
public:
Student(int id = 0, float h = 0, int s = 0) :height(h), mId(id), score(s)
{
}

//********************************

};



//主程序

int main()
{
int a[] = { 1, 2, 3, 5, 7, 9, 12, 8 };
double b[] = { 1, 2.5, 3.6, 5, 7, 9, 12.8, 8 };
myArrayList <int> list1(a, 8);
list1.sort();
list1.show();
cout << "max=" << list1.getMax() << endl;
myArrayList <double> list2(b, 8);
list2.sort();
list2.show();
cout << "max=" << list2.getMax() << endl;
Student s[3] = { Student(1, 175, 80), Student(2, 178, 90), Student(3, 195, 83) }, s1;
myArrayList <Student> list3(s, 3);
list3.sort();
list3.show();
cout << "max="<<list3.getMax()<< endl;
}

【代码】

1
myArrayList(myArrayList<ElemType>& other);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<typename ElemType>
myArrayList<ElemType>::myArrayList(ElemType* a, int n)
{
this->mSize = n;
this->mpArr = new ElemType[n];
this->mLen = n;
for (int i = 0; i < n; i++)
mpArr[i] = a[i];
}
template<typename ElemType>
void myArrayList<ElemType>::sort()
{
for (int i = 0; i < mLen - 1; i++)
{
for (int j = i + 1; j < mLen; j++)
if (mpArr[i] > mpArr[j])
swap(mpArr[i], mpArr[j]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool operator>(const Student& s) { 
return this->score > s.score;
}
bool operator<(const Student& s) {
return this->score < s.score;
}
bool operator==(const Student& s) {
return this->score == s.score;
}
friend ostream& operator <<(ostream& out, const Student& s)
{
out << "ID:" << s.mId << " Height:" << s.height << " Score:" << s.score << endl;
return out;
}

题目三

【问题描述】

理解主函数代码,设计一个将两个一维数组中同下标元素求和保存到第三个数组中的函数模板。而对两个字符串求和则要求实现两个字符串的连接并保存到第三个字符串中。编程实现上述功能并使程序正确运行。

说明:若
int x[3] = { 2,4,6 };int y[3] = { 1,2,3};
x和y求和保存到z,则:
z[3] = { 3,6,9 };
char c[]=“abc”,d[]=“xy”;
c和d求和保存到str,str中就是“abcxy”。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int  main()
{
const int N = 5;
int x[N] = { 2,-4,6,8,0},y[N], z[N] = {0};
char c[] = "Hello,", d[100] ;

for (int i(0); i < N; i++)
cin >> *(y + i);

while (cin.get() != '\n');
cin.getline(d, 100);

Add<int, N>(x, y, z);
for (int i(0); i < N; i++)
cout << z[i]<< " ";
cout << endl;

char* pc = new char[strlen(c) + strlen(d) + 1];
Add(c, d, pc);
cout << pc << endl;
delete[]pc;
return 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
#include<iostream>
#include<string.h>
using namespace std;
template<typename T,int N>
void Add(T x[], T y[], T z[])
{
for (int i = 0; i < N; i++)
z[i] = x[i] + y[i];
}

void Add(char c[], char d[], char str[])
{
strcpy(str, c);
strcat(str, d);
}


int main()
{
const int N = 5;
int x[N] = { 2,-4,6,8,0},y[N], z[N] = {0};
char c[] = "Hello,", d[100] ;

for (int i(0); i < N; i++)
cin >> *(y + i);

while (cin.get() != '\n');
cin.getline(d, 100);

Add<int, N>(x, y, z);
for (int i(0); i < N; i++)
cout << z[i]<< " ";
cout << endl;

char* pc = new char[strlen(c) + strlen(d) + 1];
Add(c, d, pc);
cout << pc << endl;
delete[]pc;
return 0;
}

题目四

【问题描述】

根据主函数代码,编写求一维数组中最大、最小元素及所有元素平均值的函数模板,使程序正确运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int  main()
{
int imax, imin;//分别保存最大、最小值
double* pd = new double[5];
assert(pd);
for (int i(0); i < 5; i++)
cin >> pd[i];

int iarry[] = { -20,30,40,50,33,5,8,9,-7,0 };
cout << GetValue<int>(iarry, imax, imin) << endl;//返回平均值,默认值为10个数
cout << imax << "," << imin << endl;//输出最大、最小值

double dmax, dmin;
cout <<GetValue<double, 5>(pd, dmax, dmin) << endl;//返回平均值
cout << dmax << "," << dmin << endl;//输出最大、最小值

return 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

#include <iostream>
#include <cassert>
using namespace std;

template<typename T, int N=10>
double GetValue(T* arr, T& maxVal, T& minVal) {
T sum = 0;
maxVal = arr[0];
minVal = arr[0];
for (int i = 0; i < N; i++) {
sum += arr[i];
if (arr[i] > maxVal) {
maxVal = arr[i];
}
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return sum / N;
}

int main()
{
int imax, imin;//分别保存最大、最小值
double* pd = new double[5];
assert(pd);
for (int i(0); i < 5; i++)
cin >> pd[i];

int iarry[] = { -20,30,40,50,33,5,8,9,-7,0 };
cout << GetValue<int>(iarry, imax, imin) << endl;//返回平均值,默认值为10个数
cout << imax << "," << imin << endl;//输出最大、最小值

double dmax, dmin;
cout <<GetValue<double, 5>(pd, dmax, dmin) << endl;//返回平均值
cout << dmax << "," << dmin << endl;//输出最大、最小值

return 0;
}