请编写一个类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-19 21:05:12 浏览: 79
```
#include <iostream>
#include <iomanip>
using namespace std;
class Test {
public:
void Swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
void Swap(int* c, int* d) {
int temp = *c;
*c = *d;
*d = temp;
}
void Swap(float& e, float& f) {
float temp = e;
e = f;
f = temp;
}
void Swap(int ints[]) {
int temp = ints[0];
ints[0] = ints[n-1];
ints[n-1] = temp;
}
void Print(int ints[]) {
for (int i = 0; i < n; i++) {
cout << ints[i] << " ";
}
cout << endl;
}
private:
const static int n = 5;
};
int main() {
int a, b;
int c, d;
float e, f;
int ints[Test::n];
Test* o = new Test();
cin >> a >> b;
cin >> c >> d;
cin >> e >> f;
for (int i = 0; i < Test::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);
delete o;
return 0;
}
```
阅读全文