• Problem E: 数量的类模板


     

    #include<iostream>
    #include<iomanip>
    #include<algorithm>
    using namespace std;
    template<class T>
    class Data
    {
        private:
            T value;
        public:
            Data():value(0){}
            Data(T v):value(v){}
            void setValue(T v){ value = v;}
            T getValue(){return value;}
            bool operator>(Data<T> b){ return value > b.value;}   /*使用类模板对象时一定要显示指出模板实参*/
            bool operator<(Data<T> b){return value < b.value;}
            Data<T> operator+(const Data<T> b)
            {
                Data<T> c;
                c.value = value + b.value;
                return c;
            }
            friend ostream& operator<<(ostream &os, const Data<T> &s);       /*若写引用必须用const, 否则不能写引用&*/
            {
                os << setprecision(2) << fixed << s.value;
                return os;
            }
    
    };
    
    template<class T>
    class GetResult
    {
        public:
             static Data<T> getSum(Data<T> *arr, int num)
             {
                 Data<T> sum(0);
                 for(int i = 0; i < num; i++)
                    sum.setValue(sum.getValue() + arr[i].getValue());
                return sum;
             }
             static Data<T> getMax(Data<T> *arr, int num)
             {
                 T Max = arr[0].getValue();
                 for(int i = 1; i < num; i++)
                    Max = max(Max, arr[i].getValue());
                return Data<T>(Max);
             }
              static Data<T> getMin(Data<T> *arr, int num)
              {
                  T Min;
                  Min = arr[0].getValue();
                  for(int i = 1; i < num; i++)
                    Min = min(Min, arr[i].getValue());
                    Data<T> m(Min);
                    return m;
              }
    
    };
    //append.cc
    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;
    }
    
  • 相关阅读:
    UINavigationBar 调整
    UILabel根据内容自动调整高度
    [iOS开发]文档导读
    [iOS开发]NSUserDefaults使用注意
    Xcode 断点的使用
    [iOS开发] UIKit Dynamics
    [iOS开发]ShareSDK
    objective-c GCD
    面向对象设计原则
    Go语言学习教程:go语言的包管理
  • 原文地址:https://www.cnblogs.com/KeepZ/p/11143774.html
Copyright © 2020-2023  润新知