generated html version of polls.cHOME

#include "stdio.h"
#include "math.h"
#include "stdlib.h"
//                 0   1   2   3   4   5   6   7   8
char *hgraph[] = {" ","▏","▎","▍","▌","▋","▊","▉","█"};

void draw2bars(int w,double r,double b){
	int i;
	b=1-b;
	double single=1.0/w;
	printf("%s","<span class='m94'>");
	for(i=0;i<w;i++){
		double before=(double)i/w;
		double after=(double)(i+1)/w;
		if(r>after)printf("%s","█");
		else if(r>before && b>after)printf("%s</span><span class='m30 m101'>",hgraph[(int)floor((r-before)/single*8)]);
		else if(r>before)printf("<span class='m101'>%s</span></span><span class='m91'>","▒");
		else if(b>after)printf("%s","█");
		else if(b>before)printf("%s</span><span class='m91'>",hgraph[(int)floor((b-before)/single*8)]);
		else printf("%s","█");
	}
	printf("%s","</span>");
}

void drawbar(int w,double r){
	int i;
	double single=1.0/w;
	for(i=0;i<w;i++){
		double before=(double)i/w;
		double after=(double)(i+1)/w;
		if(r>after)printf("%s","█");
		else if(r>before)printf("%s",hgraph[(int)floor((r-before)/single*8)]);
		else printf("%s"," ");
	}
}


//                      jan feb mar apr may jun jul aug sep oct nov dec
int monthtodays[] = {0,  0, 31, 60, 91,121,152,182,213,244,274,305,335};

#define datetoday(m,d) (monthtodays[m]+d)

int monthofday(int d){
	int m=1;
	while(d > monthtodays[m+1])m++;
	return m;
}

int main(){

	FILE *f = stdin;
	char line[200];
	char name[20];
	fgets(line,200,f);
	int mon1,day1,mon2,day2;
	sscanf(line,"%d-%d %d-%d",&mon1,&day1,&mon2,&day2);
	int startday = datetoday(mon1,day1);
	int endday = datetoday(mon2,day2);
	int length = endday-startday+1;
	double *total=calloc(length,sizeof(double));
	double *trump=calloc(length,sizeof(double));
	double *clinton=calloc(length,sizeof(double));
	int num,pctr,pccl;
	while(fgets(line,200,f)){
		sscanf(line,"%20s %d-%d %d-%d %d %d %d",name,&mon1,&day1,&mon2,&day2,&num,&pccl,&pctr);
		day1 = datetoday(mon1,day1);
		day2 = datetoday(mon2,day2);
		int ndays = day2-day1+1;
		int i;
		for(i=day1;i<=day2;i++){
			if(i<startday)continue;
			int j = i - startday;
			total[j] += (double)num/ndays;
			trump[j] += (double)num/ndays*pctr/100;
			clinton[j] += (double)num/ndays*pccl/100;
		}
	}
	int i;
//		   012345678901234567890123456789
	puts("<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />");
	puts("<link rel=\"stylesheet\" href=\"my.css\">");
	puts("<title>Poll average for election - orenwatson.be</title>");
	puts("<h1>Poll average of the election <a class='right' href=\"/\">HOME</a></h1>");
	puts("<pre>");
	puts("<a href=\"/polls.txt.htm\">raw data</a>");
	puts("<a href=\"/polls.c.htm\">poll averaging program</a>");
	printf("Clinton                        Trump Edge\n");
	printf("   ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏   ▏  ▏  ▏\n");
	for(i=0;i<length;i++){
		int month = monthofday(startday+i);
		int day = startday+i-monthtodays[month];
		double trf = trump[i]/total[i];
		double clf = clinton[i]/total[i];
		printf("%.0f ",clf*100);
		draw2bars(30,clf,trf);
		printf(" %.0f ",trf*100);
		if(clf>trf){
			printf("<span class='m94'>");
			drawbar(6,(clf-trf)*5);
			printf("</span>");
		}else{
			printf("<span class='m91'>");
			drawbar(6,(trf-clf)*5);
			printf("</span>");
		}
		printf(" %02d-%02d %.2f voters\n",month,day,total[i]);
	}
	printf("   ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏  ▏</pre>");
}