Author Topic: Decimal to roman numbers converter  (Read 10139 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Decimal to roman numbers converter
« Reply #45 on: September 02, 2020, 12:12:57 am »
@bplus great! :)
You documented my part of my code very well... I will need your help in future. LOL
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Decimal to roman numbers converter
« Reply #46 on: September 02, 2020, 01:06:09 am »
This is my approach for converting Roman into Arabic Number System.

Code: QB64: [Select]
  1. _TITLE "Roman To Arabic Convertor : By Ashish" 'Roman To Arabic Convertor : By Ashish  [2 Sep, 2020]
  2. 'NOTE:RomanToArabic~&() function assume that roman number passed to it does not have any error (except for non-roman literal)
  3.     INPUT "> ", roman$
  4.     PRINT RomanToArabic~&(roman$)
  5. LOOP UNTIL roman$ = ""
  6.  
  7. FUNCTION RomanToArabic~& (r$)
  8.     DIM x~%, y~%, mult AS _BYTE
  9.     y~% = 9
  10.     FOR i~& = 1 TO LEN(r$)
  11.         x~% = INSTR("IVXLCDM ", UCASE$(MID$(r$, i~&, 1)))
  12.         IF x~% = 0 THEN
  13.             PRINT "ERROR": EXIT FUNCTION
  14.         ELSE
  15.             IF LEN(r$) = 1 AND i~& = 1 THEN
  16.                 RomanToArabic~& = 2 ^ (INT((x~% - 1) / 2)) * 5 ^ (INT(x~% / 2)): EXIT FUNCTION
  17.             ELSE
  18.                 IF x~% = 8 THEN
  19.                     mult = 1
  20.                 ELSE
  21.                     IF mult = 1 THEN
  22.                         RomanToArabic~& = RomanToArabic~& * (2 ^ (INT((x~% - 1) / 2)) * 5 ^ (INT(x~% / 2)))
  23.                         mult = 0
  24.                     ELSE
  25.                         RomanToArabic~& = RomanToArabic~& + (2 ^ (INT((x~% - 1) / 2)) * 5 ^ (INT(x~% / 2)))
  26.                         IF y~% < x~% THEN RomanToArabic~& = RomanToArabic~& - 2 * (2 ^ (INT((y~% - 1) / 2)) * 5 ^ (INT(y~% / 2)))
  27.                     END IF
  28.                 END IF
  29.             END IF
  30.         END IF
  31.         IF x~% <> 8 THEN y~% = x~%
  32.     NEXT
  33.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Decimal to roman numbers converter
« Reply #47 on: September 02, 2020, 01:30:16 am »
This is my approach for converting Roman into Arabic Number System.

Code: QB64: [Select]
  1.         x~% = INSTR("IVXLCDM ", UCASE$(MID$(r$, i~&, 1)))
  2.  

This line is artistic

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal to roman numbers converter
« Reply #48 on: September 02, 2020, 03:07:55 am »
Nice work! @Ashish

I ran some tests and looked good, I will study what you did tomorrow.