Jump to content

Recommended Posts

Posted

Eu quero fazer uma função que recebe como argumento um mes e escreve-o numa string o código que fiz foi o seguinte:

#include<stdio.h>
#include<stdlib.h>

enum mes {Janeiro, Fevereiro, Marco, Abril, Maio, Junho, Julho, Agosto, Setembro, Outubro, Novembro, Dezembro};

typedef enum mes mes;

int mes_str(mes m, char str[100]){

switch (m){
	case 0:
		sprintf(str,"Janeiro\n");
		break;
	case 1:
		sprintf(str,"Fevereiro\n");
		break;
	case 2:
		sprintf(str,"Março\n");
		break;
	case 3:
		sprintf(str,"Abril\n");
		break;
	case 4:
		sprintf(str,"Maio\n");
		break;
	case 5:
		sprintf(str,"Junho\n");
		break;
	case 6:
		sprintf(str,"Julho\n");
		break;
	case 7:
		sprintf(str,"Agosto\n");
		break;
	case 8:
		sprintf(str,"Setembro\n");
		break;
	case 9:
		sprintf(str,"Outubro\n");
		break;
	case 10:
		sprintf(str,"Novembro\n");
		break;
	case 11:
		sprintf(str,"Dezembro\n");
		break;
}
}

Não existe nenhuma forma de simplificar isto?

Posted

É isto mesmo. Existem jeitos de fazer sem precisar do switch como criar um array com strings de todos os meses e depois acessar pelo índice:

Ex. mes= meses[numeromes]

Fernando Lage Bastos - MCP/MCTS/MCPD

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.