主题:pascal编程的一道题,我怎么搞也搞不对,请看看吧!
题目是:计算sin(x)=x-(x∧3)/3!+(x∧5)/5!-(x∧7)/7!+……直到最后一项绝对值小于10∧-7时停止运算,x由键盘输入。(x∧3表示x的3次方,下同)
我编的代码是
program ex9;
var y:real;
i,n,t,s,x:longint;
begin
write('x=');readln(x);
n:=1;
y:=0;
repeat
s:=1; t:=1;
if n mod 2=1 then
begin
for i:=1 to (2*n-1) do
begin
t:=t*x;
s:=s*i;
end;
y:=y+t/s;
end
else
begin
for i:=1 to (2*n-1) do
begin
t:=t*x;
s:=s*i;
end;
y:=y-t/s;
end;
n:=n+1;
until t/s<1/(10*10*10*10*10*10*10);
write('sin',x,'=',y:0:4);
readln;
end.
所用的软件是Borland Pascal 7
但算出来的sin值不对,请大家看看有哪里错了,谢谢了
我编的代码是
program ex9;
var y:real;
i,n,t,s,x:longint;
begin
write('x=');readln(x);
n:=1;
y:=0;
repeat
s:=1; t:=1;
if n mod 2=1 then
begin
for i:=1 to (2*n-1) do
begin
t:=t*x;
s:=s*i;
end;
y:=y+t/s;
end
else
begin
for i:=1 to (2*n-1) do
begin
t:=t*x;
s:=s*i;
end;
y:=y-t/s;
end;
n:=n+1;
until t/s<1/(10*10*10*10*10*10*10);
write('sin',x,'=',y:0:4);
readln;
end.
所用的软件是Borland Pascal 7
但算出来的sin值不对,请大家看看有哪里错了,谢谢了