MOOC Programming and Algorithm (iii) C + + object-oriented programming operator overload Job __c++ and Java Foundation

Source: Internet
Author: User

1, MyString
Required Input:

ABC def
123 456

Require output

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, look good pit operator overload
Required Input:

30

Require output

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, stunned. Point can actually input output like this
Input requirements:

2 3
4 5

Require output

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. Fourth week procedure fill in the blanks question 3
Required Input:

None

Require output

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) {//overload [] return p[i];
    int operator () (int x, int y) {//Overload () return p[x][y]; } array2& operator= (const array2& a) {//overloaded equal sign 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, don't shout, this large number has been very simple ....

#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> using namespace St
D 
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);//This is to be used in the following member function, so it must be set to the member function void print () {Cout<<p<<endl;}
    char* Add_2_chars (Char *p1, char *p2);
    Chugeint (Chugeint &c);
    Friend char* operator+ (const chugeint &AMP;C1, const chugeint &AMP;C2);
    Friend char* operator+ (const int &n, const chugeint &c);
    Friend char* operator+ (const chugeint &c, const int &n);
    Chugeint & operator++ ()//Front 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);//This is the N_c I want to add//overload + = 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)) {//guaranteed p is a shorter char *t
        t = 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;//save two digits of Single-digit 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 &AMP;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; }

Required Input:

99999999999999999999999999888888888888888812345678901234567789 12

Output Required:

99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
26
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.