zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    忏悔书(IT攻城狮)
    【博文汇总】彩色图像分割 Menu
    【Android】Android 4.2源码下载(ubuntu 12.10)
    【My Project】织物疵点检测机器视觉系统 软件测试平台
    【NFC】Android NFC API Reference中英文
    【分享】RSS订阅技巧及工具和实用RSS链接分享
    【流媒體】live555—VS2010/VS2013 下live555编译、使用及测试
    vc++ win32项目属性介绍(二、c/c++)
    c风格回调函数 vs c++风格虚基类,关于接口定义和调用的对比
    关于移动技术的我的看法(二)
  • 原文地址:https://www.cnblogs.com/KeepZ/p/11143774.html
Copyright © 2011-2022 走看看