mooc程序设计与算法(三)C++面向对象程序设计 运算符重载 作业

1、MyString
要求输入:

abc def
123 456

要求输出

abc,abc,abc
def,def,def
123,123,123
456,456,456
#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& mystr){
        if(mystr.p){
            p = new char[strlen(mystr.p)+1];
            strcpy(p, mystr.p);
        }
        else p = NULL;
    }

    void Copy(char *s){
        if(s){
            p = new char[strlen(s)+1];
            strcpy(p, s);
        }
        else p=NULL;
    }
    friend ostream&operator<<(ostream & os, const MyString & mystring){
        os<<mystring.p;
        return os;
    }

    MyString & operator = (char *s){
        if(s){
            p = new char[strlen(s)+1];
            strcpy(p, s);
        }
        else p=NULL;
        return *this;
    }
    MyString & operator = (const MyString& s){
        if(s.p){
            p = new char[strlen(s.p)+1];
            strcpy(p, s.p);
        }
        else p=NULL;
        return *this;
    }
};
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、看上去好坑的运算符重载
要求输入:

20
30

要求输出

15,12
25,22
#include <iostream> 
using namespace std;
class MyInt 
{ 
    int nVal; 
    public: 
    MyInt( int n) { nVal = n ;}
friend MyInt& operator- (MyInt &m, int k){
        m.nVal -= k;
        return m;
    }
    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竟然能这样输入输出
输入要求:

2 3
4 5

要求输出

2,3
4,5
#include <iostream> 
using namespace std;
class Point { 
    private: 
        int x; 
        int y; 
    public: 
        Point() { };
friend istream& operator>>(istream &i, Point& p){
        i>>p.x;
        i>>p.y;
        return i;
    }
    friend ostream& operator<<(ostream &o, const Point& p){
        o<<p.x<<",";
        o<<p.y;
        return o;
    }

}; 
int main() 
{ 
    Point p;
    while(cin >> p) {
        cout << p << endl;
     }
    return 0;
}

4、第四周程序填空题3
要求输入:

None

要求输出

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 {
private:
     int row;
     int col;
    int **p;
public:
    Array2():row(0), col(0){p=NULL;}
    Array2(int row_, int col_):row(row_), col(col_){
        p = new int*[row];
        for (int i = 0; i < col; ++i) {
            p[i] = new int [1];
        }
    }
    int* operator[](int i){
        //重载[]
        return p[i];
    }
    int operator()(int x, int y){
        //重载()
        return p[x][y];
    }
    Array2& operator=(const Array2& a){
        //重载等号
        if (p!=NULL){
            for (int i = 0; i < row; ++i) {
                delete[]p[row];
            }
            delete []p;
        }
        p = new int *[a.row];
        for (int i = 0; i < a.row; ++i) {
            p[i] = new int [a.col];
        }
        for (int i = 0; i < a.row; ++i) {
            for (int j = 0; j < a.col; ++j) {
                p[i][j] = a.p[i][j];
            }
        }
        return *this;
    }
};

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:
    char *p;
public:
    CHugeInt(){p=NULL;}
    CHugeInt(int x);
    CHugeInt(char *x);
    void reverse(char *x);
    char* int_to_char(int n);//这里要在下面的成员函数中使用,因此必须设置为成员函数
    void print(){cout<<p<<endl;}
    char* add_2_chars(char *p1, char *p2);
    CHugeInt(CHugeInt &c);
    friend char* operator+(const CHugeInt &c1, const CHugeInt &c2);
    friend char* operator+(const int &n, const CHugeInt &c);
    friend char* operator+(const CHugeInt &c, const int &n);
    CHugeInt & operator++();//前置
    CHugeInt operator++(int );
    operator char*(){return p;};
    CHugeInt& operator += (const int n);
};
CHugeInt::CHugeInt(char *x) {
    p = new char[210];
    strcpy(p, x);
}
CHugeInt::CHugeInt(int x) {
    p = new char[210];
    char *pp = int_to_char(x);
    strcpy(p, pp);
}
CHugeInt& CHugeInt::operator+=(const int n) {
    char *n_c = new char[210];
    n_c = int_to_char(n);//这个是我要加的n_c
    //重载+=
    char *pp = new char[210];
    strcpy(pp, add_2_chars(p, n_c));
    strcpy(p, pp);
    return *this;
}
char* CHugeInt::int_to_char(int n) {
    char *pp = new char[210];
    int i=0;
    do{
        pp[i++] = n%10+'0';
    }while((n=n/10));
    reverse(pp);
    return pp;
}
void CHugeInt::reverse(char *x) {
    int l=0, r=strlen(x)-1;
    while(l<r){
        char c;
        c = x[l];
        x[l] = x[r];
        x[r] = c;
        l++;r--;
    }
}
char* CHugeInt::add_2_chars(char *p1, char *p2) {
    if(strlen(p1)>strlen(p2)){
        //保证p是比较短的
        char *tt = new char[210];
        strcpy(tt, p1);
        strcpy(p1, p2);
        strcpy(p2, tt);
        delete []tt;
    }
    const int mins_len = strlen(p1);
    const int maxs_len = strlen(p2);
    int temp = 0;
    char *pp = new char[210];
    int x1=0, x2=0, sum_x1_x2;//保存两个数的个位数字
    reverse(p2);
    reverse(p1);

    for (int i = 0; i < maxs_len; ++i) {
        if(i<mins_len) x1 = p1[i]-'0';
        else x1 = 0;
        x2 = p2[i]-'0';
        sum_x1_x2=x1+x2+temp;
        char t = sum_x1_x2%10+'0';
        pp[i] = t;
        if(sum_x1_x2/10!=0) temp = sum_x1_x2/10;
        else temp=0;
    }
    if(temp!=0) pp[maxs_len]='1';
    reverse(pp);
    return pp;
}
CHugeInt& CHugeInt::operator++() {
    char *t = new char[2];
    t[0]='1';
    strcpy(p, add_2_chars(p, t));
    return *this;
}
CHugeInt CHugeInt::operator++(int k) {
    CHugeInt c(*this);
    char *t = new char[2];
    t[0]='1';
    strcpy(p, add_2_chars(p, t));
    return c;
}
CHugeInt::CHugeInt(CHugeInt &c) {
    p = new char[210];
    strcpy(p, c.p);
}
void reverse(char *x){
    int l=0, r=strlen(x)-1;
    while(l<r){
        char c;
        c = x[l];
        x[l] = x[r];
        x[r] = c;
        l++;r--;
    }
}
char* operator+(const CHugeInt &c1, const CHugeInt &c2) {
    char *p1 = new char[210];
    char *p2 = new char[210];
    char *pp = new char[210];
    strcpy(p1, c1.p);
    strcpy(p2, c2.p);
    if(strlen(p1)>strlen(p2)){
        char *n = new char[210];
        strcpy(n, p1);
        strcpy(p1, p2);
        strcpy(p2, n);
        delete[] n;
    }
    const int lens_min = strlen(p1);
    const int lens_max = strlen(p2);
    reverse(p1);
    reverse(p2);
    int tmp = 0;
    int x1 = 0;
    int x2 = 0;
    for (int i = 0; i < lens_max; ++i) {
        if(i<lens_min) x1=p1[i]-'0';
        else x1 = 0;
        x2 = p2[i]-'0';
        int sum_p1_p2 = x1+x2+tmp;
        char t = sum_p1_p2%10+'0';
        pp[i] = t;
        if(sum_p1_p2/10!=0) tmp=sum_p1_p2/10;
        else tmp = 0;
    }
    if(tmp!=0)pp[lens_max]='1';
    reverse(pp);
    return pp;
}
char* operator+(const int &n, const CHugeInt &c){
    CHugeInt c_n(n);
    return c_n+c;
}
char* operator+(const CHugeInt &c, const int &n){
    CHugeInt n_c(n);
    return c+n_c;
};
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;
}

要求输入:

99999999999999999999999999888888888888888812345678901234567789 12

要求输出:

99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26

猜你喜欢

转载自blog.csdn.net/abc15766228491/article/details/79982166