C++實驗——對高算用戶排序

實驗目的:

  1. 練習面向對象編程;

  2. 練習STL中deque容器的使用;

  3. 能根據要求實現相應的功能。

實驗說明: 

  1. 某某大學高性能運算需要統計年計算量,按core/小時收費 。       

實驗要求: 

  1. 找出使用時間 <24小時的用戶,刪除掉該記錄,輸出最終排序後結果。

 實驗材料:

  • 說明:將下面數據複製到新建一個txt文檔命名爲core_time.txt文件中,文件放到D盤下。
wrb   	3242309.689
luhg   	2346727.028
tianxx   	1749160.207
yuewenmu   	1231745.881
lisidian   	1142826.668
wyb   	417411.4575
demouser   	274168.7461
xlt   	269970.7442
yangjing   	87924.22444
zhangt   	36579.69889
lihua   	33894.58556
wbt11129   	17018.47056
jhadmin   	12175.30472
wrxue   	4879.451389
yangxingli  	2627.677778
yangwn   	1249.143333
fanglichao  	715.4583333
genghj   	627.3577778
caoguojin   	626.5616667
wanglaoshi  	398.1883333
jhtest3   	66.17944444
htli   	52.16222222
lvlaoshi   	43.76555556
lilaoshi   	11.53333333
hpc_c1   	9.558611111
wangwei   	2.447222222
root   	1.425277778
hongtongtong	0.498055556
zhanglei   	0.292777778
zhangpengfei	0.166666667
wangruibo   	0.138888889
caofy   	0.039444444
hrl   	0.02
wangy   	0.001111111
cuiziheng   	0

實驗代碼:

#include <iostream>
#include <deque>
#include <string>
#include <fstream>
#include <iterator>
#include <algorithm>

using namespace std;

class User;
ostream& operator<<(ostream& os,User& U);

class User  //用戶類
{
public:
    string Name;
    double Time;
public:
    User(string name,double time):Name(name),Time(time){}
    string GetName()
    {
        return Name;
    }
    double GetTime()
    {
        return Time;
    }
};

class Print:public unary_function<User,void>   //打印的函數對象
{
private:
    deque<User>deq_u;
public:
    Print(deque<User>& u):deq_u(u){}
    void operator()(User& u)
    {
        cout << u << endl;
    }
};
ostream& operator<<(ostream& os,User& U)    //重載輸出流
{
    os << U.GetName() << "\t" << U.GetTime() << endl;
    return os;
}
istream& operator>>(istream& is,User& U)    //重載輸入流
{
    is >> U.Name >> U.Time;
    return is;
}
void test()
{
    ifstream in;
    in.open("D:\\Score_time.txt");
    if(!in)
    {
        cout << "文件讀入錯誤!" << endl;
    }
    User u(" ",0.0);
    deque<User>deq_u;
    int c = 0;      //用來計數有多少個符合要求的用戶
    while(!in.eof())
    {
        in >> u;
        if(u.Time > 24.0)
        {
            deq_u.push_back(u);
        }
        c++;
    }
    for_each(deq_u.begin(),deq_u.end(),Print(deq_u));
    cout << deq_u.size() << "\t" << c << endl;
    //copy(deq_u.begin(),deq_u.end,ostream_iterator<User>(cout,"\t"));
    in.close();
}
int main()
{
    test();
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章