Lista de Exercicios
10.Construir um programa para somar dois valores no sistema numérico
hexadecimal.
- program P10;
- uses crt;
- var
- function inverte (s : string) : string; {inverte uma string}
- var
- begin
- tmp := ' ';
- for i:= length(s) downto 1 do
- tmp := tmp + copy (s, i, 1);
- inverte :=tmp;
- end;
- function dec(s : string) : longint; {converte um número hexadecimal}
- var {em decimal}
- code : integer;
- d : longint;
- begin
- s := '$' + s;
- val(s, d, code);
- dec := d;
- end;
- function hex (n : longint) : string; {converte um número
decimal}
- var
- r : byte; {em hexadecimal}
- x : string[1];
- h : string;
- begin
- h := ' ';
- repeat
- r := n mod 16;
- n := trunc(n/16);
- if r >= 10 then
- case r of
- 10 : x := 'A';
- 11 : x := 'B';
- 12 : x := 'C';
- 13 : x := 'D';
- 14 : x := 'E';
- 15 : x := 'F';
- end
- else
- h := h + x;
- until n < 16;
- if n >= 10 then
- case n of
- 10 : x := 'A';
- 11 : x := 'B';
- 12 : x := 'C';
- 13 : x := 'D';
- 14 : x := 'E';
- 15 : x := 'F';
- end
- else
- h := h + x;
- h := inverte(h);
- hex := h;
- end;
- procedure ler_valores;
- begin
- clrscr;
- highvideo;
- gotoxy(25,3);
- write('SOMA DE VALORES HEXADECIMAIS');
- gotoxy(18,1);
- write('PROGRAMA FEITO POR KARINE E MARIA DA GLORIA');
- lowvideo;
- gotoxy(10,5);
- write('Entre com o primeiro valor (at‚ 6 caracteres) => ');
- readln(a);
- gotoxy(10,7);
- write('Entre com o segundo valor (at‚ 6 caracteres) => ');
- readln(b);
- end;
- function soma_hex(a, b : string) : string; {soma dois valores hexadecimais}
- begin
- soma_hex := hex( dec(a) + dec(b) );
- end;
- function erro(x : string) : boolean; {verifica erro nos valores
hexadecimais}
- var
- i, n : byte;
- code : integer;
- a : string[1];
- begin
- erro := false;
- code := 0;
- for i:= length(x) downto 1 do
- begin
- a := '0';
- a := copy(x, i, 1);
- val (a, n, code);
- if code<>0 then
- if (a<>'A') and (a<>'a') and (a<>'B') and (a<>'b')
and (a<>'C') and (a<>'c') and
- (a<>'D') and (a<>'d') and (a<>'E') and (a<>'e')
and (a<>'F') and (a<>'f') then
- end;
- end;
- begin
- repeat
- until not (erro(a) or erro(b));
- gotoxy(10,9);
- write('A soma ‚ => ',soma_hex(a,b));
- repeat until keypressed;
- end.
Para retornar a tela principal clique aqui.