码迷,mamicode.com
首页 > 其他好文 > 详细

统计电影票房排名前10的电影并存入另一个文件

时间:2014-09-17 23:25:32      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:stl   iterator   algorithm   vector   

今天看到一个笔试题,是这样的:给定一个文件(m.dat),里面保存了各个电影票房统计,格式如下:

《2012》                                索尼                $769.7
《哈利波特与死亡圣器(上)》              华纳兄弟            $952.0
《星球大战》                            二十世纪福克斯      $775.4
《怪物史莱克4》                         派拉蒙/梦工厂       $750.0
《阿凡达》                              二十世纪福克斯      $2,782.2
《哈利波特与火焰杯》                    华纳兄弟            $895.9
《哈利波特与混血王子》                  华纳兄弟            $934.0
《指环王2:双塔奇兵》                   新线                $925.3
《蝙蝠侠前传2:黑暗骑士》               华纳兄弟            $1,001.9
《哈利波特与魔法石》                    华纳兄弟            $974.7
《海底总动员》                          迪士尼              $867.9
《功夫熊猫》                            派拉蒙/梦工厂       $631.7
《加勒比海盗3:世界的尽头》             迪士尼              $961.0
《哈利波特与阿兹卡班的囚徒》            华纳兄弟            $795.6
《E.T.》                                环球                $792.9
《夺宝奇兵4:水晶骷髅王国》             派拉蒙              $786.6
《指环王3:王者归来》                   新线                $1,119.1
《怪物史莱克2》                         梦工厂              $919.8
《玩具总动员3》                         迪士尼              $1,063.2
《黑客帝国2:重装上阵》                 华纳兄弟            $742.1

。。。。。。。


要求写程序统计票房排名前10的电影,并把统计结果存入另一个文件。自己试着用C++实现一下,代码分享如下:(linux下gcc 编译)

gcc编译,执行时要传入两个命令行参数,比如:./a.out   m.dat   li.dat (m.dat为源票房文件,li.dat 为存放前10的文件)

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class Movie {
public:
	//重载输入操作
	friend istream& operator>> (istream& is,
		Movie& movie) {
		return is >> movie.m_title >> movie.m_comp
			>> movie.m_gross;
	}
	//重载输出操作
	friend ostream& operator<< (ostream& os,
		const Movie& movie) {
		return os << movie.m_title << ' '
			<< movie.m_comp << ' ' << movie.m_gross;
	}
	//重载小于号,用于List排序
	bool operator< (const Movie& movie) const {
		return gross () > movie.gross ();
	}
private:
	//把从文件读取的string转换为double返回
	double gross (void) const {
		string str (m_gross);
		size_t pos = 0;
		while ((pos = str.find_first_of ("$,", pos)) !=  //去除票房前面的"$"和","
			string::npos)
			str.erase (pos, 1);
		return atof (str.c_str ());
	}
	string m_title;  //电影名
	string m_comp;	 //出品公司名
	string m_gross;  //票房
};
//读文件,读取结果存入Vector<Movie>& vm
bool read (const char* file, vector<Movie>& vm) {
	ifstream ifs (file);
	if (! ifs) {
		perror ("打开票房文件失败");
		return false;
	}
	Movie movie;
	while (ifs >> movie)  //调用重载的>>操作符
		vm.push_back (movie);
	ifs.close ();
	return true;
}
//写文件,把vector<Movie>& vm中数据写入文件
bool write (const char* file, const vector<Movie>& vm){
	ofstream ofs (file);
	if (! ofs) {
		perror ("打开排行文件失败");
		return false;
	}
	for (vector<Movie>::const_iterator it = vm.begin();
		it != vm.end (); ++it)
		ofs << *it << endl;  //调用重载的<<操作符
	ofs.close ();
	return true;
}
int main (int argc, char* argv[]) {
	//判断命令行参数个数是否合法
	if (argc < 3) {
		cerr << "用法:" << argv[0]
			<< " <票房文件> <排行文件>" << endl;
		return -1;
	}
	vector<Movie> vm;
	if (! read (argv[1], vm))
		return -1;
	sort (vm.begin (), vm.end ());  //对vm中元素排序
	if (vm.size () > 10)
		vm.resize (10);  //取排序前10个
	if (! write (argv[2], vm))
		return -1;
	return 0;
}




统计电影票房排名前10的电影并存入另一个文件

标签:stl   iterator   algorithm   vector   

原文地址:http://blog.csdn.net/liyuan_669/article/details/39348661

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!