Help needed ! ( displaying Roman character )

This is a discussion on Help needed ! ( displaying Roman character ) within the C Programming forums, part of the General Programming Boards category; Hello , I need help on how to change user entered numerical number into Roman character. For example , 1 ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    21

    Unhappy Help needed ! ( displaying Roman character )

    Hello ,

    I need help on how to change user entered numerical number into Roman character.

    For example , 1 = i , 2 = ii , 5 = v , 10 = x , 13 = xiii etc...

    I have done the programming half way and do not know how to continue
    from 11 onward.

    Please advice !!!
    ---------------------------------------------------------------------
    Code:
    #include<stdio.h>
    
    int romanize(int q, int base, char letter)
    {
       int counter, counter2, temp, test;
       
    	if(base == 1)
    	 {
    	 
    	    if(q < 5)
             {
    	      printf("Romanize character is ");
                for(counter = 1; counter <= q; counter++)
                 {   
                  printf("%c",letter);
                 } 
       	     }
       
            if(q >=5 && q <=9)
             {
    	      printf("Romanize character is v");
    	      temp = q - 5;
    	      for(counter = 1; counter <= temp; counter++)
    	       {
    	        printf("%c",letter);
    	       }
    		 }  
    	 }
    //-------------------------------------------------------------------------------------------
        if(base == 2)
    	 {
    	    test = q;
    		counter = 0;
    		counter2 = 0; 
    		
    		 if(test <= 10 && test <= 49)
             {
    	        
    			
    			while( test >= 9 )
    			 {
    			  test = test - 10;
    			  counter2++;
    		     }
    		    
    			printf("inside loop is : %d\n",counter2);
    			
    			printf("Romanize character is ");
                for(counter = 1; counter <= counter2; counter++)
                 {   
                  printf("%c",letter);
                 } 
    			 
    		   if(test < 5)
                {
    	          for(counter = 1; counter <= test; counter++)
                   {   
                    printf("i");
                   } 
       	        }
       
                if(test >=5 && test <=9)
                {
    	         printf("v");
    	         temp = test - 5;
    	         for(counter = 1; counter <= temp; counter++)
    	         {
    	          printf("%i");
    	         }
    		    }   	 
       	     }
       
            
    	 }
    }
    
    main()
    {
        int i, n, ch, number, count, intnumber[10];
        number = 0, count=0, i=0;
    	
    	printf("input ?: ");
        ch = getchar();
      
        while(ch!='\n')
         {
          if('0' <= ch && ch <= '9')
    	   {
    	    	
    		//This is to count the number of digit the user has enter//
    		n = 0;
    		n = n * 10;
    		n = n + (ch - '0');
    		intnumber[i] = n;//Store each entered digit into array "intnumber[]"//
    		count++;
    		i++;
    		//--------------------------------------------------------//
    		
    		number = number * 10;
    		number = number + (ch - '0');
    	   }
    	  
    	  ch = getchar();
         }
    
    	 printf("count is %d\n",count);
    	 printf("number is %d\n",number);
    	 
    	 if( count <= 1 ) // number is less than 10
    	  {
    	  romanize(number,count,'i');
    	  }
    	 else if( count > 1 ) // number is (10 <= number <=99)
    	  {
    	  romanize(number,count,'x');
          }
       
     
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the C forum.

    Think of a funnel - big at the top, but quickly starts catching the bigger things as they are poured in.

    Then the medium sized things are caught as the funnel continues to narrow down. Finally, the smallest stuff is caught.

    And it's all done in descending order (big to small).

    You can do roman numerals this same way, using a while or for loop, with some logic to make it continually handle smaller and smaller numbers, and transfer them to roman numerals, until there are no numbers left. (or until we see we want another block of code to handle some part of the funnel that we're building.

    Some pseudo code:
    Code:
    char roman[40];
    int amount, i;
    
    Get amount from user.
    i = 0; 
    do  {
      switch(amount) {
        case >= 1000:
          roman[i] = M; ++i; amount -= 1000; break;
    
        case >= 900;
          roman[i] = C; ++i; roman[i] = M; ++i; amount -= 900; break;   
    
        case >= 500;
          //etc.
    
    /*
        continue above for all cases of roman numerals. Double or triple letters,
        like ii and iii, get an assignment of ONE char per letter, into the roman array. With
        every char assigned, you increment i one more time.
    */
        default: printf("\n Error: Amount is %d", amount);
    
      }
    
    }while(amount > 0);
    If you use the "fall through" logic of the switch(amount) code, you can actually do the entire amount in one pass, with little or no looping. I put the break in there, because it's very helpful during the debug, and easier to understand - maybe.

    This will shorten your whole code up, tremendously.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    Hi thanks for your guidance.
    Because our teacher did not teach us "switch" function yet, we r not suppose to use any advance functions etc.

    I have wrote the source code for this program. A little bit long but still workable.

    Now i like to add a little bit. User can enter in hexadecimal form and i still be able to display the
    correct roman character.

    I think i know how to do it but got a little problem with the very first part that ask user whether
    they want to enter in Hexadecimal or decimal form. A simple program but my while loop does not break away even the correct 'y' or 'n' is entered. why why why? really don't understand

    Code:
    #include<stdio.h>
    
    int romanize(int q[], int base)
    {
      int counter, temp;
      
      printf("\nThe Romanize number is ");
      
       if(base == 4)
        {
    	    // 4 digit
    	     for(counter = 1; counter <= q[0]; counter++)
    	       {
    	        printf("m");
    	       }
    	
    	    // 3 digit more than 5
    	     if(q[1] >= 5)
    	      {
    	       temp = q[1] - 5;
    	       printf("d");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		  
    		 // 3 digit less than 5
    	     if(q[1] < 5)
    	      {
    	       for(counter = 1; counter <= q[1]; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		 
    		 
    		 // 2 digit more than 5
    	     if(q[2] >= 5)
    	      {
    	       temp = q[2] - 5;
    	       printf("l");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    
             // 2 digit less than 5
    	     if(q[2] < 5)
    	      {
    	       for(counter = 1; counter <= q[2]; counter++)
    	         {
    		      printf("x");
    		     }
    	      }	
       
             // 1 digit more than 5
    	     if(q[3] >= 5)
    	      {
    	       temp = q[3] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[3] < 5)
    	      {
    	       for(counter = 1; counter <= q[3]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    
    
        if(base == 3)
        {
    	    // 3 digit more than 5
    	     if(q[0] >= 5)
    	      {
    	       temp = q[0] - 5;
    	       printf("d");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		  
    		 // 3 digit less than 5
    	     if(q[0] < 5)
    	      {
    	       for(counter = 1; counter <= q[0]; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		 
    		 
    		 // 2 digit more than 5
    	     if(q[1] >= 5)
    	      {
    	       temp = q[1] - 5;
    	       printf("l");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    
             // 2 digit less than 5
    	     if(q[1] < 5)
    	      {
    	       for(counter = 1; counter <= q[1]; counter++)
    	         {
    		      printf("x");
    		     }
    	      }	
       
             // 1 digit more than 5
    	     if(q[2] >= 5)
    	      {
    	       temp = q[2] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[2] < 5)
    	      {
    	       for(counter = 1; counter <= q[2]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    	
    	
    	if(base == 2)
        {
    	    // 2 digit more than 5
    	     if(q[0] >= 5)
    	      {
    	       temp = q[0] - 5;
    	       printf("l\n");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    		  
    		 // 2 digit less than 5
    	     if(q[0] < 5)
    	      {
    	       for(counter = 1; counter <= q[0]; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    		 
    		// 1 digit more than 5
    	     if(q[1] >= 5)
    	      {
    	       temp = q[1] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[1] < 5)
    	      {
    	       for(counter = 1; counter <= q[1]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    	
    	
    	if(base == 1)
        {
    	    // 1 digit more than 5
    	     if(q[0] >= 5)
    	      {
    	       temp = q[0] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[0] < 5)
    	      {
    	       for(counter = 1; counter <= q[0]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    	
    }
    
    main()
    {
        int i, n, ch, count, hexadecimal, decimal, number[10];
    	int hex;
        count=0, i=0;
    	
    	//Ask user whether to input in hexadecimal form or not
    	
    	printf("Do you want to input in Hexdecimal? Y/N : ");
        hex = getchar();
    	while( hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N')
    	   {
    	    printf("Please enter Y or N only!\n");
    		hex = getchar();
    	   }
    	//--------------------------------------------------------   
    
    	
    	//if user input 'Y' or 'y' then proceed with the following
    	if(hex == 'Y' || hex == 'y')
    	  {
    	   printf("Please enter in hexadecimal form : \n");
    	   hexadecimal = getchar();
    	   
    	   while(hexadecimal != '\n')
               {
                if('0' <= hexadecimal && hexadecimal <= '9')
                 {
                  decimal = decimal * 16;
                  decimal = decimal + (hexadecimal - '0');
                 }
            
                else if('A' <= hexadecimal && hexadecimal <= 'F')
                {
                 decimal = decimal * 16;
                 decimal = decimal + (hexadecimal - 'A')+10;
                }
    
                else if('a' <= hexadecimal && hexadecimal <= 'f')
                {
                 decimal = decimal * 16;
                 decimal = decimal + (hexadecimal - 'a')+10;
                }
    
                else
    			{
    			  break;
    			}
    	
    	        count++;
    	        i++;		 
                hexadecimal = getchar();
               }
    		   
    		   printf("The decimal for hexadecimal is : %d\n",decimal);
    	   }
    	
    	
    	
    	else
    	 {
          while(ch!='\n')
          {
            if('0' <= ch && ch <= '9')
    	    {
    	      //This is to count the number of digit the user has enter//
    		  n = 0;
    		  n = n * 10;
    		  n = n + (ch - '0');
    		  number[i] = n;     //Store each entered digit into array "intnumber[]"//
    		  count++;
    		  i++;
    	    }
    	  
    	  ch = getchar();
         }
    
    	 if(count < 5 ) // Handle number up to 9999 only
    	  {
           romanize(number,  count);
    	  } 
    	 else
    	  printf("\nPlease enter number up to 9999 only.\n");
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 04:10 AM
  4. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  5. Using loops for check a roman number input.
    By eryell in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2006, 11:04 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21