18.03.17 運算符重載作業


A:看上去好坑的運算符重載

描述

程序填空

#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

來源Guo Wei

1     MyInt& operator -(int a){
2         nVal-=a;
3         return *this;
4     }
5     operator int () {
6     return nVal;
7     }
View Code

木有看到Inc函數是給定的,找了好久錯在哪TuT

B:驚呆!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

來源Guo Wei

1             friend istream & operator >>(istream & i,Point & p){
2             i>>p.x>>p.y;
3             return i;
4         }
5         friend ostream & operator <<(ostream & o,Point & p){
6             o<<p.x<<","<<p.y;
7             return o;
8         }        
View Code

Qt真的一點也不嚴格-。-換VS才能找出錯在哪

C:第四周程序填空題3

描述

寫一個二維數組類 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,

樣例輸入

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,
 1 int l, r;
 2     int **p;
 3 public:
 4     Array2(int a = 3, int b = 4) :l(a), r(b) {
 5         p = new int*[a];
 6         for (int i = 0; i<a; i++) {
 7             p[i] = new int[b];
 8         }
 9     }
10     ~Array2() {
11         for (int i = 0; i<l; i++) {
12             delete[]p[i];
13         }
14         delete[]p;
15     }
16     int* operator [](int a) {
17         return p[a];
18     }
19     int& operator ()(int a, int b) {
20         return p[a][b];
21     }
22     Array2&operator =(const Array2& s) {
23         l = s.l, r = s.r;
24         for (int i = 0; i<l; i++)
25             for (int j = 0; j<r; j++)
26                 p[i][j] = s.p[i][j];
27         return *this;
28     }
View Code

關於淺拷貝與深拷貝的區別:

簡單的來說就是,在有指針的情況下,淺拷貝只是增加了一個指針指向已經存在的內存,
而深拷貝就是增加一個指針並且申請一個新的內存,使這個增加的指針指向這個新的內存,
采用深拷貝的情況下,釋放內存的時候就不會出現在淺拷貝時重復釋放同一內存的錯誤!
所以我們需要重載賦值運算符
值得注意的是二維數組的動態內存申請

D:別叫,這個大整數已經很簡化了!

描述

程序填空,輸出指定結果

#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

來源Guo Wei


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM