LC Display问题

来源:互联网 发布:淘宝hd里怎么找微淘 编辑:程序博客网 时间:2024/03/29 03:44

问题原文如下:

A friend of you has just bought a new computer. Until now, the most powerful computer he ever used has been a pocket calculator. Now, looking at his new computer, he is a bit disappointed, because he liked the LC-display of his calculator so much. So you decide to write a program that displays numbers in an LC-display-like style on his computer.

Input 

The input file contains several lines, one for each number to be displayed. Each line contains two integerssn ( $1 \le s \le 10, 0\le n \le 99\,999\,999$), where n is the number to be displayed and s is the size in which it shall be displayed.

The input file will be terminated by a line containing two zeros. This line should not be processed.

Output 

Output the numbers given in the input file in an LC-display-style using s ``-'' signs for the horizontal segments and s ``|'' signs for the vertical ones. Each digit occupies exactly s+2 columns and 2s+3 rows. (Be sure to fill all the white space occupied by the digits with blanks, also for the last digit.) There has to be exactly one column of blanks between two digits.

Output a blank line after each number. (You will find a sample of each digit in the sample output.)

Sample Input 

2 123453 678900 0

Sample Output 

      --   --        --    |    |    | |  | |      |    |    | |  | |         --   --   --   --    | |       |    |    |   | |       |    |    |      --   --        -- 

自己的解法,刚开始是想建N个缓冲区,都填成8,然后根据数字来修改(因为8可以通过将‘|’改成‘ ’而变成任何数字),后来一想实在太麻烦,直接打印算了。代码如下:

#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <ctype.h>#include <string.h>#include <stdint.h>#include <assert.h>#define FLAG_ALL            0      //0#define FLAG_NO_TOP         (1<<0) //1#define FLAG_NO_MIDLLE      (1<<1) //2#define FLAG_NO_BOTTOM      (1<<2) //4#define FLAG_NO_UPLEFT      (1<<3) //8#define FLAG_NO_UPRIGHT     (1<<4) //16#define FLAG_NO_DOWNLEFT    (1<<5) //32#define FLAG_NO_DOWNRIGHT   (1<<6) //64#define BUFFER_SIZE         256#define MAX_CHARACTER_READ  (BUFFER_SIZE-1)#define MAX_POSITION        1000000000#define MAX_COUNT_LIMIT     10uint32_t led_table[10] = {    FLAG_NO_MIDLLE,    FLAG_NO_TOP|FLAG_NO_MIDLLE|FLAG_NO_BOTTOM|FLAG_NO_DOWNLEFT|FLAG_NO_UPLEFT,    FLAG_NO_UPLEFT|FLAG_NO_DOWNRIGHT,    FLAG_NO_UPLEFT|FLAG_NO_DOWNLEFT,    FLAG_NO_TOP|FLAG_NO_BOTTOM|FLAG_NO_DOWNLEFT,    FLAG_NO_UPRIGHT|FLAG_NO_DOWNLEFT,    FLAG_NO_UPRIGHT,    FLAG_NO_BOTTOM|FLAG_NO_MIDLLE|FLAG_NO_UPLEFT|FLAG_NO_DOWNLEFT,    FLAG_ALL,    FLAG_NO_DOWNLEFT};void error_exit(const char* str){    perror(str);    exit(0);}int main(int argc,  char* argv[]){    uint32_t sign_count         = 0;    uint8_t  data               = 0;    int      listSize           = 0;    uint32_t row = 0 , col      = 0;    char     input[BUFFER_SIZE] = {0};    char*    ptr = NULL;    char*    num_str = NULL;start:    printf("Please input the number:");    fgets(input , MAX_CHARACTER_READ , stdin);    ptr     = (char*)input;    num_str = NULL;    //blank character?    while(*ptr!=' ')    {        if(*ptr++=='\0')            error_exit("invalid input\n");    }    //get input number    int count = 1;    num_str = ptr;    while(isdigit(*--ptr))    {        sign_count += (*ptr-'0')*count;        count  *= 10;        if(count>MAX_POSITION && (*ptr-'0')>=4)            error_exit("nearly overflow , exit\n");        if(ptr == (char*)input)            break;    }    if(!isdigit(*ptr))        error_exit("invalid input\n");    printf("num is %u\n" , sign_count);    printf("\n");    if(sign_count==0)        error_exit("no need to show anything , just return\n");    if(sign_count > MAX_COUNT_LIMIT)        error_exit("sign count is too large  , just return\n");    while(!isdigit(*num_str) && *num_str!='\0')    {        num_str++;    }    //here is used to delete the '\n'    listSize = strlen(num_str)-1;    if(listSize==0)        error_exit("invalid number to show\n");    char** data_table = (char**)malloc(sizeof(char*)*listSize);    if(data_table == NULL)        error_exit("memory alloc error\n");    if(sign_count > UINT_MAX/2-3 || sign_count > UINT_MAX - 2)        error_exit("input sign count will cause overflow\n");    row = 2*sign_count+3;    col = sign_count+2;    assert(row>0);    assert(col>0);    for(int i=0;i<listSize;i++)    {        char** ptr = data_table+i;        *ptr = (char*)malloc(sizeof(char)*row*col);        if(ptr==NULL)            error_exit("memory alloc error");        uint32_t data  = *(num_str+i) - '0';        bool noTop       = ((led_table[data]&FLAG_NO_TOP)>0) ;         bool noMid       = ((led_table[data]&FLAG_NO_MIDLLE)>0);         bool noBottom    = ((led_table[data]&FLAG_NO_BOTTOM)>0);         bool noUpLeft    = ((led_table[data]&FLAG_NO_UPLEFT)>0);        bool noUpRight   = ((led_table[data]&FLAG_NO_UPRIGHT)>0);        bool noDownLeft  = ((led_table[data]&FLAG_NO_DOWNLEFT)>0);        bool noDownRight = ((led_table[data]&FLAG_NO_DOWNRIGHT)>0);        //assigned value        for(uint32_t m=0; m<row; ++m)        {            for(uint32_t n=0; n<col; ++n)            {                if(m==0)                {                    if(!noTop)                    {                        if(n==0||n==col-1)                            *((*ptr+m*col)+n)=' ';                        else                            *((*ptr+m*col)+n)='-';                    }                    else                        *((*ptr+m*col)+n)=' ';                }                else if(m==sign_count+1)                {                    if(!noMid)                    {                        if(n==0||n==col-1)                            *((*ptr+m*col)+n)=' ';                        else                            *((*ptr+m*col)+n)='-';                    }                    else                        *((*ptr+m*col)+n)=' ';                }                else if(m==2*sign_count+2)                {                    if(!noBottom)                    {                        if(n==0||n==col-1)                            *((*ptr+m*col)+n)=' ';                        else                            *((*ptr+m*col)+n)='-';                    }                    else                        *((*ptr+m*col)+n)=' ';                }                else if(m>0 && m<sign_count+1)                {                    if(n==0)                    {                        if(!noUpLeft)                            *((*ptr+m*col)+n)='|';                        else                            *((*ptr+m*col)+n)=' ';                    }                    else if(n==col-1)                    {                        if(!noUpRight)                            *((*ptr+m*col)+n)='|';                        else                            *((*ptr+m*col)+n)=' ';                    }                    else                        *((*ptr+m*col)+n)=' ';                }                else if(m>sign_count+1 && m<2*sign_count+2)                {                    if(n==0)                    {                        if(!noDownLeft)                            *((*ptr+m*col)+n)='|';                        else                            *((*ptr+m*col)+n)=' ';                    }                    else if(n==col-1)                    {                        if(!noDownRight)                            *((*ptr+m*col)+n)='|';                        else                            *((*ptr+m*col)+n)=' ';                    }                    else                        *((*ptr+m*col)+n)=' ';                }            }        }    }    for(uint32_t i=0;i<row;++i)    {        for(int k=0;k<listSize;++k)        {            for(uint32_t j=0;j<col;++j)            {                printf("%c" , *(*(data_table+k)+i*col+j));            }            printf("   ");        }        printf("\n");    }    //attention , after program functions well ,     //do not forget to release buffer    for(int i=0;i<listSize;++i)    {        free(*(data_table+i));    }        free(data_table);    data= 0;    row = 0;    col = 0;    listSize= 0;    sign_count = 0;    ptr = NULL;    num_str = NULL;    memset(input , 0 , BUFFER_SIZE);    goto start;    return 0;}


后来又看见一位仁兄的解法,更加简单,博客地址:http://blog.csdn.net/magicnumber/article/details/7705962,代码如下:

#include <stdio.h>  #include <string.h>  char zm[10][10][10]=  {          {              " - ",              "| |",              "   ",              "| |",              " - "          },          {               "   ",              "  |",              "   ",              "  |",              "   "          },          {              " - ",              "  |",              " - ",              "|  ",              " - "          },          {              " - ",              "  |",              " - ",              "  |",              " - "          },          {              "   ",              "| |",              " - ",              "  |",              "   "          },          {              " - ",              "|  ",              " - ",              "  |",              " - "          },          {              " - ",              "|  ",              " - ",              "| |",              " - "          },          {              " - ",              "  |",              "   ",              "  |",              "   "          },          {              " - ",              "| |",              " - ",              "| |",              " - "          },          {              " - ",              "| |",              " - ",              "  |",              " - "          }  };  char str[100];  void Expend(char *s,int re)  {      int i;      printf("%c",s[0]);      for (i=0;i<re;i++)      {          printf("%c",s[1]);      }      printf("%c",s[2]);  }  int main()  {      int i,j,n;      while(1)      {          scanf("%d",&n);          scanf("%s",str);          if (n==0) break;          for (i=0;i<2*n+3;i++)          {              for (j=0;j<strlen(str);j++)              {                  if (i==0)                  {                      Expend(zm[str[j]-'0'][0],n);                  }                  else if (i==n+1)                  {                      Expend(zm[str[j]-'0'][2],n);                  }                  else if (i==2*n+2)                  {                      Expend(zm[str[j]-'0'][4],n);                  }                  else if (i<n+1)                  {                      Expend(zm[str[j]-'0'][1],n);                  }                  else                  {                      Expend(zm[str[j]-'0'][3],n);                  }                  printf(" ");              }              printf("\n");          }          printf("\n");      }      return 0;  } 

这个更简单明了,山外青山楼外楼,高手常有,而谦虚者难求:)





原创粉丝点击