Programação em Pascal

Pascal

Program nome_do_programa; {Comentários}

    Uses CRT;


 

procedure leitura_valida(frase:string, var x:real);

    begin

    writeln(frase);

    repeat

read(x);

    until (x>0) and (x<=100);

    end;

procedure centralizar(s:frase);

    var x:integer;

begin

x:=40-(length(s)) div 2;

writeln(s:x)
{escreve s a partir da coluna x da mesma linha}

end;


 

function f(x,y:real):real;

    begin

        f:=sqr(x-y);

end;


 

Const K=3;


 

Type    registro=Record

Nome    : String[30];     {p[n].nome}

idade    : Integer;     {p[n].idade}

Sexo    : Char;    {p[n].sexo}

End;


 

matriz10x10=array[1..10,1..10] of integer;

arquivo = file of integer;


 

Var     p:array[1..20] of registro;

I:ARRAY[1..100] OF INTEGER;

x,y,z:real; {NÚMERO REAL}

Inteiro:integer; {NÚMERO INTEIRO}

letra:char; {UMA LETRA}

PALAVRA:STRING[30]; {UMA FRASE}

pergunta:boolean; {true, false}

notas:arquivo;


 

label start;


 

Begin

start:

goto start
{volta pro começo do programa – não se deve usar!!}


 

Clrscr; {LIMPA a tela}

Textcolor(4); {COR DO TEXTO}

Textbackground(1); {COR DO FUNDO}


 

WriteLN('Nome':30,'Idade':10,'Sexo':6,'Altura':4:2);


 

WriteLN('DIGITA UM TEXTO E PULA UMA LINHA');

Write('O VALOR DE A VALE',A:2); {'A' tem duas casas decimais}

Readln(x,y);
{ATRIBUI A X e y Os 2 VALORes DIGITADOs PELO TECLADO}

letra:=Readkey; {ATRIBUI A 'letra' o 1º caractere digitado}


 

y:=sqrt(x);
{ }

y:=sqr(x);
{ x2 }

y:=exp(a*ln(b));
{ Ba }

z:=(10*x/(3+x)); { 10.X/(3+X)}


 

perg_boolean := (4>3) {retorna true}

write(perg_boolean) {escreve true}


 

if Perg_boolean then (c); {executa (c) se Perg_boolean = true }

if (x<>y) AND (X>Y) then writeln(X é diferente E MAIOR QUE Y);

if (x<>y) OR (X>=0) then writeln(X é diferente DE Y OU X É MAIOR QUE 0);


 

if x>y then writeln(X,' é maior que ',Y)

else

    begin; {agrupa os comandos contidos daqui até o end;}

if x<=(y-1) then writeln(X,' é menor ou igual a ',Y)

else writeln(X,' está entre',Y,'e',y-1)

        end;

inteiro:=4 mod 3; { resto da divisão, inteiro:=1 }


 

Readkey;
{Congela a tela}


 

FOR IND:=10 TO 100 do writeln( IND);

{atribui valores a ind de 10 até 100 de 1 em 1}

FOR IND:=100 downTO 10 do writeln(ind);

{atribui valores a ind de 100 até 10 de -1 em -1}


 

fatorial:=1;

repeat {opera até n=0. obs: não inclui n=0}

fatorial:=fatorial*n;

n:=n-1;

until n=0;

Write('n fatorial = ',fatorial);


 

fatorial:=1;

while n<>0 do {só pára de operar quando n=0}

begin;

fatorial:=fatorial*n;

n:=n-1;

end;

Write('n fatorial = ',fatorial);


 

N=READKEY;

CASE N OF

A:WRITELN('VC DIGITOU a');

B:WRITELN('VC DIGITOU B');

ELSE WRITELN('vC N DIGITOU NEM A NEM B');

END;


 

leitura_valida("digite um número entre 0 e 100", a);

leitura_valida("digite um número entre 0 e 100", b);

write(f(a,b)) {não precisa do [;] pq tem um [end] embaixo}


 

assign (arquivo,'c:\file.txt'); {conecta a variável notas ao aqruivo file.txt}

rewrite(arquivo); {cria o arquivo file.txt}

for i:=1 to 100 do

    write(arquivo,i);

close(arquivo);


 

assign (arquivo,'c:\file.txt');

reset(arquivo);

while not eof(arquivo) do

read(arquivo,n); {i vale 1}

close(arquivo);


 

assign (arquivo,'c:\file.txt');

reset(arquivo);

read(arquivo,i); {i vale 1}

read(arquivo,i); {i vale 2}

read(arquivo,i); {3... o apontador é incrementado de 1 automaticamente}

seek(arquivo,0);
{posiciona o apontador na linha 0 de file.txt}

close(arquivo);


 

{var p:registro;}

assign (pesquisa,'c:\quiz.txt');

rewrite(pesquisa);

writeln("digite nome, sexo e idade. idade = 0 para parar");

readln(p.nome,p.sexo,p.idade)

while(p.idade <> 0) do

begin

write(pesquisa,p);

writeln("digite nome, sexo e idade. idade = 0 para parar");

readln(p.nome,p.sexo,p.idade)

end;

close(pesquisa);


 

End.


 

Nenhum comentário: