Convert Roman to Arabic number in Visualg

Asked

Viewed 477 times

-1

The system of Roman numerals (or Roman numerals) developed in ancient Rome and was used throughout its Empire. In this system ciphers are written with certain letters, which represent the numbers. The letters are always uppercase, since in the Roman alphabet there are no lowercase letters, the letters are I, V, X, L, C, D and M. Your task is to develop a program that converts Roman numbers into Arabic. The rules for the formation of Roman numerals are set out below. Each letter corresponds to a given value: I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000. Grouping the letters above, we can represent the numbers according to a set of rules: With the exception of V, L and D, the other numerals can be repeated at most three times:

III = 3

XXX = 30

CCC = 300

MMM = 3000

When written to the right of larger numerals, I, X and C are added to the values of the former:

VIII = 5 + 1 + 1 + 1 = 8

LXII = 50 + 10 + 1 + 1 = 62

CLVIII = 158

MCXX = 1000 + 100 + 10 + 10 = 1120

But if the numerals I, X and C are to the left of the largest, their values are subtracted as, for example, in:

IV = 5 - 1 = 4

IX = 10 - 1 = 9

XC = 100 - 10 = 90

I think I know how to finally do first I would take the typed Roman value, take the letter size assigned value would be n letter size and that letter size would take n-1, then try to divide the letter, do not know the command in Visualg to separate the letters and create a variable to store the separation of these letters in this case separator would create another variable in this case s and would receive zero then make one to where i =0 and i have to be smaller than the letter size in this and would adding by +1 then would open one if the division of the separator letters = "M" then s= s+1000 and inside this one would open another if and if separator[i-1]= "C" then s= s-200, would end this if then open another if checking separator = "D" then s=s+500 and inside would open another if and if separator[i-1] = "C" then s=s-200 then close that if and open another separator = "C" then s=s+100 and inside that one would open another if separator[i-1] = "X" and then s=s-20 then finish that if and open another if, if separator = "L" then s=s+50 inside would open another if, if separator[i-1] = "X" then

s=s-20 then finish that if and open another if, if separator="X" then s=s+10 would open another if, if separator[i-1] = "I" then s=s-2 then close that if then open another if, if separator="V" then s=s+5 and inside that one would open another if, if separator[i-1] = "I" then s=s-2 would close this if then it would open another if separator = "I" then s=s+1

then I would write (s)

The problem is time to separate the letters which command in Visualg to separate letter or not have?

The copy command is useful only if separating letter by letter but fails on time, for example to copy a Roman number, I have to already pass this command with parameters already pre-defined copy from this house to this house (roman;1;2), that would not have to put all the parameters for all the numbers from 1 to 3999 For now the code is like this

Var
   // Seção de Declarações das variáveis
   vetromano:vetor[1..9] de caractere
   romano:caractere
   i:inteiro
   m,c,d,u:caractere
   soma:inteiro


Inicio
   // Seção de Comandos, procedimento, funções, operadores, etc...
   escreval("Digite um número em romano para ser convertido em arábico")
   escreval("menor que 4000 e maior que 0")
   leia(romano)
   m <- copia(romano;1;2)
   c <- copia(romano;3;2)
   d <- copia(romano;5;2)
   u <- copia(romano;7;2)
   para i de 1 ate Compr(romano) faca
      se (m = "M") entao
         soma<-soma+1000
         se(m-1) entao
            soma<-soma-200
         fimse
      fimse

      escreval(soma)

   fimpara
   escreval

1 answer

0

Try this, I hope it helps:

algorithm "Example 2" var vetCarac :vector [1.. 9] of character text :character i : whole beginning text <- "MXLVIII" for 1 i until Buy (text) knife vetCarac[i] <- Copy(text,i,1) fimPara for 1 i until Co mpr(text) knife escreval(vetCarac[i]) fimPara fimAlgoritmo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.