comparison src/grph.c @ 6635:5339a7aef477

<oerjan> ` mv grph.c src
author HackBot
date Fri, 05 Feb 2016 00:11:52 +0000
parents grph.c@e2234a3876d1
children
comparison
equal deleted inserted replaced
6634:33eab2cd9644 6635:5339a7aef477
1 #include "stdio.h"
2 #include "string.h"
3 #include "stdlib.h"
4 #include "math.h"
5 char *vgraph[] = {" ","▁","▂","▃","▄","▅","▆","▇","█"};
6 char *hgraph[] = {"█","▉","▊","▋","▌","▍","▎","▏"," "};
7 int main(int argc,char**argv){
8 int h = 1;
9 int w = 80;
10 int color = 0,ori = 0,zebra=0,label=0;
11 if(argc >= 2&&argv[1][0]=='-'){
12 if(strchr(argv[1],'c'))color = 1;
13 if(strchr(argv[1],'z'))zebra = 1;
14 if(strchr(argv[1],'h'))ori = 1;
15 if(strchr(argv[1],'l'))label = 1;
16 argv++;argc--;
17 }
18 if(ori)h=10;
19 if(argc >= 2)h = strtol(argv[1],0,0);
20 if(argc >= 3)w = strtol(argv[2],0,0);
21 float *x=malloc(sizeof(float)*w);
22 int i,c,j;
23 float M = -HUGE_VALF;
24 float m = HUGE_VALF;
25 for(i=0;i<w;i++){
26 c=scanf(" %f",x+i);
27 if(c<1)break;
28 if(x[i]>M)M=x[i];
29 if(x[i]<m)m=x[i];
30 }
31 if(argc >= 4)sscanf(argv[3],"%f",&m);
32 if(argc >= 5)sscanf(argv[4],"%f",&M);
33 w = i;
34 float l = (M-m)/h;
35 if(ori == 0){//vertical, default
36 for(j=h-1;j>=0;j--){
37 if(color)fputs("\e[44m",stdout);
38 for(i=0;i<w;i++){
39 float r = x[i]-m-l*j;
40 if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout);
41 if(r>l){
42 fputs(vgraph[8],stdout);
43 }else if(r>0){
44 fputs(vgraph[(int)(r/l*8+0.5)],stdout);
45 }else{
46 fputs(" ",stdout);
47 }
48 }
49 if(color||zebra)fputs("\e[0m",stdout);
50 if(label&&j==h-1)printf(" %f",M);
51 if(label&&j==0)printf(" %f",m);
52 putchar(10);
53 }
54 }else{
55 for(i=0;i<w;i++){
56 if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout);
57 if(color)fputs("\e[44m",stdout);
58 for(j=0;j<h;j++){
59 float r = x[i]-m-l*j;
60 if(r>l){
61 fputs(hgraph[0],stdout);
62 }else if(r>0){
63 fputs(hgraph[8-(int)(r/l*8+0.5)],stdout);
64 }else{
65 fputs(hgraph[8],stdout);
66 }
67 }
68 if(color||zebra)fputs("\e[0m",stdout);
69 putchar(10);
70 }
71 int n;
72 if(label){
73 printf("%f%n",m,&n);
74 while(n++<h-1)putchar(' ');
75 printf("▕ %f",M);
76 }
77 }
78 }