c++实验6-B(1784)整型数组运算符重载

文章描述了一个C++编程问题,涉及如何定义一个Array类,包含length和mems数据成员,以及重载赋值运算符、输入输出运算符。重点在于使用友元函数处理数组的比较和输出,同时演示了动态内存分配的使用。
摘要由CSDN通过智能技术生成

Problem B: 整型数组运算符重载

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 5639  Solved: 3788
[Submit][Status]

Description

定义Array类:

1.拥有数据成员int length和int *mems,分别是数组中元素的个数和元素列表。

2. 无参构造函数,将mems设置为NULL,length为0。

3. 重载==运算符,用于判断两个Array对象是否相等。相等包括两种情况:(1)两个对象是同一个对象,即它们拥有相同的地址(记住: this指针指向当前对象,是当前对象的地址);(2)两个对象的length相同,且mems中对应元素的值相同。其他情况均为不相等。

4. 利用友元函数重载<<和>>运算符。输入、输出格式见下。

Input

输入分多行。

第一行是一个正整数M,表示有M个数组。

每个数组是一行,其中第一个非负整数N表示该数组的元素个数,之后有N个整数。

Output

输出有M行。

第一行输出即为第一个数组。

自第二行开始,首先输出对应的数组元素(两两之间用空格隔开,首尾不能有空格),如果数组为空,则不输出元素。之后根据这个数组与上个数组是否相同,输出“unequal to above.”(不相等)和“equal to above”(相等)。

Sample Input

5 3 1 2 3 3 1 2 3 0 7 1 2 3 4 5 6 7 7 1 2 3 4 5 6 8

Sample Output

1 2 3 1 2 3 equal to above. unequal to above. 1 2 3 4 5 6 7 unequal to above. 1 2 3 4 5 6 8 unequal to above.

HINT

Append Code

append.cc,

int main()
{
    int cases;
    cin>>cases;
    Array arraies[cases];
    for (int i = 0; i < cases; i++)
    {
        cin>>arraies[i];
    }
    cout<<arraies[0]<<endl;
    for (int i = 1; i < cases; i++)
    {
        if (arraies[i] == arraies[i - 1])
        {
            cout<<arraies[i]<<" "<<"equal to above."<<endl;
        }
        else
        {
            cout<<arraies[i]<<" "<<"unequal to above."<<endl;
        }
    }
    return 0;
}

答案:

#include<iostream>
#include<math.h>
using namespace std;
class Array
{
private:
    int l;
    int* m;
public:
    Array():m(NULL),l(0){}
    bool operator==(const Array& b)
    {
        int i=0;
        while(m[i]==(b.m)[i])
                i++;
        if(m==b.m)
            return true;
        else if(l==b.l&&i==l)
            return true;
        else
            return false;
    }
    friend istream& operator>>(istream& in,Array& t)
    {
        in>>t.l;
        t.m=new int[t.l];
        for(int j=0;j<t.l;++j)
        in>>t.m[j];
        return in;
    }
    friend ostream& operator<<(ostream& out,Array& t)
    {
        if(t.l==0)
            return out;
        else
        {
            out<<(t.m)[0];
            for(int j=1;j<t.l;j++)
                out<<" "<<(t.m)[j];
              return out;
        }
    }
};
int main()
{
    int cases;
    cin>>cases;
    Array arraies[cases];
    for (int i = 0; i < cases; i++)
    {
        cin>>arraies[i];
    }
    cout<<arraies[0]<<endl;
    for (int i = 1; i < cases; i++)
    {
        if (arraies[i] == arraies[i - 1])
        {
            cout<<arraies[i]<<" "<<"equal to above."<<endl;
        }
        else
        {
            cout<<arraies[i]<<" "<<"unequal to above."<<endl;
        }
    }
    return 0;
}

分析:

 t.m=new int[t.l];必须有;用于开辟空间存数

1.指针类型 指针变量名 = new 类型

2.指针类型 指针变量名 = new 类型(初始值)

3.指针类型 指针变量名 = new 类型[元素个数] //连续空间的开辟

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值