中四電腦問題(Pascal programme)

2007-09-29 3:31 am
我老師要我寫一個程式係當人地打quadratic equation 既a,b,c三個數時,可以指出佢既nature of root,條式係b^2-4ac
如果個三個數可以 form two real roots或者double roots,姐係D>0或者D=0,個程式都run到,但如果D<0,姐係b^2-4ac細過0,就出現runtime error,我唔知點解決,我試過寫if b*b<4*a*c,then writeln('It has no real roots'),都係照樣出runtime error,希望有人幫我解決,thx

回答 (2)

2007-09-29 3:44 am
✔ 最佳答案
program quaratic_equation;
uses
wincrt;
var
a,b,c,d:longint;
begin
writeln(&#39;ax^2+bx+c=0&#39;);
write(&#39;Enter the number of a: &#39;);
readln(a);
write(&#39;Enter the number of b: &#39;);
readln(b);
write(&#39;Enter the number of c: &#39;);
readln(c);
d:= b*b-4*a*c;
if d&lt;0 then
writeln(&#39;No real roots&#39;)
else
begin
if d=0 then
writeln(&#39;The root is &#39;,((-b+sqrt (d))/(2*a)):0:20,&#39;(Repeated)&#39;);
if d&gt;0 then
begin
writeln(&#39;the roots are:&#39;);
writeln((-b+sqrt (d))/(2*a):0:20);
writeln((-b-sqrt (d))/(2*a):0:20);
end
end
end.
參考: me
2007-09-29 8:02 pm
program quaratic_equation;
uses wincrt;
var
a,b,c,d:integer;
begin
writeln(&#39;ax^2+bx+c=0&#39;);
write(&#39;Enter the number of a: &#39;);
readln(a);
write(&#39;Enter the number of b: &#39;);
readln(b);
write(&#39;Enter the number of c: &#39;);
readln(c);
d:= b*b-4*a*c;
if d &gt; 0 then writeln(&#39;Two double roots&#39;);
if d = 0 then writeln(&#39;double root&#39;);
if d &lt; 0 then writeln(&#39;no real roots&#39;);
end.
參考: me


收錄日期: 2021-04-13 18:44:28
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070928000051KK02873

檢視 Wayback Machine 備份