Thread: Numbers printed with characters

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36

    Numbers printed with characters

    Hello, i have to make a program that would print out numbers represented with characters like this:

    8317

    Code:
    +--+ +--+ + +--+
     !  !    ! !    !
     +--+ +--+ +    + 
     !  !    ! !    !
     +--+ +--+ +    !
    
    srry i could get itto look any better, the first number is 8 and then 3 and so on...
    So far i did this:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char *deliStevilke[]={ // numbers representet with characters
    		"+--+", // 0
    			"+  +", // 1
    			"!  !", // 2
    			"   +", // 3
    			"   !", // 4
    			"!   ", // 5
    			"+",	// 6
    			"!",	// 7
    	};
    	
    	int stevilke[10][5]={ // numbers how they are to be printed on the screen
    		{0,2,1,2,0}, // 0
    		{6,7,6,7,6}, // 1
    		{0,4,0,5,0}, // 2
    		{0,4,0,4,0}, // 3
    		{1,2,0,4,3}, // 4
    		{0,5,0,4,0}, // 5
    		{0,5,0,2,0}, // 6
    		{0,4,3,4,3}, // 7
    		{0,2,0,2,0}, // 8
    		{0,2,0,4,0}, // 9
    	};
    	
    	int stevilo;
    	
    	scanf("%d", &stevilo);
    	// print the number in character style
    	printf("%s\n", deliStevilke[stevilke[stevilo][0]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][1]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][2]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][3]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][4]]);
    	
    	return 0;
    }
    I have no truble printing if you put in 1 number like 9. I dont know what to add so that i could print a number like 9999.

    Could somebody give me some idea what to do?

    Thank you.
    bnk

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    134
    take the no and use &#37;10 operand to find out the digits at the unit place and then the answer in the same loop again and print this as a single digit.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, there's different way of doing so. Basically you decompose your number into digit and make it work altogether.

    Code:
    #include <stdlib.h>
    
    int* decomposeNumber(int n, int *nbOfDigits)
    //  Return an array of *nbOfDigits numbers between 0 and 9,
    //  which is the decomposition in digits from n
    {
        int origN = n;
        int *tab;
        int i;
    
        *nbOfDigits = 0;
        do
        {
            n = n / 10;
            (*nbOfDigits)++;
        } while (n != 0);
    
        tab = malloc(*nbOfDigits * sizeof(*tab));
        // Here you should check if tab != NULL
    
        n = origN;
        for (i = *nbOfDigits - 1; i >= 0; i--)
        {
            tab[i] = n % 10;
            n = n / 10;
        }
    
        return tab;
    }
    
    void printNumberBig(int n, FILE *file)
    //  Print only non-negative numbers
    {
        static char *deliStevilke[]={
            "+--+", // 0
            "+  +", // 1
            "!  !", // 2
            "   +", // 3
            "   !", // 4
            "!   ", // 5
            "+",	// 6
            "!",	// 7
        };
    
        static int stevilke[10][5]={
            {0,2,1,2,0}, // 0
            {6,7,6,7,6}, // 1
            {0,4,0,5,0}, // 2
            {0,4,0,4,0}, // 3
            {1,2,0,4,3}, // 4
            {0,5,0,4,0}, // 5
            {0,5,0,2,0}, // 6
            {0,4,3,4,3}, // 7
            {0,2,0,2,0}, // 8
            {0,2,0,4,0}, // 9
        };
    
        if (n >= 0 && file != NULL)
        {
            int *tab;
            int nbOfDigits;
            int i, j;
    
            tab = decomposeNumber(n, &nbOfDigits);
            for (j = 0; j < 5; j++)
            {
                // Magic number aren't great
                for (i = 0; i < nbOfDigits; i++)
                {
                    fprintf(file, "%s ", deliStevilke[stevilke[tab[i]][j]]);
                }
                putchar('\n');
            }
        }
    }
    Sorry for giving the answer but it's the only way i could be clear.
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    take the no and use %10 operand to find out the digits at the unit place and then the answer in the same loop again and print this as a single digit.
    Kepil, i understad how to use the %10 operand. If for example i do:

    8713%10

    the result is 3, but this is the last digit and i have to print this one at the last place. Just thinking if i print the 3 and then jump to the start of the line and then with the help of a loop calculate the next digit. But how do i substract the 3 from the number 8713 to get the next number 1 and so on?

    foxman thank you for your help, but my knowledge is not so big in the way i could use the malloc function for the memory and you used so many pointers i kind of lost my way

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You can use a combination of divide and mod to get the digit you want. Like:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 8713;
        int digit;
        while( num > 0 )
        { 
           digit = num % 10;
           num /= 10;
           printf("%i\n", digit);
        }
        getchar();
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Mike thank you again for helping.

    This is what i did:

    Code:
    #include <stdio.h>
    
    	char *deliStevilke[]={ // numbers representet with characters
    		"+--+", // 0
    		"+  +", // 1
    		"!  !", // 2
    		"   +", // 3
    		"   !", // 4
    		"!   ", // 5
    		"+",	// 6
    		"!",	// 7
    	};
    	
    	int stevilke[10][5]={ // numbers how they are to be printed on the screen
    		{0,2,1,2,0}, // 0
    		{6,7,6,7,6}, // 1
    		{0,4,0,5,0}, // 2
    		{0,4,0,4,0}, // 3
    		{1,2,0,4,3}, // 4
    		{0,5,0,4,0}, // 5
    		{0,5,0,2,0}, // 6
    		{0,4,3,4,3}, // 7
    		{0,2,0,2,0}, // 8
    		{0,2,0,4,0}, // 9
    	};
    
    int main()
    {
    
    	
    	int stevilo,i=0,k=0,j=0;
    	int pomoznoPolje[10];
    	
    	
    	
    	scanf("%d", &stevilo);
    	
    
    	while(stevilo>0)
    	{
    		pomoznoPolje[i]=stevilo%10;
    		stevilo/=10;
    		i++;
    	}
    	k=i;
    		
    	for(j;j<i;j++)
    	{
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][0]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][1]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][2]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][3]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][4]]);
    	k--;
    	}
    	return 0;
    }
    And this is what comes out:
    Now i just have to print this in a horizontal line not vertical. Is there any way to do that?

    Code:
    8743
    +--+
    !  !
    +--+
    !  !
    +--+
    +--+
       !
       +
       !
       +
    +  +
    !  !
    +--+
       !
       +
    +--+
       !
    +--+
       !
    +--+

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'd have to form a 2D array of the output data [so, you'd have to figure out what all the digits you want on one row, then produce the first row of each digit, then second row of all the digits, etc, until you have done all lines of all digits].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Here's a better solution.. I wrote this a while ago, I believe, it explains the concept so you can adapt it to your own program.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
         char array[9][10][20] = 
         {
              {" ### ","    "," ### "," ### ","     "," ### "," ### "," ### "," ### "," ### "},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"     ","    "," ### "," ### "," ### "," ### "," ### ","     "," ### "," ### "},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {" ### ","    "," ### "," ### ","     "," ### "," ### ","     "," ### "," ### "},    
         }, 
         *nr = argv[1];
         unsigned int i = 0, h = 0;
         
         for( i = 0; i < 9; i++)
         {
              for(h = 0; h < strlen(nr); h++)
                   if(nr[h] !=  ' ')
                        printf(" %s ", array[i][nr[h] - 0x30]);
                   
              putchar('\n');
                   
         }
         
         
         return 0;
              
    }

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Thank you all, it finally worked.

    Some of the print outs, i even used the "special" character type:

    Code:
    0123456789
     ┌──┐ ┐ ┌──┐ ┌──┐ ┬    ┌──  ┌──┐ ┌──┐ ┌──┐ ┌──┐
     │  │ │    │    │ │    │    │       │ │  │ │  │
     │  │ │ ┌──┘  ──┤ └─┼  └──┐ ├──┐    ┼ ├──┤ └──┤
     │  │ │ │       │   │     │ │  │    │ │  │    │
     └──┘ ┴ └──  └──┘   ┴  ───┘ └──┘    ┴ └──┘  ──┘
    Code:
    1008887
     ┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐
     │ │  │ │  │ │  │ │  │ │  │    │
     │ │  │ │  │ ├──┤ ├──┤ ├──┤    ┼
     │ │  │ │  │ │  │ │  │ │  │    │
     ┴ └──┘ └──┘ └──┘ └──┘ └──┘    ┴
    Code:
    26101976
     ┌──┐ ┌──┐ ┐ ┌──┐ ┐ ┌──┐ ┌──┐ ┌──┐
        │ │    │ │  │ │ │  │    │ │
     ┌──┘ ├──┐ │ │  │ │ └──┤    ┼ ├──┐
     │    │  │ │ │  │ │    │    │ │  │
     └──  └──┘ ┴ └──┘ ┴  ──┘    ┴ └──┘

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2009, 02:32 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. How would I only enter numbers and not characters
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2004, 03:19 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM