码迷,mamicode.com
首页 > 其他好文 > 详细

C柱状图(link_list、struct)

时间:2016-10-15 02:15:06      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

Bar for Temperature:

技术分享

 

tiger@animal ~ $ cat -n bar.c 
     1    #include <stdio.h>
     2    #include <stdlib.h>
     3    #include <ctype.h>
     4    #include <unistd.h>
     5    
     6    #define WIDTH 3
     7    #define GAP 4
     8    
     9    typedef struct tp tp;
    10    
    11    int Nodes = 0;
    12    
    13    struct tp{
    14        int Temperature;
    15        tp* Pnext;
    16    };
    17    
    18    void draw_bar(void);
    19    tp* creat_link_list(void);
    20    int max(tp*);
    21    int min(tp*);
    22    void print_bar(int);
    23    void print_blank(void);
    24    
    25    int main(void)
    26    {
    27        draw_bar();
    28        return 0;
    29    }
    30    
    31    void draw_bar(void)
    32    {
    33        tp* First_Node = creat_link_list();
    34        system("clear");
    35        tp* Tmp = NULL;
    36        int Highest= max(First_Node);
    37        int Lowest= min(First_Node);
    38        int Height = (Highest * Lowest) < 0 ? Highest : (abs(Highest) > abs(Lowest) ? Highest : Lowest);
    39        int i, j, k;
    40        printf("   ^\n   |\n");
    41        while(Height >= (Lowest < 0? Lowest : 0))
    42        {
    43            Tmp = First_Node;
    44            if(0 == Height)
    45            {
    46                printf(" 0 +");
    47                for(i = 0; i < Nodes; ++i)
    48                {
    49                    for(j = 0; j < GAP; ++j)
    50                    {
    51                        printf("-");
    52                    }
    53                    for(k = 0; k < WIDTH; ++k)
    54                    {
    55                        printf("\033[31m-\033[00m");
    56                    }
    57                }
    58                for(i = 0; i < GAP; ++i)
    59                {
    60                    printf("-");
    61                }
    62                printf(">");
    63            }
    64            else
    65            {
    66                printf("%03i|", Height);
    67                while(Tmp != NULL)
    68                {
    69                    int Var = Tmp->Temperature;
    70                    if((Var > 0 && Height > 0 && Var >= Height) || (Var < 0 && Height < 0 && Var <= Height))
    71                    {
    72                        print_bar(Var);
    73                    }
    74                    else
    75                    {
    76                        print_blank();
    77                    }
    78                    Tmp = Tmp->Pnext;
    79                }
    80            }
    81            printf("\n");
    82            --Height;
    83        }
    84        printf("   |\n");
    85        printf("\n");
    86    }
    87    
    88    tp* creat_link_list()
    89    {
    90        tp* Current = NULL;
    91        tp* Last = NULL;
    92        tp* First = NULL;
    93        char Test;
    94        do {
    95            Current = (tp *)malloc(sizeof(tp));
    96            if(Current == NULL)
    97            {
    98                printf("Error occur!\n");
    99                perror("malloc");
   100                exit(1);
   101            }
   102            Current->Pnext = NULL;
   103            if(First == NULL)
   104            {
   105                First = Current;
   106            }
   107            else
   108            {
   109                Last->Pnext = Current;
   110            }
   111            Last = Current;
   112            printf("Please input an interger: ");
   113            scanf("%i",&Current->Temperature);
   114            ++Nodes;    
   115            getchar();
   116            printf("Continue ? (Y/N)");
   117            scanf("%c",&Test);
   118        }while(tolower(Test) == y);
   119        return First;
   120    }
   121    
   122    int max(tp* First_Node)
   123    {
   124        tp* Tmp = First_Node->Pnext;
   125        int Max = First_Node->Temperature;
   126        while (Tmp != NULL)
   127        {
   128            if (Max < Tmp->Temperature)
   129            {
   130                Max = Tmp->Temperature;
   131            }
   132            Tmp = Tmp->Pnext;
   133        }
   134        return Max;
   135    }
   136    
   137    int min(tp* First_Node)
   138    {
   139        tp* Tmp = First_Node->Pnext;
   140        int Min = First_Node->Temperature;
   141        while(Tmp != NULL)
   142        {
   143            if(Min > Tmp->Temperature)
   144            {
   145                Min = Tmp->Temperature;
   146            }
   147            Tmp = Tmp->Pnext;
   148        }
   149        return Min;
   150    }
   151    
   152    void print_blank()
   153    {
   154        int i;
   155        for (i = 0; i < (WIDTH + GAP); ++i)
   156        {
   157            printf(" ");
   158        }
   159    }
   160    
   161    void print_bar(int Var)
   162    {
   163        int i, j;
   164        for(i = 0; i < GAP; ++i)
   165        {
   166            printf(" ");
   167        }
   168        if(Var > 0)
   169        {
   170            for(j = 0; j < WIDTH; ++j)
   171            {
   172                printf("\033[33m#\033[00m");
   173            }
   174        }
   175        else
   176        {
   177            for(j = 0; j < WIDTH; ++j)
   178            {
   179                printf("\033[37m#\033[00m");
   180            }
   181        }
   182    }

 

C柱状图(link_list、struct)

标签:

原文地址:http://www.cnblogs.com/hadex/p/5962466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!