Помогни ни да направим Uroci.net по - богат! Добави урок

Светофар в C++

C++ » C++
ALWEK   трудност:    видян: 10930

 Изберете и добавете:

#pragma once

unsigned char Number=0;

unsigned char TR=0;

unsigned char TR2=0;

unsigned char TR3=0;

namespace TrafiLight

{

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 for Form1

///

/// WARNING: If you change the name of this class, you will need to change the

/// 'Resource File Name' property for the managed resource compiler tool

/// associated with all .resx files this class depends on. Otherwise,

/// the designers will not be able to interact properly with localized

/// resources associated with this form.

/// </summary>

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

{

public:Form1(void)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

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

private: System::Windows::Forms::Panel^ RedLight;

private: System::Windows::Forms::Panel^ YellowLight;private: System::Windows::Forms::Panel^ GreenLight;

 

private: System::Windows::Forms::Timer^ timer1;

private: System::Windows::Forms::Panel^ RedLight2;

private: System::Windows::Forms::Panel^ YellowLight2;

private: System::Windows::Forms::Panel^ GreenLight2;

 

 

 

private: System::Windows::Forms::Timer^ timer2;

private: System::Windows::Forms::Panel^ RedLight3;

private: System::Windows::Forms::Panel^ YellowLight3;private: System::Windows::Forms::Panel^ GreenLight3;

 

 

private: System::Windows::Forms::Timer^ timer3;

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

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

 

private: System::Windows::Forms::Timer^ timer4;

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

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

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

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

 

private: System::ComponentModel::IContainer^ components;

protected:

private:

/// <summary>

/// Required designer variable.

/// </summary>

 

#pragma region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

void InitializeComponent(void)

{

this->components = (gcnew System::ComponentModel::Container());

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

this->RedLight = (gcnew System::Windows::Forms::Panel());

this->YellowLight = (gcnew System::Windows::Forms::Panel());

this->GreenLight = (gcnew System::Windows::Forms::Panel());

this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));

this->RedLight2 = (gcnew System::Windows::Forms::Panel());

this->YellowLight2 = (gcnew System::Windows::Forms::Panel());

this->GreenLight2 = (gcnew System::Windows::Forms::Panel());

this->timer2 = (gcnew System::Windows::Forms::Timer(this->components));

this->RedLight3 = (gcnew System::Windows::Forms::Panel());

this->YellowLight3 = (gcnew System::Windows::Forms::Panel());

this->GreenLight3 = (gcnew System::Windows::Forms::Panel());

this->timer3 = (gcnew System::Windows::Forms::Timer(this->components));

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

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

this->timer4 = (gcnew System::Windows::Forms::Timer(this->components));

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

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

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

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

//

// button1

//

this->button1->Location = System::Drawing::Point(427, 400);

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

this->button1->Size = System::Drawing::Size(75, 23);

this->button1->TabIndex = 0;

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

this->button1->UseVisualStyleBackColor = true;

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

//

// RedLight

//

this->RedLight->BackColor = System::Drawing::Color::Red;

this->RedLight->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;

this->RedLight->Location = System::Drawing::Point(105, 137);

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

this->RedLight->Size = System::Drawing::Size(26, 26); this->RedLight->TabIndex = 1;

//

// YellowLight

//

this->YellowLight->BackColor = System::Drawing::Color::Transparent;

this->YellowLight->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;

this->YellowLight->Location = System::Drawing::Point(105, 169);

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

this->YellowLight->Size = System::Drawing::Size(26, 26); this->YellowLight->TabIndex = 2;

//

// GreenLight

//

this->GreenLight->BackColor = System::Drawing::Color::Transparent;

this->GreenLight->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;

this->GreenLight->Location = System::Drawing::Point(105, 201);

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

this->GreenLight->Size = System::Drawing::Size(26, 26); this->GreenLight->TabIndex = 3;

//

// timer1

//

this->timer1->Enabled = true;

this->timer1->Interval = 3000;

this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);

//

// RedLight2

//

this->RedLight2->BackColor = System::Drawing::Color::Red;

this->RedLight2->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;

this->RedLight2->ForeColor = System::Drawing::Color::Red;

this->RedLight2->Location = System::Drawing::Point(176, 139);

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

this->RedLight2->Size = System::Drawing::Size(27, 23);

this->RedLight2->TabIndex = 4;

//

// YellowLight2

//

this->YellowLight2->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;

this->YellowLight2->Location = System::Drawing::Point(176, 172);

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

this->YellowLight2->Size = System::Drawing::Size(27, 23);

this->YellowLight2->TabIndex = 5;

//

// GreenLight2

//

this->GreenLight2->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;

this->GreenLight2->Location = System::Drawing::Point(176, 201);

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

this->GreenLight2->Size = System::Drawing::Size(27, 23);

this->GreenLight2->TabIndex = 6;

//

// timer2

//

this->timer2->Enabled = true; this->timer2->Tick += gcnew System::EventHandler(this, &Form1::timer2_Tick);

//

// RedLight3

//

this->RedLight3->BackColor = System::Drawing::Color::Red;

this->RedLight3->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;

this->RedLight3->Location = System::Drawing::Point(257, 140);

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

this->RedLight3->Size = System::Drawing::Size(31, 29); this->RedLight3->TabIndex = 7;

//

// YellowLight3

//

this->YellowLight3->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;

this->YellowLight3->Location = System::Drawing::Point(257, 175);

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

this->YellowLight3->Size = System::Drawing::Size(31, 29);

this->YellowLight3->TabIndex = 8;

//

// GreenLight3

//

this->GreenLight3->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;

this->GreenLight3->Location = System::Drawing::Point(257, 210);

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

this->GreenLight3->Size = System::Drawing::Size(31, 29);

this->GreenLight3->TabIndex = 9;

//

// timer3

//

this->timer3->Enabled = true; this->timer3->Tick += gcnew System::EventHandler(this, &Form1::timer3_Tick);

//

// button2

//

this->button2->Location = System::Drawing::Point(257, 332);

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

this->button2->Size = System::Drawing::Size(75, 23);

this->button2->TabIndex = 10;

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

this->button2->UseVisualStyleBackColor = true;

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

//

// button3

//

this->button3->Location = System::Drawing::Point(56, 332);

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

this->button3->Size = System::Drawing::Size(75, 23);

this->button3->TabIndex = 11;

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

this->button3->UseVisualStyleBackColor = true;

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

//

// timer4

//

this->timer4->Interval = 1000; this->timer4->Tick += gcnew System::EventHandler(this, &Form1::timer4_Tick);

//

// button4

//

this->button4->Location = System::Drawing::Point(46, 262);

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

this->button4->Size = System::Drawing::Size(85, 23);

this->button4->TabIndex = 12;

this->button4->Text = L"1";

this->button4->UseVisualStyleBackColor = true;

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

//

// button5

//

this->button5->Location = System::Drawing::Point(155, 262);

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

this->button5->Size = System::Drawing::Size(93, 23);

this->button5->TabIndex = 13;

this->button5->Text = L"2";

this->button5->UseVisualStyleBackColor = true;

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

//

// button6

//

this->button6->Location = System::Drawing::Point(289, 262);

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

this->button6->Size = System::Drawing::Size(83, 23);

this->button6->TabIndex = 14;

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

this->button6->UseVisualStyleBackColor = true;

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

//

// button7

//

this->button7->Location = System::Drawing::Point(147, 54);

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

this->button7->Size = System::Drawing::Size(101, 23);

this->button7->TabIndex = 15;

this->button7->Text = L"";

this->button7->UseVisualStyleBackColor = true;

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

//

// Form1

//

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

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

this->ClientSize = System::Drawing::Size(514, 435);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

this->ForeColor = System::Drawing::Color::Black;

this->MaximizeBox = false;

this->MinimizeBox = false;

this->Name = L"Form1";

this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;

this->Text = L"";

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

this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_FormClosing); this->ResumeLayout(false);

}

#pragma endregion

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

{

Close();

}

private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e)

{

if(MessageBox::Show(" ?"," ",System::Windows::Forms::MessageBoxButtons::YesNo,System::Windows::Forms::MessageBoxIcon::Question)==System::Windows::Forms::DialogResult::No)e->Cancel=true;

}

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

{

switch(Number)

{

case 0:

this->RedLight->BackColor=Color::Red;

this->YellowLight->BackColor=Color::Yellow;

this->timer1->Interval=3000;

Number++;

break;

case 1:

this->RedLight->BackColor=Color::Transparent;

this->YellowLight->BackColor=Color::Transparent;

this->GreenLight->BackColor=Color::Green;this->timer1->Interval=2000;

Number++;

break;

case 2:

this->GreenLight->BackColor=Color::Transparent;

this->YellowLight->BackColor=Color::Yellow;this->timer1->Interval=3000;

Number++;

break;

default:

this->YellowLight->BackColor=Color::Transparent;

this->RedLight->BackColor=Color::Red;this->timer1->Interval=2000;

Number=0;

break;

}

}

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

{

switch(TR)

{

case 0:

this->RedLight2->BackColor=Color::Red;

this->YellowLight2->BackColor=Color::Yellow;this->timer2->Interval=3000;

TR++;

break;

 

case 1:

this->RedLight2->BackColor=Color::Transparent;

this->YellowLight2->BackColor=Color::Transparent;

this->GreenLight2->BackColor=Color::Green;this->timer2->Interval=4000;

TR++;

break;

case 2:

this->GreenLight2->BackColor=Color::Transparent;

this->YellowLight2->BackColor=Color::Yellow;this->timer2->Interval=3000;

TR++;

break;

default:

this->YellowLight2->BackColor=Color::Transparent;

this->RedLight2->BackColor=Color::Red;this->timer2->Interval=4000;

TR=0;

break;

}

}

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

{

switch(TR2)

{

case 0:

this->RedLight3->BackColor=Color::Red;

this->YellowLight3->BackColor=Color::Yellow;this->timer3->Interval=3000;

TR2++;

break;

 

case 1:

this->RedLight3->BackColor=Color::Transparent;

this->YellowLight3->BackColor=Color::Transparent;

this->GreenLight3->BackColor=Color::Green;this->timer3->Interval=8000;

TR2++;

break;

case 2:

this->GreenLight3->BackColor=Color::Transparent;

this->YellowLight3->BackColor=Color::Yellow;this->timer3->Interval=3000;

TR2++;

break;

default:

this->YellowLight3->BackColor=Color::Transparent;

this->RedLight3->BackColor=Color::Red;this->timer3->Interval=6000;

TR2=0;

break;

}

}

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

{

this->timer4->Enabled=true;

this->timer1->Enabled=false;

this->timer2->Enabled=false; this->timer3->Enabled=false;

}

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

{

this->timer4->Enabled=false;

this->timer1->Enabled=true;

this->timer2->Enabled=true;this->timer3->Enabled=true;

Number=0;

TR=0;

TR2=0;

 

}

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

{

switch(TR3)

{

case 0:

this->RedLight->BackColor=Color::Transparent;

this->GreenLight->BackColor=Color::Transparent;

this->YellowLight->BackColor=Color::Yellow;

this->RedLight2->BackColor=Color::Transparent;

this->GreenLight2->BackColor=Color::Transparent;

this->YellowLight2->BackColor=Color::Yellow;

this->RedLight3->BackColor=Color::Transparent;

this->GreenLight3->BackColor=Color::Transparent;

this->YellowLight3->BackColor=Color::Yellow;

this->timer4->Interval=1000;

TR3++;

break;

default:

this->RedLight->BackColor=Color::Transparent;

this->GreenLight->BackColor=Color::Transparent;

this->YellowLight->BackColor=Color::Transparent;

this->RedLight2->BackColor=Color::Transparent;

this->GreenLight2->BackColor=Color::Transparent;

this->YellowLight2->BackColor=Color::Transparent;

this->RedLight3->BackColor=Color::Transparent;

this->GreenLight3->BackColor=Color::Transparent;

this->YellowLight3->BackColor=Color::Transparent;this->timer4->Interval=1000;

TR3=0;

break;

}

}

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

{

if(this->timer1->Enabled)

{

this->timer1->Enabled=false;

Number=0;

this->RedLight->BackColor=Color::Transparent;

this->GreenLight->BackColor=Color::Transparent;

this->YellowLight->BackColor=Color::Transparent;this->button4->Text="";

}

else

{

this->timer1->Enabled=true;

Number=0;

this->button4->Text="";

}

}

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

{

if(this->timer2->Enabled)

{

this->timer2->Enabled=false;

TR=0;

this->RedLight2->BackColor=Color::Transparent;

this->GreenLight2->BackColor=Color::Transparent;

this->YellowLight2->BackColor=Color::Transparent;this->button5->Text="";

}

else

{

this->timer2->Enabled=true;

TR=0;

this->button5->Text="";

}

}

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

{

if(this->timer3->Enabled)

{

this->timer3->Enabled=false;

TR2=0;

this->RedLight3->BackColor=Color::Transparent;

this->GreenLight3->BackColor=Color::Transparent;

this->YellowLight3->BackColor=Color::Transparent;this->button6->Text="";

}

else

{

this->timer3->Enabled=true;

TR2=0;

this->button6->Text="";

}

}

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

{

this->RedLight->BackColor=Color::Transparent;

this->GreenLight->BackColor=Color::Transparent;

this->YellowLight->BackColor=Color::Transparent;

this->RedLight2->BackColor=Color::Transparent;

this->GreenLight2->BackColor=Color::Transparent;

this->YellowLight2->BackColor=Color::Transparent;

this->RedLight3->BackColor=Color::Transparent;

this->GreenLight3->BackColor=Color::Transparent;

this->YellowLight3->BackColor=Color::Transparent;

TR=0;

TR2=0;

Number=0;

this->timer4->Enabled=false;

this->timer1->Enabled=false;

this->timer2->Enabled=false;

this->timer3->Enabled=false;

this->button4->Text="";

this->button5->Text="";

this->button6->Text="";

}

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

{

this->button7_Click(sender,e);

}

};

}



Коментари (7)

Newbie на 18.11 2010 в 12:52ч.
Искам да попитам защото съм новак в програмирането и явно въпросът ми ще е малко ненормале :) Къде трябва да сложа този код за да се получи ? Изтеглих си Visual C++ но тя за мене е тъмна Индия . Моля помогне те !
script на 28.11 2010 в 00:02ч.
И аз съм новак, но мисля че може и в Notepad, поне така е с HTML но за C++ не съм сигурен!
joe01 на 01.12 2010 в 22:04ч.
И аз съм новак но ще се опитам да ти обясна.
Значи.. има програми които се викат компилатори.В тях слагаш това нещо.
Ти казваш че си свалил Visual C++ но аз не ти го препоръчвам като за начинаещ.
По добре свали DEV C++
tanjo на 29.01 2011 в 17:06ч.
За да пуснете програмата трябва да имате инсталирана среда за С++.
Освен Visual Studio, може да ползвате и често използваните: CodeLite, BlockCode, DevC++ и др. За мен най-добрата среда освен MS Visual Studio е CodeLite.
1. Създавате нов проект.
2. Copy на текста и paste в проекта.
3. След това компилирате и сте готови ;)
gerita30 на 16.02 2011 в 23:27ч.
а как да компилираме?
на мен нещо ми дава проблем и пише че не може да извърши компилацията?
petersvp на 24.08 2011 в 16:13ч.
Таози код е на Microsoft Managed C++. Изисква се Visual Studio 2008 като минимум.
plamen4o65 на 21.03 2012 в 18:54ч.
Newbie трябва ти КОМПИЛАТОР.Той превръща скриптове,команди и кодове в програми-приложения.
А ако на някой не му проработи компилацията има грешка в кода.

Регистрирайте се, за да добавите коментар


Калдейта ЕООД - © 2003-2010. Всички права запазени.
Препоръчваме: Национален Бизнес | Bomba.bg | IT Новини | Диплома.бг | TRAVEL туризъм | Реферати | AmAm.bg | Иде.ли | Курсови работи | Фото Форум | Spodeli.net | Фото-Култ | Atol.bg | Elmaz.com | MobileBulgaria.com | Казанлък.Com