实验目的:
-
练习面向对象编程;
-
练习STL中deque容器的使用;
-
能根据要求实现相应的功能。
实验说明:
-
某某大学高性能运算需要统计年计算量,按core/小时收费 。
实验要求:
-
找出使用时间 <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;
}