Luis Briga Posted April 16, 2020 at 06:32 PM Report Share #617897 Posted April 16, 2020 at 06:32 PM Olá, sou um aluno de informática de 11º ano e neste momento estou a estudar a linguagem de programação c++. Tenho um exercício cujo objetivo do programa é ler um número inteiro e converte-lo para números romanos usando obrigatoriamente arrays . Tentei resolver mas apenas consegui usando o comando switch, e queria conseguir resolver o exercício usando arrays (strings). O meu código (usando o comando switch) é o seguinte: Agradecia imenso a quem me pudesse ajudar. Cumprimentos. #include <stdio.h> #include <locale.h> int main (){ setlocale (LC_ALL,"Portuguese"); int numero, unidades, dezenas, centenas, milhares; printf ("\n Indique um número: "); scanf("%d",&numero); unidades = numero % 10; numero /= 10; dezenas = numero % 10; numero /= 10; centenas = numero % 10; numero /= 10; milhares = numero % 10; numero /= 10; switch (milhares){ case 1: printf ("M"); break; case 2: printf ("MM"); break; case 3: printf ("MMM"); break; } switch (centenas){ case 1: printf("C"); break; case 2: printf("CC"); break; case 3: printf("CCC"); break; case 4: printf("CD"); break; case 5: printf("D"); break; case 6: printf("DC"); break; case 7: printf("DCC"); break; case 8: printf("DCCC"); break; case 9: printf("CM"); break; } switch (dezenas){ case 1: printf("X"); break; case 2: printf("XX"); break; case 3: printf("XXX"); break; case 4: printf("XL"); break; case 5: printf("L"); break; case 6: printf("LX"); break; case 7: printf("LXX"); break; case 8: printf("LXXX"); break; case 9: printf("XC"); break; } switch (unidades){ case 1: printf("I"); break; case 2: printf("II"); break; case 3: printf("III"); break; case 4: printf("IV"); break; case 5: printf("V"); break; case 6: printf("VI"); break; case 7: printf("VII"); break; case 8: printf("VIII"); break; case 9: printf("IX"); break; } return 0; } Link to comment Share on other sites More sharing options...
Solution bruno1234 Posted April 16, 2020 at 07:43 PM Solution Report Share #617898 Posted April 16, 2020 at 07:43 PM Em vez de teres o case para cada conversão, podes usar um array. Ou seja em vez de: switch (milhares){ case 1: printf ("M"); break; case 2: printf ("MM"); break; case 3: printf ("MMM"); break; } colocas (atenção que a linguagem é pseudo-código): // declarado no inicio: string[] milharesRomano = ["M","MM","MMM"]; // no lugar do switch: printf(milharesRomano[milhares - 1]); 1 Report Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Luis Briga Posted April 19, 2020 at 11:35 AM Author Report Share #617907 Posted April 19, 2020 at 11:35 AM Obrigado, ajudou muito! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now