segunda-feira, 21 de janeiro de 2019

Função sscanf - janela gráfica

Depois de um bom período longe do C/C++
a saudades apertou, e resolvi voltar,
me desculpem mas não consegui esquecer
as duas primeiras linguagens de programação,
que tanto me fez sofrer no início, 
mas que me fez tão feliz algum tempo depois.
Aqui estamos falando da função sscanf,
A função sscanf permite ler dados
formatados de uma cadeia de caracteres,(string),
seu funcionamento não é muito diferente das funções scanf
que faz leitura do teclado e fscanf que faz sua leitura de um arquivo,
a diferença mesmo é que ela ler os valores
que foram armazenados numa string.
Fica fácil saber se a operação foi bem sucedida,
basta observar que o total de itens que foram
passados foram preenchidos totalmente.
Más se a contagem for menor,
ou se zero for retornado, houve falha na operação,
sendo que se houver falha na entrada,
antes mesmo que qualquer byte
possa ser lido EOF é retornado.
Por-ventura não é esta abaixo a síntaxe de sscanf?
int sscanf ( const char * s, const char * format, ...);
Declarei e inicializei duas matrizes bidimensional de char,
e converti uma para inteiros e outra para float,
a janela gráfica foi feira usando o C++ Buider 10.2.



#include <vcl.h>
#pragma hdrstop

#include "OnPaint.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint(TObject *Sender)
{
 Canvas->Font->Size = 20;
 Canvas->Font->Name = "Garamond";
 Canvas->Font->Color = clRed;;
 Canvas->TextOut ( 150, 18, "Função sscanf - Modo gráfico" );
 String str_1 = " ";
 String str_2 = " ";
 char Num [ 11 ] [ 2 ] = { "0", "1", "2", "3", "4", "5",
   "6", "7", "8", "9" };
 char Temp [ 9 ] [ 5 ] = { "8.9", "9.5", "8.0", "11.8",
   "12.7", "7.6","10.3", "15.4" };
 char buf_1 [ 50 ];
 int i;
 int N1 [ 10 ];
 float N2 [ 8 ];
 for ( i = 0; i < 10; i++ ) {
  sscanf ( Num [ i ], " %d ", &N1 [ i ] );
  str_1 += "   ";
  str_1 += N1 [ i ];
 }
 Canvas->Font->Size = 16;
 Canvas->Font->Color = clBlue;
 Canvas->TextOut ( 50, 48, "Vetor  convertido em inteiro" );
 Canvas->Font->Color = clBlack;
 Canvas->TextOut ( 50, 78, str_1 );1;
 i = 0;
 for ( i = 0; i < 8; i++ ) {
  sscanf ( Temp [ i ], "%f", &N2 [ i ] );
  sprintf ( buf_1, "%.1f", N2 [ i ] );
  str_2 += "   ";
  str_2 += buf_1;
 }
 Canvas->Font->Color = clBlue;
 Canvas->TextOut ( 50, 108, "Vetor  convertido em float" );
 Canvas->Font->Color = clBlack;
 Canvas->TextOut ( 50, 138, str_2 );1;

  Canvas->Font->Color = clBlack;
 Canvas->TextOut ( 200, 190, "Por: " );1;
 Canvas->Font->Color = clRed;
 Canvas->TextOut ( 240, 190, "Samuel Lima");
 Canvas->Font->Color = clBlack;
 Canvas->TextOut ( 200, 210, "sa_sp10@hotmail.com");
 Canvas->Font->Name = "Garamond";
 Canvas->Font->Size = 40;
 Canvas->Font->Color = clSkyBlue;
 Canvas->TextOut ( 76, 236, "MUITO OBRIGADO");
 SetBkMode(Canvas->Handle, TRANSPARENT);
 Canvas->Font->Color = clBlue;
 Canvas->TextOut ( 70, 230, "MUITO OBRIGADO");
}

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

#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
TShape *Shape1;
void __fastcall OnPaint(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Fun'#231#227'o sscanf - Modo gr'#225'fico'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Shape1: TShape
    Left = 0
    Top = 3
    Width = 601
    Height = 294
    Brush.Style = bsClear
    Pen.Color = clRed
    Pen.Width = 10
    Shape = stRoundRect
  end
end

Nenhum comentário:

Postar um comentário

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