-
Notifications
You must be signed in to change notification settings - Fork 0
/
dec2bin.cpp
43 lines (39 loc) · 1.23 KB
/
dec2bin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <gmpxx.h>
using namespace std;
void validate(string n) {
if(n.length() == 0) throw 2; // empty parameter case : ""
else if(n.length()>0 && n[0] == '-') throw 3;
for (int i = 0; i < n.length(); i++) {
if(!isdigit(n[i])) throw 4; // invalid cases : "+2" or "0xA" or "1,2" or "3.4" or "5.6e+07" ...
}
}
string dec2bin(string n) {
mpz_class mpz_n;
mpz_n.set_str(n, 10); // used instead of mpz_n(n) to avoid octal notation, i.e. numbers starting with one or more zeros : "016" ...
if(mpz_n == 0) return "0"; // zero cases : "0" or "00" ...
n = "";
while (mpz_n > 0) {
n += (mpz_n % 2) == 0 ? "0" : "1";
mpz_n = mpz_n / 2;
}
reverse(n.begin(), n.end());
return n;
}
int main(int argc, char *argv[]) {
try
{
if(argc > 1) {
validate(argv[1]);
cout << dec2bin(argv[1]) << endl;
} else {
throw 1;
}
} catch (int e) {
cout << "An exception occurred. (Exception number: " << e << ")" << endl;
cout << "Usage : " << argv[0] << " <number consisting of digits only>" << endl;
return e;
}
return 0;
}