添加 '3.5.cpp'
This commit is contained in:
parent
07b018cb30
commit
293561d3b2
112
3.5.cpp
Normal file
112
3.5.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
#include "MySQLWrapper.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
#include "local_db_account.h"
|
||||
#include "util.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
/// 3.5 计算机专业编程语言职位需求量前十名饼状图
|
||||
|
||||
MySQLConn conn;
|
||||
conn.connect("127.0.0.1",db_user,db_passwd,"bigdata3",3306);
|
||||
cout<<"Connected to DB"<<endl;
|
||||
conn.exec("set names gbk",nullptr);
|
||||
|
||||
/// Based on TIOBE 2017
|
||||
vector<vector<string>> langs
|
||||
{
|
||||
{ "Java" },
|
||||
{ "C++" },
|
||||
{ "C#" },
|
||||
{ "Python", "Py" },
|
||||
{ "PHP" } ,
|
||||
{ "Javascript", "JS" },
|
||||
{ "Swift" },
|
||||
{ "Ruby" },
|
||||
{ "Perl" },
|
||||
{ "Visual Basic","VisualBasic","VB" },
|
||||
{ "Go" },
|
||||
{ "MATLAB" }
|
||||
};
|
||||
|
||||
map<string,int> mp;
|
||||
|
||||
int szLang=langs.size();
|
||||
for(int i=0;i<szLang;i++)
|
||||
{
|
||||
int szAlias=langs[i].size();
|
||||
for(int j=0;j<szAlias;j++)
|
||||
{
|
||||
ostringstream ostr;
|
||||
ostr<<"select count(*) from lagou_job where ((title like '%"
|
||||
<<langs[i][j]<<"%') or (tags like '%"
|
||||
<<langs[i][j]<<"%'))";
|
||||
|
||||
string sqlcmd=ostr.str();
|
||||
|
||||
auto fn1=[&](const string& SQLCMD) {
|
||||
conn.exec(SQLCMD,[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
int cc=ParseInt(val[0]);
|
||||
|
||||
cout<<SQLCMD<<" "<<langs[i][0]<<"-"<<cc<<endl;
|
||||
|
||||
auto iter=mp.find(langs[i][0]);
|
||||
if(iter!=mp.end())
|
||||
{
|
||||
iter->second += cc;
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.insert(make_pair(langs[i][0],cc));
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
conn.exec("use bigdata3",nullptr);
|
||||
fn1(sqlcmd);
|
||||
|
||||
ostr.str("");
|
||||
ostr<<"select count(*) from pages where ((title like '%"
|
||||
<<langs[i][j]<<"%') or (tags like '%"
|
||||
<<langs[i][j]<<"%'))";
|
||||
|
||||
sqlcmd=ostr.str();
|
||||
|
||||
conn.exec("use bigdata5",nullptr);
|
||||
fn1(sqlcmd);
|
||||
}
|
||||
}
|
||||
|
||||
vector<pair<string,int>> vec;
|
||||
vec.insert(vec.end(),mp.begin(),mp.end()); /// ??? Not Naive Code?
|
||||
sort(vec.begin(),vec.end(),[](const pair<string,int>& a,const pair<string,int>& b)
|
||||
{
|
||||
return a.second > b.second;
|
||||
});
|
||||
|
||||
ofstream ofs("result/3.5.csv");
|
||||
#define cout ofs
|
||||
int szLoop=min(10u,vec.size());
|
||||
for(int i=0;i<szLoop;i++)
|
||||
{
|
||||
cout<<vec[i].first<<","<<vec[i].second<<endl;
|
||||
}
|
||||
#undef cout
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user