segunda-feira, 29 de dezembro de 2008

Mais um trabalho do segundo semestre

Esse tbm é com pascal e trabalha com arquivos:




Program Livraria;
Uses Fdelay, crt;
Const N_arq = 'trab_arq';
Type
T_Livro = Record
Autor : String[15];
Titulo: String[15];
ISBN: Integer;
Ano: Integer;
Edicao: Byte;
Preco: Real;
Quantidade: integer;
End;
T_Arquivo = File of T_livro;
Var
Livro: T_Livro;
Estoque: T_Arquivo;
Op: String;
ISBN, Pos: Integer;
Procedure Abre_Arq(var Estoque: T_Arquivo);
Begin
ClrScr;
Assign(Estoque, n_arq);
{$I-}
Reset(Estoque);
{$I+}
IF ioresult <> 0 then
Begin
ReWrite(Estoque);
WriteLn('Arquivo criado');
Delay (20000);
End
Else
Begin
WriteLn('Arquivo aberto');
Delay(20000);
End;
End;
Procedure Menu(var op: string);
Begin
ClrScr;
WriteLn('1 - Inserir');
WriteLn('2 - Pesquisar');
WriteLn('3 - Alterar');
WriteLn('4 - Excluir');
WriteLn('5 - Total do estoque');
WriteLn('6 - Sair');
op:=ReadKey;
End;
Procedure Pesquisar(var Livro:T_Livro; var Estoque: T_Arquivo; var Pos: Integer; ISBN: integer);
Var
X: integer;
Begin
x:=0;
Pos:=0;
Seek(Estoque, pos);
if filesize(estoque) <> 0 then
Repeat
Begin
Read(Estoque, Livro);
x:=x+1;
End;
Until ( x = filesize(estoque)) or (Livro.ISBN = ISBN) ;
If livro.isbn <> isbn then
Begin
pos:=1;
WriteLn('ISBN nao cadastrado');
Delay(20000);
End
Else
Begin
WriteLn('ISBN encontrado.');
pos:=filepos(estoque);
Delay(20000);
End;
End;
Procedure EntradaDeDados(Livro: T_Livro; var Estoque: T_Arquivo; Pos: Integer);
Begin
Seek(Estoque,Pos-1);
With Livro do
Begin
WriteLn('Digite o ISBN');
ReadLn(ISBN);
If isbn <> 0 then
Begin
WriteLn('Digite o nome do Autor');
ReadLn(Autor);
WriteLn('Digite o titulo do livro');
ReadLn(Titulo);
WriteLn('Digite o ano');
ReadLn(Ano);
WriteLn('Digite a edicao');
ReadLn(Edicao);
WriteLn('Digite o preco');
ReadLn(Preco);
WriteLn('Digite a quantidade');
ReadLn(Quantidade);
Write(Estoque,Livro);
WriteLN('Registro salvo');
End
Else
WriteLn('0 nao eh um numero valido para ISBN');
End;
End;
Procedure TotalEstoque(Livro: T_Livro; var Estoque: T_Arquivo);
Var
TotalEstoque: Real;
Begin
TotalEstoque:=0;
Seek(Estoque, 0);
While (not Eof(Estoque)) do
Begin
Read(Estoque, Livro);
If livro.isbn <> 0 then
With Livro do
Begin
TotalEstoque:= TotalEstoque + (Preco * Quantidade);
End;
End;
WriteLn('Valor total = ', TotalEstoque:3:2);
ReadKey;
End;

Procedure Teste (var estoque : t_arquivo; livro: t_livro);
Var
x,ISBN,Tam,Pos : Integer;
Begin
Tam := FileSize(estoque);
WriteLn('Tam ',Tam);

Seek(Estoque,0);
if tam <> 0 then
For x:= 1 to filesize(estoque) do
Begin
Read(estoque ,livro);
Tam := FileSize(estoque);
Pos := FilePos(estoque);
WriteLN('Pos ',pos);
isbn:= livro.isbn;
WriteLn('ISBN ',ISBN);
ReadKey;
End;
End;

Begin
Abre_Arq(Estoque);
teste(estoque, livro);
op:='0';
While (op <> '1') and (OP <> '2') and (op <> '3') and (op <>'4') and (op <>'5') and (op <>'6') do
Begin
Menu(op);
if op = '1' then
Begin
Pos:= FileSize(Estoque)+1;
EntradaDeDados(Livro, Estoque, Pos);
ReadKey;
op:='0';
End;
If op = '2' then
Begin
WriteLn('Digite o ISBN a pesquisar');
ReadLN(ISBN);
If isbn <> 0 then
Begin
Pesquisar(Livro,Estoque, pos, ISBN);
Seek(Estoque, pos);
IF isbn = livro.isbn then
With Livro do
Begin
Writeln('Autor ',Autor);
End;
End
Else
WriteLn('0 nao eh um numero valido para ISBN');
ReadKey;
op:='0';
End;
If op = '3' then
Begin
WriteLn('Digite o ISBN a Alterar');
ReadLN(ISBN);
If isbn <> 0 then
Begin
Pesquisar(Livro,Estoque, pos, ISBN);
Seek(Estoque,Pos);
IF isbn = livro.isbn then
EntradaDeDados(Livro, Estoque, Pos);
End
Else
WriteLn('0 nao eh um numero valido para ISBN');
ReadKey;
op:='0';
End;
If op = '4' then
Begin
WriteLn('Digite o ISBN a excluir');
ReadLn(ISBN);
If isbn <> 0 then
Begin
Pesquisar(Livro, Estoque,Pos,ISBN);
Seek(Estoque,Pos-1);
IF isbn = livro.isbn then
With Livro do
Begin
ISBN:=0;
Write(Estoque,Livro);
End;
End
Else
WriteLn('0 nao eh um numero valido para ISBN');
ReadKey;
op:='0';
End;
IF op = '5' then
Begin
TotalEstoque(Livro, Estoque);
op:='0';
End;
If op <> '6' then
op:='7';
End;
Close(estoque);
End.

Nenhum comentário: