I've started caring about standards and the beauty of my code. I doubt whether I've written it correctly and aesthetically. Can you please judge this simple, exemplary program? I want to know of any mistakes I've made. It converts an integer number given in the argument to binary, hexadecimal and octal number. Ignore the "1." prefix.
1.conversion_main.cpp:
//Conversion
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include "1.conversion_header.h"
#include "1.convert.cpp"
int parse(int, char**);
int main(int argc, char *argv[]){
int number = parse(argc, argv);
if (number == 0)
return 0;
convert converter;
std::cout << "HEX: " << converter.to_hex(number) << std::endl;
std::cout << "OCT: " << converter.to_oct(number) << std::endl;
std::cout << "BIN: " << converter.to_bin(number) << std::endl;
return 0;
}
int parse(int argc, char* argv[]){
if (argc == 2){
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0)
|| (strcmp(argv[1], "/?")) == 0){
std::cout << "Usage: a R\\{0}\n";
}
else{
int check;
std::istringstream in(argv[1]);
if ((in >> check) && (in.eof()))
return atoi(argv[1]);
}
}
return 0;
}
1.conversion_header.h:
//Conversion header
class convert{
public:
std::string to_hex(int);
std::string to_oct(int);
std::string to_bin(int);
protected:
std::string int_to_bin(int);
std::string reverse(std::string);
};
1.convert.cpp:
//Convert definition
#include <string>
std::string convert::int_to_bin(int number){
static std::string result;
static int level = 0;
level++;
if (number > 0){
if (number % 2 == 0){
result.append("0");
}
else{
result.append("1");
}
int_to_bin(number / 2);
level--;
}
if (level == 1) return reverse(result);
return result;
}
std::string convert::reverse(std::string to_reverse){
std::string result;
for (int i = to_reverse.length()-1; i >=0 ; i--)
result += to_reverse[i];
return result;
}
std::string convert::to_hex(int to_convert){
std::string result;
std::stringstream ss;
ss << std::hex <<to_convert;
ss >> result;
return result;
}
std::string convert::to_oct(int to_convert){
std::string result;
std::stringstream ss;
ss << std::oct << to_convert;
ss >> result;
return result;
}
std::string convert::to_bin(int to_convert){
return int_to_bin(to_convert);
}
std::cout << std::hex << num;
for hex \$\endgroup\$