paycheck.cpp
This program will run in Borland Turbo C++ 3.0 (it uses BGI) along with the files:
ginit.cpp and gutility.cpp
//************************************************
//*paycheck.cpp *
//*C/Turbo C++ 3.0 *
//*Program reads in an ascii file: pie.dat and *
//*creates a pie graph displaying what *
//*percentages were used, along with a key. *
//* *
//************************************************
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
#include <math.h>
#include <graphics.h>
#include <iomanip.h> //for setw
#include <string.h> //for string functions
//references files on floppy disk (a:)
#include "a:Gutility.cpp" //Graphics utilities
#include "a:Ginit.cpp" //Graphic initializers
#define OFFSET 1
void readln(FILE*, char*);
void readln(FILE*);
void pause(int=LIGHTCYAN);
main()
{
opengraph();
//opens ascii file on floppy disk (a:)
FILE* inpaycheckfile = fopen("a:pie.dat", "r");//opens file to read it
if (!inpaycheckfile) //makes sure file will open
{
cerr<<"ERROR---File could not be opened."<<endl;
exit(1);
}
int far *xvalue, *yvalue;
int xcenter,ycenter,bdistx,bdisty,counter;
xcenter = getmaxx()/2-100; //offset by 100
ycenter = getmaxy()/2;
int radius = 150;
int top,bottom,aramt[10];
char title[15];
char category[25];
float amount ,total;
char spercent[10];
char samount[15];
char persymbol[]="%"; //string contains % symbol
char decimal[]=".00"; //string contains .00
char *arcat[10];
bottom = total=0;
bdistx = xcenter+radius+20;
top = ycenter-radius;
bottom = ycenter +radius;
int boxheight= (bottom-top)/8;//figures size of boxes in legend
top +=boxheight;
//8 because n+1
int filler,color;
color = 63;//sets color to white
filler = 1;
//print_header(1,color);//1 for program#1, color is white
color = 56;//sets color to dkgrey
int i,pertotal,testnumi;
long piestart,pieend;
long double percent,testnum;
long int pint=0;
long double fpieend =0;
pertotal = 0;
i=percent=piestart=testnum=0;
readln(inpaycheckfile, title); //reads in title
while (!feof(inpaycheckfile))
{
readln(inpaycheckfile, category); //reads in category
size_t len; //allocating space for categories
len=strlen(category);
arcat[i]=new char[len];
strncpy(arcat[i],category,len);
fscanf(inpaycheckfile, "%f", &amount);//reads in amount
aramt[i] = amount; //puts amount into an array
total += amount; //figures total amount
i++; //increments arrays(ALSO KEEPS TRACK OF LAST ARRAY NUMBER
readln(inpaycheckfile);
}
fclose(inpaycheckfile); //closes file
outtextxy(xcenter,bottom,title); //outputs title
bdisty = top+boxheight;
for(int j = 0; j < i;j++) //while the number enter from the file into the array
{
percent=((100L*aramt[j])/total); //figures percentage
pertotal+=percent; //figures total percentage
fpieend = (pertotal/100.*360); //figures the ending angle(long double)
pieend = fpieend; //figures the ending angle (int)
setfillstyle(filler,color); //sets fill style
if (pieend>360 || j == i-1) //checks ending angle >360
{ pieend=360;
}
pieslice(xcenter,ycenter,piestart,pieend, radius);
bar3d(bdistx, top, bdistx+boxheight+5, bdisty,0,0);//drawing boxes for legends
settextjustify(0,1); //hor-left ver-center
outtextxy(bdistx+boxheight+15,top+5,arcat[j]);//outputs category array of strings
//THIS ROUNDS THE PERCENTAGE
testnum = ( (percent*10) );//testnum is used to figure out whether to round up or not
testnumi=testnum;
if ( (testnumi%10) >= 5 )
pint = ceil(percent); //rounds up
else
pint = floor(percent); //rounds down
ltoa(pint,spercent,10); //converts percent(int) into a string
strcat(spercent,persymbol); //adds the % symbol to the spercent
outtextxy(bdistx+boxheight+15,top+15,spercent);//outputs percent string
settextjustify(2,1); //hor-right ver-center
itoa(aramt[j],samount,10);
strcat(samount,decimal); //adds .00 to samount
outtextxy(bdistx+boxheight+100,top+15,samount);
piestart=ceil(pieend); //resets start ang to prev end ang
filler++; //changes fill pattern
color++; //changes color
bdisty+=boxheight; //increments box yvalue
top +=boxheight;
}
pause();
closegraph();
return 0;
}
void readln(FILE* f, char *s)
{
char ch;
int i = 0;
if (!feof(f) )
ch = fgetc(f);
while (!feof(f) && (ch != '\n') && (ch != '\r'))
{
s[i++] = ch;
ch = fgetc(f);
}
s[i] = NULL;
}
void readln(FILE* f)
{
char ch;
if (!feof(f))
ch = fgetc(f);
while (!feof(f) && (ch != '\n') && (ch != '\r'))
ch = fgetc(f);
}
void pause(int color)
{
char* prompt = "Press any key to continue...";
int old_color = getcolor();
fflush(stdin);
setcolor(color);
settextjustify(LEFT_TEXT, BOTTOM_TEXT);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
outtextxy(OFFSET, getmaxy()-OFFSET, prompt);
getch();
bar(0, getmaxy()-textheight(prompt)-OFFSET, textwidth(prompt)+OFFSET, getmaxy());
setcolor(old_color);
}
pie.dat
Pay Check Net 2000.00 Annuities 50.00 Withholding Tax 300.00 FICA 150.00 Retirement 200.00 Insurance 85.00 Savings 200.00