Найти число Пи методом монте карло
С++ Builder 6
Компиляция и сборка под Windows 10
=
=
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if( ComboBox1->Text == "" ){ShowMessage("Выбери число итераций"); return; }
double iterations=0;
double x,y;
double count=0;
double z;
double pi;
iterations= ComboBox1->Text.ToDouble();
srand(time(NULL));
for (int i=0; i<iterations; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1) count++;
}
pi=(double)count/iterations*4;
Edit1->Text = FloatToStr( pi ) ;
}
//--------------------только цифры-------------------------------------------------------
void __fastcall TForm1::ComboBox1KeyPress(TObject *Sender, char &Key)
{
if(Key >= '0' && Key <= '9' || Key == VK_BACK ) return;
Key=0 ;
}
//---------------------------------------------------------------------------
=
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <math.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TButton *Button3;
TComboBox *ComboBox1;
TLabel *Label1;
TLabel *Label2;
void __fastcall Button3Click(TObject *Sender);
void __fastcall ComboBox1KeyPress(TObject *Sender, char &Key);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
=
Исходник здесь
=
|