LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Roman Numeral Conversion (https://www.linuxquestions.org/questions/slackware-14/roman-numeral-conversion-4175482477/)

tronayne 10-28-2013 09:47 AM

Roman Numeral Conversion
 
Anybody know of a little utility to convert Roman numerals to Arabic numerals; type in the Roman, get the Arabic?

Checking around there's a nice looking (but will not compile) library at SourceForge, couple of Windowish things, none of which compile or run without a lot of twiddling and fiddling.

Can't use a web-based converter (there's a really nice one at http://www.onlineconversion.com/roma...s_advanced.htm but it can't be used at the site, no Internet connection).

A little click-'n'-drool thing would be the berries (bunch of Windows folk that don't like command line too much) and I'm just not too capable with Java or some such. Heck, ncurses would be good enough.

The conversion is trivial, but the simple-to-use part without opening a terminal window ain't.

STDOUBT 10-28-2013 10:00 AM

This one appears not to require a connection.
http://www.novaroma.org/via_romana/numbers.html

Just need a browser to display the page.

Didier Spaier 10-28-2013 10:28 AM

What's funny about your question is that word algorithm is derived from that of Persian mathematician عَبْدَالله مُحَمَّد بِن مُوسَى اَلْخْوَارِزْمِي‎ (in English Al-Khawarizmi), who introduced in Middle East then in Europe the so-called arabic numerals.

Also, word algebra is taken from part of the Arabic title of his best known book.

PS Now I'm sure NSA is watching me ;)

tronayne 10-28-2013 10:49 AM

Quote:

Originally Posted by STDOUBT (Post 5053723)
This one appears not to require a connection.
http://www.novaroma.org/via_romana/numbers.html

Just need a browser to display the page.

Oh, yeah! That doit toit, thanks!

Now all I have to do is get permission...

tronayne 10-28-2013 10:52 AM

Quote:

Originally Posted by Didier Spaier (Post 5053742)
What's funny about you question is that word algorithm is derived from that of Persian mathematician عَبْدَالله مُحَمَّد بِن مُوسَى اَلْخْوَارِزْمِي‎ (in English Al-Khawarizmi), who introduced in Middle East then in Europe the so-called arabic numerals.

Also, word algebra is taken from part of the Arabic title of his best known book.

PS Now I'm sure NSA is watching me ;)

You know, I'm not all that sure that NSA gives a hoot about any of us. Just a bunch of weird computer crazies and all that. :rolleyes:

Thanks for input.

perbh 10-28-2013 10:38 PM

The problem intrigued me - so I wrote a li'l c-program you can run from the command line ... so be my guest!
https://dl.dropboxusercontent.com/u/...19/roman2bin.c

Sadly, it doesn't do a reverse conversion :-(

volkerdi 10-29-2013 12:44 AM

Quote:

Originally Posted by perbh (Post 5054161)
The problem intrigued me - so I wrote a li'l c-program you can run from the command line ... so be my guest!
https://dl.dropboxusercontent.com/u/...19/roman2bin.c

Sadly, it doesn't do a reverse conversion :-(

Oh, you need something to go along with this to do Arabic to Roman conversion? Here's a handy little program for that:

Code:

      IDENTIFICATION DIVISION.
      PROGRAM-ID. roman.
      DATA DIVISION.
      WORKING-STORAGE SECTION.
      01 ARRAY.
        02 A PIC 9(4) OCCURS 7 TIMES.
        02 B PIC X OCCURS 7 TIMES.
      77 N PIC 9(4).
      77 I PIC 9(2).
      77 J PIC 9(2).
      77 Q PIC 9(3).
      77 R PIC 9(3) VALUE 1.
      PROCEDURE DIVISION.
      MAIN-PARA.
          MOVE 1000 TO A(1).
          MOVE 500 TO A(2).
          MOVE 100 TO A(3).
          MOVE 50 TO A(4).
          MOVE 10 TO A(5).
          MOVE 5 TO A(6).
          MOVE 1 TO A(7).
          MOVE "M" TO B(1).
          MOVE "D" TO B(2).
          MOVE "C" TO B(3).
          MOVE "L" TO B(4).
          MOVE "X" TO B(5).
          MOVE "V" TO B(6).
          MOVE "I" TO B(7).
          DISPLAY "ENTER ANY NUMBER: " WITH NO ADVANCING.
          ACCEPT N.
          IF (N = 0)
          DISPLAY "ZERO SHOULD NOT BE ENTERED"
          STOP RUN.
          DISPLAY "THE CORRESPONDING ROMAN NUMBER IS " WITH NO ADVANCING.
          PERFORM X-PARA VARYING I FROM 1 BY 1 UNTIL R = 0.
          DISPLAY " ".
          STOP RUN.
      X-PARA.
          DIVIDE N BY A(I) GIVING Q REMAINDER R.
          MOVE R TO N.
          PERFORM Y-PARA VARYING J FROM 1 BY 1 UNTIL J > Q.
      Y-PARA.
          DISPLAY B(I) WITH NO ADVANCING.

Save it as roman.cob and compile it with: cobc -x roman.cob

Enjoy! :D

allend 10-29-2013 01:14 AM

Seems to be an age-appropriate solution. :)

dugan 10-29-2013 01:19 AM

Here's a PyQt app I just knocked up. It needs the roman Python package to be installed.

Code:

#!/usr/bin/env python

from sip import setapi
setapi("QDate", 2)
setapi("QDateTime", 2)
setapi("QTextStream", 2)
setapi("QTime", 2)
setapi("QVariant", 2)
setapi("QString", 2)
setapi("QUrl", 2)


from PyQt4 import QtCore, QtGui
import roman
import sys


def main():
    app = QtGui.QApplication(sys.argv)
    view = ConversionWindow()
    conversionApp = Converter(view)
    conversionApp.show()
    sys.exit(app.exec_())


class ConversionWindow(QtGui.QDialog):

    def __init__(self, parent=None):
        super(ConversionWindow, self).__init__(parent)

        layout = QtGui.QVBoxLayout()
        form = QtGui.QFormLayout()
        self._roman = QtGui.QLineEdit()
        form.addRow('&Roman', self._roman)
        self._arabic = QtGui.QLineEdit()
        self._arabic.setReadOnly(True)
        form.addRow('&Arabic', self._arabic)
        layout.addLayout(form)
        self._convertButton = QtGui.QPushButton('Co&nvert')
        self._convertButton.setAutoDefault(True)
        layout.addWidget(self._convertButton)
        self.setLayout(layout)
        self.setWindowTitle('Roman to Arabic Numeral Converter')

    def connectSignals(self, converter):
        self._convertButton.clicked.connect(converter.convert)
        converter.arabic.connect(self._arabic.setText)

    def getRoman(self):
        return self._roman.text()


class Converter(QtCore.QObject):

    arabic = QtCore.pyqtSignal(unicode)

    def __init__(self, view):
        super(Converter, self).__init__(view)
        view.connectSignals(self)

    def show(self):
        self.parent().show()

    def convert(self):

        try:
            result = unicode(roman.fromRoman(self.parent().getRoman()))
        except roman.InvalidRomanNumeralError:
            result = ""

        self.arabic.emit(result)


if __name__ == '__main__':
    main()


volkerdi 10-29-2013 01:26 AM

Quote:

Originally Posted by allend (Post 5054227)
Seems to be an age-appropriate solution. :)

It does, doesn't it?

As an aside, after I posted this I thought "Gee, that should really display NUMERAL, not NUMBER"... so I edited the post. Then I got a nagging feeling that I'd better compile the edited program just in case, and sure enough it no longer compiled. It output "roman.cob:34 Error, syntax error, unexpected NO, expecting "NO ADVANCING"".

By default, COBOL uses fixed format. This is based on punch card columns. Although I'd kept everything within 80 columns, for some unknown reason columns 73 - 80 are ignored by the compiler. This can be worked around by passing -free to the compiler, which lifts all the column restrictions (and I'd be surprised if this wasn't done for all COBOL that's developed these days... but I guess you never know).

I'm remembering why I hate COBOL. Sorry I brought it up.

Richard Cranium 10-29-2013 02:29 AM

At least you didn't do it in Perl, which I consider to be the Devil's flatus.

saulgoode 10-29-2013 05:41 AM

Quote:

Originally Posted by volkerdi (Post 5054236)
By default, COBOL uses fixed format. This is based on punch card columns. Although I'd kept everything within 80 columns, for some unknown reason columns 73 - 80 are ignored by the compiler.

When I was in school, those columns were used for sequence numbering of the cards. If you happened to drop your stack of cards then you could run them through a sorting machine that would arrange them based on the numbers in those columns. The keypunch would automatically add the sequence numbers as you created your code, or you could generate new sequence numbers when you made a copy of your card deck.

tronayne 10-29-2013 08:06 AM

Quote:

Originally Posted by volkerdi (Post 5054213)
Oh, you need something to go along with this to do Arabic to Roman conversion? Here's a handy little program for that:

Wow. COBOL. Who knew.

Turns out http://www.novaroma.org/via_romana/numbers.html... um, goes both ways -- type in 1854 you get MDCCCLIV, type in MDCCCLIV you get 1854 (or, for that matter, type in mdcccliv and you get 1854). Exactly what's needed. And exactly what click-'n'-drool users like (Terminal window! We don't need no stinkin' terminal window! -- with due apologies to The Treasure of Sierra Madre).

I remember writing a fair rounding function, in BASIC, sometime in the mid 70's on a Honeywell mainframe (GECOS and all that). An actual BASIC compiler, not an interpreter, and it actually worked (which surprised the heck of me) Then had to turn around and write the same thing in FORTRAN IV (see, Roman numerals!). Thankfully, C has that built-in. Whatsit, the American Society of Testing and Materials Engineers, the ASTM method, if the digit next beyond the digit to be retained... lordy.

Thanks for the blast from the past, appreciate it -- didn't try it (yet) but appreciate it.

brianL 10-29-2013 10:57 AM

What did the Romans ever do for us?

Didier Spaier 10-29-2013 11:03 AM

Quote:

Originally Posted by brianL (Post 5054574)
What did the Romans ever do for us?

They offered you King Mandubracius


All times are GMT -5. The time now is 07:09 PM.