quarta-feira, 13 de janeiro de 2021

FMX - os dois maiores e os dois menores valores em matriz de inteiros


O programa começa exibindo uma matriz ordenada
de números inteiros de 6 x 10, sendo que cada valor,
foi inserindo individualmente em um bitimap.
Para isto declaramos um objeto TImage,
e suas posições são recebidas de valores constantes,
mais os valores numéricos das linhas e colunas
que em tempo de execução são passados para as propriedades
Position -> X e Position -> Y dos objetos TImage,
é aqui que está o segredo, é por aqui que a "mágica" acontece.
Como este objeto foi criado dentro de laços for,
para impressão dos valores numéricos, o bitimap acaba
sendo multiplicado num total do número máximo da matriz.
A mesma coisa se aplica nos valores pesquisados,
procuramos pelo maior e pelo menor valor da matriz,
e procuramos também segundo maior e pelo segundo menor.
Hora, os objetos TImage e TText também devem ser declarados,
dentro de onde se encontra as procuras para que o total
de bitimaps seja o mesmo dos valores encontrados.
Escrever os detalhes do funcionamento deste programa
deixaria o post bem extenso e cansativo, e a maioria
dos leitores são como eu, detestam textos grandes,
porém é preciso frisar que função mais importante
neste programa, é sem dúvidas a função Embaralha ( void );
porque sem ela como nós provaria o funcionamento?




//---------------------------------------------------------------------------
int maior_menor_Numero ( ) {
int i, j, maior, maior_2, menor, menor_2;
String str_1;
String str_2;
String str_3;
String str_4;
String str_5;
String str_6;

TImage *Image1;
TImage *Image2;

TText *Text1;
TText *Text4;

maior =  A [ 0 ] [ 0 ];
for ( i = 0; i < tam ; i++ ) {
for ( j = 0; j < 10 ; j++ ) {
   if ( A [ i ] [ j ] > maior ) {
maior = A [ i ] [ j ];
   }
   if ( A [ i ] [ j ] < menor ) {
menor = A [ i ] [ j ];
   }
}
}

for ( i = 0; i < tam ; i++ ) {
for ( j = 0; j < 10 ; j++ ) {
if ( A [ i ] [ j ] == maior ) {
maior_2 = A [ i ] [ j ];
str_1 = maior_2;
str_2 = i;
str_3 = j;
  Image1 = new TImage ( Form1 );
  Image1 -> Bitmap -> LoadFromFile ("Bal_pink_2.bmp");
  Image1 -> Parent = Form1;
  Image1 -> Height = 30;
  Image1 -> Width = 30;
  Image1 -> Position -> X = 130 + ( j * 35 );
  Image1 -> Position -> Y = 45 + ( i * 35 );

  Text1 = new TText ( Form1 );
  Text1 -> Parent = Form1;
  Text1 -> Height = 30;
  Text1 -> Width = 30;
  Text1 -> TextSettings -> Font -> Family = "Consolas";
  Text1 -> TextSettings -> Font -> Size = 17;
  Text1 -> TextSettings -> FontColor = claBlack;
  Text1 -> Size -> PlatformDefault = False;
  Text1 -> Font -> Style = TFontStyles ( ) << fsBold;
  Text1 -> Position -> X = 130 + ( j * 35 );
  Text1 -> Position -> Y = 45  + ( i * 35 );

  if ( A [ i ] [ j ] >= 0 && A [ i ] [ j ] < 10 ) {
Text1 -> Text = "0";
  }
  Text1 -> Text = Text1 -> Text + StrToInt ( A [ i ] [ j ] );
  }
  //////////////////////////////////////////////////////////////////////
  if ( A [ i ] [ j ] == menor ) {
menor_2 = A [ i ] [ j ];
str_4 = menor_2;
str_5 = i;
str_6 = j;
  Image2 = new TImage ( Form1 );
  Image2 -> Bitmap -> LoadFromFile ("Bal_green_claro.bmp");
  Image2 -> Parent = Form1;
  Image2 -> Height = 30;
  Image2 -> Width = 30;
  Image2 -> Position -> X = 130 + ( j * 35 );
  Image2 -> Position -> Y = 45 + ( i * 35 );

  Text4 = new TText ( Form1 );
  Text4 -> Parent = Form1;
  Text4 -> Height = 30;
  Text4 -> Width = 30;
  Text4 -> TextSettings -> Font -> Family = "Consolas";
  Text4 -> TextSettings -> Font -> Size = 17;
  Text4 -> TextSettings -> FontColor = claBlack;
  Text4 -> Size -> PlatformDefault = False;
  Text4 -> Font -> Style = TFontStyles ( ) << fsBold;
  Text4 -> Position -> X = 130 + ( j * 35 );
  Text4 -> Position -> Y = 45  + ( i * 35 );

  if ( A [ i ] [ j ] >= 0 && A [ i ] [ j ] < 10 ) {
Text4 -> Text = "0";
  }
  Text4 -> Text = Text4 -> Text + StrToInt ( A [ i ] [ j ] );
  }


}
}

Text2 -> Text = "Maior número: ";
Text2 -> Text = Text2 -> Text + str_1;
Text2 -> Text = Text2 -> Text + " linha ";
Text2 -> Text = Text2 -> Text + str_2;
Text2 -> Text = Text2 -> Text + " e coluna ";
Text2 -> Text = Text2 -> Text + str_3;

Text3 -> Text = "Menor número: ";
Text3 -> Text = Text3 -> Text + str_4;
Text3 -> Text = Text3 -> Text + " linha ";
Text3 -> Text = Text3 -> Text + str_5;
Text3 -> Text = Text3 -> Text + " e coluna ";
Text3 -> Text = Text3 -> Text + str_6;
return 0;
}
//---------------------------------------------------------------------------

Nenhum comentário:

Postar um comentário

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