I have written code in pascal ...
program pr2;
//division of number into integer and fractional parts
procedure StrToChs(s:string; var tr,fr:string);
var p:byte;
begin
p:=pos('.',s);
if p=0
then
begin
tr:=s;
fr:='';
end
else if p=length(s) then
begin
tr:=copy(s,1,p-1);
fr:='';
end
else if p=1 then
begin
tr:='';
fr:=copy(s,2,length(s)-1);
end
else
begin
tr:=copy(s,1,p-1);
fr:=copy(s,p+1,length(s)-p);
end;
end;
//bringing the integer parts of numbers to one length
procedure NormT(var s1,s2:string; var n:byte);
var n1,n2:byte;
begin
n1:=length(s1);
n2:=length(s2);
if n1>n2 then
begin
n:=n1;
while length(s2)<length(s1) do s2:='0'+s2;
end
else if n2>n1 then
begin
n:=n2;
while length(s1)<length(s2) do s1:='0'+s1;
end;
end;
//bringing the fractional parts of numbers to one length
procedure NormF(var s1,s2:string; var n:byte);
var n1,n2:byte;
begin
n1:=length(s1);
n2:=length(s2);
if n1>n2 then
begin
n:=n1;
while length(s2)<length(s1) do s2:=s2+'0';
end
else if n2>n1 then
begin
n:=n2;
while length(s1)<length(s2) do s1:=s1+'0';
end;
end;
//addition of the fractional parts
function SumFrac(s1,s2:string; n:byte; var p:byte):string;
var s3:string;
i:byte;
a1,a2,a3:array[byte] of byte;
begin
for i:=1 to n do
begin
a1
:=ord(s1)-48;
a2:=ord(s2)-48;
end;
p:=0;
for i:=n downto 1 do
begin
a3:=a1+a2+p;
if (a3>9)and(i>1) then
begin
a3:=a3 mod 10;
p:=1;
end
else p:=0;
end;
if a3[1]>9 then //transmit into the main program the first digit of the integer part of number (0 or 1)
begin
a3[1]:=a3[1] mod 10;
p:=1;
end
else p:=0;
s3:='';
for i:=1 to n do
s3:=s3+chr(a3+48);
SumFrac:=s3;
end;
//addition of the integer parts
function SumTrunc(s1,s2:string; n,p:byte):string;
var s3:string;
i:byte;
a1,a2,a3:array[byte] of byte;
begin
for i:=1 to n do
begin
a1:=ord(s1)-48;
a2:=ord(s2)-48;
end;
for i:=n downto 1 do
begin
a3:=a1+a2+p;
if (a3>9)and(i>1) then
begin
a3:=a3 mod 10;
p:=1;
end
else p:=0;
end;
if a3[1]>9 then
begin
a3[1]:=a3[1] mod 10;
n:=n+1;
for i:=n downto 2 do
a3:=a3[i-1];
a3[1]:=1;
end;
s3:='';
for i:=1 to n do
s3:=s3+chr(a3+48);
SumTrunc:=s3;
end;
var s1,s2,s1t,s1f,s2t,s2f,s3t,s3f:string;
nt,nf,p:byte;
begin
writeln('Enter two decimal number (no more than 100 characters), decimal separator is dot');
readln(s1);
readln(s2);
StrToChs(s1,s1t,s1f);
StrToChs(s2,s2t,s2f);
NormT(s1t,s2t,nt);
NormF(s1f,s2f,nf);
if (s1f='')and(s2f='') then s3f:=''
else
begin
s3f:=SumFrac(s1f,s2f,nf,p);
s3f:='.'+s3f;
end;
if (s1t='')and(s2t='') then s3t:='0'
else s3t:=SumTrunc(s1t,s2t,nt,p);
write(s3t,s3f);
end.
But i cant find the mistake .. it counts numbers wrong but why ? ))))))
Number 777.777 + 333 result is .777 and thats wrong.
Code is made on this example
They are given two "long" positive decimal numbers , each of which can have up to 100 digits and can contain a decimal point . If does not contain a decimal point , it is understood that this is an integer (as if the decimal point was located at the end) . Write a program that reads the number specified in the input and computes their sum and displays it .
Example 1:
To enter in the form:
77777777777777777777
333
program prints the result :
77777777777777778110
Example 2:
To enter in the form:
777777777777777.77
3.33333
program prints the result :
777777777777781.10333
Any ideas whete did i made mistake ?