#include "stdio.h"
#include "conio.h"
#include "process.h"
#include "graphics.h"
float angle(int,int);
void main()
{
int gd = DETECT,gm,midx,midy,temp,errorcode;
int comp,ele,civil,arc,start_angle,end_angle,total;
float percent;
char convert[6];
initgraph(&gd,&gm,"c:\\tc\\bgi");
errorcode = graphresult();
if (errorcode != grOk) /* An Error Occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* Terminate with an error code */
}
midx = getmaxx()/2;
midy = getmaxy()/2;
/* Input Data and Calculates Total */
do
{
cleardevice();
setcolor(RED);
outtextxy(15,10,"ENTER NUMBER OF STUDENTS ON FOLLOWING FACULTIES");
setcolor(BROWN);
outtextxy(15,38,"Computer :");
gotoxy(17,3);
scanf("%d",&comp);
outtextxy(15,53,"Electronics :");
gotoxy(17,4);
scanf("%d",&ele);
outtextxy(15,68,"Civil :");
gotoxy(17,5);
scanf("%d",&civil);
outtextxy(15,83,"Architect :");
gotoxy(17,6);
scanf("%d",&arc);
total = comp+ele+civil+arc;
/* Formats and Makes a Piechart with the help of */
/* function pieslice() and simple mathmatical calculations */
setcolor(RED);
rectangle(70,150,midx+260,midy+150);
setcolor(BLACK);
setfillstyle(SOLID_FILL,GREEN);
start_angle = 0;
end_angle = start_angle+angle(comp,total);
if(start_angle >= end_angle)
; /* Does Nothing */
else
pieslice(midx-120,midy+30,start_angle,end_angle,100); /* Draw a Sector */
start_angle = end_angle;
end_angle = start_angle+angle(ele,total);
if(start_angle >= end_angle)
;
else
{
setfillstyle(SOLID_FILL,BLUE);
pieslice(midx-120,midy+30,start_angle,end_angle,100);
}
start_angle = end_angle;
end_angle = start_angle+angle(civil,total);
if(start_angle >= end_angle)
;
else
{
setfillstyle(SOLID_FILL,BROWN);
pieslice(midx-120,midy+30,start_angle,end_angle,100);
}
start_angle = end_angle;
end_angle = 360;
if(start_angle >= end_angle)
;
else
{
setfillstyle(SOLID_FILL,CYAN);
pieslice(midx-120,midy+30,start_angle,end_angle,100);
}
/* Draw Bars */
setfillstyle(SOLID_FILL,GREEN);
bar(midx+50,midy-30,midx+60,midy-20);
setfillstyle(SOLID_FILL,BLUE);
bar(midx+50,midy-10,midx+60,midy);
setfillstyle(SOLID_FILL,BROWN);
bar(midx+50,midy+10,midx+60,midy+20);
setfillstyle(SOLID_FILL,CYAN);
bar(midx+50,midy+30,midx+60,midy+40);
/* Displays Informations with Percentage */
setcolor(LIGHTBLUE);
outtextxy(midx+75,midy-28,"Computer");
percent = ((float) comp/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy-28,convert);
outtextxy(midx+75,midy-8,"Electronics");
percent = ((float) ele/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy-8,convert);
outtextxy(midx+75,midy+13,"Civil");
percent = ((float) civil/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy+13,convert);
outtextxy(midx+75,midy+33,"Architect");
percent = ((float) arc/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy+33,convert);
setcolor(RED);
outtextxy(midx-90,midy+200,"Press \"ESC\" to stop.");
} while(getch() != 27);
/* Clean Up */
closegraph();
}
float angle(int x,int total)
{
float degree;
degree = ((float)x/total) * 360; /* Converts the data to degree */
return degree;
}
Friday, September 4, 2009
PROGRAM TO MAKE A PIECHART
Labels: c graphics, Nice Programs
Posted by Unknown at 1:06 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment