C++ experiment---array class (II)

Array class (II)

Description

Encapsulates a template array class for storing arrays and processing related functions, and supports the following operations:

Array::Array(int l) construction method: Create a group object of length l.
Array::size() method: returns the number of elements in the Array object.
Array::put(int n) method: output the first n large elements in descending order, if the array length is less than n, output all elements in descending order.
Subscript operator: returns the element pointed to by the subscript.
You design a template array class Array so that the main() function can run correctly.
See append.cc for the function call format.
The main() function has been given in append.cc.

Input

The first integer n entered means that there are n sets of test data.
Each subsequent line starts with an integer k, indicating that there are k array elements of the same type.
There are three types of array elements: integers, floating-point numbers, and characters, and they appear at intervals in a fixed order.

Output

Output the first 10 elements of the input array in descending order of value, and output all of them if the input is less than 10 elements. Each row of data corresponds to one output. See sample for format.

Sample Input

3
10 1 2 3 4 5 6 7 8 9 0
5 1.1 2.2 3.3 4.4 5.5
20 ABCDEGHIJMNPRSTUVWXY

Sample Output

9 8 7 6 5 4 3 2 1 0
5.5 4.4 3.3 2.2 1.1
Y X W V U T S R P N

Program given code

int main()
{
    
    
    int cases, len;
    cin >> cases;
    for(int ca = 1; ca <= cases; ca++)
    {
    
    
        cin >> len;
        if(ca % 3 == 0)
        {
    
    
            Array<char> chr_arr(len);
            for(int i = 0; i < chr_arr.size(); i++)
                cin >> chr_arr[i];
            chr_arr.put(10);
        }
        if(ca % 3 == 1)
        {
    
    
            Array<int> int_arr(len);
            for(int i = 0; i < int_arr.size(); i++)
                cin >> int_arr[i];
            int_arr.put(10);
        }
        if(ca % 3 == 2)
        {
    
    
            Array<double> dbl_arr(len);
            for(int i = 0; i < dbl_arr.size(); i++)
                cin >> dbl_arr[i];
            dbl_arr.put(10);
        }
    }
}

code:

#include<iostream>
#include<algorithm>

using namespace std;

template<class T>
class Array{
    
    
	int len;
	T* elem;
public:
	Array(int Len){
    
    
		len=Len;
		elem=new T[len];
	}
	
	int size(){
    
    
		return len;
	}
	
	void put(int n){
    
    
		T* elem2=new T[len];
		for(int i=0;i<len;i++){
    
    
			elem2[i]=elem[i];
		}
		if(n>len)n=len;
		sort(elem2,elem2+n);
		for(int i=len-1;i>=len-n;i--){
    
    
			cout<<elem2[i];
			if(i>len-n)cout<<' ';
		}
		cout<<endl;
	}
	
	T& operator[](int n){
    
    
		return elem[n];
	}
	
	friend istream& operator >>(istream &is,T t){
    
    
		is>>t;
		return is;
	}
	
};

int main()
{
    
    
    int cases, len;
    cin >> cases;
    for(int ca = 1; ca <= cases; ca++)
    {
    
    
        cin >> len;
        if(ca % 3 == 0)
        {
    
    
            Array<char> chr_arr(len);
            for(int i = 0; i < chr_arr.size(); i++)
                cin >> chr_arr[i];
            chr_arr.put(10);
        }
        if(ca % 3 == 1)
        {
    
    
            Array<int> int_arr(len);
            for(int i = 0; i < int_arr.size(); i++)
                cin >> int_arr[i];
            int_arr.put(10);
        }
        if(ca % 3 == 2)
        {
    
    
            Array<double> dbl_arr(len);
            for(int i = 0; i < dbl_arr.size(); i++)
                cin >> dbl_arr[i];
            dbl_arr.put(10);
        }
    }
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115093045