第四周 运算符重载 - PKU[课程作业]程序设计与算法(三)C++面向对象程序设计

1:MyString

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class MyString {
	char * p;
public:
	MyString(const char * s) {
		if( s) {
			p = new char[strlen(s) + 1];
			strcpy(p,s);
		}
		else
			p = NULL;
	}
	~MyString() { if(p) delete [] p; }
	MyString( const MyString & S )
	{
		p = new char[strlen(S.p)+1];
		strcpy(p,S.p);
	}
	void Copy( const char * s )
	{
		if(p) delete [] p;
		p = new char[strlen(s)+1];
		strcpy(p,s);
	}
	MyString & operator=( const char * s )
	{
		if(p) delete [] p;
		p = new char[strlen(s)+1];
		strcpy(p,s);
		return *this;
	}
	MyString & operator=( const MyString & S )
	{
		if( this == & S ) return *this;
		if(p) delete [] p;
		p = new char[strlen(S.p)+1];
		strcpy(p,S.p);
		return *this;
	} 
friend ostream & operator<<( ostream & os, const MyString & S )
	{
		cout << S.p;
		return os;
	}
};
int main()
{
	char w1[200],w2[100];
	while( cin >> w1 >> w2) {
		MyString s1(w1),s2 = s1;
		MyString s3(NULL);
		s3.Copy(w1);
		cout << s1 << "," << s2 << "," << s3 << endl;

		s2 = w2;
		s3 = s2;
		s1 = s3;
		cout << s1 << "," << s2 << "," << s3 << endl;
		
	}
}

2:看上去好坑的运算符重载

#include <iostream> 
using namespace std;
class MyInt 
{ 
	int nVal; 
	public: 
	MyInt( int n) { nVal = n ;}
	MyInt & operator-( int n )
	{
		nVal -= n;
		return *this;
	}
	operator int() {	// 将MyInt类型强制转换为int类型。类型强制转换运算符被重载时不能写返回值类型,实际上其返回值类型就是该类型强制转换运算符代表的类型 
		return nVal;
	}
}; 
int Inc(int n) {
	return n + 1;
}
int main () { 
	int n;
	while(cin >>n) {
		MyInt objInt(n); 
		objInt-2-1-3; 
		cout << Inc(objInt);
		cout <<","; 
		objInt-2-1; 
		cout << Inc(objInt) << endl;
	}
	return 0;
}

3:惊呆!Point竟然能这样输入输出

#include <iostream> 
using namespace std;
class Point { 
	private: 
		int x; 
		int y; 
	public: 
		Point() { };
	friend ostream & operator<<( ostream & os, const Point & P )
	{
		os << P.x << "," << P.y;
		return os;
	}
	friend istream & operator>>( istream & is, Point & P )
	{
		is >> P.x >> P.y;
		return is;
	} 
}; 
int main() 
{ 
 	Point p;
 	while(cin >> p) {
 		cout << p << endl;
	 }
	return 0;
}

4:第四周程序填空题3

#include <iostream>
#include <cstring>
using namespace std;

class Array2 {
	private:
		int **arr;
		int row, col;
	public:
		Array2( int r = 0, int c = 0 ){
			arr = NULL;
			row = r; col = c;
			arr = new int *[row];
			for( int i = 0; i < row; i++ )
					arr[i] = new int[col];
			cout << "Constructor called" << endl;
		}
		~Array2(){
			for( int i = 0; i < row; i++ )
					delete []arr[i];
			delete arr;
			cout << "Destructor called" << endl;
		}
		Array2 & operator=( const Array2 & a ){
			cout << "operator= invoked" << endl;
			if( arr == a.arr ) return *this;	// a = a;
			if( arr != NULL )
				for( int i = 0; i < row; i++ )
						delete []arr[i];
			if( a.arr != NULL ){	// b = a;
				row = a.row; col = a.col;
				arr = new int *[row];
				for( int i = 0; i < row; i++ )
						arr[i] = new int[col];
				for( int i = 0; i < row; i++ )
					for( int j = 0; j < col; j++ )
						arr[i][j] = a.arr[i][j];
			}
			else arr = NULL;
			return *this;
		}
		int operator()( int i, int j ){	// a(i,j)
			return arr[i][j];
		}
		int *operator[]( int n ){	// a[i][j] --> (a[i])[j] = (a.operator[](i)).operator[](j), 重载的实际上是第二维的[], 第一维的[]直接调用int型一维数组的定义
			return arr[n];
		}
};

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

5:别叫,这个大整数已经很简化了!

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
using namespace std;
const int MAX = 110; 
class CHugeInt {
	private:
		int len;
		unsigned short *num;
	public:
		CHugeInt() { num = NULL; len = 0; }
		CHugeInt( const char *s )
		{
			len = strlen(s);
			num = new unsigned short[len];
			for( int j = len-1, i = 0; j >= 0; j--, i++ )
				num[i] = s[j]-'0';
		}
		CHugeInt( int n )
		{
			num = NULL; len = 0;
			unsigned short tmp[10];	// biggest unsigned int: 4294967295
			if( !n )
				tmp[len++] = 0;
			else{
				while(n)
				{
					tmp[len++] = n%10;
					n /= 10;
				}				
			}
			num = new unsigned short[len];
			for( int i = 0; i < len; i++ )
				num[i] = tmp[i];
		}
		CHugeInt( const CHugeInt & I )
		{
			num = NULL; len = 0;
			len = I.len;
			num = new unsigned short[len];
			for( int i = 0; i < len; i++ )
				num[i] = I.num[i];
		}
		~CHugeInt()
		{
			if(num) delete [] num;
		}
		CHugeInt & operator=( const CHugeInt & I )
		{
			if(num) delete [] num;
			len = I.len;
			num = new unsigned short[len];
			for( int i = 0; i < len; i++ )
				num[i] = I.num[i];
			return *this;
		}
		CHugeInt & operator+=( int n )
		{
			unsigned short tmp[10];	// biggest unsigned int: 4294967295
			int n_len = 0;
			if( !n )
				tmp[n_len++] = 0;
			else{
				while(n)
				{
					tmp[n_len++] = n%10;
					n /= 10;
				}				
			}
			CHugeInt res;
			res.len = (len>n_len) ? len : n_len;
			int min_len = (len>n_len) ? n_len : len;
			res.num = new unsigned short[res.len+1];
			unsigned short carry = 0;
			for( int i = 0; i < min_len; i++ )
			{
				res.num[i] = (num[i]+tmp[i]+carry)%10;
				carry = (num[i]+tmp[i]+carry)/10;
			}
			if( min_len == n_len )
			{
				for( int i = min_len; i < res.len; i++ )
				{
					res.num[i] = (num[i]+carry)%10;
					carry = (num[i]+carry)/10;
				}
			}
			else{	// min_len == L.len
				for( int i = min_len; i < res.len; i++ )
				{
					res.num[i] = (tmp[i]+carry)%10;
					carry = (tmp[i]+carry)/10;
				}
			}
			if( carry )
				res.num[res.len++] = carry;	
			*this = res;	// operator =
			return *this;
		}
		CHugeInt & operator++( )	// 前置 
		{
			CHugeInt res;
			res.len = len;
			res.num = new unsigned short[res.len+1];
			unsigned short carry = 0;
			res.num[0] = (num[0]+1+carry)%10;
			carry = (num[0]+1+carry)/10;
			for( int i = 1; i < res.len; i++ )
			{
				res.num[i] = (num[i]+carry)%10;
				carry = (num[i]+carry)/10;;
			}
			if( carry )
				res.num[res.len++] = carry;	
			*this = res;	// operator =
			return *this;
		}
		CHugeInt operator++(int)	// 后置 
		{
			CHugeInt temp = *this;	// copy constructor
			CHugeInt res;
			res.len = len;
			res.num = new unsigned short[res.len+1];
			unsigned short carry = 0;
			res.num[0] = (num[0]+1+carry)%10;
			carry = (num[0]+1+carry)/10;
			for( int i = 1; i < res.len; i++ )
			{
				res.num[i] = (num[i]+carry)%10;
				carry = (num[i]+carry)/10;;
			}
			if( carry )
				res.num[res.len++] = carry;	
			*this = res;	// operator =
			return temp;
		}
	friend CHugeInt operator+( const CHugeInt & L, const CHugeInt & R )	// a+b	
	{
		CHugeInt res;
		res.len = (L.len>R.len) ? L.len : R.len;
		int min_len = (L.len>R.len) ? R.len : L.len;
		res.num = new unsigned short[res.len+1];
		unsigned short carry = 0;
		for( int i = 0; i < min_len; i++ )
		{
			res.num[i] = (L.num[i]+R.num[i]+carry)%10;
			carry = (L.num[i]+R.num[i]+carry)/10;
		}
		if( min_len == R.len )
		{
			for( int i = min_len; i < res.len; i++ )
			{
				res.num[i] = (L.num[i]+carry)%10;
				carry = (L.num[i]+carry)/10;
			}
		}
		else{	// min_len == L.len
			for( int i = min_len; i < res.len; i++ )
			{
				res.num[i] = (R.num[i]+carry)%10;
				carry = (R.num[i]+carry)/10;
			}
		}
		if( carry )
			res.num[res.len++] = carry;
		return res;
	}
	friend CHugeInt operator+( int n, const CHugeInt & R )	// n+a
	{
		unsigned short tmp[10];	// biggest unsigned int: 4294967295
		int n_len = 0; 
		while(n)
		{
			tmp[n_len++] = n%10;
			n /= 10;
		}
		CHugeInt res;
		res.len = (R.len>n_len) ? R.len : n_len;
		int min_len = (R.len>n_len) ? n_len : R.len;
		res.num = new unsigned short[res.len+1];
		unsigned short carry = 0;
		for( int i = 0; i < min_len; i++ )
		{
			res.num[i] = (tmp[i]+R.num[i]+carry)%10;
			carry = (tmp[i]+R.num[i]+carry)/10;
		}
		if( min_len == n_len )
		{
			for( int i = min_len; i < res.len; i++ )
			{
				res.num[i] = (R.num[i]+carry)%10;
				carry = (R.num[i]+carry)/10;
			}
		}
		else{	// min_len == R.len
			for( int i = min_len; i < res.len; i++ )
			{
				res.num[i] = (tmp[i]+carry)%10;
				carry = (tmp[i]+carry)/10;
			}
		}
		if( carry )
			res.num[res.len++] = carry;
		return res;		
	}
	friend CHugeInt operator+( const CHugeInt & L, int n )	// a+n
	{
		unsigned short tmp[10];	// biggest unsigned int: 4294967295
		int n_len = 0; 
		while(n)
		{
			tmp[n_len++] = n%10;
			n /= 10;
		}
		CHugeInt res;
		res.len = (L.len>n_len) ? L.len : n_len;
		int min_len = (L.len>n_len) ? n_len : L.len;
		res.num = new unsigned short[res.len+1];
		unsigned short carry = 0;
		for( int i = 0; i < min_len; i++ )
		{
			res.num[i] = (L.num[i]+tmp[i]+carry)%10;
			carry = (L.num[i]+tmp[i]+carry)/10;
		}
		if( min_len == n_len )
		{
			for( int i = min_len; i < res.len; i++ )
			{
				res.num[i] = (L.num[i]+carry)%10;
				carry = (L.num[i]+carry)/10;
			}
		}
		else{	// min_len == L.len
			for( int i = min_len; i < res.len; i++ )
			{
				res.num[i] = (tmp[i]+carry)%10;
				carry = (tmp[i]+carry)/10;
			}
		}
		if( carry )
			res.num[res.len++] = carry;
		return res;
	}	
	friend ostream & operator<<( ostream & os, const CHugeInt I )	// 不是CHugeInt & I,为了cout << a+b; 
	{
		for( int i = I.len-1; i >= 0; i-- )
			os << I.num[i];
		return os;
	}
};
int  main() 
{ 
	char s[210];
	int n;

	while (cin >> s >> n) {
		CHugeInt a(s);
		CHugeInt b(n);
		cout << a + b << endl;	// 不改变a,b 
		cout << n + a << endl;	// 不改变a
		cout << a + n << endl;	// 不改变a
		b += n;	// 改变b 
		cout  << ++ b << endl;	// 改变b 
		cout << b++ << endl;	// 不改变b 
		cout << b << endl;
	}
	return 0;
}

不要忘记CHugeInt0的情况



猜你喜欢

转载自blog.csdn.net/momo_flamboyant/article/details/80030926