DLUT C++オンマシン作業(実験四)

30974 ワード

実験4演算子は1、実験目的と要求(1)演算子のリロードの文法的要点を把握し、メンバー関数と友元関数のリロード演算子の違いを理解する.(2)各種演算子のリロード方法を把握し、参照形式をパラメータと戻り値の特徴として理解する.2、実験内容(1)平面点クラスPointを記述し、リロードマイナス演算子は2点の距離を計算し、それぞれメンバー関数と友元関数で実現する.リロード演算子<
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
class point{
public:
    point(int = 0, int = 0);
    void setpoint(int, int);
    double operator-(point&);
    friend ostream&operator<//  friend double operator-(point&, point&);
private:
    int x; int y;
};
point::point(int a , int b ) :x(a), y(b){}
double point::operator-(point &a){
    return sqrt((double)pow(x - a.x) + (double)pow(y - a.y));
}
void point::setpoint(int a, int b){
    x = a, y = b;
}
ostream& operator<" " << a.y;
    return out;
}
/*double operator-(point&a, point&b){
    return sqrt((double)pow(a.x - b.x) + (double)pow(a.y - b.y));
}*/
int main(){
    point p1(2, 1), p2;
    double d = p1 - p2;
    cout << p1 << "->" << p2 << "=" << d << endl;
}

(2)有理数を記述するRationalクラスは次のようになります.クラスの他のメンバーを補足して、様々な演算を実行できるようにしてください.1)有理数の4則演算に適用できるように算術演算子「+」、「-」、「*」、「/」を再ロードします.2)2つの有理数を比較できるように比較演算子「>」、「<=」、「=」を再ロードします.3)再ロード演算子「<<
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define LL long long int
LL gcd(LL a, LL b){
    a = abs(a);
    b = abs(b);
    if (a < b)swap(a, b);
    return b ? (gcd(b, a%b)) : a;
}
class rational{
private:
    LL num;
    LL den;
public:
    friend ostream&operator<bool operator<=(const rational&a);
    bool operator>=(const rational&a);
    bool operator==(const rational&a);
    rational operator+(const rational&a);
    rational operator-(const rational&a);
    rational operator*(const rational&a);
    rational operator/(const rational&a);
    rational(LL = 0, LL = 1);
    void simple();
};
rational::rational(LL a, LL b) :num(a), den(b){}
ostream&operator<if(!a.den){
        out<<"error"<return out;
    }
    else if(!a.num){
        out<<0<return out;
    }
    else
    out << a.num << "/" << a.den << endl;
    return out;
}
bool rational::operator<=(const rational&a){
    return (double)num / den <= (double)a.num / a.den;
}
bool rational::operator>=(const rational&a){
    return (double)num / den >= (double)a.num / a.den;
}
bool rational::operator==(const rational&a){
    return (double)num / den == (double)a.num / a.den;
}
rational rational::operator+(const rational&a){
    LL d = den / gcd(den, a.den)*a.den;
    LL n = num*d /den + a.num*d / a.den;
    rational temp;
    temp.den = d;
    temp.num = n;
    temp.simple();
    return temp;
}
rational rational::operator-(const rational&a){
    rational temp = a;
    temp.num *= -1;
    return *this + temp;
}
rational rational::operator*(const rational&a){
    rational temp;
    temp.den = den*a.den;
    temp.num = num*a.num;
    temp.simple();
    return temp;
}
rational rational::operator / (const rational &a){
    rational temp;
    temp.num = num*a.den;
    temp.den = den*a.num;
    temp.simple();
    return temp;
}
void rational::simple(){
    LL g = gcd(num, den);
    den /= g;
    num /= g;
}
int main(){
    rational n1(1, 2);
    rational n2(0, 4);
    cout << n1 << endl << n2 << endl;
    cout << n1 + n2 << endl;
    cout << n1 - n2 << endl;
    cout << n1*n2 << endl;
    cout << n1 / n2 << endl;
    if (n1 <= n2)cout << n2 << endl;
    if (n1 == n2)cout << "==" << endl;
    if (n1 >= n2)cout << n2 << endl;
}

(3)集合クラスSetを定義し、最大100個の重複しない整数を保存し、集合を実現するには以下の操作がある:1)ある整数要素を追加する場合、集合の中に重複要素がないことを保証する;指定した要素を削除し、その要素を探して集合の中で集合の中からその要素を削除する;2)再ロード演算子「+」、2つの集合オブジェクトの連結操作を実現し、演算子を再ロードする「」は、2つの集合オブジェクトの交差を求めます.例えばSet s,s 1,s 2;s = s1+s2; s = s1 s2; 3)賦値演算子=と複合賦値演算子-=を再ロードし、2つの集合オブジェクトの賦値操作と差セット操作を実現する.例えば、Set s 1,s 2;s1 = s2; s1-=s2; 集合S 1では、S 2に存在する要素を削除する.
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int//          long long int,   long int
class set{
private:
    int *p;
public:
    void add(int);
    void del(int);
    set operator+(set&);
    set operator*(set&);
    set&operator=(set&);
    set&operator-=(set&);
    set();
    ~set();
    void show();
};
void set::show(){
    for (int i = 0; i < N; i++){
        if (p[i]){
            cout << i << " ";
        }
    }
    cout << endl;
}
set::set(){
    p = new int[N];
    for (int i = 0; i < N; i++)p[i] = 0;
//  mem(p, 0);
}
set::~set(){
    if (!p)delete[]p;
}
void set::add(int x){
    if (x >= N||x<0){
        cout << "mistake" << endl;
        return;
    }
    p[x] = 1;
}
void set::del(int x){
    if (x >= N || x < 0||!p[x]){
        cout << "mistake" << endl;
        return;
    }
    p[x] = 0;

}
set set::operator+(set&a){
    set temp;
    for (int i = 0; i < N; i++)
    {
        if (a.p[i] || p[i]){
            temp.p[i] = 1;
        }
    }
    return temp;
}
set set::operator*(set&a){
    set temp;
    for (int i = 0; i < N; i++){
        if (a.p[i] && p[i])
            temp.p[i] = 1;
    }
    return temp;
}
set& set::operator=(set&a){
    for (int i = 0; i < N; i++){
        p[i] = a.p[i];
    }
    return *this;
}
set&set::operator-=(set&a){
    for (int i = 0; i < N; i++){
        if (a.p[i]){
            p[i] = 0;
        }
    }
    return *this;
}
int main(){
    set s1, s2;
    for (int i = 0; i < 10; i++){
        s1.add(i);
    }
    for (int i = 5; i < 15; i++){
        s2.add(i);
    }
    s1.show();
    s2.show();
    set s3 = s1 + s2;
    s3.show();
    set s4 = s1 * s2;
    s4.show();
    s1 -= s2;
    s1.show();
    set s5;
    s5 = s1;
    s5.show();
}

(4)データメンバー時間hour、分minute、秒secondを含む時間を記述するTimeクラスを定義し、関連関数を定義して以下の操作を実現する:1)リロード演算子「+」と「-」を定義し、時間オブジェクトと整数秒の加減操作を実現することができる.2)リロード演算子「<3)リロード演算子「+」と「-」を定義し、時間の合理的な自増自減機能を実現することを要求する(秒数の増減).
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int
class Time{
private:
    int hour, minue, second;
public:
    Time operator+(int);
    Time operator-(int);
    friend ostream&operator<operator++();
    Time operator--(int);
    Time(int h,int m,int s);
    void change(Time&);
};
void Time::change(Time& a){
    if(a.second<0){
        second+=60;
        minue--;
        if(minue<0)
            {minue+=60;
        hour--;
        }
        if(hour<0){
            hour+=24;
        }
    }
}


ostream&operator<" " << a.minue << " " << a.second << endl;
    return out;
}
Time Time::operator+(int x){
    second += x;
    int s = second % 60;
    int m = second / 60;
    second = s;
    minue += m;
    int mm = minue % 60;
    int h = minue / 60;
    minue = mm;
    hour += h;
    hour %= 24;
    return *this;
}
Time Time::operator-(int a){
    second -= a;
    if (a < 0){
        minue--;
        if (minue < 0){
            hour--;
        }
    }
    change(*this);
    return *this;
}
Time&Time::operator++(){
    //*this.operator+(1);
    return *this+1;
}
Time Time::operator--(int){
    Time temp = *this;
    *this = *this - 1;
    change(*this);
    change(temp);
    return temp;
}
Time::Time(int h, int m, int s) :hour(h), minue(m), second(s){}
int main(){
    Time s(23, 59, 59);
    s++;
    cout << s << endl;
    s--;
    cout << s << endl;
    Time s1 = s + 3;
    cout << s1 << endl;
    s1 = s - 3;
    cout << s1 << endl;
}

(5)StringクラスStringを設計し、Stringクラスオブジェクトs 1,s 2,s 3があれば、リロード演算子は以下の操作を実現する:1)「+」をリロードして文字列接続機能を実現し、式s 1=s 2+s 3を成立させる;2)「3」をリロードし「<>」をリロードし、文字列の入力と出力操作4)リロード演算子「()」を実現し、文字列オブジェクトからサブ列を返す.
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int//          long long int,   long int
class String{
private:
    string s;
    int m;
public:
    String operator()(int,int);
    friend ostream&operator<friend istream&operator>>(istream&in, String&a);
    String operator+(String &);
    bool operatoroperator()(int a, int b){
    String temp;
    temp.s.assign(s, a, b);
    return temp;
}
String String::operator+(String &a){
    String temp;
    temp.s = s + a.s;
    return temp;
}
istream&operator>>(istream&in, String&a){
    in >> a.s;
    return in;
}
bool String::operatorreturn s < a.s;
}
ostream& operator<return out;
}

int main(){
    String s1, s2;
    cin >> s1;
    cin >> s2;
    String s3 = s1 + s2;
    cout << s3;
    cout << s1(1, 3) << endl;
    if (s1 < s2)cout << "s2" << endl;
    else cout << "s1" << endl;
}



(6)【オプション】多項式クラスPolynomialを開発します.多項式の各項目は配列または構造体で表され、各項目は1つの係数と1つの指数を含みます.たとえば、2 x 4の指数は4で、係数は2です.構造関数、構造関数、get関数、set関数を含む完全なPolynomialクラスを開発してください.このクラスには、1)リロード演算子「+」も提供されます.「-」と、2つの多項式を加算または減算します.2)乗算演算子「*」を再ロードし、2つの多項式を乗算します.3)代入演算子"="を再ロードし、1つの多項式を別の多項式に割り当てます.
なんというか、どうせ宿題なので、書くコードは時間も空間も複雑で、制限があります.こうしましょう、、、.
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//#define pow(a) (a*a)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define N 100
#define LL long long int//          long long int,   long int
class node{
public:
    int n, a;
};

class pol{
    node*s;
public:
    void set(int a, int n);
    pol();
    ~pol();
    LL get(int x);
    void show();
    pol operator+(pol&);
    pol operator-(pol&);
    pol operator*(pol&);
    pol&operator=(pol&);
};
pol::pol(){
    s = new node[N];
    for (int i = 0; i < N; i++)s[i].a = 0, s[i].n = 0;
}
pol::~pol(){
    cout << "delete" << endl;
}
void pol::set(int a, int n){
    s[n].a = a;
    s[n].n = n;
}
void pol::show(){
    bool flag = false;
    for (int i = 0; i < N; i++){
        if (s[i].a != 0){
            if (!flag)
                cout << s[i].a << "x^" << s[i].n, flag = true;
            else{
                cout << "+" << s[i].a << "x^" << s[i].n;
            }
        }
    }
    cout << endl;
}
pol pol::operator+(pol&a){
    pol temp;
    for (int i = 0; i < N; i++){
        if (a.s[i].a){
            temp.s[i].a = a.s[i].a + s[i].a;
            temp.s[i].n = a.s[i].n;
        }
    }
    return temp;
}
pol pol::operator-(pol&a){
    pol temp;
    for (int i = 0; i < N; i++){
        if (a.s[i].a){
            temp.s[i].a = s[i].a - a.s[i].a;
        }
    }
    return temp;
}
pol pol::operator*(pol&a){
    pol temp;
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            if (s[i].a&&a.s[j].a){
                temp.s[i + j].a += s[i].a*a.s[j].a;
                temp.s[i + j].n = i + j;
            }
        }
    }
    return temp;
}
pol&pol::operator=(pol&a){
    for (int i = 0; i < N; i++){
        s[i].a = a.s[i].a;
        s[i].n = a.s[i].n;
    }
    return *this;
}
LL pol::get(int x){
    LL sum = 0;
    for (int i = 0; i < N; i++){
        sum += s[i].a*pow(x, s[i].n);
    }
    return sum;
}
int main(){
    pol s1, s2;
    for (int i = 0; i < 3; i++){
        s1.set(i, i);
        s2.set(i, 3 - i);
    }
    s1.show();
    s2.show();
    pol s = s1 + s2;
    s.show();
    s = s1*s2;
    s.show();
}