请编写一个类Test,其中包含了多个数据交换函数,名称都为Swap。本题已经给出了main函数的实现,请编写类Test代码并提交。main函数代码如下: int main() { int a, b; int c, d; float e, f; int ints[n]; Test* o = new Test(); cin >> a >> b; cin >> c >> d; cin >> e >> f; for (int i = 0; i < n; i++) { cin >> ints[i]; } o->Swap(a, b); o->Swap(&c, &d); o->Swap(e, f); o->Swap(ints); cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl; cout << fixed << setprecision(2) << e << endl; cout << fixed << setprecision(2) << f << endl; o->Print(ints); return 0; }
时间: 2023-12-20 19:03:53 浏览: 94
以下是类Test的代码实现:
```c++
#include<iostream>
#include<iomanip>
using namespace std;
const int n=5;
class Test{
public:
void Swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
void Swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void Swap(float &a, float &b){
float temp = a;
a = b;
b = temp;
}
void Swap(int a[]){
for (int i = 0; i < n/2; i++){
int temp = a[i];
a[i] = a[n-1-i];
a[n-1-i] = temp;
}
}
void Print(int a[]){
for (int i = 0; i < n; i++){
cout << a[i] << " ";
}
cout << endl;
}
};
int main() {
int a, b;
int c, d;
float e, f;
int ints[n];
Test* o = new Test();
cin >> a >> b;
cin >> c >> d;
cin >> e >> f;
for (int i = 0; i < n; i++){
cin >> ints[i];
}
o->Swap(a, b);
o->Swap(&c, &d);
o->Swap(e, f);
o->Swap(ints);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << fixed << setprecision(2) << e << endl;
cout << fixed << setprecision(2) << f << endl;
o->Print(ints);
return 0;
}
```
这个类中包含了四个Swap函数,分别是交换两个整数、交换指针所指的两个整数、交换两个浮点数和交换一个整数数组的前后元素。在main函数中,我们定义了一个Test类型的指针o,并且通过这个指针来调用类中的函数。最后我们使用Print函数输出数组中的元素。
阅读全文