Styleguide/Scripts: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
Cbarr (talk | contribs)
Cbarr (talk | contribs)
Line 941: Line 941:
==Google currency conversion mining code==
==Google currency conversion mining code==
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

long calculateDigits(double d)
{
long result = 0;
long temp = d;
long place = 1;

while(temp/place)
{
place *= 10;
result++;
}
return(result);
}

int main()
{
string website = "http://www.google.com/finance/";
string file = "converter?a=1&from=USD&to=";
string currency;

ofstream out;
ifstream currencyFile;

out.open("Google/ratesGoogle.txt");
currencyFile.open("currencyCodesAll.txt");

while(currencyFile >> currency)
{
string command = "wget '" + website + file + currency
+ "' && grep 'currency_converter_result' '"
+ file + currency + "' > 'resultGoogle.grep'";

system(command.c_str());
ifstream in("resultGoogle.grep");
string result;

getline(in,result,'\n');

in.close();

system("rm resultGoogle.grep");

result.erase(0,58);

bool remove = false;
for(long i = 0 ; i < result.size() ; i++)
{
if(result[i] == ' ')
remove = true;
if(remove)
result[i] = ' ';
}

if(result != "")
out << currency << " " << result << endl;
}
system("rm conv*");
out.close();
currencyFile.close();

vector<string> currencyList;
vector<double> rateList;

double rate;

ifstream in("Google/ratesGoogle.txt");

while(in >> currency && in >> rate)
{
rate = ceil(rate*100)/100.0;
currencyList.push_back(currency);
rateList.push_back(rate);
}

in.close();

unsigned options[16] = {1,2,3,5,10,15,20,25,30,35,50,75,100,150,200,250};

vector <vector <double> > currencyOptions;
currencyOptions.resize(rateList.size());

for(long i = 0 ; i < currencyOptions.size() ; i++)
{
currencyOptions[i].resize(16);
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
currencyOptions[i][j] = rateList[i]*options[j];
}
}

// ===================== raw table ==============================
out.open("Google/currencyOptionsRawGoogle.csv");

out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;

for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << currencyOptions[i][j] << ", ";
}
out << endl;
}

out.close();

// ================== rounded to nearest dollar ====================

for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
currencyOptions[i][j] = ceil(currencyOptions[i][j]);

out.open("Google/currencyOptionsRoundedGoogle.csv");

out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;

for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
out << endl;
}

out.close();

// =================== zero bottom 1% digits =========================
double temp;
long digits;

for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
temp = currencyOptions[i][j];
digits = calculateDigits(temp);

if(digits < 3)
{}
else
{
temp /= (long)(pow(10,digits-3));
temp = ceil(temp);
temp *= (long)(pow(10,digits-3));
currencyOptions[i][j] = (long)temp;
}
}

out.open("Google/currencyOptionsZeroBottomGoogle.csv");

out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;

for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
out << endl;
}

out.close();

// =================== zero bottom 1% digits 5-ify =========================

long ones;

for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
temp = currencyOptions[i][j];
digits = calculateDigits(temp);

if(digits == 1)
{
//temp /= 5;
//temp = round(temp);
//temp *= 5;
//currencyOptions[i][j] = temp;
}
else
{
if(digits > 3)
temp /= (long)(pow(10,digits-3));

ones = (long)(temp)%10;
temp = temp - ones;
if(ones > 0 and ones <= 5)
{
temp += 5;
}
else if(ones > 6 and ones <= 9)
{
temp += 10;
}
if(digits > 3)
temp *= (long)(pow(10,digits-3));

currencyOptions[i][j] = (long)temp;
}
}

out.open("Google/currencyOptions5ifyGoogle.csv");

out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;

for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
out << endl;
}

out.close();

return 0;
}

</syntaxhighlight>
</syntaxhighlight>