Jump to content

Arrays de caracteres


Luis Briga
Go to solution Solved by bruno1234,

Recommended Posts

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

  • Solution

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]);
  • Vote 1

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.