北大MOOC - 程序设计与算法(三)第四周测验

1:MyString

补足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; }
// 在此处补充你的代码
};
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;
		
	}
}

输入

多组数据,每组一行,是两个不带空格的字符串

输出

对每组数据,先输出一行,打印输入中的第一个字符串三次
然后再输出一行,打印输入中的第二个字符串三次

样例输入

abc def
123 456

样例输出

abc,abc,abc
def,def,def
123,123,123
456,456,456

代码理解

  1. 重载
MyString & operator=( const char * s )
	{
		if(p) delete [] p;
		p = new char[strlen(s)+1];
		strcpy(p,s);
		return *this;

这里每个成员函数里面以来就是delete掉p,然后将参数的字符串复制给重新分配空间的p,然后返回当前对象,此时的p也是当前对象的p
2. 重载2

if( this == & S ) return *this;

这里是想防止s=s的情况出现
3. <<重载的基本模板

friend ostream & operator<<( ostream & os, const MyString & S )
	{
		os << S.p;
		return os;
	}

这里因为cout本就是ostream类里面定义的一个对象,ostream & os 能引用cout;
加友元的原因:因为这里是对iostream中的<<进行重载,需要加上友元,并非自己的成员函数

答案代码

#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);
	}
	MyString & Copy( const char * s )
	{
		if(p) delete [] p;
		p = new char[strlen(s)+1];
		strcpy(p,s);
		return *this;
	}
	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 )
	{
		os << 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 ;}
// 在此处补充你的代码
}; 
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;
}

输入

多组数据,每组一行,整数n

输出

对每组数据,输出一行,包括两个整数, n-5和n - 8

样例输入

20
30
样例输出

15,12
25,22

代码理解

  1. 类型转换运算符的重载,固定模板
operator int()
    {
        return nVal;
    }
  1. 减号的重载我的程序
MyInt  operator - (int r)
    {
        return nVal - r;
    }

答案代码

#include <iostream> 
using namespace std;
class MyInt 
{ 
	int nVal; 
	public: 
	MyInt( int n) { nVal = n ;}
    MyInt&  operator - (int r)
    {
        nVal -= r;
        return * this;
    }
    operator 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() { };
// 在此处补充你的代码
}; 
int main() 
{ 
 	Point p;
 	while(cin >> p) {
 		cout << p << endl;
	 }
	return 0;
}

输入

多组数据,每组两个整数

输出

对每组数据,输出一行,就是输入的两个整数

样例输入

2 3
4 5

样例输出

2,3
4,5

代码理解

  1. 在>>重载函数的书写时,一直纠结于我只有一个参数对象p,却不知道重载函数里面可以写成两个如is>>a>>b,这里要想清楚。
  2. << 和 >>运算符要重载,我们只能重载成全局函数,不可能再重载为ostream和istream类的成员函数,因为这两个类都已经写好了,所以我们必须把他们重载为一个全局的函数,因为里面用到了Piont里面的私有成员所以需要成为point的友元

答案代码

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

4:第四题

描述

写一个二维数组类 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;

class Array2 {
// 在此处补充你的代码
};

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,

代码理解

  1. int **ptr;定义的指向指针的指针
    ptr = new int * [x] 动态分配出一片大小为 sizeof(T)*x字节的内存空间,并且将该内存空间的起始地址赋值给ptr。
    ptr[i] = new int [y];动态分配出一片大小为 sizeof(T)*x字节的内存空间,并且将该内存空间的起始地址赋值给ptr [i],这里的ptr[i]相对于 * p
    在这里插入图片描述
    在这里插入图片描述

答案代码

#include <iostream>
#include <cstring>
using namespace std;
 
class Array2 {
int **ptr;
    int sizex,sizey;
public:
    Array2()
    {
        ptr = NULL;
        sizex = 0;
        sizey = 0;
    }
    Array2(int x, int y)
    {
        sizex = x;
        sizey = y;
        ptr = new int*[x];
        for(int i=0;i<x;i++)
        {
            ptr[i] = new int [y];
        }
    }
    Array2(Array2 & a)
    {
        if(!a.ptr)
        {
            ptr=NULL;
            return;
        }
        ptr = new int*[a.sizex];
        for(int i=0;i<a.sizex;i++)
            ptr[i] = new int[a.sizey];
    }
    int *& operator [](const int & i)
    {
        return ptr[i];
    }
    int & operator()(const int & m,const int & n)
    {
        return ptr[m][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 {
// 在此处补充你的代码
};
int  main() 
{ 
	char s[210];
	int n;

	while (cin >> s >> n) {
		CHugeInt a(s);
		CHugeInt b(n);

		cout << a + b << endl;
		cout << n + a << endl;
		cout << a + n << endl;
		b += n;
		cout  << ++ b << endl;
		cout << b++ << endl;
		cout << b << endl;
	}
	return 0;
}

输入

多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示

输出

对每组数据,输出6行,内容分别是:

样例输入

99999999999999999999999999888888888888888812345678901234567789 12
6 6

样例输出

99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14

代码理解

  1. 这里的加号的重载,有些许灵活,根据题目意思只能在类中重载为成员函数,这样的话+重载时的参数个数为2-1个,都是涉及到类加类一个还能解决,但是常数加类和类加常数就得需要两个参数,这时又不能重载为类外的普通成员函数,所以此时还可以将运算符重载为友元
  2. 这里友元函数重载时能用到类成员函数的+的重载?

答案代码

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
using namespace std;
const int MAX = 110; 
class CHugeInt {
private:
    char maxNum[210];
    int len;
public:
    CHugeInt(char * s){
        strcpy(maxNum,s);
        int i=0,j=strlen(s)-1;
        while(i<j)
        {
            swap(maxNum[i],maxNum[j]);
            i++;
            j--;
        }
        //cout<<"init:"<<maxNum<<endl;
        len=strlen(s);
        //cout<<"Init success"<<endl;
    }
    CHugeInt(){
        len=0;
    } 
    CHugeInt(int n){
        int i=0;
        if(n==0)
        {
            maxNum[i++]='0';
        }else{
            while(n)
            {
                maxNum[i++]=n%10+'0';
                n=n/10;
            }    
        }
        maxNum[i]='\0';
        len=i;
        //cout<<maxNum<<endl;
    }
    CHugeInt  operator+(CHugeInt & a)
    {
            //cout<<"hrer"<<endl;
            int i=0,j=0;
            int t,sum=0;
            CHugeInt temps;
            strcpy(temps.maxNum,maxNum);
            temps.len=len;
            //cout<<"before:"<<temps.maxNum<<endl;
            //maxNum=new char[strlen(a.maxNum)+1];
            //cout<<a.len<<","<<len<<endl;
            int flag=0;
            while(j<a.len&&i<temps.len)
            {
                t=a.maxNum[j]-'0';
                int te=temps.maxNum[i]-'0';
                sum=t+te;
                //cout<<t<<"+"<<te<<":"<<sum<<endl;
                if(sum>=10)
                {
                    temps.maxNum[i]=sum%10+'0';
                    //cout<<temps.maxNum[i]<<endl;
                    temps.maxNum[i+1]=sum/10+temps.maxNum[i+1];
                    if(i+1>=temps.len)
                    {
                        temps.maxNum[i+1]+='0'; 
                    }
                    flag=1;
                }else{
                    //cout<<"sum:"<<sum<<endl;
                    flag=0;
                    temps.maxNum[i]=sum+'0';
                }
                //cout<<temps.maxNum[i]<<endl;
                i++,j++;
                sum=0;
            }
            while(j<a.len)
            {
                if(flag==1)
                {
                    temps.maxNum[i+1]=a.maxNum[j];
                    i++,j++;    
                }else{
                    temps.maxNum[i]=a.maxNum[j];
                    i++,j++;
                }
            }
            if(i>=len)
            {
                if(flag==1){
                    temps.maxNum[i+1]='\0';
                    temps.len=i+1;
                }
                else{
                    temps.maxNum[i]='\0';
                    temps.len=i;
                }        
            }
        return temps;
    }
    /*operator char *()
    {
        return maxNum;
    }*/
    CHugeInt & operator +=(int n)
    {
        CHugeInt temps(n);
        *this=this->operator+(temps);
        //cout<<this->maxNum<<endl;
        return *this;
    }
    friend ostream & operator<<(ostream & os,const CHugeInt & s)
    {
            int i=0,j=s.len-1;
            //cout<<"len:"<<s.len<<endl;
            //cout<<"输出:"<<s.maxNum<<endl;
            for(;j>=i;j--)
                os<<s.maxNum[j];
            return os;
    }
    friend CHugeInt  operator+(int n,CHugeInt  s)
    {
        CHugeInt temps(n);
        s=s+temps;
        return s;
    }
    friend CHugeInt  operator+(CHugeInt  s,int n)
    {
        CHugeInt temps(n);
        s=s+temps;
        return s;
    }
    CHugeInt &  operator++()
    {
        (*this)+=1;
        //cout<<"前置自增后:"<<this->maxNum<<endl;
        return *(this);
    }
    CHugeInt   operator++(int n)
    {
        CHugeInt temps;
        strcpy(temps.maxNum,maxNum);
        temps.len=len;
        this->operator +=(1);
        //cout<<temps.maxNum<<endl;
        //cout<<"len:"<<temps.len<<endl;
        return temps;
    }
 
};
int  main() 
{ 
	char s[210];
	int n;
 
	while (cin >> s >> n) {
		CHugeInt a(s);
		CHugeInt b(n);
 
		cout << a + b << endl;
		cout << n + a << endl;
		cout << a + n << endl;
		b += n;
		cout  << ++ b << endl;
		cout << b++ << endl;
		cout << b << endl;
	}
	return 0;
}
发布了20 篇原创文章 · 获赞 1 · 访问量 148

猜你喜欢

转载自blog.csdn.net/weixin_42503072/article/details/104907395
今日推荐