#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <iostream.h>
#define MAXPTS 1000
#define W 1000
#define H 700
#define DW 10
#define DH 10
#define nx 10
#define ny 10
#define PI 3.1415926
struct PTS
{
double x;
double y;
};
struct PTS p_water[MAXPTS];
struct PTS p_concrete[MAXPTS];
struct PTS p_steel[MAXPTS];
struct PTS p_lead[MAXPTS];
double func_water(double a);
double func_concrete(double a);
double func_steel(double a);
double func_lead(double a);
void draw_water(void);
void draw_concrete(void);
void draw_steel(void);
void draw_lead(void);
void draw_all(void);
void main()
{
cout<<"函数操作说明:"<<endl;
cout<<"鼠标左键选择材料厚度,右键退出"<<endl;
cout<<"Press any key to continue..."<<endl;
getch();
initgraph(W,H);
HWND hwnd=GetHWnd();
SetWindowText(hwnd,"几种常用材料的中子减弱函数近似计算");
draw_all();
MOUSEMSG m;
struct PTS p_cross1,p_cross2,p_cross3,p_cross4;
while(true)
{
m=GetMouseMsg();
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
{
p_cross1.x=m.x;
p_cross2.x=m.x;
p_cross3.x=m.x;
p_cross4.x=m.x;
p_cross1.y=func_water(p_cross1.x);
p_cross2.y=func_concrete(p_cross2.x);
p_cross3.y=func_steel(p_cross3.x);
p_cross4.y=func_lead(p_cross4.x);
draw_all();
char str1[20]="材料厚度(mm):",str2[20];
gcvt(p_cross1.x,3,str2);
strcat(str1,str2);
outtextxy(W/2+100,DH+40,str1);
char str3[20]="水的减弱因子为:",str4[20];
gcvt(p_cross1.y,3,str4);
strcat(str3,str4);
outtextxy(W/2+100,DH+60,str3);
char str5[20]="混凝土的减弱因子为:",str6[20];
gcvt(p_cross2.y,3,str6);
strcat(str5,str6);
outtextxy(W/2+100,DH+80,str5);
char str7[20]="钢铁的减弱因子为:",str8[20];
gcvt(p_cross3.y,3,str8);
strcat(str7,str8);
outtextxy(W/2+100,DH+100,str7);
char str9[20]="铅的减弱因子为:",str10[20];
gcvt(p_cross4.y,3,str10);
strcat(str9,str10);
outtextxy(W/2+100,DH+120,str9);
p_cross1.x = DW + (p_cross1.x)*(W-2*DW)/1000.0;
p_cross1.y = (H-DH) - (p_cross1.y)*(H-2*DH);
p_cross2.x = DW + (p_cross2.x)*(W-2*DW)/1000.0;
p_cross2.y = (H-DH) - (p_cross2.y)*(H-2*DH);
p_cross3.x = DW + (p_cross3.x)*(W-2*DW)/1000.0;
p_cross3.y = (H-DH) - (p_cross3.y)*(H-2*DH);
p_cross4.x = DW + (p_cross4.x)*(W-2*DW)/1000.0;
p_cross4.y = (H-DH) - (p_cross4.y)*(H-2*DH);
setcolor(RGB(0,225,130));
line(p_cross1.x,DH,p_cross1.x,H-DH);
setcolor(RGB(255,0,0));
line(p_cross1.x-5,p_cross1.y,p_cross1.x+5,p_cross1.y);
line(p_cross2.x-5,p_cross2.y,p_cross2.x+5,p_cross2.y);
line(p_cross3.x-5,p_cross3.y,p_cross3.x+5,p_cross3.y);
line(p_cross4.x-5,p_cross4.y,p_cross4.x+5,p_cross4.y);
break;
}
case WM_RBUTTONUP:
return;
}
}
getch();
closegraph();
}
double func_water(double a)
{
double b;
b=exp(-0.11*a*0.1);
return(b);
}
double func_concrete(double a)
{
double b;
b=exp(-0.083*a*0.1);
return(b);
}
double func_steel(double a)
{
double b;
b=exp(-0.063*a*0.1);
return(b);
}
double func_lead(double a)
{
double b;
b=exp(-0.042*a*0.1);
return(b);
}
void draw_water(void)
{
p_water[0].x=0;
p_water[0].y=1;
for(int i=1;i<MAXPTS;i++)
{
p_water[i].x=i;
p_water[i].y=func_water(i);
}
for(i=0;i<MAXPTS;i++)
{
p_water[i].x = DW + (p_water[i].x)*(W-2*DW)/1000.0;
p_water[i].y = (H-DH) - (p_water[i].y)*(H-2*DH);
}
for(i=0;i<MAXPTS-1;i++)
{
moveto(p_water[i].x,p_water[i].y);
lineto(p_water[i+1].x,p_water[i+1].y);
}
}
void draw_concrete(void)
{
p_concrete[0].x=0;
p_concrete[0].y=1;
for(int i=1;i<MAXPTS;i++)
{
p_concrete[i].x=i;
p_concrete[i].y=func_concrete(i);
}
for(i=0;i<MAXPTS;i++)
{
p_concrete[i].x = DW + (p_concrete[i].x)*(W-2*DW)/1000.0;
p_concrete[i].y = (H-DH) - (p_concrete[i].y)*(H-2*DH);
}
for(i=0;i<MAXPTS-1;i++)
{
moveto(p_concrete[i].x,p_concrete[i].y);
lineto(p_concrete[i+1].x,p_concrete[i+1].y);
}
}
void draw_steel(void)
{
p_steel[0].x=0;
p_steel[0].y=1;
for(int i=1;i<MAXPTS;i++)
{
p_steel[i].x=i;
p_steel[i].y=func_steel(i);
}
for(i=0;i<MAXPTS;i++)
{
p_steel[i].x = DW + (p_steel[i].x)*(W-2*DW)/1000.0;
p_steel[i].y = (H-DH) - (p_steel[i].y)*(H-2*DH);
}
for(i=0;i<MAXPTS-1;i++)
{
moveto(p_steel[i].x,p_steel[i].y);
lineto(p_steel[i+1].x,p_steel[i+1].y);
}
}
void draw_lead(void)
{
p_lead[0].x=0;
p_lead[0].y=1;
for(int i=1;i<MAXPTS;i++)
{
p_lead[i].x=i;
p_lead[i].y=func_lead(i);
}
for(i=0;i<MAXPTS;i++)
{
p_lead[i].x = DW + (p_lead[i].x)*(W-2*DW)/1000.0;
p_lead[i].y = (H-DH) - (p_lead[i].y)*(H-2*DH);
}
for(i=0;i<MAXPTS-1;i++)
{
moveto(p_lead[i].x,p_lead[i].y);
lineto(p_lead[i+1].x,p_lead[i+1].y);
}
}
void draw_all(void)
{
cleardevice();
setcolor(RGB(255,0,0));
rectangle(DW,DH,W-DW,H-DH);
double value;
char stt[10];
double xstep=(W-2*DW)/nx;
double ystep=(H-2*DH)/ny;
for(int i=0;i<=nx;i++)
{
setcolor(RGB(255,255,255));
line(DW+xstep*i,H-DH,DW+xstep*i,H-DH-5);
value=1000.0*i/nx;
gcvt(value,3,stt);
outtextxy(DW+xstep*i-10,H-DH-20,stt);
}
for(i=0;i<=ny;i++)
{
setcolor(RGB(255,255,255));
line(DW,DH+ystep*i,DW+5,DH+ystep*i);
value=1.0-1.0*i/ny;
gcvt(value,3,stt);
outtextxy(DW+15,DH+ystep*i,stt);
}
setcolor(RGB(0,225,130));
draw_water();
setcolor(RGB(0,225,130));
draw_concrete();
setcolor(RGB(0,225,130));
draw_steel();
setcolor(RGB(0,225,130));
draw_lead();
}
