当前位置:网站首页>Count the top 10 films at the box office and save them in another file
Count the top 10 films at the box office and save them in another file
2022-07-07 23:20:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Today I saw a written test question , It's this : Given a file (m.dat). It saves the box office statistics of various films . The format is as follows :
《2012》 SONY 769.7 《 Harry Potter and the Deathly Hallows ( On )》 Warner Brothers 952.0 《 Star Wars: 》 Twentieth century fox 775.4 《 Shrek 4》 Paramount / DreamWorks 750.0 《 "Avatar" 》 Twentieth century fox 2,782.2 《 Harry Potter and the Goblet of Fire 》 Warner Brothers 895.9 《 Harry Potter and the Half-Blood Prince 》 Warner Brothers 934.0 《 The Lord of the rings 2: Twin towers 》 New line 925.3 《 Batman prequel 2: The dark knight 》 Warner Brothers 1,001.9 《 Harry Potter and the Sorcerer's Stone 》 Warner Brothers 974.7 《 Finding Nemo 》 Disney 867.9 《 Kung Fu Panda 》 Paramount / DreamWorks 631.7 《 Pirates of the Caribbean 3: The end of the world 》 Disney 961.0 《 Harry Potter and the prisoner of Azkaban 》 Warner Brothers 795.6 《E.T.》 World 792.9 《 Raiders of the lost ark 4: Crystal skeleton Kingdom 》 Paramount 786.6 《 The Lord of the rings 3: Return of the king 》 New line 1,119.1 《 Shrek 2》 DreamWorks 919.8 《 Toy Story Mania 3》 Disney 1,063.2 《 The matrix 2: regroup 》 Warner Brothers 742.1
.....
..
It is required to knock the code to count the top box office 10 In the movie . And save the statistical results into another file . Try to use it yourself C++ Achieve it , Code sharing, such as the following :(linux Next gcc compile )
gcc compile , Two command line parameters should be passed in at run time , example :./a.out m.dat li.dat (m.dat Is the source box office file .li.dat Before storage 10 The file of )
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class Movie {
public:
// Overload input operation
friend istream& operator>> (istream& is,
Movie& movie) {
return is >> movie.m_title >> movie.m_comp
>> movie.m_gross;
}
// Overload output operation
friend ostream& operator<< (ostream& os,
const Movie& movie) {
return os << movie.m_title << ' '
<< movie.m_comp << ' ' << movie.m_gross;
}
// Overload less than sign , be used for List Sort
bool operator< (const Movie& movie) const {
return gross () > movie.gross ();
}
private:
// Read from the file string Convert to double return
double gross (void) const {
string str (m_gross);
size_t pos = 0;
while ((pos = str.find_first_of ("$,", pos)) != // Remove the front of the box office "$" and ","
string::npos)
str.erase (pos, 1);
return atof (str.c_str ());
}
string m_title; // The movie name
string m_comp; // Company name
string m_gross; // box office
};
// Reading documents , The read results are stored in Vector<Movie>& vm
bool read (const char* file, vector<Movie>& vm) {
ifstream ifs (file);
if (! ifs) {
perror (" Failed to open box office file ");
return false;
}
Movie movie;
while (ifs >> movie) // Call overloaded >> The operator
vm.push_back (movie);
ifs.close ();
return true;
}
// Writing documents , hold vector<Movie>& vm Write data to file
bool write (const char* file, const vector<Movie>& vm){
ofstream ofs (file);
if (! ofs) {
perror (" Failed to open ranking file ");
return false;
}
for (vector<Movie>::const_iterator it = vm.begin();
it != vm.end (); ++it)
ofs << *it << endl; // Call overloaded << The operator
ofs.close ();
return true;
}
int main (int argc, char* argv[]) {
// Infer whether the number of command line parameters is legal
if (argc < 3) {
cerr << " Usage method :" << argv[0]
<< " < Box office documents > < Ranking file >" << endl;
return -1;
}
vector<Movie> vm;
if (! read (argv[1], vm))
return -1;
sort (vm.begin (), vm.end ()); // Yes vm Order of elements in
if (vm.size () > 10)
vm.resize (10); // Before sorting 10 individual
if (! write (argv[2], vm))
return -1;
return 0;
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116208.html Link to the original text :https://javaforall.cn
边栏推荐
- Network security - phishing
- 成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
- ./ setup. Insufficient sh permission
- FreeLink开源呼叫中心设计思想
- LeeCode -- 6. Z 字形变换
- Statistical method for anomaly detection
- Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
- ROS2专题(03):ROS1和ROS2的区别【02】
- ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
- Introduction to anomaly detection
猜你喜欢

LeeCode -- 6. Z 字形变换

When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK

微信论坛交流小程序系统毕业设计毕设(1)开发概要

微信论坛交流小程序系统毕业设计毕设(3)后台功能

Wechat forum exchange applet system graduation design (5) assignment
![[microservices SCG] gateway integration Sentinel](/img/f3/410d7228b4b253ebf41015a785099f.png)
[microservices SCG] gateway integration Sentinel

十四、数据库的导出和导入的两种方法

re1攻防世界逆向

U盘拷贝东西时,报错卷错误,请运行chkdsk

Matlab 信号处理【问答随笔·2】
随机推荐
Installing spss25
Redhat下安装fedora
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
Why does the market need low code?
经纬度PLT文件格式说明
Transform XL translation
智慧社區和智慧城市之間有什麼异同
Unity3D学习笔记6——GPU实例化(1)
VS扩展工具笔记
十三、系统优化
网络安全-永恒之蓝
ArcGIS:矢量要素相同字段属性融合的两种方法
智慧社区和智慧城市之间有什么异同
Network security - install CentOS
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
网络安全-CSRF
网络安全-钓鱼
Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
LeeCode -- 6. Zigzag transformation
Kubernetes' simplified data storage storageclass (creation, deletion and initial use)