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