面向对象程序设计-实验六 类模板和模板类

2022/5/13
1.以下是交换两个变量值的模板演示程序,之所以能实现交换,在于指针参数的传递。现要求模板改为引用参数templatevoid exchange(T& a, T& b);实现模板函数的非地址实参的自然调用,即exchangge(x,y);而其功能不变。请写出修改后的程序:

(1) 修改后的模板;
(2) 改写main函数。

#include<iostream>
using namespace std;
template<class T>
void exchange(T* a, T* b){
  T tmp = *a;
  *a = *b;
  *b = tmp;
}
int main(){
  int x=3,y=5;
  exchange(&x, &y);
  cout<<x<<" "<<y<<"\n";
  double d=3.5, e=6.2;
  exchange(&d, &e);
  cout<<d<<" "<<e<<"\n";
}

//运行结果为:

5 3
6.2 3.5
#include<iostream>
using namespace std;
template<class T>
void exchange(T& a, T& b) {
	T tmp = a;
	a = b;
	b = tmp;
}
template<class T>void exchange(T& a, T& b);
int main() {
	int x = 3, y = 5;
	exchange(x, y);
	cout << x << " " << y << "\n";
	double d = 3.5, e = 6.2;
	exchange(d, e);
	cout << d << " " << e << "\n";
}
2.编写一个程序,使用类模板对数组元素进行排序,倒置、查找和求和。

要求:
1)具有对数组元素进行排序,倒置、查找和求和功能;
2)产生类型实参分别为int型和double型的两个模板类,
3)分别对整型数组与双精度数组完成所要求的操作。

#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
template<class T>
class Array {
public:
	void set(int n);
	void sortt(int n);
	void reversee(int n);
	void find(int n, int x);
	T sum(int n);
	void display(int n);
private:
	T a[100];
};
template<class T>
void Array<T>::set(int n) {
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
}//输入
template<class T>
void Array<T>::sortt(int n) {
	sort(a, a + n);
}//排序
template<class T>
void Array<T>::reversee(int n) {
	reverse(a, a + n);
}//倒置
template<class T>
void Array<T>::find(int n, int x) {
	int i = 0;
	for (i = 0; i < n; i++) {
		if (a[i] == x) {
			cout << "找到" << endl;
			break;
		}
	}
	if (i == n) {
		cout << "未找到" << endl;
	}
}//查找
template<class T>
T Array<T>::sum(int n) {
	T sum = 0;
	for (int i = 0; i < n; i++) {
		sum = sum + a[i];
	}
	return sum;
}//求和
template<class T>
void Array<T>::display(int n) {
	for (int i = 0; i < n; i++) {
		cout << setw(4) << a[i];
	}
	cout << endl;
}//输出
int main() {
	int n = 0;
	cout << "输入元素总数:";
	cin >> n;
	Array<int>array1;
	cout << "整型数组:" << endl;
	cout << "请输入数组元素:";
	array1.set(n);
	cout << "元素排序:" << endl;
	array1.sortt(n);
	array1.display(n);
	cout << "元素倒置:" << endl;
	array1.reversee(n);
	array1.display(n);
	int x = 0;
	cout << "请输入查找的元素:";
	cin >> x;
	cout << "元素查找:";
	array1.find(n, x);
	cout << "元素求和:";
	cout << array1.sum(n) << endl;
	Array<double>array2;
	cout << "双精度型数组:" << endl;
	cout << "请输入数组元素:";
	array2.set(n);
	cout << "元素排序:" << endl;
	array2.sortt(n);
	array2.display(n);
	cout << "元素倒置:" << endl;
	array2.reversee(n);
	array2.display(n);
	double y;
	cout << "请输入查找的元素:";
	cin >> y;
	cout << "元素查找:";
	array2.find(n, y);
	cout << "元素求和:";
	cout << array2.sum(n) << endl;
	return 0;
}
3.编写一个print函数模板,它能接收一个数组的引用,能处理任意大小、任意元素类型的数组。
#include<iostream>
using namespace std;
template<class T>
class print {
public:
	void set(int n);
	void display(int n);
private:
	T a[100];
};
template<class T>
void print<T>::set(int n) {
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	cout << endl;
}//输入
template<class T>
void print<T>::display(int n) {
	for (int i = 0; i < n; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
}//输出
int main() {
	print<int>i;
	int n1;
	cout << "输入int型数组元素个数:";
	cin >> n1;
	cout << "输入元素:";
	i.set(n1);
	print<string>s;
	int n2;
	cout << "输入string型数组元素个数:";
	cin >> n2;
	cout << "输入元素:";
	s.set(n2);
	print<char>c;
	int n3;
	cout << "输入char型数组元素个数:";
	cin >> n3;
	cout << "输入元素:";
	c.set(n3);
	cout << "输出结果:" << endl;
	i.display(n1);
	s.display(n2);
	c.display(n3);
	return 0;
}
4.编程求100以内的所有素数。 将所得数据存入文本文件和二进制文件。对写入文本文件的素数,要求存放格式是每行10个素数,每个数占6个字符,左对齐;可用任一文本编辑器将它打开阅读。二进制文件整型数的长度用sizeof ()来获得,要求可以正序读出,也可以逆序读出(利用文件定位指针移动实现),读出数据按文本文件中的格式输出显示。
#include<iostream>
#include<fstream>
#include<iomanip>
#include<algorithm>
using namespace std;
const int n = 100;
int main() {
	ofstream ofile;
	ifstream ifile;
	int a[n];
	int i;
	int j;
	int count = 0;
	for (i = 0; i < n; i++) {
		a[i] = i + 1;
	}
	a[0] = 0;
	for (i = 0; i < n; i++) {
		if (a[i] == 0) {
			continue;
		}
		for (j = i + 1; j < n; j++) {
			if (a[j] % a[i] == 0) {
				a[j] = 0;
			}
		}
	}
	ofile.open("prime.txt");
	ofile.setf(ios::left);//左对齐
	ofile << "1-" << n << "之间的素数:" << endl;
	for (i = 0; i < n; i++) {
		if (a[i] != 0) {
			ofile << setw(6) << a[i];
			count = count + 1;
			if (count % 10 == 0) {
				ofile << endl;
			}
		}
	}
	ofile.close();//读文本文件
	char c;
	char b[100];
	cout << "是否将文本文件输出:";
	cin >> c;
	if (c == 'y' || c == 'Y') {
		ifile.open("prime.txt");
	}
	i = 0;
	while (ifile.get(b[i])) {
		if (b[i] == '\n')break;
		i = i + 1;
	}
	b[i] = '\n';
	cout.setf(ios::left);
	count = 0;
	while (1) {
		ifile >> i;
		if (ifile.eof() != 0) {
			break;
		}
		cout << setw(6) << i;
		count = count + 1;
		if (count % 10 == 0) {
			cout << endl;
		}
	}
	ifile.close();
	cout << endl;
	ofile.open("prime.dat", ios::out | ios::binary);
	for (i = 0; i < n; i++) {
		if (a[i] != 0) {
			ofile.write((char*)&a[i], sizeof(int));
		}
	}
	ifile.close();
	cout << "是否将二进制文件输出:"; cin >> c;
	if (c == 'y' || c == 'Y') {
		ifile.clear(0);//清流数据
		ifile.open("prime.dat", ios::in | ios::binary);
		if (!ifile) {
			cout << "文件打开失败!" << endl;
			abort();//退出
		}
		count = 0;
		while (!ifile.eof()) {
			for (i = 0; i < n; i++) {
				ifile.read((char*)&a[i], sizeof(a[i]));
				if (a[i] != 0) {
					cout << setw(6) << a[i];
					count = count + 1;
					if (count % 10 == 0) {
						cout << endl;
					}
				}
			}
		}
	}
	ifile.close();
	cout << endl;
	cout << "是否将二进制文件逆序输出:"; cin >> c;
	if (c == 'y' || c == 'Y') {
		ifile.clear(0);//清流数据
		ifile.open("prime.dat", ios::in | ios::binary);
		count = 0;
		while (!ifile.eof()) {
			for (i = 0; i < n; i++) {
				ifile.read((char*)&a[i], sizeof(a[i]));
				count = count + 1;
			}
		}
		ifile.clear(0);
		int f = 0;
		for (j = 0; j < count; j++)
			ifile.read((char*)&a[j], sizeof(a[j]));
		reverse(a, a + count);
		for (j = 0; j < count; j++) {
			if (a[j] != 0) {
				cout << setw(6) << a[j];
				f = f + 1;
				if (f % 10 == 0) {
					cout << endl;
				}
			}
		}
	}
	ifile.close();
	cout << endl;
	system("pause");
	return 0;
}
实验总结
  1. 注意二进制文件读写的格式
  2. 注意逆序输出的先后
  3. 注意模板类的格式与使用
  4. 注意变量的初始化和循环
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻闻闻闻笛声

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值