Problem A: Why cannot I reference A number of class templates for help comments? problem comments
Problem A: number of class templates Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1140 Solved: 788
[Submit] [Status] [Web Board] Description
Defines a class template Data for packaging the basic Data types int and double in C ++. It includes:
1. The value of the data member is the value encapsulated by the object.
2. No parameter Constructor (the initial value is 0) and a parameter constructor.
3. Reload operators:>, <, +, and <. "+" Returns the sum and does not change the values of the two operands.
4. The setValue function is used to set the value.
Define another class template GetResult, which has only threeStaticMember functions (the following "T" is a type parameter ):
1. static Data <T> getSum (Data <T> * arr, int num): Calculate the sum of num Data objects stored in arr, and return a Data object composed of the sum.
2. static Data <T> getMax (Data <T> * arr, int num): calculates the maximum value of the num Data objects stored in arr and returns the objects corresponding to the maximum value.
3. static Data <T> getMin (Data <T> * arr, int num): calculates the minimum value of the num Data objects stored in arr and returns the objects corresponding to the minimum value.
Input
Input Multiple rows.
The first line M> 0 indicates that there are M test cases.
Only M rows. Each row starts with a letter I or d, and the second is a positive integer N> 0. If the first letter is I, it indicates that the row contains N int data types. If the first letter is d, it indicates that the row has N double data types.
Output
Except the first six rows of output, the number of other output rows is equal to M. Three data records are output for each row, which correspond to the maximum, minimum, and sum values of the test case. The real number outputs the decimal point, and only two decimal places are output.
Sample Input3i 3 1 2 3d 3 1.1 2.2 3.3i 1 10 Sample Outputa + B = 30max (a, B) = 20 min (a, B) = 10c + d =-0.96max (c, d) = 3.14 min (c, d) =-4.103 1 63.30 1.10 6.6010 10 10 HINT
# Include <iostream>
# Include <iomanip>
Using namespace std;
Template <class T>
Class Data
{
Private:
T value;
Public:
Data (): value (0 ){}
Data (T B): value (B ){}
Bool operator> (Data & B)
{
If (value> B. value) return 1;
Else return 0;
}
Bool operator <(Data & B)
{
If (value <B. value) return 1;
Else return 0;
}
T operator + (Data & B)
{
Return value + B. value;
}
Void setValue (T u)
{
Value = u;
}
T getv () {return value ;}
Friend ostream & operator <(ostream & OS, Data <T> & B) // *** why is an error reported when a reference is used here? (& B)
{
OS <fixed <setprecision (2) <B. value;
Return OS;
}
};
Template <class T>
Class GetResult
{
Public:
Static Data <T> getSum (Data <T> * arr, int num)
{
T sum = 0;
For (int I = 0; I <num; I ++)
Sum + = arr [I]. getv ();
Data <T> D (sum );
Return D;
}
Static Data <T> getMax (Data <T> * arr, int num)
{
T max = arr [0]. getv ();
For (int I = 1; I <num; I ++)
Max = max> arr [I]. getv ()? Max: arr [I]. getv ();
Data <T> D (max );
Return D;
}
Static Data <T> getMin (Data <T> * arr, int num)
{
T max = arr [0]. getv ();
For (int I = 1; I <num; I ++)
Max = max <arr [I]. getv ()? Max: arr [I]. getv ();
Data <T> D (max );
Return D;
}
};
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;
}