C++ experiment --- number of class templates

Number of class templates

Description

Define a class template Data, which is used to wrap the basic data types int and double in C++. it includes:

The data member value is the value wrapped by the object.

No-argument constructor (initialized value is 0) and parameterized constructor.

Overloaded operators: >, <, +, and <<. Among them, "+" returns a sum without changing the values ​​of the two operands.

The member function setValue is used to set the value of value.

Define another class template GetResult, which has only 3 static member functions (the following "T" is a type parameter):

static Data getSum(Data *arr, int num): Find the sum of num Data objects stored in arr, and return a Data object composed of this sum.

static Data getMax(Data *arr, int num): Find the maximum value of num Data objects stored in arr, and return the object corresponding to this maximum value.

static Data getMin(Data *arr, int num): Find the minimum value of num Data objects stored in arr, and return the object corresponding to this minimum value.

Input

Enter multiple lines.

In the first line, M>0 means there are M test cases.

There are only M lines, each line starts with a letter i or d, and the second is a positive integer N>0. If the first letter is i, it means that the row contains N int data; if the first letter is d, it means that there are N double data in this row.

Output

Except for the first 6 lines of output, the number of lines of other output is equal to M. Each row outputs 3 data: the maximum, minimum, and sum of the corresponding test case. Real numbers output fixed-point decimals, and only output 2 decimals.

Sample Input

3
i 3 1 2 3
d 3 1.1 2.2 3.3
i 1 10

Sample Output

a + b = 30
max(a, b) = 20
min(a, b) = 10
c + d = -0.96
max(c, d) = 3.14
min(c, d) = -4.10
3 1 6
3.30 1.10 6.60
10 10 10

Title given code

int main()
{
    
    
    Data<int> iData[1001];
    Data<double> dData[1001];
    int cases, num;
    char ch;
    int u;
    double v;
    Data<int> a(10), b(20);
    Data<double> c(3.14), d(-4.1);
    cout<<"a + b = "<<(a + b)<<endl;
    cout<<"max(a, b) = "<<(a > b ? a : b)<<endl;
    cout<<"min(a, b) = "<<(a < b ? a : b)<<endl;
    cout<<"c + d = "<<(c + d)<<endl;
    cout<<"max(c, d) = "<<(c > d ? c : d)<<endl;
    cout<<"min(c, d) = "<<(c < d ? c : d)<<endl;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
    
    
        cin>>ch;
        cin>>num;
        for (int j = 0; j < num; j++)
        {
    
    
            if (ch == 'i')
            {
    
    
                cin>>u;
                iData[j].setValue(u);
            }
            else if (ch == 'd')
            {
    
    
                cin>>v;
                dData[j].setValue(v);
            }
        }
        if (ch == 'i')
        {
    
    
            cout<<GetResult<int>::getMax(iData, num);
            cout<<" "<<GetResult<int>::getMin(iData, num);
            cout<<" "<<GetResult<int>::getSum(iData, num)<<endl;
        }
        else if (ch == 'd')
        {
    
    
            cout<<GetResult<double>::getMax(dData, num);
            cout<<" "<<GetResult<double>::getMin(dData, num);
            cout<<" "<<GetResult<double>::getSum(dData, num)<<endl;
        }
    }
    return 0;
}

code:

#include<iostream>
#include<iomanip>
using namespace std;
template<class T>
class Data{
    
    
	T value;
public:
	Data(){
    
    
		value=0;
	}
	
	Data(T t){
    
    
		value=t;
	}
	
	T operator+(Data<T> &t2){
    
    
		return t2.value+value;
	}
	
	friend bool operator >(Data<T> &d1,Data<T> &d2){
    
    
		return d1.value>d2.value;
	}
	
	friend bool operator <(Data<T> &d1,Data<T> &d2){
    
    
		return d1.value<d2.value;
	}
	
	friend ostream& operator <<(ostream &os,const Data<T> &D){
    
    
		os<<fixed<<setprecision(2)<<D.value;
		//注意,fixed和setprecision在<iomanip>头文件里面 
		return os;
	}
	
	T getValue(){
    
    
		return value;
	}
	
	void setValue(T t){
    
    
		value=t;
	}
	
};

template<class T>
class GetResult{
    
    
public:
	static Data<T> getSum(Data<T> *arr, int num){
    
    
		T t=0;
		for(int i=0;i<num;i++){
    
    
			t+=arr[i].getValue();
		}
		return Data<T>(t);
	}
	
	static Data<T> getMax(Data<T> *arr,int num){
    
    
		T t=arr[0].getValue();
		for(int i=1;i<num;i++){
    
    
			if(arr[i].getValue()>t)t=arr[i].getValue();
		}
		return Data<T>(t);
	}
	
	static Data<T> getMin(Data<T> *arr,int num){
    
    
		T t=arr[0].getValue();
		for(int i=1;i<num;i++){
    
    
			if(arr[i].getValue()<t)t=arr[i].getValue();
		}
		return Data<T>(t);
	}
};

int main()
{
    
    
    Data<int> iData[1001];
    Data<double> dData[1001];
    int cases, num;
    char ch;
    int u;
    double v;
    Data<int> a(10), b(20);
    Data<double> c(3.14), d(-4.1);
    cout<<"a + b = "<<(a + b)<<endl;
    cout<<"max(a, b) = "<<(a > b ? a : b)<<endl;
    cout<<"min(a, b) = "<<(a < b ? a : b)<<endl;
    cout<<"c + d = "<<(c + d)<<endl;
    cout<<"max(c, d) = "<<(c > d ? c : d)<<endl;
    cout<<"min(c, d) = "<<(c < d ? c : d)<<endl;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
    
    
        cin>>ch;
        cin>>num;
        for (int j = 0; j < num; j++)
        {
    
    
            if (ch == 'i')
            {
    
    
                cin>>u;
                iData[j].setValue(u);
            }
            else if (ch == 'd')
            {
    
    
                cin>>v;
                dData[j].setValue(v);
            }
        }
        if (ch == 'i')
        {
    
    
            cout<<GetResult<int>::getMax(iData, num);
            cout<<" "<<GetResult<int>::getMin(iData, num);
            cout<<" "<<GetResult<int>::getSum(iData, num)<<endl;
        }
        else if (ch == 'd')
        {
    
    
            cout<<GetResult<double>::getMax(dData, num);
            cout<<" "<<GetResult<double>::getMin(dData, num);
            cout<<" "<<GetResult<double>::getSum(dData, num)<<endl;
        }
    }
    return 0;
}

Guess you like

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