Last active
March 2, 2016 17:31
-
-
Save Riketta/cbbeda4fc5f84e416095 to your computer and use it in GitHub Desktop.
dec2bin with stroke
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// version 3, final | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <cstdlib> // atoi | |
#include <iomanip> // set, setfill | |
#include <cmath> // pow | |
using namespace std; | |
const int base = 1000 * 1000 * 1000; | |
int mult(vector<int> &a, int b) // mult long * short | |
{ | |
int carry = 0; // offset | |
for (size_t i = 0; i < a.size() || carry; i++) | |
{ | |
if (i == a.size()) | |
a.push_back(0); | |
long long cur = carry + a[i] * 1ll * b; | |
a[i] = (int)(cur % base); | |
carry = (int)(cur / base); | |
} | |
//while (a.size() > 1 && a.back() == 0) | |
// a.pop_back(); | |
} | |
int intlen(int value) | |
{ | |
int len; | |
for (len = 1; value > 9; len++) | |
value /= 10; | |
return len; | |
} | |
void print(vector<int> &a) | |
{ | |
cout << (a.empty() ? 0 : a.back()); | |
for (int i = (int)a.size() - 2; i >= 0; i--) | |
cout << setw(8) << setfill('0') << a[i]; | |
cout << endl; | |
} | |
int main() | |
{ | |
cout << "Enter number:" << endl; | |
string raw; | |
cin >> raw; | |
string out = ""; | |
cout << "Line length: " << raw.length() << endl; | |
int count = 0; // digits before dot | |
// part before dot | |
cout << "Part before dot:" << endl; | |
for (int i = 0; i < raw.length() && raw[i] != '.'; i++) | |
{ | |
count++; | |
char digit[5] = "0000"; | |
int temp = raw[i] - '0'; | |
for (int j = 0; j < 4; j++) | |
{ | |
if (temp % 2 == 1) | |
digit[3 - j] = '1'; | |
else digit[3 - j] = '0'; | |
temp /= 2; | |
} | |
cout << digit << endl; | |
out += digit; | |
} | |
cout << "Result: " << out << "." << endl; | |
// now read part after dot | |
out += '.'; | |
cout << "Part after dot:" << endl; | |
string s = raw.substr(count + 1, raw.length() - 1); // string after dot. not best solution for reading long int | |
cout << "Length: " << s.length() << endl; | |
vector<int> a; | |
for (int i = (int)s.length(); i > 0; i -= 9) | |
if (i < 9) | |
a.push_back(atoi(s.substr(0, i).c_str())); | |
else | |
a.push_back(atoi(s.substr(i - 9, 9).c_str())); // cutting blocks of 9 | |
// now convert part after dot | |
int limit = 64; // max count of digits to convert | |
print(a); | |
for (int i = 0; i < limit; i++) | |
{ | |
int len = (intlen(a[a.size() - 1]) == 9 ? 0 : intlen(a[a.size() - 1])); | |
mult(a, 2); | |
int newlen = intlen(a[a.size() - 1]); | |
if (newlen - len == 1) // to remove first "bad" digit | |
a[a.size() - 1] %= (int)(pow(10, newlen - 1)); | |
print(a); | |
//cout << "Debug: " << newlen - len << endl; | |
out += (char)(newlen - len + '0'); | |
} | |
cout << out << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment