Ir ao conteúdo

Imprimir e Ordenar registros dentro de um vetor


Thiago Garrocho

Posts recomendados

Postado

Boa noite, estou fazendo o programa por enquanto com o seguinte MENU:

cadastrar

consultar

excluir

imprimir**

ordenar**

sair

Sendo que já fiz e está funcionando:

CADASTRAR

CONSULTAR

EXCLUIR

SAIR

Agora preciso de uma ajuda para imprimir todos os funcionarios cadastrados e Ordenar os funcionarios cadastrados pela sua MATRICULA, ou seja, após cadastrar os funcionarios eu irei imprimir eles como eu cadastrei, tanto faz a seguencia de matricula, pode ser 1 ,,, 10 ,,, 5 ,,,. agora após eu acessar o menu ordenar, ele vai ordenar os registros pela matricula e quando eu voltar ao menu imprimir ele irá imprimir da seguinte forma: 1,,, 5 ,,, 10 ,,,,.

Não sei ordenar registros, quem puder me ajudar agradeço. estou online para tirar dúvidas. ;)

Código:

Program exemplo;
uses crt;
const max=3;

type treg=record
matricula:integer;
nome:string;
salario:real;
end;
tvet=array[1..max] of treg;

var vetor:tvet;
i, total, matricula:integer;
op:char;
achou:boolean;

Begin
total:=0;
repeat
clrscr;
writeln('-------------');
writeln('1 - Cadastro|');
writeln('2 - Consulta|');
writeln('3 - Excluir |');
writeln('4 - Imprimir|');
writeln('5 - Ordenar |');
writeln('6 - Sair |');
writeln('-------------');
op:=readkey;
case op of
'1':if total = max then
begin
clrscr;
writeln('Todas as matricula já cadastradas');
readkey;
end
else
begin
clrscr;
writeln('Matricula:');
readln(matricula);
i:=1;
achou:=false;
while (achou=false) and (i<=total) do
begin
if matricula = vetor[i].matricula then
achou:=true
else
i:=i+1;
end;
if achou = true then
begin
clrscr;
writeln('Matricula já cadastrada!');
readkey;
end
else
begin
vetor[total+1].matricula:=matricula;
writeln('Nome: ');
readln(vetor[total+1].nome);
writeln('Salario: ');
readln(vetor[total+1].salario);
total:=total+1;
writeln('Cadastro Efetuado');
readkey;
end;
end;
'2':if total = 0 then
begin
clrscr;
writeln('Nenhuma matricula cadastrada');
readkey;
end
else
begin
clrscr;
writeln('Matricula:');
readln(matricula);
i:=1;
achou:=false;
while (achou=false) and (i<=total) do
begin
if matricula = vetor[i].matricula then
achou:=true
else
i:=i+1;
end;
if achou <> true then
begin
clrscr;
writeln('Matricula já cadastrada!');
readkey;
end
else
begin
clrscr;
writeln('Nome: ',vetor[i].nome);
writeln('Salario: ',vetor[i].salario:2:2);
readkey;
end;
end;
'3':if total = 0 then
begin
clrscr;
writeln('Nenhuma matricula cadastrada');
readkey;
end
else
begin
clrscr;
writeln('Matricula:');
readln(matricula);
i:=1;
achou:=false;
while (achou=false) and (i<=total) do
begin
if matricula = vetor[i].matricula then
achou:=true
else
i:=i+1;
end;
if achou = true then
vetor[i].matricula:=vetor[total].matricula;
vetor[i].nome:=vetor[total].nome;
vetor[i].salario:=vetor[total].salario;
total:=total-1;
writeln ('Exclusão Efetuada');
readkey;
end;
'4':if total = 0 then
begin
clrscr;
writeln('Nenhuma matricula cadastrada');
readkey;
end;

end;
until op='6';

End.

Postado
Pesquize no sitio wikipedia, bubble sort, merge sort, quick sort, não alguns tipos de ordenação, para estudo recomendo bubble sort, pois é fácil entender, e performance quick sort.

OK, procurei e achei, agora como faço para imprimir todos em ordem?

Fiz a opção 4 que é IMPRIMIR todos os funcionarios cadastrados e ficou assim: Só que está dando erro:

'4':if total = 0 then

begin

clrscr;

writeln('Nenhuma matricula cadastrada');

readkey;

end

else

begin

clrscr;

i:=1;

while i <= max do

writeln('Matricula: ',vetor.matricula);

writeln('Nome: ',vetor.nome);

writeln('Salario: ',vetor.salario:2:2);

i:=i+1;

readkey;

end;

Postado

Bom a questão de imprimir eu resolve o código é esse:

'4':if j = 0 then

begin

clrscr;

writeln('Nenhuma matricula cadastrada');

readkey;

end

else

begin

clrscr;

i:=1;

repeat

if i <= max then

writeln('Matricula: ',vetor.matricula);

writeln('Nome: ',vetor.nome);

writeln('Salario: ',vetor.salario:2:2);

i:=i+1;

readkey;

until i = max;

end;

agora falta resolver a de ordenar...

Postado

não programo em pascal, mas para imprimir, faça

for i=1 to max

begin

writeln('Matricula: ',vetor.matricula);

writeln('Nome: ',vetor.nome);

writeln('Salario: ',vetor.salario:2:2);

end

recomendo você aprender procedures e functions.

Postado
não programo em pascal, mas para imprimir, faça

for i=1 to max

begin

writeln('Matricula: ',vetor.matricula);

writeln('Nome: ',vetor.nome);

writeln('Salario: ',vetor.salario:2:2);

end

recomendo você aprender procedures e functions.

Melhorou bem, esse seu codigo ai funcionou melhor, olha só to tentando fazer o de ordenação como ficou:

procedure ordenar(var vet:tvet);

var aux,posic,i,r:integer;

begin

for i:=1 to max do

begin

posic:=i;

for r:=i+1 to max do

if vet.matricula < vet[posic].matricula then

posic:=r;

aux:=vet.matricula;

vet.matricula:=vet[posic].matricula;

vet[posic].matricula:=aux;

end;

for i:=1 to max do

begin

writeln('Matricula: ', vetor.matricula);

writeln('Nome: ', vetor.nome);

writeln('Salario: ', vetor.salario:2:2);

readkey;

end;

end;

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

GRÁTIS: Aprenda a Ler Resistores e Capacitores

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!