dev/bash/bff/kt2bf.cpp

216 lines
3.9 KiB
C++
Raw Normal View History

2011-04-07 17:03:45 +04:00
//---------------------------------------------------------------------------
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
typedef unsigned char UCHAR;
using namespace std;
//---------------------------------------------------------------------------
string sCode;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
vector<int> fucktorize(int numm)
{
if (numm == 1) {
vector<int> v;
v.push_back(1);
return v;
}
int newnum = numm;
vector<int> newtext;
int checker = 2; // First possible factor to check
while (checker *checker <= newnum)
if (newnum % checker == 0) {
newtext.push_back(checker);
newnum = newnum / checker;
} else
checker++;
if (newnum != 1)
newtext.push_back(newnum);
return newtext;
}
// fuck funcktors... <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> =)
void fuckfucktors(const vector<int>& fucktors, const int i, UCHAR op = 0)
{
if (op == 0)
op = '+';
UCHAR notop = (op == '+') ? '-' : '+';
if (i != 0)
sCode += "[>";
UCHAR tmpop;
if (i != (int)fucktors.size() - 1)
tmpop = '+';
else
tmpop = op;
for (int j = 1; j <= fucktors[i]; j++)
sCode += tmpop;
if (i + 1 < (int)fucktors.size())
fuckfucktors(fucktors, i + 1, op);
if (i != 0)
sCode += "<-] ";
}
int min_val(const string& s)
{
if (s == "")
return -1;
int m = (UCHAR)s[0];
for (size_t i = 1, max_i = s.size(); i < max_i; i++)
if (m > (UCHAR)s[i])
m = (UCHAR)s[i];
return m;
}
int max_val(const string& s)
{
if (s == "")
return -1;
int m = (UCHAR)s[0];
for (size_t i = 1, max_i = s.size(); i < max_i; i++)
if (m < (UCHAR)s[i])
m = (UCHAR)s[i];
return m;
}
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>-<2D><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> .)
string brainfuckit(const string &str)
{
string aStr, aStrPrinted;
sCode = "";
if (str == "")
return "";
for (size_t i = 0, max_i = str.length(); i < max_i; i++)
aStr += str[i];
int iMinvalue = min_val(aStr); // *std::min_element(aStr.begin(), aStr.end());
vector<int> fucktors = fucktorize(iMinvalue);
int residuo = 0;
if (fucktors.size() == 1) {
fucktors = fucktorize(iMinvalue - 1);
residuo = 1;
}
fuckfucktors(fucktors, 0);
sCode += " ";
for (size_t i = 0, max_i = fucktors.size() - 1; i < max_i; i++)
sCode += ">";
for (int i = 0; i < residuo; i++)
sCode += "+";
for (size_t i = 0, max_i = aStr.length(); i < max_i; i++) {
if ((UCHAR)aStr[i] == iMinvalue) {
sCode += ". ";
continue;
}
if ((UCHAR)aStr[i] < iMinvalue) {
int x = iMinvalue - (UCHAR)aStr[i];
vector<int> ff = fucktorize(x);
int res = 0;
if ((ff.size() == 1) && (x != 1)) {
x = x - 1;
ff = fucktorize(x);
res = 1;
}
int maxFucktor = *std::max_element(ff.begin(), ff.end());
int complement = x / maxFucktor;
sCode += '<';
vector<int> tmp_v;
tmp_v.push_back(maxFucktor);
tmp_v.push_back(complement);
fuckfucktors(tmp_v, 0, '-');
sCode += ">";
for (int k = 0; k < res; k++)
sCode += "-";
sCode += ". ";
iMinvalue -= iMinvalue - (UCHAR)aStr[i];
continue;
}
if ((UCHAR)aStr[i] > iMinvalue) {
int x = (UCHAR)aStr[i] - iMinvalue;
vector<int> ff = fucktorize(x);
int res = 0;
if ((ff.size() == 1) && (x != 1)) {
x = x - 1;
ff = fucktorize(x);
res = 1;
}
int maxFucktor = *std::max_element(ff.begin(), ff.end());
int complement = x / maxFucktor;
sCode += '<';
vector<int> tmp_v;
tmp_v.push_back(maxFucktor);
tmp_v.push_back(complement);
fuckfucktors(tmp_v, 0, '+');
sCode += ">";
for (int k = 0; k < res; k++)
sCode += "+";
sCode += ". ";
iMinvalue += (UCHAR)aStr[i] - iMinvalue;
continue;
}
}
return sCode;
}
int main(int argc, char *argv[])
{
if (argc != 2) {
cout << "Need string as a parameter to program" <<endl;
cout << "Exiting..." << endl;
return -1;
}
string bff_code = brainfuckit(argv[1]);
cout << bff_code <<endl;
// system("pause");
return 0;
}