imfede Comune mortale

Registrato: 08/07/10 17:18 Messaggi: 1
|
Inviato: 08 Lug 2010 17:21 Oggetto: [Pascal] problema somma vettori. attenzione e complicato! |
|
|
sono giorni che mi scervello: ho tentato di scrivere un programma che faccia somme di numeri con molte cifre semplicemente caricando due array e facendo i conti in colonna. il problema è che sputa fuori risultati come "10-78" anche se gli chiedo esplicitamente con un writeln una variabile intera. ecco il programma:
program somma_vettori;
uses EmptyPlaceHolderUnit;
var add1,add2:string[50];
var ad1,ad2,sum:array[0..50]of integer;
var i,x:integer;
var esc:char;
function myint(mychar:char):integer;
begin
myint:=ord(mychar)-48;
end;
begin
for i:=0 to 50 do write('-');
for i:=0 to 6 do writeln;
repeat
write('a='); readln(add1);
write('b='); readln(add2);
(*---- Caricamento vettori---*)
x:=length(add1);
for i:=0 to x do
begin
ad1[i]:=myint(add1[i]);
end;
(*---control---*)
writeln; for i:=x downto 0 do write(ad1[i]); writeln;
(*---end control---*)
x:=length(add2);
for i:=0 to x do
begin
ad2[i]:=myint(add2[i]);
end;
if length(add1)>length(add2) then x:=length(add1);
writeln('a+b=');
(*----- Somma-----*)
for i:=x downto 0 do
begin
sum[i]:=ad1[i]+ad2[i];
if sum[i]>9 then
begin
sum[i]:=sum[i]-10;
ad1[i-1]:=ad1[i-1]+1;
end;
end;
for i:=x downto 0 do write(sum[i]);
writeln;
write('Ancora? [S/N]'); readln(esc);
until (esc='n')or(esc='N');
end. |
|