为什么我在对 3 个实变量求和时得到一个奇怪的结果?
why i get a strange result when summing 3 real variables?
这是我的代码:
program P1;
var a,b,c,p :real;
var i,o,n: string;
begin
i := 'isoscel';
o := 'echilateral';
n :='scalen';
writeln('Introduceti 3 numerele reale');
readln(a,b,c);
case (a>0) and (b>0) and (c>0) and (a+b>c) and (a+c>b) and (b+c>a) of
true:
begin
p:=a+b+c;
writeln('perimetrul este ',p);
if (a=b) and (b<>c) or (b=c) and (c<>a) or (a=c) and (c<>b) then write('triunghiul este ',i);
if (a=b) and (a=c) and (b=c) then write('triunghiul este ',o);
if (a<>b) and (b<>c) and (c<>a) then write('triunghiul este ',n);
end;
false:
writeln('nu este posibil');
end;
readln();
end.
如果我插入 5 5 5 我会得到这个答案:
perimetrul este 1.5000000000000000E+001
triunghiul este echilateral
但如果我将 a、b、c、p 作为整数而不是实数,我会得到正常结果:
perimetrul este 15
triunghiul este echilateral
我需要我的变量是真实的,你能帮我得到一个正常的答案吗?
1.500E001 是 1.5x10^1,即 15。您需要一个格式化的值。尝试
writeln(‘Here is the answer: ’, p:5:1)
将结果打印在宽度为 5 的字段中,并保留 1 个小数位。根据需要调整格式。
这是我的代码:
program P1;
var a,b,c,p :real;
var i,o,n: string;
begin
i := 'isoscel';
o := 'echilateral';
n :='scalen';
writeln('Introduceti 3 numerele reale');
readln(a,b,c);
case (a>0) and (b>0) and (c>0) and (a+b>c) and (a+c>b) and (b+c>a) of
true:
begin
p:=a+b+c;
writeln('perimetrul este ',p);
if (a=b) and (b<>c) or (b=c) and (c<>a) or (a=c) and (c<>b) then write('triunghiul este ',i);
if (a=b) and (a=c) and (b=c) then write('triunghiul este ',o);
if (a<>b) and (b<>c) and (c<>a) then write('triunghiul este ',n);
end;
false:
writeln('nu este posibil');
end;
readln();
end.
如果我插入 5 5 5 我会得到这个答案:
perimetrul este 1.5000000000000000E+001
triunghiul este echilateral
但如果我将 a、b、c、p 作为整数而不是实数,我会得到正常结果:
perimetrul este 15
triunghiul este echilateral
我需要我的变量是真实的,你能帮我得到一个正常的答案吗?
1.500E001 是 1.5x10^1,即 15。您需要一个格式化的值。尝试
writeln(‘Here is the answer: ’, p:5:1)
将结果打印在宽度为 5 的字段中,并保留 1 个小数位。根据需要调整格式。