十六进制转换,请用free Pascal语言编写代码?【试题描述】 输入一个不超过100000位的十六进制数,请转换成八进制数.注:十六进制数中,字母0-9还对应表示数字0-9,字母“A”(大写)表示10,“B”

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/27 23:49:17
十六进制转换,请用free Pascal语言编写代码?【试题描述】 输入一个不超过100000位的十六进制数,请转换成八进制数.注:十六进制数中,字母0-9还对应表示数字0-9,字母“A”(大写)表示10,“B”

十六进制转换,请用free Pascal语言编写代码?【试题描述】 输入一个不超过100000位的十六进制数,请转换成八进制数.注:十六进制数中,字母0-9还对应表示数字0-9,字母“A”(大写)表示10,“B”
十六进制转换,请用free Pascal语言编写代码?
【试题描述】
输入一个不超过100000位的十六进制数,请转换成八进制数.
注:十六进制数中,字母0-9还对应表示数字0-9,字母“A”(大写)表示10,“B”表示11,…,“F”表示15.比如:十六进制A10B表示的10进制数是:10*16^3+1*16^2+0*16^1+11*16^0=41227.转换成八进制数是:120413,因为1*8^5+2*8^4+0*8^3+4*8^2+1*8^1+3*8^0=41227.
【输入描述】
一个十六进制数,没有前导0(除非是数字0).
【输出描述】
一个八进制数,没有前导0(除非是数字0).
【输入样例】
123ABC
【输出样例】
4435274

十六进制转换,请用free Pascal语言编写代码?【试题描述】 输入一个不超过100000位的十六进制数,请转换成八进制数.注:十六进制数中,字母0-9还对应表示数字0-9,字母“A”(大写)表示10,“B”
program testcase;
Uses sysutils;
var hex,result:string;
hex_len,i,ch,chv,value,bits:Integer;
good_char :boolean;
begin
writeln('Please input a HEX string:');
readln(hex);
hex_len:=length(hex);
hex:=UpperCase(hex);//统一转换成大写
//writeln('Total number of Chars:',hex_len);
//writeln('b:',(127 shr 1));
bits:=0;
value:=0;
result:='';
for i:=hex_len downto 1 do
begin
ch:=ord(hex[i]);
//writeln('ch:',ch);
if (ch>=48) and (ch =65) and (ch =3 do
begin
chv:=value and 7;
result:=chr(chv+48)+result;
value:= value shr 3;
bits:=bits-3;
end;
end;
end;
if (bits>0) and (value0) then
begin
chv:=value and 7;
result:=chr(chv+48)+result;
end;
if (result[1]='0') and (length(result)>1) then
Delete(result,1,1);
writeln('Result:',result);
writeln('Press ENTER to end the program.');
readln;
end.
一直总想学pascal,可没有机会.拿着你的题练了一下.
FreePascal2.4编译器,16进制转8进制输出:
Please input a HEX string:
ffffff
Result:77777777
Press ENTER to end the program.
Please input a HEX string:
123abc
Result:4435274
Press ENTER to end the program.
感觉Pascal真够古拙的.比c系更接近自然语言英语,更接近伪代码.
方便的string,集合,特别是更严格地遵循结构化编程的原则.
由于严格连垃圾回收都不需要.
Statement,“语句”这种概念在现代编程语言都很难找到了
(感慨.开始很不适应end.还是end; 还是end.哈哈)
总之,在Java没出来之前,的确是一个好且强大的高级应用语言.