论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

弹簧单摆的模拟演示

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:42:43

#include<graphics.h>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define A 480
#define B 120
main()
{
 char q;
 int gdriver=VGA,gmode=VGAHI;
 int i,cl=0,w=1;
 float x,y,v1,v2,a1,a2,L,M,m,k,X,X0,F,F1,F2,g,c,s,G,G1,G2;
 float sp=2;
 initgraph(&gdriver,&gmode,"");

BG: cleardevice();
 setcolor(5);
 setbkcolor(0);
 settextstyle(3,0,4);
 outtextxy(80,50,"Single Pendulum Parameters Input");
 setcolor(1);
 rectangle(70,50,600,90);
        setcolor(2);
 rectangle(65,45,605,95);
 setcolor(14);
 rectangle(60,40,610,100);
 setcolor(5);
 moveto(100,100);
 gotoxy(49,12);printf("");
 settextstyle(1,0,2);
 outtextxy(80,130,"PLEASE INPUT :  ( 0<L<120 )");
 setcolor(15);
 line(50,160,620,160);
 setcolor(5);
 outtextxy(80,170,"The Length Of Pendulum L : ");
 scanf("%f",&L);
 gotoxy(35,15);printf("");
 settextstyle(1,0,2);
 outtextxy(80,205,"Elastic Coefficient k : ");
 gotoxy(40,14);printf("");
 scanf("%f",&k);
 outtextxy(80,250,"The Quality Of Spherule  M :");
 gotoxy(50,17);printf("");
 scanf("%f",&M);
 outtextxy(80,297,"The Quality Of Pedulum Line  m : ");
 gotoxy(58,20);printf("");
 scanf("%f",&m);
 setcolor(15);
 line(50,380,620,380);
 setcolor(5);
 outtextxy(60,400,"Press E to Quit AND Press Any Other Key to Go on");
 q=getch();
 if(q=='e'||q=='E') goto END;

 cleardevice();

GN: setcolor(2);
 setbkcolor(0);
 g=9.8;G=m*g;x=A+L;y=B;v1=0;v2=0;

 settextstyle(1,0,3);
 outtextxy(380,20,"Single Pendulum");
 settextstyle(1,0,1);
 outtextxy(390,60,"(with a spherule)");
 settextstyle(1,0,3);
 outtextxy(20,40,"S-Pendulum Parameters :");
 settextstyle(1,0,1);
 moveto(100,90);
 gotoxy(29,7);printf("%.1f",L);
 outtextxy(30,90,"Length of line  L :");
 gotoxy(30,9);printf("%.2f",k);
 outtextxy(30,123,"P-Coefficient  k :");
 gotoxy(30,11);printf("%.2f",M);
 outtextxy(30,156,"Spherule Quality M :");
 gotoxy(30,13);printf("%.2f",m);
 outtextxy(30,189,"Line Quality m :");
 gotoxy(32,15);printf("%d",w);
 outtextxy(30,222,"Control Increment w : ");

 settextstyle(2,0,6);
 outtextxy(25,290,"Exegesis :");
 settextstyle(2,0,4);
 outtextxy(30,320,"  Demo Speed : A -- accelerate , S -- decelerate");
 outtextxy(30,340,"   SP-Length : - -- decrease   , + -- increase  ");
 outtextxy(30,360," Coefficient : < -- decrease   , > -- increase  ");
 outtextxy(30,380," Sph-Quality : Z -- decrease   , X -- increase  ");
 outtextxy(30,400,"Line-Quality : C -- decrease   , V -- increase  ");
 outtextxy(30,420,"   Increment : Q -- decrease   , W -- increase  ");
 outtextxy(30,440," Press R to Reload AND Press E to Quit");
 rectangle(20,285,320,460);
 do
 {
   for(i=0;i<1;i++)
   {
    setcolor(cl++);
    cl %= 16;
    for(i=0;i<180;i=i+5)
    {
     arc(A,B,180+i,i+182,L);
    }
    setcolor(0);
    line(x,y,A,B);
    setcolor(11);
    circle(x,y,1);
    X0=sqrt((x-A)*(x-A)+(y-B)*(y-B));
    X=X0-L;
    c=(x-A)/X0;
    s=(y-B)/X0;
    G1=G*c;
    G2=G*s;
    F=k*X;
    F1=-F*c;
    F2=M*g-F*s-G2*s+G1*c;
    a1=F1/M;
    a2=F2/M;
    v1=v1+a1;
    v2=v2+a2;
    x=x+v1;
    y=y+v2;
    setcolor(1);
    circle(x,y,1);
    setcolor(14);
    line(x,y,A,B);
    for(i=1;i<sp;i++)
    {
                    delay(1000);
    }

   }
 }
 while(!kbhit()==1);
 q=getch();
 cleardevice();
 if(q=='e'||q=='E') goto END;
 if(q=='r'||q=='R') goto BG;
 if(q=='s'||q=='s') sp=sp+1;
 if(q=='a'||q=='A') sp=sp-1;
 if(q=='-'||q=='_') L=L-1*w;
 if(q=='='||q=='+') L=L+1*w;
 if(q==','||q=='<') k=k-0.1*w;
 if(q=='.'||q=='>') k=k+0.1*w;
 if(q=='z'||q=='Z') M=M-0.1*w;
 if(q=='x'||q=='X') M=M+0.1*w;
 if(q=='c'||q=='C') m=m-0.1*w;
 if(q=='v'||q=='V') m=m+0.1*w;
 if(q=='w'||q=='W') w=w+1;
 if(q=='q'||q=='Q') w=w-1;
 goto GN;


END: closegraph();
}

已经在TC2.0上调试过,能够完整模拟弹性系数变化时摆球的变化轨迹。速度,质量,摆线长度等都是可调的。

有的地方还不完善,请大家多指教!

视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058