各位达人帮忙找错误:)

8099289wyy 2003-04-12 11:56:46
这是一个关于行星绕恒星的c程序,我用c++builder改了编译后无法绕出运动轨迹
望各位大哥帮我找找错误
注:px为行星在x轴上的速度,s[]为行星在y轴上的速度
x,y为行星运行的轨迹,m为行星速度,(b=GMm但行星能否绕出轨迹好象与这无关)
#include<d:\tc\ccbp\ccbp.h>
main()
{
float px,py,b,x,y,t,m;
int s[5];
int j;
long int graphdriver=DETECT,graphmode,i;
initgraph(&graphdriver,&graphmode,"");
setfillstyle(1,12);
fillellipse(200,220,15,15);

s[0]=800;
s[1]=1000;
s[2]=1200;
s[3]=1500;
s[4]=-1500;
for (j=0;j<5;j++)
{
t=0.1;m=1000;px=0;py=s[j];x=100;y=220;b=100000;

for(i=1;i<15000;i++)
{
px=px-(b*(x-200)*t)/sqrt(pow((x-200)*(x-200)+(y-220)*(y-220),3));
py=py-(b*(y-220)*t)/sqrt(pow((x-200)*(x-200)+(y-220)*(y-220),3));
x=x+(px*t)/m;
y=y+(py*t)/m;
fillellipse(x,y,5,5);
setfillstyle(1,2);
delay(20);
}
}
getch();
closegraph();
}



我用c++builder改写的程序
image1为64X64的图片(恒星),image2为32X32的图片(行星)
//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdio.h>
#include <math.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
signed int a=0,x=-200,y=0;
float Px,Py,m;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------



void __fastcall TForm1::FormCreate(TObject *Sender)
{
Form1->Left=0;
Form1->Top=0;
Form1->Width=Screen->Width;
Form1->Height=Screen->Height;
Form1->Button1->Top=Screen->Height-100;
Form1->Button2->Top=Screen->Height-50;
Form1->Button1->Left=Screen->Width-70;
Form1->Button2->Left=Screen->Width-70;
Form1->Edit1->Top=Screen->Height-100;
Form1->Edit2->Top=Screen->Height-50;
Form1->Edit1->Left=Screen->Width-150;
Form1->Edit2->Left=Screen->Width-150;
Form1->Label1->Top=Screen->Height-100;
Form1->Label2->Top=Screen->Height-50;
Form1->Label1->Left=Screen->Width-230;
Form1->Label2->Left=Screen->Width-230;
Form1->Image1->Left=Screen->Width/2-32;
Form1->Image1->Top=Screen->Height/2-32;
Form1->Image2->Left=Screen->Width/2-200-16;
Form1->Image2->Top=Screen->Height/2-16;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
float B;
if(a==1)
{
B=100*m;
Px-=B*x/pow(x*x+y*y,1.5);
Py-=B*y/pow(x*x+y*y,1.5);
x=x+Px/m;
y=y+Py/m;
Form1->Image2->Left=Screen->Width/2+x-16;
Form1->Image2->Top=Screen->Height/2+y-16;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
a=1;
Form1->Image1->Left=Screen->Width/2-32;
Form1->Image1->Top=Screen->Height/2-32;
Form1->Image2->Left=Screen->Width/2-200-16;
Form1->Image2->Top=Screen->Height/2-16;
Py=StrToFloat(Form1->Edit1->Text);
m=StrToFloat(Form1->Edit2->Text);
Px=0;
x=-200;
y=0;
}
...全文
20 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
DavidBone 2003-04-17
  • 打赏
  • 举报
回复
up
lxmfll2000 2003-04-13
  • 打赏
  • 举报
回复
请把出错信息发上来.也好知道你那里出错了...
8099289wyy 2003-04-13
  • 打赏
  • 举报
回复
程序是可以进行编译的
但是编译出的程序无法象正确的c程序一样绕出轨迹
我改了很久,但是一直无法找到错误在哪里
其实我想主要的就是timer里的那几行
可是我觉得和c代码中的完全是一样的,根本无法改
这道题其实是用计算机图形来描绘物理运动的一个比较简单的题目
各位大哥有兴趣可以帮我改一下,你们也可以根据那个c程序自己另外再写一个
将正确的代码贴上来,谢谢
IT-司马青衫 2003-04-12
  • 打赏
  • 举报
回复
太长了,一时没有时间帮助你查错.还是自己学会去查错吧.我们除了CODING 就是BUGING
你也不给出错信息

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧