Mercurial > repo
changeset 6635:5339a7aef477
<oerjan> ` mv grph.c src
author | HackBot |
---|---|
date | Fri, 05 Feb 2016 00:11:52 +0000 |
parents | 33eab2cd9644 |
children | 5789c1bba534 |
files | grph.c src/grph.c |
diffstat | 2 files changed, 78 insertions(+), 78 deletions(-) [+] |
line wrap: on
line diff
--- a/grph.c Thu Feb 04 01:34:02 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -#include "stdio.h" -#include "string.h" -#include "stdlib.h" -#include "math.h" -char *vgraph[] = {" ","▁","▂","▃","▄","▅","▆","▇","█"}; -char *hgraph[] = {"█","▉","▊","▋","▌","▍","▎","▏"," "}; -int main(int argc,char**argv){ - int h = 1; - int w = 80; - int color = 0,ori = 0,zebra=0,label=0; - if(argc >= 2&&argv[1][0]=='-'){ - if(strchr(argv[1],'c'))color = 1; - if(strchr(argv[1],'z'))zebra = 1; - if(strchr(argv[1],'h'))ori = 1; - if(strchr(argv[1],'l'))label = 1; - argv++;argc--; - } - if(ori)h=10; - if(argc >= 2)h = strtol(argv[1],0,0); - if(argc >= 3)w = strtol(argv[2],0,0); - float *x=malloc(sizeof(float)*w); - int i,c,j; - float M = -HUGE_VALF; - float m = HUGE_VALF; - for(i=0;i<w;i++){ - c=scanf(" %f",x+i); - if(c<1)break; - if(x[i]>M)M=x[i]; - if(x[i]<m)m=x[i]; - } - if(argc >= 4)sscanf(argv[3],"%f",&m); - if(argc >= 5)sscanf(argv[4],"%f",&M); - w = i; - float l = (M-m)/h; - if(ori == 0){//vertical, default - for(j=h-1;j>=0;j--){ - if(color)fputs("\e[44m",stdout); - for(i=0;i<w;i++){ - float r = x[i]-m-l*j; - if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout); - if(r>l){ - fputs(vgraph[8],stdout); - }else if(r>0){ - fputs(vgraph[(int)(r/l*8+0.5)],stdout); - }else{ - fputs(" ",stdout); - } - } - if(color||zebra)fputs("\e[0m",stdout); - if(label&&j==h-1)printf(" %f",M); - if(label&&j==0)printf(" %f",m); - putchar(10); - } - }else{ - for(i=0;i<w;i++){ - if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout); - if(color)fputs("\e[44m",stdout); - for(j=0;j<h;j++){ - float r = x[i]-m-l*j; - if(r>l){ - fputs(hgraph[0],stdout); - }else if(r>0){ - fputs(hgraph[8-(int)(r/l*8+0.5)],stdout); - }else{ - fputs(hgraph[8],stdout); - } - } - if(color||zebra)fputs("\e[0m",stdout); - putchar(10); - } - int n; - if(label){ - printf("%f%n",m,&n); - while(n++<h-1)putchar(' '); - printf("▕ %f",M); - } - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/grph.c Fri Feb 05 00:11:52 2016 +0000 @@ -0,0 +1,78 @@ +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +#include "math.h" +char *vgraph[] = {" ","▁","▂","▃","▄","▅","▆","▇","█"}; +char *hgraph[] = {"█","▉","▊","▋","▌","▍","▎","▏"," "}; +int main(int argc,char**argv){ + int h = 1; + int w = 80; + int color = 0,ori = 0,zebra=0,label=0; + if(argc >= 2&&argv[1][0]=='-'){ + if(strchr(argv[1],'c'))color = 1; + if(strchr(argv[1],'z'))zebra = 1; + if(strchr(argv[1],'h'))ori = 1; + if(strchr(argv[1],'l'))label = 1; + argv++;argc--; + } + if(ori)h=10; + if(argc >= 2)h = strtol(argv[1],0,0); + if(argc >= 3)w = strtol(argv[2],0,0); + float *x=malloc(sizeof(float)*w); + int i,c,j; + float M = -HUGE_VALF; + float m = HUGE_VALF; + for(i=0;i<w;i++){ + c=scanf(" %f",x+i); + if(c<1)break; + if(x[i]>M)M=x[i]; + if(x[i]<m)m=x[i]; + } + if(argc >= 4)sscanf(argv[3],"%f",&m); + if(argc >= 5)sscanf(argv[4],"%f",&M); + w = i; + float l = (M-m)/h; + if(ori == 0){//vertical, default + for(j=h-1;j>=0;j--){ + if(color)fputs("\e[44m",stdout); + for(i=0;i<w;i++){ + float r = x[i]-m-l*j; + if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout); + if(r>l){ + fputs(vgraph[8],stdout); + }else if(r>0){ + fputs(vgraph[(int)(r/l*8+0.5)],stdout); + }else{ + fputs(" ",stdout); + } + } + if(color||zebra)fputs("\e[0m",stdout); + if(label&&j==h-1)printf(" %f",M); + if(label&&j==0)printf(" %f",m); + putchar(10); + } + }else{ + for(i=0;i<w;i++){ + if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout); + if(color)fputs("\e[44m",stdout); + for(j=0;j<h;j++){ + float r = x[i]-m-l*j; + if(r>l){ + fputs(hgraph[0],stdout); + }else if(r>0){ + fputs(hgraph[8-(int)(r/l*8+0.5)],stdout); + }else{ + fputs(hgraph[8],stdout); + } + } + if(color||zebra)fputs("\e[0m",stdout); + putchar(10); + } + int n; + if(label){ + printf("%f%n",m,&n); + while(n++<h-1)putchar(' '); + printf("▕ %f",M); + } + } +}