一次C++作業(模板類的構造& C++的I/O流類庫)1

1.理解下面的動態數組類模板,它由一系列位置連續、任意數量相同類型的元素組成,其元素個數可在程序運行時改變,並完成該類中沒有完成的成員函數(不允許改變已有代碼),並設計主函數,實現對該模板類的功能測試。

#include<iostream>
#include<stdlib.h>
using namespace std;
//容錯處理
enum ErrorType {
	invalidArraySize, memoryAllocatetionError, indexOutOfRang
};
const char* errorMsg[] = { "invalid Array Size",",memory Allocatetion Error","indexOutOfRang" };
template<class T>
class Array {
private:
	T* alist;
	int size;
	void Error(ErrorType error) const //輸出錯誤信息
	{
		cout << errorMsg[error] << endl;
	}
public:
	Array(int sz = 50);//構造函數
	Array(const Array<T>& X);//拷貝構造函數
	~Array(void);//析構函數
	Array<T>& operator=(const Array<T>& rhs);//重載賦值運算符
	T& operator[](int i);//重載下標運算符
	int getsize(void) const;//獲取數組大小
	void resize(int sz);//重新設置數組大小
};
template<class T>
Array<T>::Array(int sz) {
	if (sz <= 0) {
		Error(invalidArraySize);//返回值void
	}
	size = sz;
	alist = new T[size];//錯誤會null或0
	if (alist == 0) {
		Error(memoryAllocatetionError);//返回值void
	}
}
template<class T>
Array<T>::Array(const Array<T>& X) {
	int n = X.size;
	size = n;
	alist = new T[n];
	if (alist == 0) {
		Error(memoryAllocatetionError);
	}
	T* srcptr = X.alist;
	T* destptr = alist;
	while (n--) {
		*destptr++ = *srcptr++;
	}
}
template<class T>
Array<T>::~Array() {
	delete[]alist;
}
template<class T>
Array<T>& Array<T>:: operator=(const Array<T>& rhs) {
	int n = rhs.size;
	if (size != n) {//比較左右長度
		delete[] alist;
		alist = new T[n];
		if (alist == 0) {
			Error(memoryAllocatetionError);
		}		
	}
	size = n;
	T* destptr= alist;
	T* srcptr = rhs.alist;
	while (n--) {
		*destptr++ = *srcptr++;
	}
	return *this;
}
template<class T>
T& Array<T>::operator[](int n) {
	if (n<0 || n>size - 1) {
		Error(indexOutOfRang);
	}
	return alist[n];
}

template<class T>
int Array<T>::getsize(void)const {
	return size;
}
template<class T>
void Array<T>::resize(int sz) {
	size = sz;
	delete alist;
	alist = new T[size];//new 分空間
}
int main() {
	int i;
	int n1, n2;
	cout << "請分別輸入數組a1,a2的長度";
	cin >> n1 >> n2;
	Array<int> a1(n1), a2(n2);
	cout << "請輸入a1數組的數字成員:";
	for (i = 0; i < a1.getsize(); i++) {
		cin >> a1[i];
	}
	cout << "數組a1爲:";
	for (i = 0; i < a1.getsize(); i++) {
		cout << a1[i] << "  ";
	}
	cout << endl;
	cout << "將數組a1的值複製到a2中" << endl;;
	a2 = a1;
	cout << "數組a2爲:";
	for (i = 0; i < a1.getsize(); i++) {
		cout << a2[i] << "  ";
	}
	cout << endl;
	cout << "現在更改數組a1的長度,請輸入修改後的長度:";
	int m;
	cin >> m;
	a1.resize(m);
	cout << "請輸入" << m << "個數" << endl;
	for (i = 0; i < a1.getsize(); i++) {
		cin >> a1[i];
	}
	cout << "現在數組a1爲:";
	for (i = 0; i < a1.getsize(); i++) {
		cout << a1[i] << "  ";
	}
}


2.閱讀下列程序,然後上機運行驗證輸出結果,並回答下列問題。

#include<iostream>
using namespace std;

void showflags(long f)
{
	long i = 0x8000;
	for (; i; i = i >> 1)
	{
		if (i & f)
			cout << "1";
		else
			cout << "0";
	}
	cout << endl;
}
void main()
{
	showflags(cout.flags());
	cout << "x_width=" << cout.width() << endl;
	cout << "x_fill=" << cout.fill() << endl;
	cout << "x_precision=" << cout.precision() << endl;
	cout << 123 << " " << 123.45678 << "\n";
	cout << "-------------" << endl;
	cout << "* * * x_width=10,x_fill= ,x_precision=4 * * *" << endl<<endl<<endl<<endl;
	cout.width(10);
	cout.precision(4);
	cout << 123 << " " << 123.45678 << " " << 234.567 << endl;//1

	cout << "x_width=" << cout.width() << endl;
	cout << "x_fill=" << cout.fill() << endl;
	cout << "x_precision=" << cout.precision() << endl;
	cout << "-------------" << endl;
	cout << "* * * x_width=10,x_fill=&,x_precision=4 * * *" << endl;
	cout.fill('&');
	cout.width(10);
	cout << 123 << " " << 123.45678 << endl;
	cout.setf(ios::left);
	cout.width(10);
	cout << 123 << " " << 123.45678 << endl;
	cout << "x_width=" << cout.width() << endl;
	cout << "x_fill=" << cout.fill() << endl;
	cout << "x_precision=" << cout.precision() << endl<<endl<<endl;
	showflags(cout.flags());//2

	cout.setf(ios::right | ios::unitbuf);
	cout << endl << endl << endl;
	showflags(cout.flags());//3

}
   123     123.5     234.6
   0000001001000001
   0000001011000011
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章