/**************Welcome!*****************/
/* Composed by Zhaowei 赵威 */
/* Shixiaobing 石小兵 */
/* Computer Science 0405 */
/***************************************/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <graphics.h>
#include <math.h>
#include <time.h>
#define interface(intername,internum) intername ## internum
#define STEP 260
void showPI();
void mypaint(void);
void creatPI(void);
void interface1 (void);
void interface2 (void);
void interface3 (void);
float PI=0,n1=0,a=0,b,x,y,n=0,l=2,N=20000,L=2,PO=0;
int k,j,i;
int main(void)
{
mypaint();
creatPI();
showPI();
getch();
return 0;
}
void mypaint(void)
{
char til[]="This a programme to creat PI.";
char tl[]="l=";
char tL[]="L=";
char tN[]="N=";
char tn[]="n=";
char tPI[]="PI=";
char tI[]="Infor";
char tE[]="ENTER";
int gdriver=DETECT,grapmode;
initgraph(&gdriver,&grapmode,"c://tc");
cleardevice();
setbkcolor(WHITE);
setcolor(BLUE);
rectangle(180,20,480,280);
outtextxy(190,30, til);
line (180,40,480,40);
line (180,240,280,240);
line (350,240,480,240);
rectangle(330,80,230,100);
outtextxy(200,90,tl);
rectangle(330,120,230,140); /* the 2nd sqr "L=" */
outtextxy(200,125,tL);
rectangle(330,160,230,180);
outtextxy(200,165,tN);
rectangle(470,105,380,125); /*the 4st sqr*/
outtextxy(350,110,tn);
rectangle(470,150,380,170);
outtextxy(350,158,tPI);
outtextxy(297,240,tI);
setcolor(8);
rectangle(362,202,262,222);
setfillstyle(1,8);
floodfill(300,210,8);
setcolor(BLUE);
rectangle(360,200,260,220);
setfillstyle(1,LIGHTBLUE);
floodfill(300,210,BLUE);
setcolor(8);
outtextxy(281,208,tE);
interface(interface,1) ();
interface(interface,2) ();
interface(interface,3) ();
}
void interface1 (void)
{
char c[10]={0};
char cc;
int i;
setfillstyle(1,LIGHTGRAY);
floodfill(279,90,BLUE);
setcolor(6);
outtextxy(220,260,"Please input an integer.");
i=0;
setcolor(5);
while( (cc=getch())!='/r' &&i<=8)
{
if(isdigit(cc))
{
c[i]=cc;
outtextxy(235+8*i,88,c+i);
i++;
}
}
l= atoi(c);
setfillstyle(1,WHITE);
floodfill(279,90,BLUE);
outtextxy(235,88,c);
}
void interface2 (void)
{
char c[10]={0};
char cc;
int i;
setfillstyle(1,LIGHTGRAY);
floodfill(300,130,BLUE);
setfillstyle(1,WHITE);
bar(215,255,460,275);
setcolor(2);
outtextxy(220,260,"/"L/" should be bigger than /"l/".");
i=0;
setcolor(5);
while( (cc=getch())!='/r' &&i<=8)
{
if(isdigit(cc))
{
c[i]=cc;
outtextxy(235+8*i,128,c+i);
i++;
}
}
L = atoi(c);
setfillstyle(1,WHITE);
floodfill(300,130,BLUE);
outtextxy(235,128,c);
}
void interface3 (void)
{
char cc;
char c[10]={0};
int i;
setfillstyle(1,LIGHTGRAY);
floodfill(300,170,BLUE);
setfillstyle(1,WHITE);
bar(215,255,460,275);
setcolor(4);
outtextxy(220,260,"/"N/" should be 10--30000.");
i=0;
setcolor(5);
while( (cc=getch())!='/r' &&i<=8)
{
if(isdigit(cc))
{
c[i]=cc;
outtextxy(235+8*i,168,c+i);
i++;
}
}
N= atoi(c);
setfillstyle(1,WHITE);
floodfill(300,170,BLUE);
outtextxy(235,168,c);
}
void creatPI (void)
{
setfillstyle(1,WHITE);
bar(210,255,460,275);
setcolor(13);
outtextxy(240,260,"Calculating...");
srand((unsigned int) time(NULL));
for(j=1;j<=10;j++)
{ n=0;
for (k=0;k<=N;k++)
{
b=rand()%(958)+rand()%9*0.1+
rand()%9*0.01+rand()%9*0.001+
rand()%9*0.0001+rand()%9*0.00001+
rand()%9*0.000001+rand()%9*0.0000001+
rand()%9*0.00000001+rand()%9*0.000000001+
rand()%9*0.0000000001+rand()%9*0.00000000001;
x=rand()%10000;
y=rand()%10000;
a=l*x/sqrt(x*x+y*y);
for(i=1;i<=STEP;i++)
if (b+a>=2*L*i&&b-a<=2*L*i)
{
n++;
break;
}
}
PO=2*N*l/(n*L);
PI+=PO;
n1+=n;
}
PI=PI/10;
n=n1/10;
}
void showPI(void)
{
char pi[20];
setfillstyle(1,WHITE);
bar(210,255,460,275);
setcolor(6);
outtextxy(210,260,"Thanks! Press any key to exit.");
setcolor(12);
sprintf(pi,"%f",PI);
outtextxy(382,158,pi);
sprintf(pi,"%d",(int)n);
outtextxy(382,110,pi);
}