CArray 2&CHugeInt

来源:互联网 发布:windows程序设计书籍 编辑:程序博客网 时间:2024/06/04 23:19
好久没来博客了,我决定把这里改造成一个程序媛的博客
HOMEWORK_03_03
<1>二维指针是什么鬼 int** ptr;
HOMEWORK_03_04
<1>静态数组是否需要重载operator= & copy constructor?——NO
the memory space is constructed  separately
<2>when the & is needed?——when you wanted the variable you call to change.
/*Write a class of 2D array
Array2,
And the output of the following code should be :
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> 
using namespace std;
const int MAX = 20;
class Array2{
int row;
int col;
int ** ptr;//二维指针
public:
Array2(int r_ = 0, int c_ = 0,int ** ptr_=NULL) :row(r_), col(c_){
ptr = new int *[r_];
for (int i = 0; i < r_; i++)
ptr[i] = new int[c_];
}
Array2(Array2 &a){
if (a.ptr){
ptr = new int* [a.col];
row = a.row;
col = a.col;
for (int i = 0; i < a.row; i++){
ptr[i] = new int[a.col];
memcpy(ptr[i], a.ptr[i], sizeof(int)*a.col);
}
}
else{
ptr = NULL;
row = 0;
col = 0;
}
}
~Array2(){
row=col=0;
delete[] ptr;
}
Array2& operator =(const Array2& a){
row = a.row;
col = a.col;

if (ptr == a.ptr){
for (int i = 0; i < a.row*a.col; i++)
ptr[i] = a.ptr[i];
return *this;
}
if (ptr)
delete[] ptr;
if (a.ptr){
ptr = new int*[a.col];
for (int i = 0; i < a.row; i++){
ptr[i] = new int[a.col];
memcpy(ptr[i], a.ptr[i], sizeof(int)*a.col);
}
}
else
ptr = NULL;

}
int & operator ()(int subscript_i_, int subscript_j_){
return this->operator[](subscript_i_)[subscript_j_];
}
int *operator [](int subscript){
return ptr[subscript];
}


};
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;
}

/*Write a class HugeInt, the output should be :
1)100000089000000 
2)100000089000000
3)10000
4)10000
5)10001
6)10006
7)100000089010006*/
#include <iostream>
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
const int MAX = 110;
using namespace std;
class CHugeInt{
int length;
int num[MAX];
public:
CHugeInt(char *string);
CHugeInt(int n_);
friend CHugeInt & operator +(CHugeInt & Cn1_,CHugeInt & Cn2_);
friend CHugeInt & operator +(CHugeInt & Cnum_,int num_);
friend CHugeInt & operator +(int num_, CHugeInt & Cnum_);
friend ostream & operator<<(ostream & o, const CHugeInt & Cnum_);
CHugeInt & operator ++(){
*this = *this + 1;
return *this;
}
CHugeInt  operator ++(int){
CHugeInt tmp(*this);
*this = *this + 1;
return tmp;
}
CHugeInt & operator +=(const int num_){
return *this + num_;
}
};
CHugeInt::CHugeInt(char *string){
memset(num, 0, sizeof(int)*MAX);
length = strlen(string);
for (int i = 0; i < length; i++){
char cTmp = string[length - 1 - i];
num[i] = atoi(&cTmp);
}
}
CHugeInt::CHugeInt(int n_){
memset(num, 0, sizeof(int)*MAX);
int i = 0;
for (i = 0; n_; i++){
num[i] = n_ % 10;
n_ = n_ / 10;
}
length = i;
}


CHugeInt & operator +(CHugeInt & Cn1_, CHugeInt & Cn2_){
int LengthMax = 0;
if (Cn1_.length < Cn2_.length)
LengthMax = Cn2_.length;
else
LengthMax = Cn1_.length;
for (int i = 0; i < LengthMax; i++){
int nTmp = Cn1_.num[i] + Cn2_.num[i];
Cn1_.num[i] = nTmp% 10;
Cn1_.num[i + 1] += (nTmp / 10);
}
if (Cn1_.num[LengthMax])
LengthMax++;
Cn1_.length = LengthMax;
return Cn1_;
}
CHugeInt & operator +(CHugeInt & Cnum_,int num_){
CHugeInt tmp = num_;
return Cnum_ + tmp;
}
CHugeInt & operator +(int num_, CHugeInt & Cnum_){
CHugeInt tmp = num_;
return Cnum_ + tmp;
}

ostream & operator<<(ostream & o, const CHugeInt & Cnum_){
for (int i = Cnum_.length - 1; i >= 0; i--)
o << Cnum_.num[i];
return o;
}

int  main()
{
CHugeInt a("1234545436342424354354365289899834234235");
CHugeInt d(9999);
CHugeInt temp = CHugeInt("100000088888888") + 111112;
CHugeInt temp2 = 111112 + CHugeInt("100000088888888");
cout << "1)" << temp << endl;
cout << "2)" << temp2 << endl;
cout << "3)" << ++d << endl;
cout << "4)" << d++ << endl;
cout << "5)" << d << endl;
d += 5;
cout << "6)" << d << endl;
cout << "7)" << d + temp;


return 0;
}

0 0
原创粉丝点击