Bar for Temperature:
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 while(First_Node != NULL) 87 { 88 Tmp = First_Node; 89 First_Node = First_Node->Pnext; 90 free(Tmp); 91 } 92 } 93 94 tp* creat_link_list() 95 { 96 tp* Current = NULL; 97 tp* Last = NULL; 98 tp* First = NULL; 99 char Test; 100 do { 101 Current = (tp *)malloc(sizeof(tp)); 102 if(Current == NULL) 103 { 104 printf("Error occur!\n"); 105 perror("malloc"); 106 exit(1); 107 } 108 Current->Pnext = NULL; 109 if(First == NULL) 110 { 111 First = Current; 112 } 113 else 114 { 115 Last->Pnext = Current; 116 } 117 Last = Current; 118 printf("Please input an interger: "); 119 scanf("%i",&Current->Temperature); 120 ++Nodes; 121 getchar(); 122 printf("Continue ? (Y/N)"); 123 scanf("%c",&Test); 124 }while(tolower(Test) == 'y'); 125 return First; 126 } 127 128 int max(tp* First_Node) 129 { 130 tp* Tmp = First_Node->Pnext; 131 int Max = First_Node->Temperature; 132 while (Tmp != NULL) 133 { 134 if (Max < Tmp->Temperature) 135 { 136 Max = Tmp->Temperature; 137 } 138 Tmp = Tmp->Pnext; 139 } 140 return Max; 141 } 142 143 int min(tp* First_Node) 144 { 145 tp* Tmp = First_Node->Pnext; 146 int Min = First_Node->Temperature; 147 while(Tmp != NULL) 148 { 149 if(Min > Tmp->Temperature) 150 { 151 Min = Tmp->Temperature; 152 } 153 Tmp = Tmp->Pnext; 154 } 155 return Min; 156 } 157 158 void print_blank() 159 { 160 int i; 161 for (i = 0; i < (WIDTH + GAP); ++i) 162 { 163 printf(" "); 164 } 165 } 166 167 void print_bar(int Var) 168 { 169 int i, j; 170 for(i = 0; i < GAP; ++i) 171 { 172 printf(" "); 173 } 174 if(Var > 0) 175 { 176 for(j = 0; j < WIDTH; ++j) 177 { 178 printf("\033[33m#\033[00m"); 179 } 180 } 181 else 182 { 183 for(j = 0; j < WIDTH; ++j) 184 { 185 printf("\033[37m#\033[00m"); 186 } 187 } 188 }