Jump to content

I've committed a ridiculous abuse of control flow.

- - - - -

  • Please log in to reply
1 reply to this topic

#1
rocketboy9000

rocketboy9000

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
While looking through my old code, I found this function which converts numbers to roman numerals. It works perfectly, but has some really strange control flow.
const char * utoroman(unsigned int i){

	static char s[16];

	if(i>=4000)return "Magnus";

	if(i==0)return "Nihil";

	char *t=s+15;//writing the numbers in backwards, old trick.

	int m=0;

	do{

		switch(i%10){

		case 1 ... 3://this is a gnu extension

		case 6 ... 8:

			*--t="IXCM"[m];

			i--;

			continue;                //ok, this is weird

		case 5:	*--t="VLD"[m];

			break;

		case 4:	*--t="VLD"[m];

		if(0)                                      //is this even LEGAL?!

		case 9:	*--t="XCM"[m];    //on 4 it skips this line, on 9 it doesn't.

			*--t="IXCM"[m];

		case 0: break;

		}

		m++;

		i/=10;

	}while(i>0);

	return t;

}
So, is the weird if on line 18 legal?

Edited by rocketboy9000, 30 March 2011 - 07:12 PM.
figured out how to get rid of the goto that was in it


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Excluding extensions; what you have written is legal C 1999. The way you have written it however, our compiler technology appears to be fifteen years old as there is no possible way to optimize this.

Edited by Alexander, 31 March 2011 - 02:49 AM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users