C++程序设计 第7周 文件操作与模板 编程题1#

这题看上去很简单,结果还是费了些功夫,问题还是出在基础不牢:

1. 类的前后顺序很重要,成员函数如果含有其他类的对象,一定要放到其他类的后面。

2. 数组类的对象是需要一个一个初始化的。

3. int *p=new int[10]和 int *p[10]有本质区别:第一个p是整型数组的地址,p[i]是变量;第二个p是指针数组的地址,p[i]是指针

---------------------------------------以下是题目-----------------------------------------------

描述

实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组,使得下面程序输出结果是:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,

注意,只能写一个类模版,不能写多个。


#include<iostream>
using namespace std;

template <class T>
class CArray3D
{
	class A//二维数组
	{
		class B//一维数组
		{
			T *ppp;
		public:
			B() { ppp = NULL; }
			~B() { if (ppp) delete []ppp; }
			void creat(int k) { ppp = new T[k]; }
			T& operator[](int k) { return ppp[k]; }
			
		};
		B *pp;
	public:
		A() { pp = NULL; }
		~A() { if (pp) delete[]pp; }
		void creat(int j, int k) {
			pp = new B[j];
			for (int l = 0; l < j; l++)
				pp[l].creat(k);
		}
		B & operator[](int j) { return pp[j]; }
	};
	A *p;
public:
	CArray3D(int i, int j, int k)
	{ 
		p = new A[i]; 
		for (int l = 0; l < i; l++)
			p[l].creat(j, k);
	}
	~CArray3D()
	{
		if (p) delete[]p;
	}
	A & operator[](int i) { return p[i]; }
};

int main()
{
	CArray3D<int> a(3, 4, 5);
	int No = 0;
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 4; ++j)
			for (int k = 0; k < 5; ++k)
				a[i][j][k] = No++;
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 4; ++j)
			for (int k = 0; k < 5; ++k)
				cout << a[i][j][k] << ",";
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第8章主要介绍了C++中的文件操作,包括文件的打开、读写、关闭等基本操作。下面是一些重点内容: 1. 文件的打开 可以使用fstream库中的ifstream、ofstream、fstream三个类分别表示输入文件流、输出文件流、输入输出文件流。它们的构造函数可以传入文件名和打开模式,例如: ```c++ #include <fstream> using namespace std; int main() { // 打开一个文本文件用于输入 ifstream input("input.txt"); if (!input) { cout << "打开文件失败!" << endl; return 1; } // 打开一个文本文件用于输出 ofstream output("output.txt"); if (!output) { cout << "打开文件失败!" << endl; return 1; } // 打开一个文本文件用于输入输出 fstream io("data.txt", ios::in | ios::out); if (!io) { cout << "打开文件失败!" << endl; return 1; } // ... } ``` 2. 文件的读写 使用文件流对象的<<、>>、getline等操作符进行读写。例如: ```c++ #include <fstream> #include <iostream> using namespace std; int main() { // 打开一个文本文件用于输入 ifstream input("input.txt"); if (!input) { cout << "打开文件失败!" << endl; return 1; } // 打开一个文本文件用于输出 ofstream output("output.txt"); if (!output) { cout << "打开文件失败!" << endl; return 1; } int x; input >> x; // 从文件中读取一个整数 output << "x = " << x << endl; // 将x输出到文件中 string line; getline(input, line); // 从文件中读取一行字符串 output << line << endl; // 将字符串输出到文件中 // ... } ``` 3. 文件的关闭 使用文件流对象的close()方法来关闭文件。例如: ```c++ #include <fstream> using namespace std; int main() { // 打开一个文本文件用于输入 ifstream input("input.txt"); if (!input) { cout << "打开文件失败!" << endl; return 1; } // 关闭文件 input.close(); } ``` 以上是新标准C++程序设计第8章文件操作的一些重点内容,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值