Pascal- obratenie textu

Programovacie jazyky, rady, poradňa...
feri777
Novice
Novice
Príspevky: 3
Registrovaný: 28 feb 2012, 2:35

Pascal- obratenie textu

Príspevok od používateľa feri777 »

Ahoj. Potreboval by som pomoc s programom Pascal. Som začiatočník a napísal som program, ktorý premení číslo z 10tkovej sústavy do 16tkovej lenže ho vypíše odzadu a chcem vedieť či sa nedá ten výsledok obrátiť. Hľadal som aj na googli ale nič som nenašiel.

Kód: Vybrať všetko

program prevod_16_sustava;
uses crt;
var x,q,w:longint;
begin
clrscr;
write('Napis cislo v 10-tkovej sustave: ');
readln(x);
writeln('----------------------------------------------------------------');
write('V 16-tkovej sustave sa cislo zapise (zapis citaj odzadu): ');
repeat
q:=x div 16;
w:=x mod 16;
begin
if w=0 then write('0')
else if w=1 then write('1')
else if w=2 then write('2')
else if w=3 then write('3')
else if w=4 then write('4')
else if w=5 then write('5')
else if w=6 then write('6')
else if w=7 then write('7')
else if w=8 then write('8')
else if w=9 then write('9')
else if w=10 then write('A')
else if w=11 then write('B')
else if w=12 then write('C')
else if w=13 then write('D')
else if w=14 then write('E')
else if w=15 then write('F')
end;
x:=q;
until q<1;
writeln;
writeln('----------------------------------------------------------------');
writeln('Pre ukoncenie programu stlac ENTER');
readln;
end.
ferdo93
Amateur
Amateur
Príspevky: 21
Registrovaný: 27 feb 2011, 12:00

Re: Pascal- obratenie textu

Príspevok od používateľa ferdo93 »

nacitaj vysledky do pola, pouzi cyklus for a namiesto do napis downto.

//na vetvenie pouzi prikaz case of, bude to prehladnejsie.
feri777
Novice
Novice
Príspevky: 3
Registrovaný: 28 feb 2012, 2:35

Re: Pascal- obratenie textu

Príspevok od používateľa feri777 »

poradil by si mi prosím s tým príkazom pole?
hľadal som na nete ale nenašiel som čo som chcel, alebo som to našiel ale nepochopil :(
Baseilos
Light Expert
Light Expert
Príspevky: 57
Registrovaný: 01 máj 2011, 2:15

Re: Pascal- obratenie textu

Príspevok od používateľa Baseilos »

Prevod do HEXa.

Kód: Vybrať všetko

function ToHex(int cislo): string;
var HEXValues: string[16];
     result: string[255]; { Pole kde sa budu ukladat vysledne hodnoty }
     index: int;
begin
   { Inicializacia jednotlivych moznych HEX znakov }
   HEXValues := '0123456789ABCDEF';
   
   index := 1;
   while cislo > 0 do begin
      result[index] := HEXValues[(cislo mod 16) + 1]; { Vysledny znak v 16 sustave - ziskany na zaklade indexu v poli HEXValues }
      cislo := cislo div 16; 
      index := index + 1; { Posun na dalsiu poziciu vo vyslednom poli }
   end;

   ToHex := result; { Vratenie vysledku z funkcie }
end;
Vypisanie stringu v opacnom poradi

Kód: Vybrať všetko

procedure PrintHex(hex: string);
var index, strlen: int;
begin
   strlen := Length(hex); { Dlzka vypisovaneho retazca }
   for index := strlen downto 1 do begin { Vypisovanie od zadu }
      write(hex[index]);
   end;
   writeln(''); { Zalomenie riadku }
end;
feri777
Novice
Novice
Príspevky: 3
Registrovaný: 28 feb 2012, 2:35

Re: Pascal- obratenie textu

Príspevok od používateľa feri777 »

dik za rady
Napísať odpoveď