1.函数模版及函数重载
题目:
编写一个函数模版,能够处理整数、实数、串类对象的大小比较,返回两个值中的最小者。为了能够处理字符数组存储的字符串的大小比较,则需要使用函数重载的形式。为了能够处理串类对象的大小比较,则需要对串类实现关系运算符’>‘或’<'的重载。
主函数如下,请勿修改:
int main()
{
int x,y;
double a,b;
char c1[20],c2[20],c3[20],c4[20];
cin>>x>>y;
cout<<Min(x,y)<<endl;
cin>>a>>b;
cout<<Min(a,b)<<endl;
cin>>c1>>c2;
cout<<Min(c1,c2)<<endl;
cin>>c3>>c4;
String s1(c3),s2(c4);
cout<<Min(s1,s2)<<endl;
return 0;
}
【样例输入】
5 9
13.2 7.8
Zhang Helen
Wang Jack
【样例输出】
5
7.8
Helen
Jack
代码:
#include<iostream>
#include<cstring>
using namespace std;
class String{
public:
String(){
s = NULL;
}
String(const char *a){
s = new char[strlen(a)]; //s指向一个新空间
strcpy(s, a); //给新空间赋值
}
friend bool operator<(const String &s1, const String &s2);
friend ostream& operator<<(ostream & out, String a);
private:
char *s;
};
template<typename T>
T Min(T a, T b){
return a < b ? a : b;
}
bool operator<(const String &s1, const String &s2){
if(strcmp(s1.s, s2.s)<0){
return true;
}
else{
return false;
}
}
char *Min(char *a, char *b)
{
return strcmp(a, b) < 0 ? a : b;//strcmp比较函数,若a<b则值<0;若a=b则值=0;若a>b则值>0
}
ostream& operator<<(ostream &out, String a){ //String a 不能加引用,因为Min返回的是临时变量;
out << a.s << endl;
return out;
}
int main(){
int x, y;
double a, b;
char c1[20], c2[20], c3[20], c4[20];
cin >> x >> y;
cin >> a >> b;
cin >> c1 >> c2; //char * 是可以直接输出的!
cin >> c3 >> c4;
cout << Min(x, y) << endl;
cout << Min(a, b) <<endl;
cout << Min(c1, c2) <<endl;
String s1(c3), s2(c4);
cout << Min(s1, s2) <<endl;
return 0;
}
2.函数模版(冒泡排序、输出)
题目:
编写两个函数模板,实现对不同类型数据的冒泡排序及输出。
(1)void Bubble_Sort(T a[], int n); 实现对数组a中n个数据进行升序排列
(2)void outputArray(T a[],int n); 实现对数组a的数据元素的输出。
主函数中定义3个数组:int a[8]; double b[8]; Student c[3];
其中Student类有私有数据成员name,score,排序是对score的排序,输出则是输出这2个私有数据成员的值。
主函数如下,请勿修改:
int main( ) { //主函数
const int A_COUNT = 8, B_COUNT = 8, C_COUNT = 3;
int a[A_COUNT] = { 7, 8, 1, 6, 5, 4, 3, 2 };//定义int数组
double b[B_COUNT] = { 5.5, 2.2, 8.8, 4.4, 1.1, 6.6, 7.7, 3.3 }; //定义double数组
Student c[C_COUNT]={{“Zhao”,90},{“Qian”,88},{“Sun”,66}}; //定义Student数组
cout << " a array contains:" << endl;
Bubble_Sort(a, A_COUNT);
outputArray(a, A_COUNT);//调用函数模板
cout << " b array contains:" << endl;
Bubble_Sort(b, B_COUNT);
outputArray(b, B_COUNT);//调用函数模板
cout << " c array contains:" << endl;
Bubble_Sort(c, C_COUNT);
outputArray(c, C_COUNT);cout<<endl;
return 0;
}
【样例输出】
a array contains:
1 2 3 4 5 6 7 8
b array contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8
c array contains:
Sun 66
Qian 88
Zhao 90
代码:
#include<iostream>
#include<cstring>
using namespace std;
class Student{
public:
Student(){
name = NULL;
score = 0;
}
Student(const char *n, int s){
name = new char[strlen(n)];
strcpy(name, n);
score = s;
}
char* getname(){ return name;}
int getscore(){ return score;}
friend ostream& operator<<(ostream &out, Student &stu){ //<<要重载为友元函数,定义可在类内;
out << stu.getname() << ' ' << stu.getscore() << endl;
return out;
}
void operator=(Student &a){ //=只能重载为成员函数;
name = new char[strlen(a.name)];
strcpy(name, a.name);
score = a.score;
}
friend bool operator>(Student &a, Student &b );
private:
char *name;
int score;
};
template<typename T>
void Bubble_Sort(T str[], int num){
int i,j;
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
if(str[i] > str[j]){
T temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
bool operator>(Student &a, Student &b ){
return a.score > b.score ? 1 : 0;
}
template<typename T>
void outputArray(T str[], int num){
for(int i = 0; i < num; i ++){
cout << str[i];
if(num != 3)cout << ' '; //如果是对象则不用空;
}
cout << endl;
}
int main( ) {
const int A_COUNT = 8, B_COUNT = 8, C_COUNT = 3;
int a[A_COUNT] = { 7, 8, 1, 6, 5, 4, 3, 2 };//定义int数组
double b[B_COUNT] = { 5.5, 2.2, 8.8, 4.4, 1.1, 6.6, 7.7, 3.3 }; //定义double数组
Student c[C_COUNT]={{"Zhao",90},{"Qian",88},{"Sun",66}}; //定义Student数组
cout << " a array contains:" << endl;
Bubble_Sort<int>(a, A_COUNT);
outputArray<int>(a, A_COUNT);//调用函数模板
cout << " b array contains:" << endl;
Bubble_Sort<double>(b, B_COUNT);
outputArray<double>(b, B_COUNT);//调用函数模板
cout << " c array contains:" << endl;
Bubble_Sort<Student>(c, C_COUNT);
outputArray<Student>(c, C_COUNT);cout<<endl;
return 0;
}
3.类模版(栈)
题目:
栈是一种特殊的数据结构,它只允许在一端进行插入元素操作和删除元素操作,这一端叫做栈顶。即,不允许在任何的中间位置进行插入和删除。
请用类模板方式设计一个栈类Stack,其中有两个私有数据成员:stack[SIZE](存放栈元素)和 top(栈顶元素下标),以及 3个公有成员函数:push(元素入栈)、pop(元素出栈)和 stackempty(判断栈是否为空栈)。
在主函数中建立一个整数栈和一个字符栈,例如 Stack A; Stack B。然后自行设计循环进行压栈操作,利用栈是否为空来判断进行出栈操作,以达到下面的输出描述形式。注意,元素个数由n给出,字符串的输入只能单个字符的接收,不能按整体字符串进行接收。
提示,主函数可以如下设计:
int main()
{
Stack A;
Stack B;
int x, n;
char ch;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
A.push(x);
}
while(!A.stackempty())
{
x=A.pop();
cout<<x<<" ";
}
… … // 请自行补充
return 0;
}
【样例输入】
5
1 2 3 4 5
7
abcdefg
【样例输出】
5 4 3 2 1
g f e d c b a
代码:
#include<iostream>
using namespace std;
template<typename T>
class Stack{
public:
Stack(){
top = -1;
}
void push(T a){
top ++;
stack[top] = a;
}
T pop(){
return stack[top --];
}
int stackempty(){
if(top == -1) return 1;
else return 0;
}
private:
int top;
T stack[100];
};
int main(){
Stack<int > A;
int x, n;
cin >> n;
for(int i = 0; i < n; i ++){
cin >> x;
A.push(x);
}
while(!A.stackempty()){
x = A.pop();
cout << x << " ";
}
cout << endl;
Stack<char> B;
char ch;
cin >> n;
for(int i = 0; i < n; i ++){
cin >> ch;
B.push(ch);
}
while(!B.stackempty()){
ch = B.pop();
cout << ch << ' ';
}
return 0;
}
欢迎提问,学弟学妹们加油~