Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Otchet3.doc
Скачиваний:
2
Добавлен:
01.08.2019
Размер:
523.26 Кб
Скачать

4. Примеры работы программы.

4.1 После запуска программы запускается основная форма

При нажатии кнопки «запуск» с параметрами по умолчанию произойдет отрисовка траектории полета тела и вывода максимальной высоты и дальности полета

При нажатии кнопки «очистка экрана» форма вернется начальное состояние

При вводе неверных данных будет выведено уведомление об этом

При нажатии кнопки «Если надоело» произойдет выход из программы.

Ниже представлены примеры выполнения программы при различных введенных данных:

5. Заключение

В ходе данной курсовой работы была реализована программа выполняющая моделирование полета тела, брошенного под углом к горизонту.

6. Список литературы

1 Дейтел П.Дж. Как программировать на Си++ / П. Дж. Дейтел, Х.М. Дейтел. – М.: ЗАО Издательство «Бином», 2008, 1152 с.

2 Подбельский В.В. Язык Си++ / В.В. Подбельский. – М.: Финансы и статистика, 2008. – 560с.

3 Гулд Х., Тобочник Я. Компьютерное моделирование в физике Издательство: Мир, 1990, 352 с.

7. Листинг

#pragma once

#define _USE_MATH_DEFINES

#include <math.h>

namespace Kurs {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

/// <summary> /// </summary>

public ref class Form1 : public System::Windows::Forms::Form

{

String ^str1,^str2,^str3,^str4,^str5,^str6;

System::Drawing::Graphics^ graph;

float

m,//переменная проверки введенных данных

w,//скорость ветра

x,y,x0,y0,x1,y1,xx,yy,//переменные для работы с координатами

g,//ускорение свободного падения

vx,vy,//скорость по Х и У

ax,ay,//ускорения по Х и У

ro,//плотность воздуха

F,//сила ветра

r,//радиус камня

mas,//масса тела

ve,//модуль скорости

f0,q,//углы броска и ветра

xmax,//пиксельная длина для рисунка

ymax,//пиксельная высота для рисунка

tmax,//время полета

lmax,//длина

hmax,//максимальная высота

kx,//масштаб по Ох

ky,//масштаб по Оу

k,//общий коэффициент

v0;//скорость броска

private: System::Windows::Forms::Label^ label2;

private: System::Windows::Forms::Label^ label3;

private: System::Windows::Forms::Label^ label4;

private: System::Windows::Forms::Label^ label5;

private: System::Windows::Forms::Label^ label6;

private: System::Windows::Forms::Button^ button2;

private: System::Windows::Forms::Label^ label7;

private: System::Windows::Forms::TextBox^ textBox3;

private: System::Windows::Forms::TextBox^ textBox4;

private: System::Windows::Forms::TextBox^ textBox5;

private: System::Windows::Forms::TextBox^ textBox6;

private: System::Windows::Forms::Label^ label8;

private: System::Windows::Forms::Label^ label9;

private: System::Windows::Forms::Label^ label10;

private: System::Windows::Forms::Label^ label11;

private: System::Windows::Forms::Button^ button3;

private: System::Windows::Forms::Label^ label1;

//конструктор

public:

Form1(void)

{

InitializeComponent();

graph=CreateGraphics();

m=0;

x0=180;

y0=380;

x1=180;

y1=380;

g=9.81;

xmax=720;

ymax=360;

ro=0.129;

//

//TODO: добавьте код конструктора

//

}

//деструктор

protected:

/// <summary>

/// Освободить все используемые ресурсы.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::TextBox^ textBox2;

private: System::Windows::Forms::Button^ button1;

private: System::Windows::Forms::TextBox^ textBox1;

protected:

private:

/// <summary>

/// Требуется переменная конструктора.

/// </summary>

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

/// <summary>

/// Обязательный метод для поддержки конструктора - не изменяйте

/// содержимое данного метода при помощи редактора кода.

/// </summary>

void InitializeComponent(void)

{

this->textBox2 = (gcnew System::Windows::Forms::TextBox());

this->button1 = (gcnew System::Windows::Forms::Button());

this->textBox1 = (gcnew System::Windows::Forms::TextBox());

this->label1 = (gcnew System::Windows::Forms::Label());

this->label2 = (gcnew System::Windows::Forms::Label());

this->label3 = (gcnew System::Windows::Forms::Label());

this->label4 = (gcnew System::Windows::Forms::Label());

this->label5 = (gcnew System::Windows::Forms::Label());

this->label6 = (gcnew System::Windows::Forms::Label());

this->button2 = (gcnew System::Windows::Forms::Button());

this->label7 = (gcnew System::Windows::Forms::Label());

this->textBox3 = (gcnew System::Windows::Forms::TextBox());

this->textBox4 = (gcnew System::Windows::Forms::TextBox());

this->textBox5 = (gcnew System::Windows::Forms::TextBox());

this->textBox6 = (gcnew System::Windows::Forms::TextBox());

this->label8 = (gcnew System::Windows::Forms::Label());

this->label9 = (gcnew System::Windows::Forms::Label());

this->label10 = (gcnew System::Windows::Forms::Label());

this->label11 = (gcnew System::Windows::Forms::Label());

this->button3 = (gcnew System::Windows::Forms::Button());

this->SuspendLayout();

//

// textBox2

//

this->textBox2->Location = System::Drawing::Point(15, 64);

this->textBox2->Name = L"textBox2";

this->textBox2->Size = System::Drawing::Size(84, 20);

this->textBox2->TabIndex = 1;

this->textBox2->Text = L"45";

//

// button1

//

this->button1->Location = System::Drawing::Point(19, 244);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(83, 58);

this->button1->TabIndex = 2;

this->button1->Text = L"Запуск";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

//

// textBox1

//

this->textBox1->Location = System::Drawing::Point(15, 25);

this->textBox1->Name = L"textBox1";

this->textBox1->Size = System::Drawing::Size(84, 20);

this->textBox1->TabIndex = 3;

this->textBox1->Text = L"20";

//

// label1

//

this->label1->AutoSize = true;

this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label1->Location = System::Drawing::Point(283, 28);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(0, 17);

this->label1->TabIndex = 4;

//

// label2

//

this->label2->AutoSize = true;

this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label2->Location = System::Drawing::Point(283, 50);

this->label2->Name = L"label2";

this->label2->Size = System::Drawing::Size(0, 17);

this->label2->TabIndex = 5;

//

// label3

//

this->label3->AutoSize = true;

this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label3->Location = System::Drawing::Point(166, 28);

this->label3->Name = L"label3";

this->label3->Size = System::Drawing::Size(0, 17);

this->label3->TabIndex = 6;

//

// label4

//

this->label4->AutoSize = true;

this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label4->Location = System::Drawing::Point(166, 50);

this->label4->Name = L"label4";

this->label4->Size = System::Drawing::Size(0, 17);

this->label4->TabIndex = 7;

//

// label5

//

this->label5->AutoSize = true;

this->label5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label5->Location = System::Drawing::Point(-2, 9);

this->label5->Name = L"label5";

this->label5->Size = System::Drawing::Size(126, 13);

this->label5->TabIndex = 8;

this->label5->Text = L"Начальная скорость, м/с";

//

// label6

//

this->label6->AutoSize = true;

this->label6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label6->Location = System::Drawing::Point(26, 48);

this->label6->Name = L"label6";

this->label6->Size = System::Drawing::Size(66, 13);

this->label6->TabIndex = 9;

this->label6->Text = L"Угол броска";

//

// button2

//

this->button2->Location = System::Drawing::Point(20, 355);

this->button2->Name = L"button2";

this->button2->Size = System::Drawing::Size(82, 41);

this->button2->TabIndex = 10;

this->button2->Text = L"Если надоело";

this->button2->UseVisualStyleBackColor = true;

this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);

//

// label7

//

this->label7->AutoSize = true;

this->label7->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label7->Location = System::Drawing::Point(910, 381);

this->label7->Name = L"label7";

this->label7->Size = System::Drawing::Size(16, 17);

this->label7->TabIndex = 11;

this->label7->Text = L"0";

//

// textBox3

//

this->textBox3->Location = System::Drawing::Point(15, 103);

this->textBox3->Name = L"textBox3";

this->textBox3->Size = System::Drawing::Size(83, 20);

this->textBox3->TabIndex = 12;

this->textBox3->Text = L"3";

//

// textBox4

//

this->textBox4->Location = System::Drawing::Point(14, 142);

this->textBox4->Name = L"textBox4";

this->textBox4->Size = System::Drawing::Size(84, 20);

this->textBox4->TabIndex = 13;

this->textBox4->Text = L"225";

//

// textBox5

//

this->textBox5->Location = System::Drawing::Point(15, 180);

this->textBox5->Name = L"textBox5";

this->textBox5->Size = System::Drawing::Size(84, 20);

this->textBox5->TabIndex = 14;

this->textBox5->Text = L"100";

//

// textBox6

//

this->textBox6->Location = System::Drawing::Point(15, 218);

this->textBox6->Name = L"textBox6";

this->textBox6->Size = System::Drawing::Size(87, 20);

this->textBox6->TabIndex = 15;

this->textBox6->Text = L"3";

//

// label8

//

this->label8->AutoSize = true;

this->label8->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label8->Location = System::Drawing::Point(12, 87);

this->label8->Name = L"label8";

this->label8->Size = System::Drawing::Size(104, 13);

this->label8->TabIndex = 16;

this->label8->Text = L"Скорость ветра, м/с";

//

// label9

//

this->label9->AutoSize = true;

this->label9->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label9->Location = System::Drawing::Point(-2, 126);

this->label9->Name = L"label9";

this->label9->Size = System::Drawing::Size(135, 13);

this->label9->TabIndex = 17;

this->label9->Text = L"Направление ветра, град.";

//

// label10

//

this->label10->AutoSize = true;

this->label10->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label10->Location = System::Drawing::Point(13, 164);

this->label10->Name = L"label10";

this->label10->Size = System::Drawing::Size(86, 13);

this->label10->TabIndex = 18;

this->label10->Text = L"Масса камня, гр";

//

// label11

//

this->label11->AutoSize = true;

this->label11->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(204)));

this->label11->Location = System::Drawing::Point(12, 202);

this->label11->Name = L"label11";

this->label11->Size = System::Drawing::Size(92, 13);

this->label11->TabIndex = 19;

this->label11->Text = L"Радиус камня, см";

//

// button3

//

this->button3->Location = System::Drawing::Point(19, 308);

this->button3->Name = L"button3";

this->button3->Size = System::Drawing::Size(83, 41);

this->button3->TabIndex = 21;

this->button3->Text = L"Очистка\r\n экрана";

this->button3->UseVisualStyleBackColor = true;

this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->BackColor = System::Drawing::SystemColors::Desktop;

this->ClientSize = System::Drawing::Size(930, 402);

this->Controls->Add(this->button3);

this->Controls->Add(this->label11);

this->Controls->Add(this->label10);

this->Controls->Add(this->label9);

this->Controls->Add(this->label8);

this->Controls->Add(this->textBox6);

this->Controls->Add(this->textBox5);

this->Controls->Add(this->textBox4);

this->Controls->Add(this->textBox3);

this->Controls->Add(this->label7);

this->Controls->Add(this->button2);

this->Controls->Add(this->label6);

this->Controls->Add(this->label5);

this->Controls->Add(this->label4);

this->Controls->Add(this->label3);

this->Controls->Add(this->label2);

this->Controls->Add(this->label1);

this->Controls->Add(this->textBox1);

this->Controls->Add(this->button1);

this->Controls->Add(this->textBox2);

this->Location = System::Drawing::Point(11, 15);

this->Name = L"Form1";

this->Text = L"Моделирование полета тела";

this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);

this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Пэйнт <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)

{

Brush^ blackBrush=gcnew SolidBrush(Color::Black);

Brush^ redBrush=gcnew SolidBrush(Color::Red);

Pen^ blackPen=gcnew Pen(Color::Gray);

Pen^ redPen=gcnew Pen(Color::Red);

for(int i=0;i<6;i++)

{

graph->DrawLine(blackPen,150-i,0,150-i,400);

}

graph->DrawLine(blackPen,180,380,900,380);

}

//кнопка "запуск"

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)

{

System::Windows::Forms::DialogResult v;

Pen^ blackPen=gcnew Pen(Color::Black);

Pen^ grayPen=gcnew Pen(Color::Gray);

Pen^ redPen=gcnew Pen(Color::Red);

Brush^ blackBrush=gcnew SolidBrush(Color::Black);

Brush^ grayBrush=gcnew SolidBrush(Color::Gray);

Brush^ xBrush=gcnew SolidBrush(Color::Black);

label1->Text=("");

label3->Text=("");

label2->Text=("");

label4->Text=("");

graph->FillRectangle(xBrush,0,0,930,400);

for(int i=0;i<6;i++)

{

graph->DrawLine(grayPen,150-i,0,150-i,400);

}

graph->DrawLine(grayPen,180,380,900,380);

str1=textBox1->Text;

str2=textBox2->Text;

str3=textBox3->Text;

str4=textBox4->Text;

str5=textBox5->Text;

str6=textBox6->Text;

double a;

a=0;

//Проверочка

try

{

m=0;

if((Convert::ToDouble(str1)<0)||(Convert::ToDouble(str1)>30)) {m=1;}

if((Convert::ToDouble(str2)<0)||(Convert::ToDouble(str2)>180)) {m=2;}

if((Convert::ToDouble(str3)>10)||(Convert::ToDouble(str3)<0)) {m=3;}

if((Convert::ToDouble(str4)<0)||(Convert::ToDouble(str4)>360)) {m=4;}

if((Convert::ToDouble(str5)>200)||(Convert::ToDouble(str5)<0)) {m=5;}

if((Convert::ToDouble(str6)>10)||(Convert::ToDouble(str6)<0)) {m=6;}

}

catch(System::FormatException^){m=7;};

if (m==1)

{

v=MessageBox::Show("Скорость броска должна находиться в интервале [0;30]\nВвести заново?","Ошибка!", MessageBoxButtons::YesNo);

m=1;

}

if (m==2)

{

v=MessageBox::Show("Угол броска должен находиться в интервале [0;180]\nВвести заново?","Ошибка!", MessageBoxButtons::YesNo);

m=2;

}

if (m==3)

{

v=MessageBox::Show("Скорость ветра должна находиться в интервале [0;10]\nВвести заново?","Ошибка!", MessageBoxButtons::YesNo);

m=3;

}

if (m==4)

{

v=MessageBox::Show("Направление ветра должно находиться в интервале [0;360]\nВвести заново?","Ошибка!", MessageBoxButtons::YesNo);

m=4;

}

if (m==5)

{

v=MessageBox::Show("Масса камня должна находиться в интервале [0;200]\nВвести заново?","Ошибка!", MessageBoxButtons::YesNo);

m=5;

}

if (m==6)

{

v=MessageBox::Show("Радиус камня должна находиться в интервале [0;5]\nВвести заново?","Ошибка!", MessageBoxButtons::YesNo);

m=6;

}

if (m==7)

{

v=MessageBox::Show("Все данные могут содержать только цифры\nВвести заново?","Ошибка", MessageBoxButtons::YesNo);

m=7;

}

if (v==System::Windows::Forms::DialogResult::No)

{

Close();

}

else if (v==System::Windows::Forms::DialogResult::Yes)

{

str1=textBox1->Text;

str2=textBox2->Text;

str3=textBox3->Text;

str4=textBox4->Text;

str5=textBox5->Text;

str6=textBox6->Text;

}

if (m==0)

{

v0=Convert::ToDouble(str1);

f0=Convert::ToDouble(str2);

w=Convert::ToDouble(str3);

q=Convert::ToDouble(str4);

mas=Convert::ToDouble(str5);

r=Convert::ToDouble(str6);

mas=mas/1000;

r=r/100;

f0=f0*M_PI/180;

q=q*M_PI/180;

vx=v0*cos(f0)+w*cos(q);

vy=v0*sin(f0)+w*sin(q);

ve=sqrt(v0*v0+w*w+2*v0*w*cos(f0-q));

F=ro*M_PI*r*r*ve*ve/2;

ax=F*cos(q)/mas;

ay=(F*sin(q)-mas*g)/mas;

tmax=abs(-2*vy/ay);

lmax=(vx*tmax+ax*tmax*tmax/2);

hmax=abs(vy*vy/2/(-ay));

yy=vy*tmax+ay*tmax*tmax/2;

if(lmax>=0){

kx=(xmax)/lmax;

ky=(ymax)/hmax;}

if(lmax<0){

kx=(xmax-250)/-lmax;

ky=(ymax)/hmax;}

if (kx>=ky) k=ky;

else k=kx;

if(lmax>=0){x0=180;y0=380;x1=180;y0=380;}

if(lmax<0){x0=650;y0=380;x1=650;y1=380;}

//Человек

graph->DrawLine(grayPen,x0,380.,x0-5,370.);

graph->DrawLine(grayPen,x0-5,370.,x0-10,380.);

graph->DrawLine(grayPen,x0-5,370.,x0-5,385.);

graph->DrawLine(grayPen,x0,395.,x0-5,385.);

graph->DrawLine(grayPen,x0-10,395.,x0-5,385.);

graph->DrawEllipse(grayPen,x0-15,350.,20.,20.);

graph->DrawArc(redPen,x0-15,346.,20.,20.,60.,60.);

graph->DrawEllipse(grayPen,x0-12,355.,6.,3.);

graph->DrawEllipse(grayPen,x0-4,355.,6.,3.);

graph->DrawLine(grayPen,x0-5,360.,x0-5,363.);

//Отрисовка траектории

for(float t=0;t<tmax-0.0001;t=t+0.0001)

{

x=vx*t+ax*t*t/2;

y=vy*t+ay*t*t/2;

xx=k*x;

yy=k*y;

graph->DrawLine(redPen,x0,y0,x1+xx,y1-yy);

x0=x1+xx;

y0=y1-yy;

}

//вывод дополнительной информации

graph->DrawLine(grayPen,180,380,900,380);

graph->FillPie(grayBrush,850,0,80,80,180-q*180/M_PI-15,30);//стрелка направления ветра

label1->Text=("l_max, м");

label2->Text=Convert::ToString(abs(lmax));

label3->Text=("h_max, м");

label4->Text=Convert::ToString(hmax);

}

}

//кнопка "если надоело"

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

Close();

}

//кнопка "очистка экрана"

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {

Brush^ xBrush=gcnew SolidBrush(Color::Black);

graph->FillRectangle(xBrush,0,0,930,400);

label1->Text=("");

label3->Text=("");

label2->Text=("");

label4->Text=("");

}

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

}

};

}

21

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]