bar đồ họa
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
#include<dos.h>
#include<alloc.h>
#define PI 3.14128
char xau[10][50]={ " MENU CHINH CHUONG TRINH ",
"1.Nhap tep",
"2.Ve hinh Bar",
"3.Ve hinh Sin",
"4.Ve hinh Cos",
"5.Thoat"
};
void nhaptep()
{
cleardevice();
setbkcolor(BLUE);
setcolor(2);
int n,m,i,*a;
printf("Nhap so phan tu cua tep:n=");
scanf("%d",&n);
FILE *f;
f=fopen("huy.txt","wt");
for(i=0;i<n;i++)
{
printf("Nhap phan tu thu %d :" ,i+1);
scanf("%d",&m);
fprintf(f,"%d
",m);
}
fclose(f);
fflush(stdin);
fflush(f);
printf("Tep vua nhap vao co gia tri la:
");
f=fopen("huy.txt","rt");
for(i=0;i<n;i++)
{
fscanf(f,"%d",&a[i]);
printf("%d\t",a[i]);
}
fclose(f);
getch();
}
void hinhBar()
{
cleardevice();
FILE *f;
int i=0,n,*a,m;
f=fopen("huy.txt","rt");
while(!feof(f))
{
fscanf(f,"%d",&a[i]);
i++;
}
n=i-1;
fclose(f);
fflush(f);
int kc=0,drc=20;
int max=a[0];
for(i=1;i<n;i++)
{ if(max<abs(a[i]))
max=abs(a[i]);
}
int x=getmaxx()/2-50;
int y=getmaxy()/2;
int hsy=(y-15)/max;
int hsx=(x+50)/(n+1);
hsx=hsx-15;
setbkcolor(11);
setcolor(4);
line(0,y-2,getmaxx()-2,y-2);
line(getmaxx()-2,y-2,getmaxx()-8,y-4);
line(getmaxx()-2,y-2,getmaxx()-8,y);
outtextxy(getmaxx()-12,y+2,"Y");
line(x,2,x,getmaxy()-2);
line(x-2,5,x,2);
line(x+2,5,x,2);
outtextxy(x-8,5,"X");
outtextxy(x+2,y,"0");
char *tg;
for (i=0;i<n;i++)
{
setfillstyle(1,4);
delay(200);
bar(hsx+x+kc,y-2,hsx+x+kc+drc,y-2-a[i]*hsy);
if(a[i]>0)
{ sprintf(tg,"%d",i+1);
outtextxy(hsx+x+kc+10,y+2,tg);
sprintf(tg,"%d",a[i]);
outtextxy(x-15,y-2-a[i]*hsy,tg);
outtextxy(x-2,y-2-a[i]*hsy,"-");
}
else
{
sprintf(tg,"%d",i+1);
outtextxy(hsx+x+kc+10,y-10,tg);
sprintf(tg,"%d",a[i]);
outtextxy(x-24,y-2-a[i]*hsy,tg);
outtextxy(x-2,y-2-a[i]*hsy,"-");
}
kc=hsx+kc+drc;
}
getch();
}
void hinhSin()
{
cleardevice();
int xc=getmaxx()/2,yc=getmaxy()/2;
setbkcolor(0);
setcolor(4);
outtextxy(50,40,"VE DO THI HINH SIN:Y=SIN(X)");
setviewport(xc,yc,xc*2,yc*2,0);
line(0,-yc,0,yc);
line(-3,-yc+5,0,-yc+2);
line(3,-yc+5,0,-yc+2);
outtextxy(-8,-yc+8,"Y");
line(-xc,0,xc,0);
line(xc-2,0,xc-5,3);
line(xc-2,0,xc-5,-3);
outtextxy(xc-10,8,"X");
outtextxy(-8,3,"0");
double canduoi=-PI*100;
double cantren=-canduoi,x,y;
for(x=canduoi;x<=cantren;x++)
{
delay(10);
y=90*sin(x/50);
putpixel(x,y,4);
}
setviewport(0,0,0,0,0);
outtextxy(50,yc*2-90,"GIAO VIEN HUONG DAN:");
outtextxy(50,yc*2-80,"-------------------");
outtextxy(50,yc*2-70," ThS: VU VAN NAM ");
outtextxy(50,yc*2-60,"NHOM THUC HIEN:");
outtextxy(50,yc*2-50,"--------------");
outtextxy(50,yc*2-40,"1.DUC HUY");
outtextxy(50,yc*2-30,"2.TRONG QUY");
outtextxy(50,yc*2-20,"3.NGOC CUONG");
outtextxy(50,yc*2-10,"4.MANH CUONG");
getch();
}
void hinhCos()
{
cleardevice();
int xc=getmaxx()/2,yc=getmaxy()/2;
setbkcolor(0);
setcolor(4);
outtextxy(50,40,"VE DO THI HINH COS:Y=COS(X)");
setviewport(xc,yc,xc*2,yc*2,0);
line(0,-yc,0,yc);
line(-3,-yc+5,0,-yc+2);
line(3,-yc+5,0,-yc+2);
outtextxy(-8,-yc+8,"Y");
line(-xc,0,xc,0);
line(xc-2,0,xc-5,3);
line(xc-2,0,xc-5,-3);
outtextxy(xc-10,8,"X");
outtextxy(-8,3,"0");
double canduoi=-PI*100;
double cantren=-canduoi,x,y;
for(x=canduoi;x<=cantren;x++)
{
delay(10);
y=90*cos(x/50);
putpixel(x,y,4);
}
setviewport(0,0,0,0,0);
outtextxy(50,yc*2-90,"GIAO VIEN HUONG DAN:");
outtextxy(50,yc*2-80,"-------------------");
outtextxy(50,yc*2-70," ThS: VU VAN NAM ");
outtextxy(50,yc*2-60,"NHOM THUC HIEN:");
outtextxy(50,yc*2-50,"--------------");
outtextxy(50,yc*2-40,"1.DUC HUY");
outtextxy(50,yc*2-30,"2.TRONG QUY");
outtextxy(50,yc*2-20,"3.NGOC CUONG");
outtextxy(50,yc*2-10,"4.MANH CUONG");
getch();
}
void vekhung(int i)
{
setfillstyle(1,8);
bar(getmaxx()/2-120,60+((i-1)*20),getmaxx()/2+120,80+((i-1)*20));
setcolor(5);
rectangle(getmaxx()/2-120,60+((i-1)*20),getmaxx()/2+120,80+((i-1)*20));
setcolor(14);
outtextxy(getmaxx()/2-100,65+((i-1)*20),xau[i]);
}
void luachon(int j)
{
setcolor(15);
setfillstyle(1,1);
bar(getmaxx()/2-118,62+((j-1)*20),getmaxx()/2+118,78+((j-1)*20));
outtextxy(getmaxx()/2-100,65+((j-1)*20),xau[j]);
}
void main()
{
char c1,c2;
int i,j=1;
int gm,gd=DETECT,n;
initgraph(&gd,&gm,"");
Lap:
cleardevice();
settextstyle(0,0,0);
int xi=getmaxx()/2;
setcolor(15);
outtextxy(200,320,"MENU THUC HIEN CAC CHUC NANG");
outtextxy(350,390," NHOM THUC HIEN:");
outtextxy(350,400,"---------------------");
outtextxy(350,410,"1.TON DUC HUY");
outtextxy(350,420,"2.VU TRONG QUY");
outtextxy(350,430,"3.PHAN NGOC CUONG");
outtextxy(350,440,"4.NGUYEN MANH CUONG");
outtextxy(200,300," MA DE 036 ");
outtextxy(20,390, " GIAO VIEN HUONG DAN ");
outtextxy(20,400, "-----------------------");
outtextxy(20,410, " TH.S:VU VAN NAM ");
settextstyle(7,0,1);
outtextxy(200,270," DE TAI THUC TAP CO SO ");
setbkcolor(3);
settextstyle(0,0,0);
setfillstyle(1,2);
setcolor(4);
setfillstyle(1,6);
bar(xi-120,20,xi+120,40);
setcolor(5);
rectangle(xi-120,20,xi+120,40);
setcolor(13);
outtextxy(xi-120,25,xau[0]);
setcolor(14);
for(i=1;i<=5;i++)
vekhung(i);
luachon(j);
while(1)
{
if ((c1=getch())==0) c2=getch();
if (c1==13 && j==5) break;
else
if (c1==0 && (c2==80 || c2==72))
{
if (c2==80)
{
j++;
if (j>1) vekhung(j-1);
if (j==6)
j=1; luachon(j);
}
else
{
j--;
if (j<5 && j>1)vekhung(j+1);
if (j<1) j=5 ; luachon(j);
}
}
else
if (c1==13)
{
switch(j)
{
case 1: nhaptep();break;
case 2: hinhBar();break;
case 3: hinhSin();break;
case 4: hinhCos();break;
}
}
goto Lap;
}
closegraph();
}
Bạn đang đọc truyện trên: Truyen247.Pro