Experiment 12:problem H: integer array operator overloading

Source: Internet
Author: User

Home Web Board Problemset Standing Status Statistics
Problem H: integer array operator overloading Problem H: integer array operator overloading time limit: 1 Sec Memory Limit: MB
Submit: 643 Solved: 401
[Submit] [Status] [Web Board] Description

Define the Array class:

1. Owns the data member int length and int *mems, respectively, the number of elements in the array and the list of elements.

2. No parameter constructor, set MEMS to Null,length to 0.

3. Overload = = operator, which is used to determine whether two array objects are equal. Equality consists of two cases: (1) Two objects are the same object, that is, they have the same address (remember: this pointer points to the current object, is the address of the current object), and (2) two objects have the same length, and the corresponding elements in the MEMS have the same values. All other cases are unequal.

4. Use the friend function to overload the << and >> operators. The input and output formats are shown below.

Input

The input is divided into multiple lines.

The first line is a positive integer m, which indicates that there are m arrays.

Each array is a row, where the first non-negative integer n represents the number of elements of the array, followed by n integers.

Output

The output has m rows.

The first line of output is the first array.

Starting with the second line, the corresponding array elements (22 separated by a space, without spaces) are first exported, and if the array is empty, the element is not output. Then, depending on whether the array is the same as the previous array, the output is "unequal to above." (Not equal) and "equal to above" (equal).

Sample Input1 2 1 2 307 1 2 3 4 5 6 1 2 3 4 5 6 8Sample Output1 2 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 codeappend.cc, [Submit] [Status] [Web Board]

??? < Chinese????? 中文版???
All Copyright Reserved 2010-2011 sdustoj TEAM
GPL2.0 2003-2011 hustoj Project TEAM
Anything about the problems admin:admin

#include <iostream>#include<cmath>#defineDD Deleteusing namespacestd;classarray{ Public:    intlength; int*MEMS; Array (): Length (0), MEMS (NULL) {}int operator= = (Array &a) {inti;  for(i=0; i<length;i++)        {            if(mems[i]!=A.mems[i]) Break; }        if( This==&a)return 1; Else if(a.length==length&&length==i)return 1; Else            return 0; }    ~array () {if(mems!=NULL) dd []mems;} Friend IStream&operator>> (IStream & is, Array &a); Friend Ostream&operator<< (ostream &os,array &a);}; IStream&operator>> (IStream & is, Array &a) {     is>>a.length; A.mems=New int[A.length];  for(intI=0; i<a.length;i++){         is>>A.mems[i]; }    return  is;} Ostream&operator<< (ostream &os,array &a) {     for(intI=0; i<a.length;i++)        if(i!=a.length-1) OS<<a.mems[i]<<" "; ElseOS<<A.mems[i]; returnos;}intMain () {intcases; CIN>>cases;    Array arraies[cases];  for(inti =0; i < cases; i++) {cin>>Arraies[i]; } cout<<arraies[0]<<Endl;  for(inti =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;}

Experiment 12:problem H: integer array operator overloading

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.