当前位置:网站首页>统计电影票房排名前10的电影并存入还有一个文件
统计电影票房排名前10的电影并存入还有一个文件
2022-07-07 21:50:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
今天看到一个笔试题,是这种:给定一个文件(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;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116208.html原文链接:https://javaforall.cn
边栏推荐
- iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
- FPGA基础篇目录
- One question per day - pat grade B 1002 questions
- [untitled] reprint melting ice - track icedid server with a few simple steps
- I wish you all the best and the year of the tiger
- Install a new version of idea. Double click it to open it
- V20变频器手自动切换(就地远程切换)的具体方法示例
- Brush question 6
- 消息队列与快递柜之间妙不可言的关系
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
猜你喜欢

Wechat forum exchange applet system graduation design completion (8) graduation design thesis template

Matlab-SEIR传染病模型预测

Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~

V20变频器手自动切换(就地远程切换)的具体方法示例

聊聊 Dart 的空安全 (null safety) 特性

leetcode-520. 检测大写字母-js

Inftnews | the wide application of NFT technology and its existing problems

微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT

聊聊支付流程的设计与实现逻辑

Brush question 3
随机推荐
Network security sqlmap and DVWA explosion
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
Database daily question --- day 22: last login
About idea cannot find or load the main class
USB(十四)2022-04-12
Advantages and disadvantages of rest ful API
Why does the market need low code?
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
ArcGIS: two methods of attribute fusion of the same field of vector elements
Cases of agile innovation and transformation of consumer goods enterprises
Inftnews | the wide application of NFT technology and its existing problems
Brush question 4
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
JMeter-接口自动化测试读取用例,执行并结果回写
Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
Exploratory data analysis of heartbeat signal
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors