2015-12-28 73 views
1

我正在創建一個將十六進制數轉換爲十進制數的程序。下面是代碼:十六進制到十進制Pascal錯誤使用Dev-Pascal非法表達式

program hexadesimal; 
uses crt; 
var 
d, j, l, pow, y:integer; 
x:longint; 
h:string; 
label z; 
begin 
z: 
readln(h); 
d:=0; 
l:=length(h); 
for j:=1 to length(h) do 
begin 
if (h[j] = 'A') then h[j] = '10'; 
if (h[j] = 'B') then h[j] = '11'; 
if (h[j] = 'C') then h[j] = '12'; 
if (h[j] = 'D') then h[j] = '13'; 
if (h[j] = 'E') then h[j] = '14'; 
if (h[j] = 'F') then h[j] = '15'; 
l:=l - 1; 
pow := power(16, l); 
val(h[j], x, y); 
d := d + (x * pow); 
end; 
writeln(d); 
readln; 
end. 

然而,當我編譯錯誤出現非法的表達和它指向這些行:

if (h[j] = 'A') then h[j] = '10'; 
if (h[j] = 'B') then h[j] = '11'; 
if (h[j] = 'C') then h[j] = '12'; 
if (h[j] = 'D') then h[j] = '13'; 
if (h[j] = 'E') then h[j] = '14'; 
if (h[j] = 'F') then h[j] = '15'; 

我該怎麼辦?

+0

你能顯示完整的錯誤? –

+0

錯誤:非法表達式(6行),有6個錯誤編譯模塊,停止@ManojSalvi –

回答

0

if (h[j] = 'A') then h[j] = '10';沒有意義。 h[j]引用字符串中的字符,因此它不能等於像'10'這樣的字符串(包含2或3個字符)。如果您希望將該值分配給變量,則需要使用:=運算符。 h[j] = '10'不是一個賦值運算符,而是一個比較運算符,在您的情況下會導致FALSE。換句話說,你會得到以下結構:if (TRUE/FALSE) then FALSE;

+0

謝謝,它的工作原理! –