Code:
/* earlier version of this is at fuelgraph1.c
This version has bar shifting, and the s-l-i-d-e. ;)
*/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define PI 3.14159
#define FEETMILE 63360
#define MIN 300
#define ROWS 10
#define COLS 30
void slide(char dat[ROWS][COLS]);
void printGraph(double milesPG[], char dat[ROWS][COLS], int min) {
int i,j,r,c,n;
double num=0.0;
for(i=0;i<ROWS;i++) { //set all dat elements to spaces
for(j=0;j<COLS;j++)
dat[i][j]=' ';
}
/* data[][] assignment block */
printf("\n\n\n");
for(i=0,c=0;i<min;i++) {
printf("\n %lf", milesPG[i]);
num=milesPG[i]/5; //to match vertical graduations
for(j=0;j<num;j++) {
dat[j][c]='*';
dat[j][c+1]='*';
dat[j][c+2]='*';
dat[j][c+3]='*';
dat[j][c+4]='*';
}
c+=6;
}
//print out block
printf("\n\n");
for(r=ROWS-1,n=45;r>-1;r--) {
printf(" %2d| ", n);
for(c=0;c<COLS;c++) {
printf("%c",dat[r][c]);
}
putchar('\n');
n-=5;
}
printf(" -----------------------------------\n");
printf(" 00-05 05-10 10-15 15-20 20-25 25-30\n");
}
void slide(char dat[ROWS][COLS]) {
int c,i,j,k,n,x,y,r,maxcol=79,before,after;
char aux[ROWS][5];
for(i=0;i<ROWS;i++)
for(j=0;j<5;j++)
aux[i][j]=' ';
//shift the columns
for(i=0,j=0;i<ROWS;i++) { //copy to aux
for(j=0;j<COLS-12;j++) {
if(i<ROWS && j<5)
aux[i][j]=dat[i][j];
dat[i][j]=dat[i][j+6];
}
}
//print out shifted graph
printf("\n\n");
for(r=ROWS-1,n=45;r>-1;r--) {
printf(" %2d| ", n);
for(c=0;c<COLS;c++) {
printf("%c",dat[r][c]);
}
putchar('\n');
n-=5;
}
printf(" -----------------------------------\n");
printf(" 00-05 05-10 10-15 15-20 20-25 25-30\n");
//print out aux w/s-l-i-d-e ;)
x=18; //18 = our target y
y=ROWS+3;
before = x; after=0;
for(k=0;k<maxcol-x;k++) {
for(i=ROWS-1,j=0;i>-1;i--) {
gotoxy(x,y);
before=x;
while(++before<maxcol-k) putchar(' ');
if(aux[i][j]=='*') {
putchar('*');
++j;
}
after=before+4;
gotoxy(after, y++);
while(after++<maxcol)
putchar(' ');
}
y=ROWS+3;
delay(30);
}
}
int main() {
int i, j, min, inches, length;
double allRev=0, allGas=0, rev, gas;
char file[20];
char dat[ROWS][COLS];
FILE *fp;
double mpg[MIN];
printf("\n\n\n");
printf("What file stores the car data?\n");
scanf("%s", &file); printf("\n");
getchar();
/*
//for IDE debug only
strcpy(file, "fuelmpg.txt"); //for debug in IDE only
length = strlen(file);
if(file[length-1]=='\n') // ditto
file[length-1]='\0';
*/
fp = fopen(file, "r");
if(fp==NULL) {
printf("\nError opening %s\n", file);
return 1;
}
fscanf(fp, "%d", &min);
fscanf(fp, "%d", &inches);
min = min/5;
i =allRev=allGas=0;
while(i < min) {
for(j=0;j<300;j++) {
fscanf(fp, "%lf%lf", &rev, &gas);
allRev+=rev, allGas+=gas;
}
mpg[i] = ( PI * inches * 2 * allRev / FEETMILE ) / allGas;
allRev=0, allGas=0;
++i;
}
fclose(fp);
printGraph(mpg, dat, min);
slide(dat);
gotoxy(30,23); printf("press enter when ready");
(void) getchar();
return 0;
}