quinta-feira, 24 de outubro de 2019

C++ builder - Par ou Ímpar?

Escolhendo uma das duas opções em
dois RadioButtons, que são Par ou ìmpar,
este programinha roda aleatoriamente,
num tempo determinado pelo evento OnTimer,
um contador numa na faixa muito ampla,
e no término, isto é, quando o botão parar
é pressionado ele informa se o último número
é par ou ímpar, indicando num label se o usuário
acertou ou não na escolha.



//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int i;
bool a = false;
bool b = false;
String str_1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
   : TForm ( Owner )
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Verifica_Par_Impar ( TObject *Sender ) {
      if (  ( i % 2 ) == 0 ) {
         Edit1 -> Clear ( );
         Form1 -> Edit1 -> Text = "Par";
     }
     else {
        Edit1 -> Clear ( );
        Form1 -> Edit1 -> Text = "Ímpar";
    }
    if ( str_1 == Edit1 -> Text ) {
            Label2 -> Caption = "Acertou";
    }
    else {
     Label2 -> Caption = "Errou";
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Sorteia_Par_Impar ( TObject *Sender ) {
     for ( i = 0; i <= random ( 10000 ); i++ ) {
         Form1 -> Edit1 -> Text = i;
     }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click ( TObject *Sender )
{
          Label2 -> Caption = " ";
          if ( b == false ) {
           Timer1 -> Interval = 0;
           Label2 -> Caption = "Escolha Par ou Ímpar";
          }
          Timer1 -> Interval = 100;
          a = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click ( TObject *Sender )
{        Label2 -> Caption = " ";
    if ( a == true && b == true ) {
      Timer1 -> Interval = 0;
      Verifica_Par_Impar ( Sender );
      }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    if ( a == true && b == true ) {
    Sorteia_Par_Impar ( Sender );
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clRed;
    Canvas -> Rectangle ( 05, 05, 595, 295 );
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Name = "arial";
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 170, 10, "C++ BUILDER - PAR OU ÍMPAR" );
}
//---------------------------------------------------------------------------
//Par
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
Label2 -> Caption = " ";
str_1 = "Par";
b = true;
}
//---------------------------------------------------------------------------
//Ímpar
void __fastcall TForm1::RadioButton2Click(TObject *Sender)
{
Label2 -> Caption = " ";
str_1 = "Ímpar";
b = true;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TTimer *Timer1;
    TButton *Button1;
    TEdit *Edit1;
    TButton *Button2;
    TBevel *Bevel1;
    TLabel *Label1;
    TRadioButton *RadioButton1;
    TRadioButton *RadioButton2;
    TLabel *Label2;

    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall Timer1Timer(TObject *Sender);
    void __fastcall FormPaint(TObject *Sender);
    void __fastcall RadioButton1Click(TObject *Sender);
    void __fastcall RadioButton2Click(TObject *Sender);
    void __fastcall Sorteia_Par_Impar ( TObject *Sender );
    void __fastcall Verifica_Par_Impar ( TObject *Sender );

private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 342
  Top = 165
  Caption = 'C++ BUILDER - PAR OU '#205'MPAR'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  PixelsPerInch = 600
  TextHeight = 13
  object Bevel1: TBevel
    Left = 200
    Top = 40
    Width = 210
    Height = 209
  end
  object Label1: TLabel
    Left = 248
    Top = 51
    Width = 134
    Height = 23
    Caption = 'PAR OU '#205'MPAR?'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label2: TLabel
    Left = 248
    Top = 176
    Width = 4
    Height = 16
    Color = clBlue
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentColor = False
    ParentFont = False
  end
  object Button1: TButton
    Left = 216
    Top = 211
    Width = 75
    Height = 25
    Caption = 'In'#237'ciar'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 257
    Top = 129
    Width = 94
    Height = 41
    Color = clOlive
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -27
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
  end
  object Button2: TButton
    Left = 319
    Top = 211
    Width = 75
    Height = 25
    Caption = 'Parar'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 2
    OnClick = Button2Click
  end
  object RadioButton1: TRadioButton
    Left = 248
    Top = 80
    Width = 43
    Height = 17
    Caption = 'Par'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 3
    OnClick = RadioButton1Click
  end
  object RadioButton2: TRadioButton
    Left = 248
    Top = 103
    Width = 57
    Height = 17
    Caption = #205'mpar'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 4
    OnClick = RadioButton2Click
  end
  object Timer1: TTimer
    Interval = 100
    OnTimer = Timer1Timer
    Left = 512
    Top = 32
  end
end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.