北京大学C++程序设计coursera课程第四周编程填空题3

题目

填空题-3

1 point possible (graded)
总时间限制:
1000ms
内存限制:
65536kB
描述

写一个二维数组类 Array2,使得下面程序的输出结果是:

0,1,2,3,

4,5,6,7,

8,9,10,11,

next

0,1,2,3,

4,5,6,7,

8,9,10,11,

程序:

#include <iostream>
#include <cstring>
using namespace std;
// 在此处补充你的代码
int main() {
    Array2 a(3,4);
    int i,j;
    for(  i = 0;i < 3; ++i )
        for(  j = 0; j < 4; j ++ )
            a[i][j] = i * 4 + j;
    for(  i = 0;i < 3; ++i ) {
        for(  j = 0; j < 4; j ++ ) {
            cout << a(i,j) << ",";
        }
        cout << endl;
    }
    cout << "next" << endl;
    Array2 b;     b = a;
    for(  i = 0;i < 3; ++i ) {
        for(  j = 0; j < 4; j ++ ) {
            cout << b[i][j] << ",";
        }
        cout << endl;
    }
    return 0;
}
输入
输出
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
样例输入
样例输出
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,

答题:
 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 class Array2
 5 {
 6   int *ptr;
 7   int row;//行数
 8   int col;//列数
 9 public:
10   Array2(int a,int b):row(a),col(b)
11   {
12     ptr=new int[row];
13     for(int i = 0 ; i < row; i++)
14     {
15       ptr[i] = reinterpret_cast<int>(new int[col]); //把int* 转化成int
16     }
17   }
18   Array2(){ptr=NULL;row=0;col=0;}
19   ~Array2()
20   {
21     for(int i = 0 ; i < row; i++)
22     {
23       delete []reinterpret_cast<int*>(ptr[i]);
24     }
25     delete []ptr;
26   }
27   int* operator[](int a)
28   {
29     return reinterpret_cast<int*>(ptr[a]);
30   }
31   Array2& operator =(const Array2 &k)
32   {
33     if(ptr==k.ptr) return *this;
34     if(ptr!=NULL)
35     {
36       for(int i = 0 ; i < row; i++)
37       {
38         delete []reinterpret_cast<int*>(ptr[i]);
39       }
40     delete []ptr;
41     }
42     if(k.ptr==NULL)
43     {
44       ptr=NULL;
45       row=0;
46       col=0;
47       return *this;
48     }
49 
50     ptr=new int[k.row];
51     row=k.row;
52     col=k.col;
53 
54     for(int i = 0 ; i < row; i++)
55     {
56       ptr[i] = reinterpret_cast<int>(new int[col]);
57     }
58 
59     for(int i = 0 ; i < row; i++)
60     for(int j = 0 ; j < col; j++)
61       {(reinterpret_cast<int*>(ptr[i]))[j]=(reinterpret_cast<int*>(k.ptr[i]))[j];}
62     cout<<"done"<<endl;
63     return (*this);
64   }
65   int& operator()(int a,int b)
66   {
67     return (*this)[a][b];
68   }
69 };
70  
71 int main()
72 {
73   Array2 a(3,4);
74   int i,j;
75   for( i = 0;i < 3; ++i )
76   for( j = 0; j < 4; j ++ )
77     a[i][j] = i * 4 + j;
78   for( i = 0;i < 3; ++i )
79   {
80     for( j = 0; j < 4; j ++ ) 
81     {
82       cout << a(i,j) << ",";
83     }
84     cout << endl;
85   }
86   cout << "next" << endl;
87   Array2 b;
88   b = a;
89   for( i = 0;i < 3; ++i )
90   {
91     for( j = 0; j < 4; j ++ )
92     {
93       cout << b[i][j] << ",";
94     }
95     cout << endl;
96   }
97   return 0;
98 }

 

核心难点是双[]运算符怎么重载出来,思想是第一个[]中放地址,然后第二个[]就可以用默认的寻址运算,这样只需要对第一个[]进行重载。
实现的过程中注意运用reinterpret_cast即c++的类型转换。





posted @ 2018-06-30 21:03  许氏Forwards  阅读(1025)  评论(0编辑  收藏  举报