216 lines
3.9 KiB
C++
216 lines
3.9 KiB
C++
//---------------------------------------------------------------------------
|
|
#include <iostream>
|
|
#include <stdio.h>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <list>
|
|
#include <algorithm>
|
|
typedef unsigned char UCHAR;
|
|
using namespace std;
|
|
//---------------------------------------------------------------------------
|
|
|
|
string sCode;
|
|
|
|
// Ôàêòîðèçàöèÿ ÷èñëà
|
|
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... íå ïåðåâîäèòñÿ =)
|
|
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;
|
|
}
|
|
|
|
// òàêæå ïåðåâîäèòñÿ ñ òðóäîì, íî ýòà ôóíêöèÿ äåëàåò ÷òî-òî ïðèêîëüíîå .)
|
|
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;
|
|
}
|
|
|
|
|