1. 编写学生模板类
【问题描述】根据声明的学生模板类,编程实现指定功能,具体功能见代码注释部分。
【输入形式】输入学生数据验证模板类
【输出形式】输出删除及排序后后的结果
【样例输入】
37343 47 73309 69 60681 80 30953 62
10313
33513 wXpas 89934 ZJUWS 25965 fgbHL 36314 mBnWw
33472
【样例输出】
60681 80
73309 69
30953 62
37343 47
89934 ZJUWS
25965 fgbHL
36314 mBnWw
33513 wXpas
【样例输入】
37343 47 73309 69 60681 80 30953 62
37343
33513 wXpas 89934 ZJUWS 25965 fgbHL 36314 mBnWw
36314
【样例输出】
60681 80
73309 69
30953 62
89934 ZJUWS
25965 fgbHL
33513 wXpas
#include <iostream>
#include <string>
using namespace std;
// 声明学生模板类【代码补充1/2】
template <typename TNo, typename TScore, int num>
class Student
{
public:
Student() { count = 0; }
void append(TNo sid, TScore sscore); // 添加一个学生数据
void deleteStu(TNo sid); // 删除指定学号的学生数据
void sort(bool reverse = false); // 根据成绩排序,默认升序排序
void displayStudents(); // 依次输出学生数据,每个学生数据空格分隔,多个学生数据分行显示
private:
TNo stu_id[num]; // 学号,可以为整型,可以为字符串
TScore stu_score[num]; // 成绩,可以为整型,可以为字符串
int count; // 实际学生数量
};
// 实现学生模板类成员函数【代码补充2/2】
template <typename TNo, typename TScore, int num>
void Student<TNo, TScore, num>::append(TNo sid, TScore sscore)
{
if (count < num)
{
stu_id[count] = sid;
stu_score[count] = sscore;
count++;
}
}
template <typename TNo, typename TScore, int num>
void Student<TNo, TScore, num>::deleteStu(TNo sid)
{
for (int i = 0; i < count; i++)
{
if (stu_id[i] == sid)
{
for (int j = i; j < count - 1; j++)
{
stu_id[j] = stu_id[j + 1];
stu_score[j] = stu_score[j + 1];
}
count--;
break;
}
}
}
template <typename TNo, typename TScore, int num>
void Student<TNo, TScore, num>::sort(bool reverse)
{
if (reverse)
{
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - i - 1; j++)
{
if (stu_score[j] < stu_score[j + 1])
{
swap(stu_id[j], stu_id[j + 1]);
swap(stu_score[j], stu_score[j + 1]);
}
}
}
}
else
{
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - i - 1; j++)
{
if (stu_score[j] > stu_score[j + 1])
{
swap(stu_id[j], stu_id[j + 1]);
swap(stu_score[j], stu_score[j + 1]);
}
}
}
}
}
template <typename TNo, typename TScore, int num>
void Student<TNo, TScore, num>::displayStudents()
{
for (int i = 0; i < count; i++)
{
cout << stu_id[i] << " " << stu_score[i] << endl;
}
}
int main()
{
int i, tn, ts;
Student<int, int, 10> stu;
for (i = 0; i < 4; i++) // 添加4个学生信息
{
cin >> tn >> ts;
stu.append(tn, ts);
}
cin >> tn;
stu.deleteStu(tn); // 删除指定学号的学生
stu.sort(true);
Student<int, string, 4> stu2;
string ts2;
for (i = 0; i < 4; i++) // 添加4个学生信息
{
cin >> tn >> ts2;
stu2.append(tn, ts2);
}
cin >> tn;
stu2.deleteStu(tn); // 删除指定学号的学生
stu2.sort();
stu.displayStudents();
stu2.displayStudents();
return 0;
}
2. 采用模板技术实现数据查找
【问题描述】从键盘读入5个复数存入到复数数组中,输入一个复数对象,从数组查找该对象是否存在。如果存在,返回该对象在数组中的下标号,如果不存在,返回not found。
【样例输入】
1 2 3 4 5 6 7 8 9 0
1 2
【样例输出】
0
【样例输入】
1 2 3 4 5 6 7 8 9 0
1 8
【样例输出】
not found
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Complex
{
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(r) {}
// 重载==运算符
bool operator==(const Complex &other) const
{
if (real == other.real && imag == other.imag)
return true;
return false;
}
// 重载输入运算符
friend istream &operator>>(istream &is, Complex &obj);
};
// 重载输入运算符
istream &operator>>(istream &is, Complex &obj)
{
is >> obj.real >> obj.imag;
return is;
}
// 定义IndexOf函数
int IndexOf(const Complex *pData, const Complex &flag)
{
if (pData == NULL)
return -1;
for (int i = 0; i < MAX_SIZE; i++)
{
if (pData[i] == flag)
return i;
}
return -1;
}
int main()
{
Complex ar[MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++)
cin >> ar[i];
Complex flag;
cin >> flag;
int idx = IndexOf(ar, flag);
if (idx < 0)
cout << "not found" << endl;
else
cout << idx << endl;
return 0;
}
3. 模板函数应用(求两对象的最大值)
【问题描述】完成下面代码,使程序运行结果正确(求两对象的最大值)。
【输入形式】整数1 整数2 浮点数1 浮点数2 字符串1 字符串2
【输出形式】整数 浮点数 字符串
【样例输入】1 2 3.4 5.2 Zhang Li
【样例输出】2 5. Zhang
#include <iostream>
#include <string.h>
using namespace std;
class String
{
char *pData;
public:
String() { pData = NULL; }
String(const char *pStr)
{
pData = new char[strlen(pStr) + 1];
strcpy(pData, pStr);
}
String(const String &other)
{
pData = new char[strlen(other.pData) + 1];
strcpy(pData, other.pData);
}
~String()
{
delete[] pData;
}
// ">" 运算符重载
bool operator>(const String &other) const
{
return strcmp(pData, other.pData) > 0;
}
// 采用友元形式完成输出运算符重载
friend ostream &operator<<(ostream &out, const String &obj);
};
// 输出运算符重载
ostream &operator<<(ostream &out, const String &obj)
{
out << obj.pData;
return out;
}
// 设计GetMax函数模板
template <typename T>
T GetMax(const T &a, const T &b)
{
return a > b ? a : b;
}
int main()
{
int aInt = 0, bInt = 0;
float aFloat = 0.0f, bFloat = 0.0f;
char temp[50];
cin >> aInt >> bInt >> aFloat >> bFloat;
cin >> temp;
String aString(temp);
cin >> temp;
String bString(temp);
cout << GetMax(aInt, bInt) << " ";
cout << GetMax(aFloat, bFloat) << " ";
cout << GetMax(aString, bString) << endl;
return 0;
}
今天的内容就分享这么多
求三连!!!
求关注!!!