![]() |
|
[Challenge: 4] :Roman and decimal - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: [Challenge: 4] :Roman and decimal (/Thread-Challenge-4-Roman-and-decimal) |
[Challenge: 4] :Roman and decimal - Vi-Sion - 12-04-2016 In this challenge you'll have to do two functions : The first function called binaryToRoman,it converts numbers from binary to roman. The second function called romanToBinary,it converts numbers from roman to binary. You'll get 50 points for each function done,good luck it's easy
RE: [Challenge: 4] :Roman and decimal - Pikami - 12-04-2016 (12-04-2016, 10:11 AM)Vi-Sion Wrote: In this challenge you'll have to do two functions :The challenges are getting harder I see ![]() Here's my solution in C++: Code: #include <iostream>
#include <string>
struct romanNumeral{
std::string roman;
int decimal;
};
std::string binaryToRoman(long n, romanNumeral R[]){
//Convert binary to decimal
long factor = 1;
long decimal = 0; //The new decimal number
while (n != 0){
decimal += (n%10) * factor;
n /= 10;
factor *= 2;
}
//Convert decimal to roman
std::string result = "";
for(int i=12;i>=0;i--){
while(decimal>=R[i].decimal){
result+=R[i].roman;
decimal-=R[i].decimal;
}
}
//
return result;
}
long romanToBinary(std::string roman,romanNumeral R[]){
//Convert roman to decimal
int result=0;
while(roman.length()>0){
//find biggest numeral from left
char c1=roman[0] ,c2=roman[1];
int i1=-1,i2=-1;
for(int i=0;i<13;i++){
if(R[i].roman.length()==1 && R[i].roman[0]==c1)i1=i;
else if(R[i].roman.length()==2 && R[i].roman[0]==c1 && R[i].roman[1]==c2)i2=i;
}
if(i2!=-1){
roman.erase(0,2);
result+=R[i2].decimal;
} else {
roman.erase(0,1);
result+=R[i1].decimal;
}
}
//Convert decimal to binary
long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (result!=0)
{
remainder = result%2;
result /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
int main(){
//Build an array of roman numerals
romanNumeral R[13] = {
"I", 1,
"IV", 4,
"V", 5,
"IX", 9,
"X", 10,
"XL", 40,
"L", 50,
"XC", 90,
"C", 100,
"CD", 400,
"D", 500,
"CM", 900,
"M", 1000
};
//Do the stuff
long num;
std::cout<<"Enter the binary number(1s and 0s) : ";
std::cin>>num;
std::string roman = binaryToRoman(num, R);
std::cout<<"binaryToRoman("<<num<<") = "<<roman<<std::endl;
std::cout<<"romanToBinary("<<roman<<") = "<<romanToBinary(roman, R)<<std::endl;
return 0;
}![]() I'm sure there's a better way, but whatever... If someone want's to criticize my code I'll gladly take it. RE: [Challenge: 4] :Roman and decimal - Vi-Sion - 12-04-2016 @Pikami well done my friend one thing i have to say you always forgot about the input restriction,user should only be allowed to use 0 and 1 in binary number, fix this you'll have the +100. RE: [Challenge: 4] :Roman and decimal - bitm0de - 12-04-2016 Here's a solution in C: Code: {buggy code removed}RE: [Challenge: 4] :Roman and decimal - bitm0de - 12-04-2016 (12-04-2016, 12:51 PM)Pikami Wrote:(12-04-2016, 10:11 AM)Vi-Sion Wrote: In this challenge you'll have to do two functions :The challenges are getting harder I see What? Code: Enter the binary number(1s and 0s) : 101101001000
binaryToRoman(2147483647) = MMDLIX
romanToBinary(MMDLIX) = 1326863303^ 101101001000 == 2888 -> "MMDCCCLXXXVIII" You'll have to correct this. RE: [Challenge: 4] :Roman and decimal - Vi-Sion - 12-04-2016 @bitmode can you pls screenshot a test of some values in your code ? try 110011 101 1000101 (12-04-2016, 02:26 PM)bitm0de Wrote: 101101001000 == 2888 -> "MMDCCCLXXXVIII"that's true sry i didn't notice this thanks @bitmode. @Pikami you have some work to be done on your code. RE: [Challenge: 4] :Roman and decimal - Pikami - 12-04-2016 (12-04-2016, 02:26 PM)bitm0de Wrote: What?I used the type long and long has a limit of 2147483647, my code is fixed now, see the code below (12-04-2016, 03:02 PM)Vi-Sion Wrote: @bitmode can you pls screenshot a test of some values in your code ? Here's an updated version of my code: Code: #include <iostream>
#include <string>
struct romanNumeral{
std::string roman;
int decimal;
};
std::string binaryToRoman(unsigned long long n, romanNumeral R[]){
//Convert binary to decimal
unsigned long long factor = 1;
unsigned long long decimal = 0; //The new decimal number
while (n != 0){
decimal += (n%10) * factor;
n /= 10;
factor *= 2;
}
//Convert decimal to roman
std::string result = "";
for(int i=12;i>=0;i--){
while(decimal>=R[i].decimal){
result+=R[i].roman;
decimal-=R[i].decimal;
}
}
//
return result;
}
unsigned long long romanToBinary(std::string roman,romanNumeral R[]){
//Convert roman to decimal
unsigned long long result=0;
while(roman.length()>0){
//find biggest numeral from left
char c1=roman[0] ,c2=roman[1];
int i1=-1,i2=-1;
for(int i=0;i<13;i++){
if(R[i].roman.length()==1 && R[i].roman[0]==c1)i1=i;
else if(R[i].roman.length()==2 && R[i].roman[0]==c1 && R[i].roman[1]==c2)i2=i;
}
if(i2!=-1){
roman.erase(0,2);
result+=R[i2].decimal;
} else {
roman.erase(0,1);
result+=R[i1].decimal;
}
}
//Convert decimal to binary
unsigned long long binaryNumber = 0;
unsigned long long remainder, i = 1, step = 1;
while (result!=0)
{
remainder = result%2;
result /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
int main(){
//Build an array of roman numerals
romanNumeral R[13] = {
"I", 1,
"IV", 4,
"V", 5,
"IX", 9,
"X", 10,
"XL", 40,
"L", 50,
"XC", 90,
"C", 100,
"CD", 400,
"D", 500,
"CM", 900,
"M", 1000
};
//Do the stuff
unsigned long long num;
std::cout<<"Enter the binary number(1s and 0s) : ";
std::cin>>num;
std::string roman = binaryToRoman(num, R);
std::cout<<"binaryToRoman("<<num<<") = "<<roman<<std::endl;
std::cout<<"romanToBinary("<<roman<<") = "<<romanToBinary(roman, R)<<std::endl;
return 0;
}
RE: [Challenge: 4] :Roman and decimal - bitm0de - 12-04-2016 Fixed a few issues with my previous code: ![]() I can't believe I got it that far at 5am in the morning lol. Here's the new version I've corrected this morning. Code: #include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <assert.h>
#define UNUSED(x) (void)(x)
#define BIT_COUNT(x) (sizeof(x) * CHAR_BIT)
/* won't need more than 16 chars (+1 for null-byte) */
#define ROMAN_NUMERAL_BUFFER_SIZE 17
static const int numerals_len[9] = { 1, 2, 3, 2, 1, 2, 3, 4, 2 };
static const char *numerals[27] =
{
"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX",
"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
};
uint32_t binstr_to_uint32(const char *bin)
{
uint32_t v = 0;
size_t e = strlen(bin);
while (e--)
{
assert(*bin == '0' || *bin == '1');
v += (*bin++ - '0') << e;
}
return v;
}
int uint32_to_binstr(uint32_t value, char *bin, size_t len)
{
size_t l = BIT_COUNT(value) - 1;
int flag = 0;
char *p = bin;
do
{
if (BIT_COUNT(value) - l - 1 >= len) return 0;
if ((value >> l) & 1) *p++ = '1', flag = 1;
else if (flag) *p++ = '0';
}
while (l-- > 0);
*p = 0;
return 1;
}
void bin_to_roman(const char *bin, char *roman)
{
uint32_t value = binstr_to_uint32(bin);
assert(value && value < 5000); /* roman numerals limit standard is 4999 */
while (value >= 1000) { *roman++ = 'M'; value -= 1000; }
if (value / 100)
{
strcpy(roman, numerals[(value / 100) + 17]);
roman += numerals_len[(value / 100) - 1];
value %= 100;
}
if (value / 10)
{
strcpy(roman, numerals[(value / 10) + 8]);
roman += numerals_len[(value / 10) - 1];
value %= 10;
}
if (value)
{
strcpy(roman, numerals[value - 1]);
roman += numerals_len[value - 1];
}
*roman = 0;
}
int roman_to_bin(const char *roman, char *bin, size_t len)
{
int i = 26, e = 100;
uint32_t value = 0;
while (*roman == 'M') { value += 1000; ++roman; }
while (i > -1)
{
if (strncmp(roman, numerals[i], numerals_len[i % 9]) == 0)
{
value += ((i % 9) + 1) * e;
roman += numerals_len[i % 9];
}
if (i-- % 9 == 0) e /= 10;
}
return uint32_to_binstr(value, bin, len);
}
int main(void)
{
uint32_t value;
char bin[BIT_COUNT(value) + 1];
char roman[ROMAN_NUMERAL_BUFFER_SIZE];
const int offset = 33;
const uint32_t limit = 4999;
for (uint32_t i = 1; i <= limit; ++i)
{
if (!uint32_to_binstr(i, bin, sizeof(bin)))
{
fputs("ERORR: uint32_to_binstr failed.\n", stderr);
return 1;
}
bin_to_roman(bin, roman);
printf("%*s -> %*s| ", offset, bin, offset, roman);
roman_to_bin(roman, bin, sizeof(bin));
printf("%*s -> %*s\n", offset, roman, offset, bin);
}
}Result: 1 - 1999 - http://pastebin.com/raw/y226Vkkr 2000 - 4999 - http://pastebin.com/raw/7nyWkJb7 ^ Tons of values to sample. Next challenge? RE: [Challenge: 4] :Roman and decimal - Vi-Sion - 12-07-2016 @Pikami working good you just have to fix one thing for the points in the input user shouldn't be allowed to input letters or numbers other then 0 1 id the input contains something other then 0 1 it should tell the user to only enter 0 and 1 . put since you've done what's most important i'll give you the points, +100 for you. @bitm0de good job,you didn't let user input the value instead you've generated values from 1 to 4999 but that's fine since you've done the hard part working onthis is ez so +100 |