必须在tc中运行
1.#include <stdio.h>
#include<dos.h>
void mysound(unsigned int freq,int times)
{
int i;
union{
int n;
char c[2];
}count;
char bits;
count.n=1193180/freq;
outportb(0x43,0xb6);
outportb(0x42,count.c[0]);
outportb(0x42,count.c[1]);
bits=inportb(0x61);
outportb(0x61,bits|3);
for(i=0;i<times;i++)
delay(250);
outportb(0x61,bits&0xfc);
outportb(0x61,bits);
}
int main()
{
int freq[8]={0,262,294,330,349,392,440,494};
int song[]={1,1,5,5,6,6,5,4,4,3,3,2,2,1,0};
int div[]={8,8,8,8,8,8,4,8,8,8,8,8,8,4,0};
unsigned int f;
int times,i;
for(i=0;song[i];i++)
{
if(song[i]<0)
f=freq[-song[i]]/2;
else
if(song[i]>7)
f=freq[song[i]%7]*2;
else
f=freq[song[i]];
times=16/div[i];
mysound(f,times);
}
return 0;
}
2.正弦曲线
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159
int main()
{
int gmode,gdriver=DETECT,i;
double x,y,originx,originy;
initgraph(&gdriver,&gmode,"c:\tc20\BGI");
line(20,250,600,250);
line(600,250,590,240);
line(600,250,590,260);
line(320,450,320,20);
line(320,20,310,30);
line(320,20,330,30);
setcolor(RED);
for(i=0;i<180;i++)
{
line(320-i,250+150*sin(i*PI/180),320-(i+1),250+150*sin((i+1)*PI/180));
line(320+i,250-150*sin(i*PI/180),320+(i+1),250-150*sin((i+1)*PI/180));
}
getch();
closegraph();
return 0;
}
3.这满天星有点LOW,有时间在改进^^
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <graphics.h>
int main()
{
int driver=DETECT;
int mode,i,n,j,p;
initgraph(&driver,&mode,"C:\TC20\BGI");
srand(time(NULL));
for(i=1;i<640;i++)
{
for(j=1;j<=30;j++)
{
if(j<=15)
{
n=rand()%699;
putpixel(n+i,n+10*i,14);
}
else
{
n=rand()%666;
putpixel(n+11*i,n+i,14);
}
}
}
do
{
x=rand()%996;
y=rand()%777;
delay(10);
putpixel(x,y,0);
delay(10);
putpixel(x,y,14);
} while(!kbhit());
getch();
closegraph();
return 0;
}
4.模拟乒乓
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXNUM 100
int main()
{
char *buf;
char sign,sign1[MAXNUM],sign2[MAXNUM];
int i,size,a;
int x[MAXNUM],y[MAXNUM],r[MAXNUM][MAXNUM];
int gd=DETECT,gm;
clrscr();
scanf("%d",&a);
initgraph(&gd,&gm,"c:\tc20\BGI");
setcolor(YELLOW);
setfillstyle(SOLID_FILL,9);
rectangle(0,0,getmaxx(),getmaxy());
rectangle(1,1,getmaxx()-1,getmaxy()-1);
bar(2,2,getmaxx()-2,getmaxy()-2);
for(i=8;i>=0;i--)
{
setcolor(i+7);
circle(11,getmaxy()-11,i);
}
size=imagesize(2,getmaxy()-20,20,getmaxy()-2);
buf=malloc(size);
getimage(2,getmaxy()-20,20,getmaxy()-2,buf);
for(i=8;i>=0;i--)
{
setcolor(9);
circle(11,getmaxy()-11,i);
}
randomize();
for(i=0;i<a;i++)
{
x[i]=random(600)+20;
y[i]=random(400)+32;
sign1[i]=random(2);
sign2[i]=random(2);
}
while(1)
{
if(kbhit())
{
sign=getch();
if(sign==0x1b)
{
free(buf);
break;
}
}
else
{
for(i=0;i<a;i++)
{
putimage((sign1[i]==1?x[i]++:x[i]--),
(sign2[i]==1?y[i]++:y[i]--),
buf,COPY_PUT);
if(x[i]>getmaxx()-21)
sign1[i]=0;
if(x[i]<3)
sign1[i]=1;
if(y[i]>getmaxy()-21)
sign2[i]=0;
if(y[i]<3)
sign2[i]=1;
}}}
free(buf);
getch();
closegraph();
return 0;}
5.成绩柱状图
#include <graphics.h>
#include <stdio.h>
struct TU
{
int math;
int eng;
int comp;
}s[100];
int main()
{
int gmode,gdriver=DETECT,i,n=2;
initgraph(&gdriver,&gmode,"c:\tc20\BGI");
for(i=0;i<n;i++)
scanf("%d%d%d",&s[i].math,&s[i].eng,&s[i].comp);
line(50,450,600,450);
line(50,450,50,50);
line(50,50,40,60);
line(50,50,60,60);
line(600,450,590,440);
line(600,450,590,460);
for(i=0;i<n;i++)
{
setfillstyle(2,9);
bar3d(80+(500/n+5)*i,440-s[i].math*4-s[i].math%10,80+(500/n+5)*i+500/n/3,440,500/n/3,1);
setfillstyle(3,8);
bar3d(80+(500/n+5)*i+500/n/3,440-s[i].eng*4-s[i].eng%10,80+(500/n+5)*i+500/n/3+500/n/3,440,500/n/3,1);
setfillstyle(4,6);
bar3d(80+(500/n+5)*i+500/n/3*2,440-s[i].comp*4-s[i].comp%10,80+(500/n+5)*i+500/n/3+500/n/3*2,440,500/n/3,1);
}
getch();
closegraph();
return 0;
}