Swap
描述
用模板函数Swap实现对不同类型的数据进行交换。
并使用如下主函数测试。
int main() { int a1, a2; std::cin >> a1 >> a2; Swap(a1, a2); std::cout << a1 << "," << a2 << std::endl; double b1, b2; std::cin >> b1 >> b2; Swap(b1, b2); std::cout << b1 << "," << b2 << std::endl; char c1, c2; std::cin >> c1 >> c2; Swap(c1, c2); std::cout << c1 << "," << c2 << std::endl; return 0; }
注意,本题只需要提交Swap函数代码,头文件和main函数系统已经提供。
输入
输入共三行。第一行两整数,第二行两浮点数,第三行两字符
输出
输出共三行。每一行为对应输入处理后的结果,输出的两个数用逗号隔开
输入样例 1
2 3 1.2 2.3 a b
输出样例 1
3,2 2.3,1.2 b,a
//注意,本题只需要提交Swap函数代码,头文件和main函数系统已经提供。 #include<iostream> using namespace std; template<typename T> void Swap(T& t1, T& t2) { T temp = t1; t1 = t2; t2 = temp; }
SortFunctionTemplate
描述
用模板函数实现数组的输入、排序和输出。并使用如下主函数测试你的模板
int main() { const int LEN = 5; int type; while (std::cin >> type) { switch (type) { case 0: { int a1[LEN]; Input<int>(a1, LEN); Sort<int>(a1, LEN); Output<int>(a1, LEN); break; } case 1: { char a2[LEN]; Input(a2, LEN); Sort(a2, LEN); Output(a2, LEN); break; } case 2: { double a3[LEN]; Input(a3, LEN); Sort(a3, LEN); Output(a3, LEN); break; } } } return 0; }
注意:本题只提交Input,Sort, Output函数代码。其余部分系统已包含。
输入
输入包含多组测试数据。每组数据为两行,第一行整数type(0、1、2)。第二行为相应数组的5个元素。
输出
对于每一组测试数据,将其排序后在一行内输出,相邻元素逗号空格分离,最后为换行。
输入样例 1
0 3 6 1 4 5 1 A B C B A
输出样例 1
1, 3, 4, 5, 6 A, A, B, B, C
#include<iostream> using namespace std; template<typename T> void Input(T arr[], int len) { for (int i = 0; i < len; i++) { cin >> arr[i]; } } template<typename T> void Swap(T &a, T &b) { T t = a; a = b; b = t; } template <typename T> void Sort(T arr[], int len) { for (int i = 0; i < len; i++) { int max = i; for (int j = i + 1; j < len; j++) { if (arr[max] >arr[j]) { max = j; } } if (max != i) { Swap(arr[max], arr[i]); } } } template<class T> void Output(T arr[], int len) { for (int i = 0; i < len; i++) { cout << arr[i]; if (i < len - 1) cout << ", "; } cout << endl; } //本题只提交Input,Sort, Output函数代码,否则编译出错(其余部分系统已包含)
Search
描述
设计一个模板函数,实现在一个给定的数组中查找给定数据是否存在,如果存在则输出该数据在数组中最小的下标,如果不存在,输出-1。以下为测试函数
int main() { int n; std::cin >> n; int *nValues = new int[n]; for (int i = 0; i < n; i++) { std::cin >> nValues[i]; } int d; std::cin >> d; std::cout << Search(nValues, n, d) << std::endl; delete[] nValues; double f; std::cin >> n; double *dValues = new double[n]; for (int i = 0; i < n; i++) { std::cin >> dValues[i]; } std::cin >> f; std::cout << Search(dValues, n, f) << std::endl; delete[] dValues; std::cin >> n; char *cValues = new char[n]; for (int i = 0; i < n; i++) { std::cin >> cValues[i]; } char c; std::cin >> c; std::cout << Search(cValues, n, c) << std::endl; delete[] cValues; return 0; }
输入
输入共三组数据,每组数据占三行。
第一组第一行整数n1,第二行是n1个整数,第三行待查找整数n
第二组第一行整数n2,第二行是n2个浮点数,第三行待查找浮点数d
第三组第一行整数n3,第二行是n3个字符,第三行待查找字符c
输出
对于每一组输入,如果查找数据存在,则输出其最小下标(下标从0开始计),否则输出-1。
输入样例 1
7 1 1 2 5 8 10 13 8 5 -1.0 1.1 1.2 1000.10101 8.9 3.5 4 B J F U j
输出样例 1
4 -1 -1
提示
使用模板函数
使用模板函数
template <class T> int Search(const T * array, int arrayLen, const T & value)
#include<iostream> using namespace std; template <class T> int Search(const T* array, int arrayLen, const T& value) { int index = -1; for (int i = 0; i < arrayLen; i++) { if (value == array[i]) index = i; } return index; } int main() { int n; std::cin >> n; int* nValues = new int[n]; for (int i = 0; i < n; i++) { std::cin >> nValues[i]; } int d; std::cin >> d; std::cout << Search(nValues, n, d) << std::endl; delete[] nValues; double f; std::cin >> n; double* dValues = new double[n]; for (int i = 0; i < n; i++) { std::cin >> dValues[i]; } std::cin >> f; std::cout << Search(dValues, n, f) << std::endl; delete[] dValues; std::cin >> n; char* cValues = new char[n]; for (int i = 0; i < n; i++) { std::cin >> cValues[i]; } char c; std::cin >> c; std::cout << Search(cValues, n, c) << std::endl; delete[] cValues; return 0; }
TVector3
描述
构造一个模板类(Vector),数据成员如下:
template<typename T> class Vector { private: T x, y, z; };
完成Vector,并用以下函数测试
int main() { double a, b, c; std::cin >> a >> b >> c; Vector<double> v1(a, b, c), v2(v1), v3, v4; double d; std::cin >> d; v4 = d * v1 + v2; std::cout << v4 <<std::endl; Vector<double> v; std::cin >> v; int flag = (v4 == v); std::cout << flag << std::endl; return 0; }
输入
见样例
输出
见样例
输入样例 1
3 4 5 2.2 9.6 12.8 16
输出样例 1
9.6 12.8 16 1
#include<iostream> using namespace std; template<typename T> class Vector { private: T X, Y, Y; public: Vector(T a=0.0,T b=0.0,T c=0.0):X(a),Y(b),Y(c){} Vector(const Vector& v) { this->X = v.X; this->Y = v.Y; this->Y = v.Y; } friend Vector operator*(const double X, const Vector &v) { Vector c; c.X = X * v.X; c.Y = X * v.Y; c.Y = X * v.Y; return c; } friend Vector operator+(const Vector& v1,const Vector &v2) { Vector c; c.X = v1.X + v2.X; c.Y = v1.Y + v2.Y; c.Y = v1.Y + v2.Y; return c; } friend ostream & operator<<(ostream& os,const Vector &v) { os << v.X <<" " <<v.Y<<' '<< v.Y; return os; } friend istream & operator>>(istream& is,Vector& v) { double a, b, c; std::cin >> a >> b >> c; v.X = a; v.Y = b; v.Y = c; return is; } friend int operator==(const Vector& v1,const Vector& v2) { if ((fabs(v1.X - v2.X) < 1e-6) && (fabs(v1.Y - v2.Y) < 1e-6) && (fabs(v1.Y - v2.Y) < 1e-6)) return 1; else return 0; } }; int main() { double a, b, c; std::cin >> a >> b >> c; Vector<double> v1(a, b, c), v2(v1), v3, v4; double d; std::cin >> d; v4 = d * v1 + v2; std::cout << v4 << std::endl; Vector<double> v; std::cin >> v; int flag = (v4 == v); std::cout << flag << std::endl; return 0; }
StackClassTemplate
描述
实现一个Stack类模板并测试这一模板.
template<class T, int SIZE = 20> class Stack { private: T array[SIZE]; //数组,用于存放栈的元素 int top; //栈顶位置(数组下标) public: Stack(); //构造函数,初始化栈 void Push(const T & ); //元素入栈 T Pop(); //栈顶元素出栈 void Clear(); //将栈清空 const T & Top() const; //访问栈顶元素 bool Empty() const; //测试栈是否为空 bool Full() const; //测试是否栈满 int Size(); //返回当前栈中元素个数 };
测试函数:
int main() { Stack<int, 10> intStack; int n; cin >> n; //n<=10 for (int i = 0; i < n; i++) { int temp; cin >> temp; intStack.Push(temp); } for (int i = 0; i < n; i++) { cout << intStack.Top() << " "; intStack.Pop(); } cout<<endl; if(intStack.Empty()) cout<<"Now, intStack is empty."<<endl; Stack<string,5> stringStack; stringStack.Push("One"); stringStack.Push("Two"); stringStack.Push("Three"); stringStack.Push("Four"); stringStack.Push("Five"); cout<<"There are "<<stringStack.Size()<<" elements in stringStack."<<endl; stringStack.Clear(); if(stringStack.Empty()) cout<<"Now, there are no elements in stringStack"<<endl; return 0; }
输入
参考样例
输出
参考样例
输入样例 1
3 1 2 3
输出样例 1
3 2 1 Now, intStack is empty. There are 5 elements in stringStack. Now, there are no elements in stringStack
#define _CTR_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<stack> using namespace std; template<class T, int SIZE = 20> class Stack { private: T array[SIZE]; int top; public: Stack(); void Push(const T &); T Pop(); void Clear(); const T & Top() const; bool Empty() const; bool Full() const; int Size(); }; void Stack<T, SIZE>::Push(const T &x) { array[top] = x; top++; } template<class T, int SIZE> T Stack<T, SIZE>::Pop() { top--; return array[top + 1]; } template<class T, int SIZE> Stack<T, SIZE>::Stack() { top = 0; } template<class T, int SIZE> template<class T, int SIZE> void Stack<T, SIZE>::Clear() { top = 0; } template<class T, int SIZE> const T& Stack<T, SIZE>::Top()const { return array[top - 1]; } template<class T, int SIZE> bool Stack<T, SIZE>::Empty()const { return top == 0; } template<class T, int SIZE> bool Stack<T, SIZE>::Full()const { return top == SIZE; } template <class T, int SIZE> int Stack<T, SIZE>::Size() { return top; } int main() { Stack<int, 10> intStack; int n; cin >> n; //n<=10 for (int i = 0; i < n; i++) { int temp; cin >> temp; intStack.Push(temp); } for (int i = 0; i < n; i++) { cout << intStack.Top() << " "; intStack.Pop(); } cout << endl; if (intStack.Empty()) cout << "Now, intStack is empty." << endl; Stack<string, 5> stringStack; stringStack.Push("One"); stringStack.Push("Two"); stringStack.Push("Three"); stringStack.Push("Four"); stringStack.Push("Five"); cout << "There are " << stringStack.Size() << " elements in stringStack." << endl; stringStack.Clear(); if (stringStack.Empty()) cout << "Now, there are no elements in stringStack" << endl; return 0; }